简体   繁体   English

如何在 iText7 的特定页面底部插入元素?

[英]How can I insert an element to the bottom of a specific page in iText7?

I'm exploring different options for .NET PDF libraries.我正在探索 .NET PDF 库的不同选项。 One of my requirements is to place a box at the bottom of the first page and if any of the content reaches the box, it should overflow onto the next page.我的一个要求是在第一页的底部放置一个框,如果任何内容到达该框,它应该溢出到下一页。

For example:例如: 在此处输入图像描述

Shown above, paragraph 7 would normally take up some of the space that's occupied by the "reserved" area.如上所示,第 7 段通常会占用“保留”区域所占用的一些空间。 Instead, the part that would have taken up that space is shifted to the next page.相反,将占用该空间的部分转移到下一页。

That image was achieved using Gembox.Document by adding the box as a footer element that only renders on the first page.该图像是使用 Gembox.Document 通过将框添加为仅在第一页上呈现的页脚元素来实现的。 However, in iText7, the examples I've seen for adding a footer ( such as this one ), places the content as a floating element that renders over the existing content and does not affect the layout/flow of the rest of the document.但是,在 iText7 中,我看到的添加页脚的示例(例如这个)将内容作为浮动元素呈现在现有内容之上,并且不影响文档的 rest 的布局/流程。

I also tried adding a paragraph on the PageEnd event handler without the canvas (snippet below), but instead of adding it to the specified page, it's added to the end of the entire document.我还尝试在没有 canvas(下面的片段)的情况下在 PageEnd 事件处理程序上添加一个段落,但不是将其添加到指定页面,而是将其添加到整个文档的末尾。

public void HandleEvent(Event evt)
{
    var docEvent = (PdfDocumentEvent)evt;
    var page = docEvent.GetPage();
    int pageNum = docEvent.GetDocument().GetPageNumber(page);

    if (pageNum == 1)
    {
        doc.Add(new Paragraph("Testing a thing"));
    }
}

Is the type of effect I'm looking for something that I can replicate using iText7?我正在寻找可以使用 iText7 复制的效果类型吗?

As you said, you are exploring different .NET PDF libraries.正如您所说,您正在探索不同的 .NET PDF 库。 So I would advise PDFFlow library , which does exactly what you need.所以我会建议PDFFlow library ,它完全可以满足您的需求。 If you have a footer, main document flow will take the rest of page area and will be automatically continued at the next page without overlaying footer.如果您有页脚,则主文档流将采用页面区域的 rest 并且将自动继续到下一页而不覆盖页脚。

DocumentBuilder.New()
    .AddSection()
        .AddParagraph("long text")
    .ToSection()
        .AddFooterToBothPages(40)
            .AddParagraph("this a footer set for each page of this section")
.ToDocument()
    .Build("result.pdf");

重复区域示例

Here is a tutorial with code examples of using headers, footers, left/right repeating areas: AddingRepeatingArea tutorial .这是一个教程,其中包含使用页眉、页脚、左/右重复区域的代码示例: AddingRepeatingArea 教程

Hope, this will help you:)希望对你有帮助:)

I believe you can combine the concepts of https://github.com/itext/i7ns-samples/blob/develop/itext/itext.samples/itext/samples/sandbox/acroforms/AddExtraTable.cs and https://github.com/itext/i7ns-samples/blob/develop/itext/itext.samples/itext/samples/sandbox/events/TextFooter.cs to achieve what you need.我相信你可以结合https://github.com/itext/i7ns-samples/blob/develop/itext/itext.samples/itext/samples/sandbox/acroforms/AddExtraTable.cshttps://github的概念。 com/itext/i7ns-samples/blob/develop/itext/itext.samples/itext/samples/sandbox/events/TextFooter.cs来实现你所需要的。

The idea is as follows:思路如下:

  • reserve place for your box by making iText give the document's renderer less space for the first page通过使 iText 为文档的渲染器减少第一页的空间,为您的盒子保留位置
  • fill this box with a help of iText's end page events借助 iText 的结束页面事件填充此框

Another option was suggested in How can I insert an element to the bottom of a specific page in iText7? 如何在 iText7 的特定页面底部插入元素? : you can temporary call Document#setBottomMargin , since elements added via Document#add will not be placed on margins. :您可以临时调用Document#setBottomMargin ,因为通过Document#add的元素不会放置在边距上。 Then, once the first page is layouted, you can set the initial margins again.然后,一旦第一页布局,您可以再次设置初始边距。 This option, however, requires understanding of you layout flow, since the margins should be set only after the content of the first page is layouted.但是,此选项需要了解您的布局流程,因为只有在第一页的内容布局后才应设置边距。

One more suggestion: althouth event functionality is rather flexible and useful, it seems like using a sledgehammer to crack a nut.再提一个建议:虽然事件功能还是比较灵活有用的,感觉就像是用大锤敲碎了坚果。 You need to call Canvas#ShowTextAligned , which could be done without any event handling.您需要调用Canvas#ShowTextAligned ,这可以在没有任何事件处理的情况下完成。 So ideally I would prefer to do the following:因此,理想情况下,我更愿意执行以下操作:

  • handle page's layout area via an extension of DocumentRenderer通过DocumentRenderer的扩展处理页面的布局区域
  • Calling Canvas#ShowTextAligned to fill the reserved box.调用Canvas#ShowTextAligned填充保留框。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM