簡體   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