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

问题描述 投票:6回答:3

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

我试图用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),但我无法对我的表做同样的事情。我不明白它是如何工作的。

编辑:部分解决方案:

在努力了解它是如何工作之后,我的代码部分正常工作。部分因为我发现右对齐的唯一方法是创建一个具有近似值的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函数的好价值?我把67放,因为68to70不起作用。

c# pdf pdf-generation pdfsharp migradoc
3个回答
8
投票

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

然后确保只定义了一个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
投票

在单行上,您可以使用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);
    }

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

© www.soinside.com 2019 - 2024. All rights reserved.