我正在使用 MigraDoc 创建 PDF 文件。我在文本框中添加两个段落。一个段落是标签,另一个段落是文本框。将段落添加到文本框架后,我将文本框架添加到表行单元格。目前,文本框位置位于标签下方。我希望它位于标签旁边,或者它们位于一行。有人知道这个问题的解决方案吗?请帮忙。这是我的代码和图像
代码:
static void AddTextBlockAndTextBoxToRow(Row row, int cellIndex, Paragraph label, Paragraph textbox)
{
var textFrame = new TextFrame();
label.Format.Alignment = ParagraphAlignment.Left;
textbox.Format.Alignment = ParagraphAlignment.Left;
textFrame.Add(label);
textFrame.Add(textbox);
row.Cells[cellIndex].Add(textFrame);
}
图片
MigraDoc 无法并排显示两个段落。不在一个表格单元格中,不在一个 TextFrame 中。
您可以在 TextFrame 中创建一个包含两列的表格来解决此限制。
或者不使用 TextFrame 并在主表中创建两个单元格(您可以对其他行使用 MergeRight 来合并其他行中的这两个单元格)。
pdfsharp 中的示例表格程序....
Table table = document.LastSection.AddTable();
table.Borders.Visible = true;
table.Format.Shading.Color = Colors.LavenderBlush;
table.Shading.Color = Colors.Salmon;
table.TopPadding = 5;
table.BottomPadding = 5;
Column column = table.AddColumn();
column.Format.Alignment = ParagraphAlignment.Left;
column = table.AddColumn();
column.Format.Alignment = ParagraphAlignment.Center;
column = table.AddColumn();
column.Format.Alignment = ParagraphAlignment.Right;
table.Rows.Height = 35;
Row row = table.AddRow();
row.VerticalAlignment = VerticalAlignment.Top;
row.Cells[0].AddParagraph("Text");
row.Cells[1].AddParagraph("Text");
row.Cells[2].AddParagraph("Text");
row = table.AddRow();
row.VerticalAlignment = VerticalAlignment.Center;
row.Cells[0].AddParagraph("Text");
row.Cells[1].AddParagraph("Text");
row.Cells[2].AddParagraph("Text");
row = table.AddRow();
row.VerticalAlignment = VerticalAlignment.Bottom;
row.Cells[0].AddParagraph("Text");
row.Cells[1].AddParagraph("Text");
row.Cells[2].AddParagraph("Text");
最高分答案是错误的。可以使用负 SpaceBefore 值将段落添加到同一行:
var document = new Document { };
var section = document.AddSection();
section.AddParagraph("paragraph1" + Environment.NewLine + "second line");
var par2 = section.AddParagraph("paragraph2");
par2.Format.LeftIndent = "4cm";
par2.Format.SpaceBefore = "-0.8cm";
return document;
输出: