Amira
Amira

Reputation: 3270

Inserting image loses PDF content

I'm trying to insert an image in an existing PDF file but iText puts it on the first page and I'm losing the rest of the page content. How can I insert it without losing existing content?

I used this code:

public static void main(String[] args) {
    Document document = new Document(PageSize.A4);
    try {
        PdfWriter.getInstance(document, new FileOutputStream(
          "/home/amira/work/APPS-579/word/generatedMergedDocs/FinalTest/1.pdf"));
        document.open();

        Image image = Image.getInstance(
          "/home/amira/work/APPS-579/word/generatedMergedDocs/FinalTest/a.jpg");
        document.add(image);     
    } catch (DocumentException de) {
        de.printStackTrace();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }
    document.close();
}

Upvotes: 0

Views: 52

Answers (1)

Sirs
Sirs

Reputation: 1297

You're adding the image to an empty document, because you're not reading the original document, just overwriting it.

To modify an existing document with itext with an image, please see the following tutorial which explains it perfectly.

Upvotes: 3

Related Questions