MigraDoc C#在同一条线上左右对齐
我有一个表格,有一个单元格,我想要两个文本,第一个在左边对齐,第二个在右边对齐,在同一个单元格中,在同一行 我试图用MigraDoc复制这个细胞,但没有成功。我只能添加左右对齐的两个文本,但不能在同一行上 这是我的代码:MigraDoc C#在同一条线上左右对齐,c#,pdf,pdf-generation,pdfsharp,migradoc,C#,Pdf,Pdf Generation,Pdfsharp,Migradoc,我有一个表格,有一个单元格,我想要两个文本,第一个在左边对齐,第二个在右边对齐,在同一个单元格中,在同一行 我试图用MigraDoc复制这个细胞,但没有成功。我只能添加左右对齐的两个文本,但不能在同一行上 这是我的代码: Cell cellFooter1 = rowFooter.Cells[0]; Paragraph paraphTot = new Paragraph(); paraphTot.Format.Alignment = ParagraphAlignment.Left; paraphT
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);
这里提供了一个解决方案(),但我无法对我的表执行相同的操作。我不明白它是怎么工作的
编辑:部分解决方案:
在努力理解它是如何工作的之后,我的代码部分工作正常。部分,因为我发现右对齐的唯一方法是创建一个具有近似值的制表符。。。不好
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放进去,因为68到70不起作用。链接帖子中显示的技巧相当简单:你只需要一个段落,左对齐 然后确保只定义了一个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;
// 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");
在单行上,您可以使用等于fontsize负值的SpaceAfter属性“更正”行高
右对齐标题样式示例:
// 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);
示例代码:
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;
// 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");
这是另一种方式。。。在发票应用程序中使用,其中数字右对齐,项目描述左对齐 谢谢你的解释。明天上班时我会再测试一次,但像这样,我不知道怎么做。明天有更多新闻。只是添加了示例代码。右制表位必须考虑表格单元格的内页边距和边框。目前我没有更好的想法比试错法(3毫米为我工作)。它可以不使用样式(如我的示例代码所示),但是如果它在您的文档中多次出现,建议使用样式。感谢示例,它可以帮助我。问题是关于MigraDoc的,您的答案使用PDFsharp。没什么用。