Attach a Cover Page
When working with PDF documents, adding a cover page provides a method for increasing the visual appeal of the PDF itself. It gives developers like yourself a space to add branding to the PDF, or simply attract more readers with a cover page that stands out. IronPDF makes it easy to add cover pages to your PDFs in just a couple of lines of code, thanks to its merge tool.
5 Steps to Adding Cover Pages to Your PDFs
- var renderer = new ChromePdfRenderer();
- var cover = renderer.RenderHtmlAsPdf("<h1>This is a Cover Page</h1>");
- renderer.RenderingOptions.FirstPageNumber = 2;
- var pdf = renderer.RenderUrlAsPdf("https://www.nuget.org/packages/IronPdf/");
- pdf.InsertPdf(cover, 0).SaveAs("combined.pdf");
For today's example, we will be converting an HTML string to PDF, however, you can load in an existing PDF document that contains the cover page you want to attach to your PDF file. To do this, we must create a new ChromePdfRenderer
object. This class handles HTML to PDF conversion, with this you can render pixel-perfect PDF documents from HTML, CSS, and JavaScript content.
Then we create our cover page by rendering our HTML string to PDF format using RenderHtmlAsPdf
. If you're working with a pre-existing PDF document as your cover, you would need to load in your PDF using PdfDocument.FromFile
. Next, we configure the renderer with custom rendering options. We are setting the first-page number to 2, which ensures that when we render our main PDF document, the page numbering will start at 2 instead of the default (1) when we render the main PDF.
Next, we need to render our main PDF. Here, we are rendering web content from a URL into a PDF document. At this point, we have two PDF objects, one containing our PDF cover, and the second containing our PDF from the URL.
Now, it's time to attach our cover page to our core PDF document. We can do this in just one easy line of code. InsertPdf(cover, 0)
inserts the cover PDF at page index 0, which is the beginning of the PDF document. Now, the pdf
object is one PDF document that contains the cover page and the URL content. Then, using SaveAs
we save the final merged PDF document to the specified file location.
Click here to view the How-to Guide, including examples, sample code, and files >