繁体   English   中英

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

[英]How to add picture to pdf file using 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);

请告诉我有什么问题?

我只是想重现你的问题:

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

我没有得到EOFException ,而是看起来像预期的结果:

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

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

暂无
暂无

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

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