How do you track the location of PDPageContentStream's text output?

☆樱花仙子☆ 提交于 2019-12-05 21:04:06

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;

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.

        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();

    } 

this is my configuration on the top for your reference how I am using. happy coding ..

    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; 
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!