我无法使用Pdfbox添加新页面。

huangapple go评论62阅读模式
英文:

I'm unable to add new pages using Pdfbox

问题

public static void main(String[] args) {
    try {
        PDDocument document = new PDDocument();
        PDPage page = new PDPage(PDRectangle.LETTER);
        document.addPage(page);
        addText(document, page);
        document.save("C:/Java/cda.pdf");
        document.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

private static void addText(PDDocument document, PDPage page) {
    try {
        PDPageContentStream contentStream = new PDPageContentStream(document, page,
                PDPageContentStream.AppendMode.APPEND, true);
        float sY = 750;
        contentStream.beginText();
        contentStream.setFont(PDType1Font.HELVETICA, 12);
        contentStream.newLineAtOffset(60, sY);
        for (int i = 1; i <= 50; i++) {
            contentStream.showText("Lorem Ipsum is simply dummy text of the printing and typesetting industry.");
            contentStream.newLineAtOffset(0, -18);
            System.out.println(sY - 18);
            sY = sY - 18;
            if (sY - 18 < 18) {
                contentStream.endText();
                contentStream.close();
                contentStream = new PDPageContentStream(document, new PDPage(PDRectangle.LETTER),
                        PDPageContentStream.AppendMode.APPEND, true);
                contentStream.setFont(PDType1Font.HELVETICA, 12);
                contentStream.beginText();
                sY = 750;
            }
        }
        contentStream.endText();
        contentStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Note: The provided code is returned in its original form without translation, as requested.

英文:
public static void main(String[] args) {
try {
PDDocument document = new PDDocument();
PDPage page = new PDPage(PDRectangle.LETTER);
document.addPage(page);
addText(document, page);
document.save(&quot;C:/Java/cda.pdf&quot;);
document.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void addText(PDDocument document, PDPage page) {
try {
PDPageContentStream contentStream = new PDPageContentStream(document, page,
PDPageContentStream.AppendMode.APPEND, true);
float sY = 750;
contentStream.beginText();
contentStream.setFont(PDType1Font.HELVETICA, 12);
contentStream.newLineAtOffset(60, sY);
for (int i = 1; i &lt;= 50; i++) {
contentStream.showText(&quot;Lorem Ipsum is simply dummy text of the printing and typesetting industry.&quot;);
contentStream.newLineAtOffset(0, -18);
System.out.println(sY - 18);
sY = sY - 18;
if (sY - 18 &lt; 18) {
contentStream.endText();
contentStream.close();
contentStream = new PDPageContentStream(document, new PDPage(PDRectangle.LETTER),
PDPageContentStream.AppendMode.APPEND, true);
contentStream.setFont(PDType1Font.HELVETICA, 12);
contentStream.beginText();
sY = 750;
}
}
contentStream.endText();
contentStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
  • for single-page code working fine but when content is exceeding the first page then it won't add a new
    page
  • unable to find what I'm missing
  • I think the problem is in 'if' block of addText()

答案1

得分: 1

你需要再次调用:

document.addPage(page);

您在问题中的代码创建了一个“孤立”的PDPage对象中的第二个页面内容流,但没有将其添加到文档中。

我没有重写代码,因为您需要进行一些重构,因为addText()已经有一个PDPage对象。

英文:

You need to call

document.addPage(page);

a second time too. Your code in the question creates a second page content stream in an "orphan" PDPage object, and doesn't add it to the document.

I haven't rewritten the code because you'll need some refactoring, because addText() already has a PDPage object.

huangapple
  • 本文由 发表于 2020年4月6日 11:44:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/61052634.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定