如何跟踪 PDPageContentStream 文本输出的位置? [英] How do you track the location of PDPageContentStream's text output?

查看:80
本文介绍了如何跟踪 PDPageContentStream 文本输出的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Java 将输出写入 PDDocument,然后将该文档附加到现有文档,然后再将其提供给客户端.

I am using Java to write output to a PDDocument, then appending that document to an existing one before serving it to the client.

大部分都运行良好.我在写入 PDDocument 时尝试处理内容溢出只有一个小问题.我想跟踪文本插入文档的位置,以便当光标"可以说超过某个点时,我将创建一个新页面,将其添加到文档中,创建一个新的内容流,并照常继续.

Most of it is working well. I only have a small problem trying to handle content overflow while writing to that PDDocument. I want to keep track of where text is being inserted into the document so that when the "cursor" so to speak goes past a certain point, I'll create a new page, add it to the document, create a new content stream, and continue as normal.

以下是一些代码,显示了我想做的事情:

Here is some code that shows what I'd like to do:

// big try block
PDDocument doc = new PDDocument();
PDPage page = new PDPage();
doc.addPage(page);
PDPageContentStream content = new PDPageContentStream(doc, page);
int fontSize = 12;

content.beginText();
content.setFont(...);
content.moveTextPositionByAmount(margin, pageHeight-margin);
for ( each element in a collection of values ) {
    content.moveTextPositionByAmount(0, -fontSize); // only moves down in document

    // at this point, check if past the end of page, if so add a new page
    if (content.getTextYPosition(...) < margin) { // wishful thinking, doesn't exist
        content.endText();
        content.close();
        page = new PDPage();
        doc.addPage(page);
        content = new PDPageContentStream(doc, page);
        content.beginText();
        content.setFont(...);
        content.moveTextPositionByAmount(margin, pageHeight-(margin+fontSize));
    }
    content.drawString(...);
}
content.endText();
content.close();

重要的是content.getTextYPosition().它实际上并不存在,但我确信 PDPageContentStream 必须跟踪类似的值.有没有办法访问这个值?

The important bit is the content.getTextYPosition(). It doesn't actually exist, but I'm sure PDPageContentStream must be keeping track of a similar value. Is there any way to access this value?

谢谢.

推荐答案

创建一个 heightCounter 变量来跟踪您移动文本位置的距离.它的初始值可以是您的起始 Y 位置.

Create a heightCounter variable that tracks how far you've moved the text location. It's initial value can be your starting Y position.

        PDRectangle mediabox = page.findMediaBox();
        float margin = 72;
        float width = mediabox.getWidth() - 2 * margin;
        float startX = mediabox.getLowerLeftX() + margin;
        float startY = mediabox.getUpperRightY() - margin;
        float heightCounter = startY;

每次移动文本位置时,从 heightCounter 中减去该位置.当 heightCounter 小于您移动文本位置的距离时,则创建一个新页面.

Every time you move the text position, subtract that from your heightCounter. When heightCounter is less than what you're moving the text position by, then create a new page.

这篇关于如何跟踪 PDPageContentStream 文本输出的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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