我有一个表格,其中有一个单元格,我想要两个文本,第一个在左对齐,第二个在右对齐,在同一个单元格中,在同一行上。
我尝试使用 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 不工作。
最佳答案
链接帖子中显示的技巧非常简单:您只需要一个左对齐的段落。
然后确保只定义了一个制表位,一个位于单元格右边缘的右对齐制表位。
在段落中,添加您想要左对齐的文本,然后添加一个制表位,然后添加您想要右对齐的文本。
示例代码:
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;
关于c# - MigraDoc C# 在同一行左右对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16774066/