5

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

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

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

// 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必须跟踪类似的值。有没有办法访问这个值?

谢谢。

4

3 回答 3

4

创建一个heightCounter变量来跟踪您将文本位置移动了多远。它的初始值可以是您的起始 Y 位置。

        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小于您移动文本位置的时间,然后创建一个新页面。

于 2013-12-10T06:02:12.263 回答
0
        contentStream.beginText();
        contentStream.setFont(pdfFont, fontSize);
        contentStream.moveTextPositionByAmount(startX, startY);

        for (String line : lines) {
            if(height>705){  //this is the height of my bottom line where I want cutoff. you can check yours by sysoout the content.
            line = line.trim();
            float charSpacing = 0;
            if (line.length() > 1) {
                float size = fontSize * pdfFont.getStringWidth(line) / 1000;
                float free = width - size;

                if (free > 0) {
                    charSpacing = free / (line.length() - 1);
                }
            }
            contentStream.drawString(line);
            contentStream.moveTextPositionByAmount(0, -leading);
            System.out.println("content Stream line :" + line);
            height--;
            System.out.println("value of height:"+ height);                          
                }

            else{
                contentStream.endText();
                contentStream.close();
                page = new PDPage(PDPage.PAGE_SIZE_A4);
                doc.addPage(page);
                contentStream = new PDPageContentStream(doc, page,false, true, true);
                contentStream.beginText();
                contentStream.setFont(pdfFont, fontSize);
                contentStream.moveTextPositionByAmount(startX, startY);
                System.out.println("Height counter value :"+ height);
                System.out.println("line insde the second page:" + line);
                contentStream.drawString(line);
                System.out.println("Output on second page:"+contentStream.toString());
                contentStream.moveTextPositionByAmount(0, -leading);  
                height=mediabox.getHeight() - 2 * margin; //

            }

        }
        contentStream.endText();
        contentStream.close();

    } 
于 2019-07-12T21:55:52.187 回答
0

这是我在顶部的配置,供您参考我的使用方式。快乐编码..

    PDPageContentStream contentStream = new PDPageContentStream(doc, page,true, true, true);
   //PDPage page1 = new PDPage(PDPage.PAGE_SIZE_A4);
    PDPageContentStream contentStream1 = new PDPageContentStream(doc, page,true, true, true);
    PDFont pdfFont = PDType1Font.COURIER;
    PDFont fontBold = PDType1Font.TIMES_BOLD;
    float leading = 1.5f * fontSize;
    PDRectangle mediabox = page.getMediaBox();
    float margin = 45;
    float width = mediabox.getWidth() - 2 * margin;
    float height = mediabox.getHeight() - 2 * margin;
    float startX = mediabox.getLowerLeftX() + margin - statVarX;
    float startY = mediabox.getUpperRightY() - margin - statVarY; 
于 2019-07-12T22:00:24.077 回答