Apache PDFbox Java获取错误无法读取输入文件
我正在尝试使用PDFBox 2.0.1版将徽标添加到我的PDF文件中。我有以下代码:Apache PDFbox Java获取错误无法读取输入文件,java,pdfbox,Java,Pdfbox,我正在尝试使用PDFBox 2.0.1版将徽标添加到我的PDF文件中。我有以下代码: public class PDFService { public void createPdf() { // Create a document and add a page to it PDDocument document = new PDDocument(); PDPage page = new PDPage(); docume
public class PDFService {
public void createPdf() {
// Create a document and add a page to it
PDDocument document = new PDDocument();
PDPage page = new PDPage();
document.addPage(page);
// Create a new font object selecting one of the PDF base fonts
PDFont font = PDType1Font.HELVETICA_BOLD;
ServletContext servletContext = (ServletContext) FacesContext
.getCurrentInstance().getExternalContext().getContext();
try {
PDImageXObject pdImage = PDImageXObject.createFromFile(
servletContext.getRealPath("/resources/images/logo.png"),
document);
PDPageContentStream contentStream = new PDPageContentStream(
document, page);
contentStream.drawImage(pdImage, 20, 20);
contentStream.beginText();
contentStream.setFont(font, 12);
contentStream.endText();
// Make sure that the content stream is closed:
contentStream.close();
// Save the results and ensure that the document is properly closed:
document.save("Hello World.pdf");
document.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
我收到错误javax.imageio.IIOException:无法读取输入文件!排队
PDImageXObject pdImage = PDImageXObject.createFromFile(
servletContext.getRealPath("/resources/images/logo.png"),
document);
servletContext.getRealPath返回的路径是C:\Users\erickpezoa\Desktop\Multivision\Materials\apps\eclipse-Kepler\eclipse\Projects\.metadata\.plugins\org.eclipse.core.resources\service\u Exequiales\build\weboutput\resources\images\logo.png
我做错了什么?如果您使用的是Maven,并且您的图像文件夹位于Eclipse中的src/main/resources下,您可以尝试:
PDImageXObject pdImage = PDImageXObject.createFromFile(
PDFService.class.getResource("/images/logo.png").getPath(),
document);
如果在src/main/resources
下有另一个名为resources
的文件夹,则只需要使用/resources/images/logo.png
作为路径。或者不使用Maven,输出文件夹包含:/resources/images。在这种情况下:
PDImageXObject pdImage = PDImageXObject.createFromFile(
PDFService.class.getResource("/resources/images/logo.png").getPath(),
document);
希望有帮助。如果您使用的是Maven,并且您的图像文件夹位于Eclipse中的src/main/resources下,您可以尝试:
PDImageXObject pdImage = PDImageXObject.createFromFile(
PDFService.class.getResource("/images/logo.png").getPath(),
document);
如果在src/main/resources
下有另一个名为resources
的文件夹,则只需要使用/resources/images/logo.png
作为路径。或者不使用Maven,输出文件夹包含:/resources/images。在这种情况下:
PDImageXObject pdImage = PDImageXObject.createFromFile(
PDFService.class.getResource("/resources/images/logo.png").getPath(),
document);
希望有帮助。该目录中是否有
logo.png
?是的,logo是there@Erick如果插入ImageIO.read(新文件(servletContext.getRealPath(“/resources/images/logo.png”)),会发生什么代码>-你在那一行有例外吗?如果是,则表示文件根本不存在。(可能是您在其他时间检查的),或者它没有所需的权限。该目录中是否有logo.png
?是的,logo是there@Erick如果插入ImageIO.read(新文件(servletContext.getRealPath(“/resources/images/logo.png”)),会发生什么代码>-你在那一行有例外吗?如果是,则表示文件根本不存在。(可能是您在其他时间检查的),或者它没有所需的权限。