Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add picture to pdf file using PDFClown

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

java.lang.RuntimeException: java.io.EOFException

Here you can 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?

like image 497
Milad-Irani Avatar asked Dec 20 '15 16:12

Milad-Irani


1 Answers

I just tried to reproduce your issue:

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 get no EOFException, instead the result looks as expected:

Screenshot of resulting PDF with image

Thus, the issue seems to be related to your JPG file, its content probably is broken or beyond PdfClown's JPG support, or it may be a file system permissions related issue.

like image 151
mkl Avatar answered Oct 07 '22 21:10

mkl