英文:
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("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();
}
}
- 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论