Attach a Cover Page

import com.ironsoftware.ironpdf.PdfDocument;  
import com.ironsoftware.ironpdf.headerfooter.HeaderFooterOptions;
import java.io.IOException;  
import java.nio.file.Paths;


// Create a Sample Cover Page using RenderHtmlAsPdf
PdfDocument coverPage = PdfDocument.renderHtmlAsPdf("<h1>This is a Cover Page</h1>");
PdfDocument webpage = PdfDocument.renderUrlAsPdf("https://www.nuget.org/packages/IronPdf/");

// Set the page number of the PDF document to be created to 2.
HeaderFooterOptions headerFooterOptions = new HeaderFooterOptions();
headerFooterOptions.setFirstPageNumber(1);
TextHeaderFooter footer = new TextHeaderFooter();
footer.setLeftText("");
footer.setCenterText("Page {page}");
footer.setRightText("");
webpage.addTextFooter(footer, headerFooterOptions);

// Convert a web page's content to a PDF document.
// Merge the cover page with the web page and save the new PDF to the filesystem.
try {
    PdfDocument.merge(coverPage, webpage).saveAs(Paths.get("assets/cover_page_pdf.pdf"));
} catch (IOException e) {
    throw new RuntimeException(e);
}
mvn install
Attach a Cover Page

Attach a Cover Page

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

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 within 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 alteration 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.

Learn more about PdfDocument.merge method
View on GitHub

Ready to Get Started?

Version:2026.10just released