0

I need some help with iText. I need to add an existing pdf cover in other existing iText document or PDF document. Anybody could help me? I've searched in some books (iText in action) but it's somewhat compicated.

Doug LN
  • 132
  • 2
  • 11
  • Can you please describe what you have tried so far? As asked, your question is too broad and open-ended for Stack Overflow. – dgvid Apr 10 '15 at 17:05
  • dgvid, What I needed was exactly what Bruno Lowagie answered. I needed to add a cover in my iText Document and later edit it. But thanks for you help. – Doug LN Apr 10 '15 at 21:38

1 Answers1

2

Suppose that we have a document named pages.pdf and that we want to add the cover hero.pdf as the cover of this document.

Approach 1: Use PdfCopy

Take a look at the AddCover1 example:

PdfReader cover = new PdfReader("hero.pdf");
PdfReader reader = new PdfReader("pages.pdf");
Document document = new Document();
PdfCopy copy = new PdfCopy(document, new FileOutputStream("pages_with_cover.pdf"));
document.open();
copy.addDocument(cover);
copy.addDocument(reader);
document.close();
cover.close();
reader.close();

The result is a document where you first have the cover and then the rest of the document: pages_with_cover.pdf

Approach 2: Use PdfStamper

Take a look at the AddCover2 example:

PdfReader cover = new PdfReader("hero.pdf");
PdfReader reader = new PdfReader("pages.pdf");
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("cover_with_pages.pdf"));
stamper.insertPage(1, cover.getPageSizeWithRotation(1));
PdfContentByte page1 = stamper.getOverContent(1);
PdfImportedPage page = stamper.getImportedPage(cover, 1);
page1.addTemplate(page, 0, 0);
stamper.close();
cover.close();
reader.close();

In this case, we take the original document pages.pdf and we insert a new page 1 with the same page size as the cover. We then get this page1 and we add the first page of hero.pdf to this first page. The result is cover_with_pages.pdf

What is the difference between the two approaches?

With PdfCopy, you may lose some of the properties that are defined at the document level (e.g. the page layout setting), but you keep the interactive features of both files. You may need to set some parameters in case you want to preserve tagging, form fields, etc... but for simple PDFs, you don't need all of that.

With PdfStamper, you preserve the properties that are defined at the document level of pages.pdf, but you lose all the interactive features of the cover page (if any). You may need to tweak the example if you want to define the cover as an artifact and if the original cover page has odd page boundaries, but it would lead us too far to discuss this in this simple answer.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • Bruno Lowagie, I'm sorry for the comments about your books. I apologize. Your answer was what I needed. Helped me a lot. Thank you! – Doug LN Apr 10 '15 at 21:36
  • I've removed the comment from my answer. It is custom to accept answers that provide the information that you needed. – Bruno Lowagie Apr 11 '15 at 07:55
  • Bruno Lowagie, Thanks again with your help. A last question, do you know how to resize the imported PDF to fit on the PDF document? My "hero.pdf" is a little large than the "pages.pdf". I'll need to create another stack question to it? – Doug LN Apr 14 '15 at 12:51
  • This is already answered in other questions. The best way is to use approach 2 and to scale the `page` object. This can either be done by using a different `addTemplate()` method (with more parameters) or by wrapping the `page` object inside an `Image` and use `scaleToFit()` to fit the image inside a specific rectangle. – Bruno Lowagie Apr 14 '15 at 13:20