Search code examples
javapdfpng

How can I convert a PNG file to PDF using java?


Are there any open source libraries that I can use?


Solution

  • itext may help you. you don't really convert a png to pdf but create a pdf with a png in it. simple example:

    Document document = new Document(PageSize.A4, 20, 20, 20, 20);
    PdfWriter.getInstance(document, new FileOutputStream("C:/test.pdf"));
    document.open();
    Image image = Image.getInstance(getClass().getResource("/logo.png"));
    document.add(image);
    document.close();