Backgrounds & Foregrounds

You may want to use a specific background and foreground as you create and render your PDF documents in IronPDF. In such a case, you can use an existing or rendered PDF as the background or foreground for another PDF document. This is particularly useful for design consistency and templating.

This example shows you how to use a PDF document as the background or foreground of another PDF document.

You can do this in C# by loading or creating a multi-page PDF as an IronPdf.PdfDocument object.

You can add backgrounds using PdfDocument.AddBackgroundPdf. For more details on background insertion methods, refer to the IronPDF.PdfDocument background documentation; it describes several background insertion methods and their overrides. This adds a background to each page of your working PDF. The background is copied from a page in another PDF document.

You can add foregrounds, also known as "Overlays," using PdfDocument.AddForegroundOverlayPdfToPage. For detailed information on foreground insertion methods, consult the IronPDF.PdfDocument overlay documentation.

Here is a code snippet demonstrating how you might accomplish this in C#:

using IronPdf;

public class PdfExample
{
    public static void Main()
    {
        // Load or create the main PDF document
        var mainPdf = PdfDocument.FromFile("main.pdf"); // Load an existing PDF
        // OR
        // var mainPdf = new HtmlToPdf().RenderHtmlAsPdf("<p>Hello, World!</p>"); // Create a new PDF from HTML

        // Load the PDF you want to use as a background
        PdfDocument backgroundPdf = PdfDocument.FromFile("background.pdf");

        // Add the background PDF to each page of the main PDF
        // This operation will overlay each page of 'backgroundPdf' onto the corresponding page of 'mainPdf'
        mainPdf.AddBackgroundPdf(backgroundPdf);

        // Load the PDF you want to use as a foreground (overlay)
        PdfDocument overlayPdf = PdfDocument.FromFile("overlay.pdf");

        // Add the overlay PDF to each page of the main PDF
        // This operation overlays each page of 'overlayPdf' onto the corresponding page of 'mainPdf'
        for (int i = 0; i < mainPdf.PageCount; i++)
        {
            mainPdf.AddForegroundOverlayPdfToPage(overlayPdf, i);
        }

        // Save the modified main PDF to a file
        mainPdf.SaveAs("output.pdf");
    }
}
using IronPdf;

public class PdfExample
{
    public static void Main()
    {
        // Load or create the main PDF document
        var mainPdf = PdfDocument.FromFile("main.pdf"); // Load an existing PDF
        // OR
        // var mainPdf = new HtmlToPdf().RenderHtmlAsPdf("<p>Hello, World!</p>"); // Create a new PDF from HTML

        // Load the PDF you want to use as a background
        PdfDocument backgroundPdf = PdfDocument.FromFile("background.pdf");

        // Add the background PDF to each page of the main PDF
        // This operation will overlay each page of 'backgroundPdf' onto the corresponding page of 'mainPdf'
        mainPdf.AddBackgroundPdf(backgroundPdf);

        // Load the PDF you want to use as a foreground (overlay)
        PdfDocument overlayPdf = PdfDocument.FromFile("overlay.pdf");

        // Add the overlay PDF to each page of the main PDF
        // This operation overlays each page of 'overlayPdf' onto the corresponding page of 'mainPdf'
        for (int i = 0; i < mainPdf.PageCount; i++)
        {
            mainPdf.AddForegroundOverlayPdfToPage(overlayPdf, i);
        }

        // Save the modified main PDF to a file
        mainPdf.SaveAs("output.pdf");
    }
}
CONVERTER NOT RUNNING
$vbLabelText   $csharpLabel

Key Points

  • Background PDF is added to each page from the provided background.pdf.
  • Foreground PDF (Overlay) is added to each page from the provided overlay.pdf.
  • Modify file paths ("main.pdf", "background.pdf", "overlay.pdf", "output.pdf") as needed to fit your file system setup.

This code illustrates how to integrate additional design elements on top of a base PDF using IronPDF. Always refer to the official documentation for more advanced techniques and additional options.