如何使用PDFBox将文本写入生成的pdf文档的末尾?

时间:2015-10-06 13:13:47

标签: java pdfbox

使用PDPageContentStream我可以将图像和文本放在生成的pdf页面的顶部,但我无法将文本放在pdf文档的末尾。 如何获取调用moveTextPositionByAmount(x,y)的确切x,y值?请参阅下面的示例代码

PDDocument document = new PDDocument();  
PDXObjectImage img = new PDJpeg(document, new FileInputStream("F:/Project/demo_logo.jpg"));  
PDPage imagePage = PDFUtility.addNewPage(document);  
PDPageContentStream contentStream = new PDPageContentStream(document, imagePage);  
contentStream.beginText();  
contentStream.setFont(PDType1Font.HELVETICA_BOLD, 14);
contentStream.moveTextPositionByAmount(10, 680);
contentStream.drawString("Budget Report");  
contentStream.endText();
contentStream.drawImage(img, 10, 700);
float yStartNewPage = imagePage.findMediaBox().getHeight() - (12 * 10);
float tableWidth = imagePage.findMediaBox().getWidth() - (2 * 10);
boolean drawContent = true;
float yStart = yStartNewPage;
float bottomMargin = 500;
BaseTable table  = new BaseTable(yStart,yStartNewPage, bottomMargin, tableWidth, 10, document, imagePage, true, drawContent);
.............generating reports using table(BaseTable)..............
table.draw();
File file = new File("F:/project/demo.pdf");
Files.createParentDirs(file);
resultArray.put(file.getAbsolutePath());
contentStream.beginText();
contentStream.setFont(PDType1Font.HELVETICA_BOLD, 14 );
/* reports are generating dynamically, i cannot give value to x and y */
contentStream.moveTextPositionByAmount(x, y);
contentStream.drawString("End Of Report");
contentStream.endText();
contentStream.close();
document.save(String.valueOf(file));
document.close();

0 个答案:

没有答案