首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >防止MigraDoc中的段落分裂

防止MigraDoc中的段落分裂
EN

Stack Overflow用户
提问于 2015-11-13 00:04:05
回答 1查看 1.9K关注 0票数 2

我使用MigraDoc从一些数据库表生成c#中的PDF文件。

我的主要问题是,对于我补充的一些段落,它们不能插入到当前的页面中,所以被分割到下一页,它如何被阻止?我希望他们在一页(当前页或下一页)。

代码语言:javascript
代码运行次数:0
运行
复制
        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);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-11-13 07:52:00

段落在KeepTogether成员中有一个Format属性。如果属实,该段的所有行都保留在一页内。

还有一个KeepWithNext属性。如果属实,该段的最后一行将与下一段的第一行放在同一页上。

如果您有一个段落,只需编写如下代码:

代码语言:javascript
代码运行次数:0
运行
复制
paragraphBody.Format.KeepTogether = true;

另请参阅:

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

表格单元格永远不会跨越页面。因此,当应用于表格单元格中的段落时,属性KeepTogetherKeepWithNext没有任何影响。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33683728

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档