【问题标题】:NoClassDefFoundError on adding image to a pdf page in PDFBox Android在 PDFBox Android 中将图像添加到 pdf 页面时出现 NoClassDefFoundError
【发布时间】:2017-05-10 07:35:48
【问题描述】:

尝试在 pdf 页面上写入图像

      PDDocument document = null;
        File inputFile = new File(mFilePath);
        document = PDDocument.load(inputFile);
        PDPage page = document.getPage(0);

        File image = new File("/storage/emulated/0/", "1.jpg");

        PDImageXObject img = JPEGFactory.createFromStream(document, inputStream);
        PDPageContentStream contentStream = new PDPageContentStream(document, page);
        contentStream.drawImage(img, 100, 100);
        contentStream.close();

        File outputFile = new File(inputFile.getParent(), "new file.pdf");
        document.save(outputFile);
        document.close();

但得到这个异常:

 java.lang.NoClassDefFoundError: Failed resolution of: Ljavax/imageio/ImageIO;
      at org.apache.pdfbox.pdmodel.graphics.image.JPEGFactory.readJPEG(JPEGFactory.java:99)
      at org.apache.pdfbox.pdmodel.graphics.image.JPEGFactory.createFromStream(JPEGFactory.java:78)

注意:我也试过用

PDImageXObject img = PDImageXObject.createFromFile(image.getPath(), document);

但没有发生任何不同。 我该怎么做才能毫无例外地将图像添加到当前页面中的位置? (如果您知道更好的解决方案,请告诉我)

【问题讨论】:

    标签: android pdf pdfbox


    【解决方案1】:

    最后,我使用Android Port of PDFBox(Link to answer) 并使用来自this link 的示例代码将图像添加到pdf:

     /**
     * Add an image to an existing PDF document.
     *
     * @param inputFile The input PDF to add the image to.
     * @param imagePath The filename of the image to put in the PDF.
     * @param outputFile The file to write to the pdf to.
     *
     * @throws IOException If there is an error writing the data.
     */
    public void createPDFFromImage( String inputFile, String imagePath, String outputFile )
            throws IOException
    {
        // the document
        PDDocument doc = null;
        try
        {
            doc = PDDocument.load( new File(inputFile) );
    
            //we will add the image to the first page.
            PDPage page = doc.getPage(0);
    
            // createFromFile is the easiest way with an image file
            // if you already have the image in a BufferedImage, 
            // call LosslessFactory.createFromImage() instead
            PDImageXObject pdImage = PDImageXObject.createFromFile(imagePath, doc);
            PDPageContentStream contentStream = new PDPageContentStream(doc, page, AppendMode.APPEND, true);
    
            // contentStream.drawImage(ximage, 20, 20 );
            // better method inspired by https://stackoverflow.com/a/22318681/535646
            // reduce this value if the image is too large
            float scale = 1f;
            contentStream.drawImage(pdImage, 20, 20, pdImage.getWidth()*scale, pdImage.getHeight()*scale);
    
            contentStream.close();
            doc.save( outputFile );
        }
        finally
        {
            if( doc != null )
            {
                doc.close();
            }
        }
    }
    

    【讨论】:

    猜你喜欢
    • 2019-04-16
    • 2012-01-21
    • 1970-01-01
    • 2014-06-28
    • 2015-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多