简体   繁体   English

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

[英]How to add picture to pdf file using PDFClown

I use PDFClown .jar library in order to convert jpeg images to pdf files. 我使用PDFClown .jar库将jpeg图像转换为pdf文件。 However, I get the below error: 但是,我得到以下错误:

java.lang.RuntimeException: java.io.EOFException 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? 请告诉我有什么问题?

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 ) ShowImage.java

I get no EOFException , instead the result looks as expected: 我没有得到EOFException ,而是看起来像预期的结果:

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

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. 因此,问题似乎与您的JPG文件有关,其内容可能已被破坏或超出PdfClown的JPG支持,或者它可能与文件系统权限相关的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM