Generate PDF with images on Android

Asked

Viewed 1,098 times

0

I need to generate a PDF on Android with some simple fields and several images. Researching I found this question Error while creating PDF file on Android that is helping me. However I don’t know how to add the images in PDF, at the moment they are saved on the device and I have the path of them.

1 answer

0


There is another library called Pdfbox if you are unable to use iText.

https://pdfbox.apache.org/

With iText it is possible to merge (combine), then even with new images, could be added as it appears.

The necessary you already have, the path, if static, can use directly or dynamically if you need to choose.

http://developers.itextpdf.com/

Ex:

private void combinaImagensEmPdf(String caminhoImagens, String caminhoSaida) {
    try {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream("arquivo.pdf"));
        document.open();
        File files[] = new File(caminhoImagens).listFiles();
        PdfPTable table = new PdfPTable(1);
        for (File file : files) {
            table.setWidthPercentage(100); //Altera e configura
            table.addCell(criarCelulaDeImagem(file.getAbsolutePath()));
        }
        document.add(table);
        document.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}

public static PdfPCell criarCelulaDeImagem(String caminhoImagens) {
    Image img = Image.getInstance(caminhoImagens);
    return new PdfPCell(img, true);
}

Ref: https://stackoverflow.com/questions/27425738/merge-multiple-images-in-a-pdf-file

Browser other questions tagged

You are not signed in. Login or sign up in order to post.