2015-11-13 68 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不起作用。