用pdfBox给pdf文档插入图片时,eclipse本地run as application开发,代码抛出异常java.lang.IllegalStateException
代码如下:
public class pdfdemo {
public static void main(String args[]) {
pdfdemo pdf = new pdfdemo();
//插入图片
try {
pdf.AddImagetoPDF("F:\yunsign\pdfwithText.pdf", "F:\yunsign\userseal.jpg", "F:\yunsign\pdfwithImage.pdf");
} catch (COSVisitorException | IOException e) {
e.printStackTrace();
}
}
//插入函数
public void AddImagetoPDF(String inputFile, String image, String outputFile)
throws IOException, COSVisitorException {
PDDocument doc = null;
try {
doc = PDDocument.load(inputFile);
PDPage page = (PDPage) doc.getDocumentCatalog().getAllPages().get(0);
PDXObjectImage ximage = null;
ximage = new PDJpeg(doc, new FileInputStream(image)); //报错行*******************//报错行
PDPageContentStream contentStream = new PDPageContentStream(doc, page, true, true);
float scale = 0.5f;
contentStream.drawXObject(ximage, 20, 20, ximage.getWidth() * scale, ximage.getHeight() * scale);
//contentStream.drawImage(ximage, 20, 20);
contentStream.close();
doc.save(outputFile);
} finally {
if (doc != null) {
doc.close();
}
}
}
}
报错截图:
给pdf文档插入图片的代码,我是直接在pdfbox组件中招的源代码的例子,怎么还会报错呢?
网上搜了下,java.lang.IllegalStateException异常,一般是在web容器生成的servlet代码中有out.write(””),这个和JSP中调用的response.getOutputStream()冲突,可是我的代码压根儿没部署到web端,只是在本地run as javaApplication
希望知道的人帮我解决一下,感激不尽。