首页 文章

MigraDoc C#在同一条线上左右对齐

提问于
浏览
5

我有一个带有单元格的表格,我想要两个文本,第一个在左边对齐,第二个在右边,在同一个单元格中,在同一行上 .

我试图用MigraDoc重现这个细胞而没有成功 . 我只能添加两个左右对齐但不在同一行的文本 .

这是我的代码:

Cell cellFooter1 = rowFooter.Cells[0];
Paragraph paraphTot = new Paragraph();
paraphTot.Format.Alignment = ParagraphAlignment.Left;
paraphTot.AddText("Left text");
cellFooter1.Add(paraphTot);
Paragraph paraphDetails = new Paragraph();
paraphDetails.Format.Alignment = ParagraphAlignment.Right;
paraphDetails.AddText("Right text");
cellFooter1.Add(paraphDetails);

这里给出了一个解决方案(http://forum.pdfsharp.net/viewtopic.php?f=2&t=2373),但我理解它是如何工作的 .

Edit : Partial solution :

在努力了解它是如何工作之后,我的代码部分正常工作 . 部分因为我发现右对齐的唯一方法是创建一个具有近似值的TabStop ...不好 .

Table table = new Table();
table.Borders.Width = 0.75;
Column myColumn = table.AddColumn(Unit.FromCentimeter(7));
Row myRow = table.AddRow();
Cell myCell = myRow.Cells[0];
Paragraph myParagraph = new Paragraph();
Style myStyle = doc.AddStyle("myStyle", "Normal");
myStyle.ParagraphFormat.Font.Size = 6.5;
myStyle.ParagraphFormat.Font.Bold = true;
myStyle.ParagraphFormat.TabStops.Clear();
myStyle.ParagraphFormat.AddTabStop(Unit.FromMillimeter(67), TabAlignment.Right);
myParagraph.Style = "myStyle";
myParagraph.Format.Alignment = ParagraphAlignment.Left;
myParagraph.AddFormattedText("left", "myStyle");
myParagraph.AddTab();
myParagraph.AddFormattedText("right", "myStyle");
myCell.Add(myParagraph);

它可以工作,但如何找到AddTab函数的好 Value ?我把 67 因为68to70无效 .

3 回答

  • 7

    链接帖子中显示的技巧相当简单:你只需要一个左对齐的段落 .

    然后确保只定义了一个tabstop,在单元格的右边缘有一个右对齐的tabstop .

    在段落中,添加您想要左对齐的文本,然后添加一个tabstop,然后添加您想要右对齐的文本 .

    示例代码:

    var table = section.AddTable();
    table.AddColumn("8cm");
    table.AddColumn("8cm");
    
    var row = table.AddRow();
    var paragraph = row.Cells[0].AddParagraph("Left text");
    paragraph.AddTab();
    paragraph.AddText("Right text");
    paragraph.Format.ClearAll();
    // TabStop at column width minus inner margins and borders:
    paragraph.Format.AddTabStop("7.7cm", TabAlignment.Right);
    row.Cells[1].AddParagraph("Second column");
    table.Borders.Width = 1;
    
  • 3

    在单行上,您可以"correct"行高, SpaceAfter 属性等于fontsize的负值 .

    示例RightAlignedTitle样式:

    // Define style: RightAlignedTitle
      style = document.Styles.AddStyle(Styles.RightAlignedTitle, StyleNames.Normal);
      style.Font.Size = new Unit(18, UnitType.Point);
      style.ParagraphFormat.Alignment = ParagraphAlignment.Right;
      style.ParagraphFormat.SpaceAfter = new Unit(-18, UnitType.Point);
    

    示例代码:

    // First right aligned paragraph
      p = section.AddParagraph();
      p.Style = Styles.RightAlignedTitle;
      p.AddText("Right aligned text");
    
      // Second left aligned paragraph
      p = section.AddParagraph();
      p.Format.Alignment = ParagraphAlignment.Left;
      p.AddText("Left aligned text");
    
  • -1
    private void **PDF_DrawTextRight**(string text, PdfPage page, XGraphics gfx, XFont font, double x, double y, double x2, double y2)
        {
            var m = gfx.MeasureString(text, font);
    
            // Draw the text
            gfx.DrawString(text, font, XBrushes.Black,
              new XRect(x+x2-m.Width, y, x2, y2),
              XStringFormats.TopLeft);
        }
    

    这是另一种方式...在发票应用程序中使用,其中数字右对齐,项目描述左对齐 .

相关问题