Attach a Cover Page

IronPDF's PdfDocument.merge method appends the contents of one PDF document to another PDF document.

PdfDocument.merge(PdfDocument a, PdfDocument b);
JAVA

When invoked, PdfDocument.merge will add all the pages of PDF document b behind the pages of PDF document A.

This code example shows how developers can use this method to attach cover pages to existing PDF documents. Create the desired cover page for the PDF document using PdfDocument.renderHtmlAsPdf (or load a pre-created one using PdfDocument.fromFile). Then, create (or load) the PDF document that needs the cover page and call PdfDocument.merge to combine them together. Specify the cover page as the first argument of the merge method, followed by the other PDF document.

Observe the use of the setFirstPageNumber on a ChromePdfRenderOptions object to Page 2 on line 15 of the code example. This allows us to adjust the page numbers of the PDF document to reflect the addition of the cover page prior to the merger. Note that this feature is available only to PDF documents that will be generated on the fly. For existing documents, this page number alternation will need to be done prior to the merger using other means.

The developer can also attach a cover page to a PDF using the PdfDocument.prependPdf method.

PdfDocument.prependPdf(PdfDocument anotherPdfFile)
JAVA