How to add an image to a pdf file using PDFClown

I use the PDFClown.jar library to convert jpeg images to pdf files. However, I get the following error:

java.lang.RuntimeException: java.io.EOFException

Here you will find the code:

org.pdfclown.documents.contents.entities.Image image = org.pdfclown.documents.contents.entities.Image.get("c:" + java.io.File.separator + "bg.jpg"); org.pdfclown.documents.contents.xObjects.XObject imageXObject = image.toXObject(document); composer.showXObject(imageXObject); composer.flush(); document.getFile().save("c:\\test.pdf" , SerializationModeEnum.Standard); 

Please let me know what is wrong?

+6
source share
1 answer

I just tried to reproduce your problem:

 public void testAddPicture() throws IOException { org.pdfclown.files.File file = new org.pdfclown.files.File(); Page page = new Page(file.getDocument()); file.getDocument().getPages().add(page); PrimitiveComposer primitiveComposer = new PrimitiveComposer(page); Image image = Image.get("src\\test\\resources\\mkl\\testarea\\pdfclown0\\content\\Willi-1.jpg"); XObject imageXObject = image.toXObject(file.getDocument()); primitiveComposer.showXObject(imageXObject, new Point2D.Double(100,100), new Dimension(300, 300)); primitiveComposer.flush(); file.save(new File(RESULT_FOLDER, "PdfWithImage.pdf"), SerializationModeEnum.Standard); file.close(); } 

( ShowImage.java )

I am not getting an EOFException , instead the result looks as expected:

Screenshot of the resulting PDF image

So the problem seems to be related to your JPG file, its contents are probably broken or outside of PdfClown JPG support, or it could be a file system permissions issue.

+1
source

All Articles