首页 文章

MigraDoc / PDFSharp TextFrame中心对齐

提问于
浏览
0

我在TextFrame中使用Paragraph来获取向上显示的文本方向,这几乎是我想要的方式,我得到的最后一个问题是文本似乎是左对齐的,我试过设置段落对齐到中心这没有任何影响,也无法看到使用TextFrame执行此操作的选项 . 输出的文字每次都不一样 .

这就是我现在拥有的

enter image description here

这就是我想要实现的目标

enter image description here

以下是我用于使用MigraDoc实现此目的的代码

for (int i = 0; i < section2Items.Length; i++)
{
    TextFrame colXTextFrame = bothSection2ItemHeadersRow.Cells[i + 1].AddTextFrame();
    colXTextFrame.Orientation = TextOrientation.Upward;
    colXTextFrame.Height = new Unit(140);

    Paragraph colXParagraph = new Paragraph();
    colXParagraph.Format.Alignment = ParagraphAlignment.Center;
    colXParagraph.AddText(section2Items[i].Section2ItemTitle);
    colXTextFrame.Add(colXParagraph);

    bothSection2ItemHeadersRow.Cells[i + 1].Borders.Bottom = new Border() { Color = new MigraDoc.DocumentObjectModel.Color(255, 255, 255), Width = new MigraDoc.DocumentObjectModel.Unit(0), Style = MigraDoc.DocumentObjectModel.BorderStyle.None };
}

1 回答

  • 3

    这是一个应该工作的代码示例 .

    您可以使用 TextFrameMarginLeft 属性将其移动到列的中间 .

    // Create the table 
    Table Table = section.AddTable();
    Table.Borders.Width = 0.5;
    
    // create 3 columns
    Column column1 = Table.AddColumn("4cm");
    Column column2 = Table.AddColumn("4cm");
    Column column3 = Tabl.AddColumn("4cm");
    
    
    // make the row
    Row row = Table.AddRow();
    
    
    for (int i = 0; i < 3; i++)
    {
        TextFrame t = row.Cells[i].AddTextFrame();
        t.Orientation = TextOrientation.Upward;
        t.Height = new Unit(140);
    
        // set the left margin to half of the column width
        t.MarginLeft = this.Tabelle.Columns[i].Width/2;
    
        Paragraph p = new Paragraph();
        p.Format.Alignment = ParagraphAlignment.Center;       
        p.AddText("Test_" + i.ToString());
    
        t.Add(p);
    }
    

    这会产生以下输出:

    enter image description here

相关问题