如何使用PDFClown将图片添加到pdf文件

Mil*_*ani 6 java eofexception pdfclown

我使用PDFClown .jar库将jpeg图像转换为pdf文件.但是,我得到以下错误:

java.lang.RuntimeException:java.io.EOFException

在这里你可以找到代码:

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);
Run Code Online (Sandbox Code Playgroud)

请告诉我有什么问题?

mkl*_*mkl 1

我只是尝试重现您的问题:

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();
}
Run Code Online (Sandbox Code Playgroud)

显示图像.java

我得到 no EOFException,结果看起来如预期:

生成的带有图像的 PDF 的屏幕截图

因此,该问题似乎与您的 JPG 文件有关,其内容可能已损坏或超出 PdfClown 的 JPG 支持,或者可能是与文件系统权限相关的问题。