有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

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

java。RuntimeException:java。木卫一。EOFEException

您可以在此处找到代码:

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);

请告诉我怎么了


共 (1) 个答案

  1. # 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();
    }
    

    ShowImage.java

    我没有得到EOFException,相反,结果看起来和预期的一样:

    Screenshot of resulting PDF with image

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