在 PDFBox 中写入多行和多页 - 获取 PDPageContentStream Y 轴
Writing multiple lines and pages in PDFBox - Get PDPageContentStream Y-Axis
我正在测试 PDFBox,我对编写新文档有疑问..
下面的代码在一个pdf文件中写了75行。文件的高度不够。所以我需要知道 contentStream 何时到达页面末尾,以便创建一个新的页面并继续写行。
有什么办法可以解决我的问题吗?
非常感谢!
File pdfFile = new File("hello.pdf");
PDDocument doc = new PDDocument();
PDPage page = new PDPage();
PDPageContentStream contentStream = new PDPageContentStream(doc, page);
contentStream.setFont( PDType1Font.TIMES_ROMAN, 12);
float initPosY = page.getMediaBox().getHeight()-50;
contentStream.beginText();
contentStream.newLineAtOffset(25, initPosY);
contentStream.setLeading(30.5f);
for(int i=1; i<75;i++){
contentStream.showText("Line: "+i);
contentStream.newLine();
}
contentStream.endText();
contentStream.close();
doc.addPage(page);
doc.save(pdfFile);
doc.close();
调用 newLine() 后,从循环中的 initPosY 中减去主值 (30.5f)。当它 < 0 或低于有用值(例如 50,这是您的上边距)时,您应该开始一个新页面。
我正在测试 PDFBox,我对编写新文档有疑问..
下面的代码在一个pdf文件中写了75行。文件的高度不够。所以我需要知道 contentStream 何时到达页面末尾,以便创建一个新的页面并继续写行。
有什么办法可以解决我的问题吗?
非常感谢!
File pdfFile = new File("hello.pdf");
PDDocument doc = new PDDocument();
PDPage page = new PDPage();
PDPageContentStream contentStream = new PDPageContentStream(doc, page);
contentStream.setFont( PDType1Font.TIMES_ROMAN, 12);
float initPosY = page.getMediaBox().getHeight()-50;
contentStream.beginText();
contentStream.newLineAtOffset(25, initPosY);
contentStream.setLeading(30.5f);
for(int i=1; i<75;i++){
contentStream.showText("Line: "+i);
contentStream.newLine();
}
contentStream.endText();
contentStream.close();
doc.addPage(page);
doc.save(pdfFile);
doc.close();
调用 newLine() 后,从循环中的 initPosY 中减去主值 (30.5f)。当它 < 0 或低于有用值(例如 50,这是您的上边距)时,您应该开始一个新页面。