我正在编写一个使用pdfbox库从头开始创建PDF的Java应用程序。
我需要放置一个JPG图像在其中一个页面。无法使用PDFBox将图像添加到PDF
我使用这个代码:
PDDocument document = new PDDocument();
PDPage page = new PDPage(PDPage.PAGE_SIZE_A4);
document.addPage(page);
PDPageContentStream contentStream = new PDPageContentStream(document, page);
/* ... */
/* code to add some text to the page */
/* ... */
InputStream in = new FileInputStream(new File("c:/myimg.jpg"));
PDJpeg img = new PDJpeg(document, in);
contentStream.drawImage(img, 100, 700);
contentStream.close();
document.save("c:/mydoc.pdf");
当我运行的代码,它成功终止,但如果我打开使用Acrobat Reader软件生成的PDF文件,页面完全是白色和图像不放在里面。
文本正确放置在页面中。
任何提示如何把我的形象在PDF?
在PDPageContentStream解决我的问题之前移动PDJpeg创建,非常感谢。 – 2011-12-22 09:11:19