防止 MigraDoc 中的段落拆分
Preventing paragraph splitting in MigraDoc
我使用 MigraDoc 从一些数据库表中用 c# 生成 PDF 文件。
我的主要问题是我添加的一些段落无法放入当前页面,因此被拆分到下一页,如何预防?
我希望它们在一页中(当前页或下一页)。
Document doc = new Document();
Section section = doc.AddSection();
Paragraph paragraph = section.AddParagraph();
paragraph.AddLineBreak();
paragraph.AddLineBreak();
paragraph.AddLineBreak();
paragraph.Format.TabStops.ClearAll();
paragraph.Format.TabStops.AddTabStop("16cm", TabAlignment.Right, TabLeader.Lines);
paragraph.AddTab();
for (int i = 0; i < 20; i++)
{
Paragraph paragraphBody = paragraph.Section.AddParagraph();
FormattedText ft = paragraphBody.AddFormattedText("This is a title", TextFormat.Bold);
ft.Italic = true; ft.Font.Size = 11;
ft.Font.Color = Color.FromRgbColor((byte)255, Color.Parse("0x1E9BC6")); //equal to rgb(30, 155, 196);
ft.AddLineBreak();
//--detail:---adding text---------------------------------
String DetailText = "This is detail. This is detail. This is detail.This is detail.This is detail.This is detail.This is detail.This is detail. This is detail. This is detail. This is detail. This is detail. This is detail. This is detail. This is detail. This is detail. ";
FormattedText ftdet;
ftdet = paragraphBody.AddFormattedText(DetailText, TextFormat.NotBold);
ftdet.Font.Size = 10;
ftdet.Font.Name = "Arial";
ftdet.AddLineBreak();
ftdet.AddLineBreak();
ftdet.AddText("Event Date: " + DateTime.Now.ToString("MM/dd/yyyy h:mm tt"));
}
PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer(false, PdfFontEmbedding.Always);
pdfRenderer.Document = doc;
pdfRenderer.RenderDocument();
//Save the PDF to a file:
string filename = "e:\Report" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".pdf";
pdfRenderer.PdfDocument.Save(filename);
Process.Start(filename);
段落在 Format
成员中有一个 KeepTogether
属性。如果为 true,则该段落的所有行都保留在一页上。
还有一个KeepWithNext
属性。如果为 true,该段落的最后一行将与下一段的第一行在同一页上。
如果你有一个段落,只需像这样编写代码:
paragraphBody.Format.KeepTogether = true;
另请参阅:
http://www.nudoq.org/#!/Packages/PDFsharp-MigraDoc-GDI/MigraDoc.DocumentObjectModel/ParagraphFormat
Table 单元格永远不会跨页。因此,属性 KeepTogether
和 KeepWithNext
在应用于 table 单元格中的段落时无效。
我使用 MigraDoc 从一些数据库表中用 c# 生成 PDF 文件。
我的主要问题是我添加的一些段落无法放入当前页面,因此被拆分到下一页,如何预防? 我希望它们在一页中(当前页或下一页)。
Document doc = new Document();
Section section = doc.AddSection();
Paragraph paragraph = section.AddParagraph();
paragraph.AddLineBreak();
paragraph.AddLineBreak();
paragraph.AddLineBreak();
paragraph.Format.TabStops.ClearAll();
paragraph.Format.TabStops.AddTabStop("16cm", TabAlignment.Right, TabLeader.Lines);
paragraph.AddTab();
for (int i = 0; i < 20; i++)
{
Paragraph paragraphBody = paragraph.Section.AddParagraph();
FormattedText ft = paragraphBody.AddFormattedText("This is a title", TextFormat.Bold);
ft.Italic = true; ft.Font.Size = 11;
ft.Font.Color = Color.FromRgbColor((byte)255, Color.Parse("0x1E9BC6")); //equal to rgb(30, 155, 196);
ft.AddLineBreak();
//--detail:---adding text---------------------------------
String DetailText = "This is detail. This is detail. This is detail.This is detail.This is detail.This is detail.This is detail.This is detail. This is detail. This is detail. This is detail. This is detail. This is detail. This is detail. This is detail. This is detail. ";
FormattedText ftdet;
ftdet = paragraphBody.AddFormattedText(DetailText, TextFormat.NotBold);
ftdet.Font.Size = 10;
ftdet.Font.Name = "Arial";
ftdet.AddLineBreak();
ftdet.AddLineBreak();
ftdet.AddText("Event Date: " + DateTime.Now.ToString("MM/dd/yyyy h:mm tt"));
}
PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer(false, PdfFontEmbedding.Always);
pdfRenderer.Document = doc;
pdfRenderer.RenderDocument();
//Save the PDF to a file:
string filename = "e:\Report" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".pdf";
pdfRenderer.PdfDocument.Save(filename);
Process.Start(filename);
段落在 Format
成员中有一个 KeepTogether
属性。如果为 true,则该段落的所有行都保留在一页上。
还有一个KeepWithNext
属性。如果为 true,该段落的最后一行将与下一段的第一行在同一页上。
如果你有一个段落,只需像这样编写代码:
paragraphBody.Format.KeepTogether = true;
另请参阅:
http://www.nudoq.org/#!/Packages/PDFsharp-MigraDoc-GDI/MigraDoc.DocumentObjectModel/ParagraphFormat
Table 单元格永远不会跨页。因此,属性 KeepTogether
和 KeepWithNext
在应用于 table 单元格中的段落时无效。