使用iTextSharp在PDF页面上查找最后一个对象 [英] Find location last object on PDF page using iTextSharp

查看:160
本文介绍了使用iTextSharp在PDF页面上查找最后一个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在PDF页面上找到最后一个对象的位置和高度?没有页脚。我想在最后一项下方标记模板。不在页脚中,而是直接在最后一行文本下方。文本的数量可能会有所不同,所以我需要计算页面底部是否有足够的空间来插入我的模板。

How can I find the location and the height of the last object on a PDF page? There is no footer. I'm wanting to stamp a template below the last item. Not in the footer but directly below the last line of text. The amount of text may vary so I need to calculate if there is enough room at the bottom of the page to insert my template.

感谢您的帮助,
Aaron

Thanks for any help, Aaron

推荐答案

要确定给定页面上文本和(位图)图像的结束位置,请查看动作中的iText ,第2版示例 ShowTextMargins ,它解析PDF并添加一个显示文本的矩形和(位图)图像边距。

To determine where text and (bitmap) images on a given page end, have a look at the iText in Action, 2nd edition example ShowTextMargins which parses a PDF and adds a rectangle showing the text and (bitmap) images margin.

    PdfReader reader = new PdfReader(src);
    PdfReaderContentParser parser = new PdfReaderContentParser(reader);
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT));
    TextMarginFinder finder;
    for (int i = 1; i <= reader.getNumberOfPages(); i++) {
        finder = parser.processContent(i, new TextMarginFinder());
        PdfContentByte cb = stamper.getOverContent(i);
        cb.rectangle(finder.getLlx(), finder.getLly(),
            finder.getWidth(), finder.getHeight());
        cb.stroke();
    }
    stamper.close();
    reader.close();

请注意,此机制忽略矢量图形(通常也是用于强调文本,彩色背景和装饰性分隔线)因为底层iText解析器包仍然(从iText版本5.4.5开始)忽略它们。

Be aware, though, that this mechanism ignores vector graphics (often also used for underlining text, colored backgrounds, and decorative separating lines) because the underlying iText parser package still (as of iText version 5.4.5) ignores them.

更新:从版本5.5.6开始,iText扩展了解析器包,还包括矢量图解析。这允许扩展原始的 TextMarginFinder 类,实现新的 ExtRenderListener 方法,如此答案产生类似的类 MarginFinder ,它不仅考虑文本,还考虑还有其他类型的内容,例如位图图像和矢量图形。

Update: As of version 5.5.6 iText extends the parser package to also include vector graphics parsing. This allows for an extension of the original TextMarginFinder class implementing the new ExtRenderListener methods as presented in this answer resulting in an analogous class MarginFinder which does not only consider text but also other kind of content, e.g. bitmap images and vector graphics.

这篇关于使用iTextSharp在PDF页面上查找最后一个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆