Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PDFBox v2 write PNG image to PDF file, getting empty file

I'm using PDFBox 2. Trying to write a PNG image file to new PDF file.

I saw there was already an answer that mention it was fixed on PDFBox2: How to add .png images to pdf using Apache PDFBox and https://issues.apache.org/jira/browse/PDFBOX-1990

This is my code:

package pdfProj;

import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.graphics.image.LosslessFactory;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;

public class b {

    public static void main(String[] args) {
        PDDocument doc = null;
        doc = new PDDocument();        
        doc.addPage(new PDPage());
        try{
            BufferedImage awtImage = ImageIO.read( new File( "c://temp//line_chart.png" ) );
            PDImageXObject  pdImageXObject = LosslessFactory.createFromImage(doc, awtImage);
            PDPageContentStream contentStream = new PDPageContentStream(doc, new PDPage(), true, false);
            contentStream.drawImage(pdImageXObject, 200, 300, awtImage.getWidth() / 2, awtImage.getHeight() / 2);
                contentStream.close();
                doc.save( "c://temp//pdf//PDF_image.pdf" );
            doc.close();
        } catch (Exception io){
            System.out.println(" -- fail --" + io);
        }

    }
}

There is no exception. Just getting an empty PDF file created.

like image 756
Danny L. Avatar asked Feb 14 '16 20:02

Danny L.


People also ask

How do I add a picture to a PDF on PDFBox?

You can insert an image into a PDF document using the createFromFile() and drawImage() methods of the classes PDImageXObject and PDPageContentStream respectively.

Is PDFBox free for commercial use?

Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative ...

What is PDFBox preflight?

The Apache Preflight library is a Java tool that implements a parser compliant with the ISO-19005 specification (aka PDF/A-1).


1 Answers

The issue is that you add a new page to the document

doc.addPage(new PDPage());

but then create a content stream for yet another new page which you don't add to the document:

PDPageContentStream contentStream = new PDPageContentStream(doc, new PDPage(), true, false);

You should create the content stream for the page you added to the document, e.g. like this:

PDDocument doc = null;
doc = new PDDocument();
PDPage page = new PDPage();
doc.addPage(page);
try{
    BufferedImage awtImage = ImageIO.read( new File( "c://temp//line_chart.png" ) );
    PDImageXObject  pdImageXObject = LosslessFactory.createFromImage(doc, awtImage);
    PDPageContentStream contentStream = new PDPageContentStream(doc, page, true, false);
    contentStream.drawImage(pdImageXObject, 200, 300, awtImage.getWidth() / 2, awtImage.getHeight() / 2);
    contentStream.close();
    doc.save( "c://temp//pdf//PDF_image.pdf" );
    doc.close();
} catch (Exception io){
    System.out.println(" -- fail --" + io);
}
like image 52
mkl Avatar answered Sep 25 '22 01:09

mkl