2015-11-13 27 views
0

我使用MigraDoc在c#中从某些数据库表中生成PDF文件。防止MigraDoc中的段落分割

我的主要问题是一些我添加了一段,他们不能被安装到的当前页面,所以被划分到下一个页面,如何预防? 我希望他们在一个页面(当前页面或下一页)。

 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); 

回答

1

段具有在Format构件KeepTogether属性。如果为true,则该段落的所有行都保留在一页上。
还有一个KeepWithNext属性。如果为true,则该段落的最后一行将与下一段落的第一行位于同一页面上。

如果你有一个段落,简直像这样写代码:

paragraphBody.Format.KeepTogether = true; 

参见:
http://www.nudoq.org/#!/Packages/PDFsharp-MigraDoc-GDI/MigraDoc.DocumentObjectModel/ParagraphFormat

表单元格将永远不会跨页断。因此,应用于表格单元格中的段落时,属性KeepTogetherKeepWithNext不起作用。