How to Add Background and Overlay Foreground on PDFs

Adding a background allows you to insert an image or another PDF document as a background layer behind the existing content of a PDF. It's useful for creating letterheads, watermarks, or adding decorative elements to your documents.

Overlaying the foreground lets you place text, images, or other content on top of an existing PDF, effectively overlaying it. This is commonly used for adding annotations, stamps, signatures, or additional information to a PDF without altering the original content.

Adding a background and overlaying the foreground are both available in IronPdf with the options to use PDF as background and foreground.

Get started with IronPDF

Start using IronPDF in your project today with a free trial.

First Step:
green arrow pointer



Add Background Example

Utilize the AddBackgroundPdf method to add a background to a newly rendered or existing PDF document. The code example below demonstrates providing the method with a PdfDocument object. However, you can also specify the file path to automatically import the PDF and add it as the background in a single line of code.

Code

:path=/static-assets/pdf/content-code-examples/how-to/background-foreground-background.cs
using IronPdf;

ChromePdfRenderer renderer = new ChromePdfRenderer();

PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Main HTML content</h1>");

// Render background
PdfDocument background = renderer.RenderHtmlAsPdf("<body style='background-color: cyan;'></body>");

// Add background
pdf.AddBackgroundPdf(background);

pdf.SaveAs("addBackground.pdf");
Imports IronPdf

Private renderer As New ChromePdfRenderer()

Private pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Main HTML content</h1>")

' Render background
Private background As PdfDocument = renderer.RenderHtmlAsPdf("<body style='background-color: cyan;'></body>")

' Add background
pdf.AddBackgroundPdf(background)

pdf.SaveAs("addBackground.pdf")
$vbLabelText   $csharpLabel

Output PDF


Overlay Foreground Example

Similar to adding a background, you can specify the PDF file path to import the document and overlay it as a foreground over the main PDF. Use the AddForegroundOverlayPdf method to overlay the foreground on the main PDF document.

Code

:path=/static-assets/pdf/content-code-examples/how-to/background-foreground-foreground.cs
using IronPdf;

ChromePdfRenderer renderer = new ChromePdfRenderer();

PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Main HTML content</h1>");

// Render foreground
PdfDocument foreground = renderer.RenderHtmlAsPdf("<h1 style='transform: rotate(-45deg); opacity: 50%;'>Overlay Watermark</h1>");

// Overlay foreground
pdf.AddForegroundOverlayPdf(foreground);

pdf.SaveAs("overlayForeground.pdf");
Imports IronPdf

Private renderer As New ChromePdfRenderer()

Private pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Main HTML content</h1>")

' Render foreground
Private foreground As PdfDocument = renderer.RenderHtmlAsPdf("<h1 style='transform: rotate(-45deg); opacity: 50%;'>Overlay Watermark</h1>")

' Overlay foreground
pdf.AddForegroundOverlayPdf(foreground)

pdf.SaveAs("overlayForeground.pdf")
$vbLabelText   $csharpLabel

Output PDF


Select Pages for Background or Foreground

It is possible to choose which page of the PDF to use as your background or foreground. Let's take applying a background as an example, using a similar code example from the 'Add Background Example' section. We generate a two-page PDF with a different color to use as the background. By specifying the number 1 as our second parameter in the AddBackgroundPdf method, we use the 2nd page as the background.

TipsAll page indexes follow zero-based indexing.

Code

:path=/static-assets/pdf/content-code-examples/how-to/background-foreground-background-page-2.cs
using IronPdf;

string backgroundHtml = @"
<div style = 'background-color: cyan; height: 100%;'></div>
<div style = 'page-break-after: always;'></div>
<div style = 'background-color: lemonchiffon; height: 100%;'></div>";

ChromePdfRenderer renderer = new ChromePdfRenderer();

PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Main HTML content</h1>");

// Render background
PdfDocument background = renderer.RenderHtmlAsPdf(backgroundHtml);

// Use page 2 as background
pdf.AddBackgroundPdf(background, 1);

pdf.SaveAs("addBackgroundFromPage2.pdf");
Imports IronPdf

Private backgroundHtml As String = "
<div style = 'background-color: cyan; height: 100%;'></div>
<div style = 'page-break-after: always;'></div>
<div style = 'background-color: lemonchiffon; height: 100%;'></div>"

Private renderer As New ChromePdfRenderer()

Private pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Main HTML content</h1>")

' Render background
Private background As PdfDocument = renderer.RenderHtmlAsPdf(backgroundHtml)

' Use page 2 as background
pdf.AddBackgroundPdf(background, 1)

pdf.SaveAs("addBackgroundFromPage2.pdf")
$vbLabelText   $csharpLabel

Output PDF


Apply Background or Foreground on Specified Pages

Lastly, it is also possible to apply background or foreground to a single page or multiple pages. This action requires using a slightly different method name. Use the AddBackgroundPdfToPage and AddForegroundOverlayPdfToPage methods for adding background and overlaying foreground to a single particular page of the PDF, respectively.

TipsAll page indexes follow zero-based indexing.

Apply on a Single Page

:path=/static-assets/pdf/content-code-examples/how-to/background-foreground-single-page.cs
using IronPdf;

ChromePdfRenderer renderer = new ChromePdfRenderer();

PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Main HTML content</h1>");

// Render background
PdfDocument background = renderer.RenderHtmlAsPdf("<body style='background-color: cyan;'></body>");

// Add background to page 1
pdf.AddBackgroundPdfToPage(0, background);

pdf.SaveAs("addBackgroundOnASinglePage.pdf");
Imports IronPdf

Private renderer As New ChromePdfRenderer()

Private pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Main HTML content</h1>")

' Render background
Private background As PdfDocument = renderer.RenderHtmlAsPdf("<body style='background-color: cyan;'></body>")

' Add background to page 1
pdf.AddBackgroundPdfToPage(0, background)

pdf.SaveAs("addBackgroundOnASinglePage.pdf")
$vbLabelText   $csharpLabel

Apply on Multiple Pages

:path=/static-assets/pdf/content-code-examples/how-to/background-foreground-multiple-pages.cs
using IronPdf;
using System.Collections.Generic;

string html = @"<p> This is 1st Page </p>
<div style = 'page-break-after: always;'></div>
<p> This is 2nd Page</p>
<div style = 'page-break-after: always;'></div>
<p> This is 3rd Page</p>";

ChromePdfRenderer renderer = new ChromePdfRenderer();

PdfDocument pdf = renderer.RenderHtmlAsPdf(html);

// Render background
PdfDocument background = renderer.RenderHtmlAsPdf("<body style='background-color: cyan;'></body>");

// Create list of pages
List<int> pages = new List<int>() { 0, 2 };

// Add background to page 1 & 3
pdf.AddBackgroundPdfToPageRange(pages, background);

pdf.SaveAs("addBackgroundOnMultiplePage.pdf");
Imports IronPdf
Imports System.Collections.Generic

Private html As String = "<p> This is 1st Page </p>
<div style = 'page-break-after: always;'></div>
<p> This is 2nd Page</p>
<div style = 'page-break-after: always;'></div>
<p> This is 3rd Page</p>"

Private renderer As New ChromePdfRenderer()

Private pdf As PdfDocument = renderer.RenderHtmlAsPdf(html)

' Render background
Private background As PdfDocument = renderer.RenderHtmlAsPdf("<body style='background-color: cyan;'></body>")

' Create list of pages
Private pages As New List(Of Integer)() From {0, 2}

' Add background to page 1 & 3
pdf.AddBackgroundPdfToPageRange(pages, background)

pdf.SaveAs("addBackgroundOnMultiplePage.pdf")
$vbLabelText   $csharpLabel

Output PDF

Frequently Asked Questions

What are the benefits of adding a background to a PDF?

Adding a background to a PDF can enhance its appearance by incorporating images or other PDFs as background layers. This feature is ideal for creating professional-looking documents with elements like letterheads, watermarks, or decorative designs using IronPDF.

How can I overlay text on an existing PDF document?

To overlay text on an existing PDF document, you can use IronPDF's AddForegroundOverlayPdf method. This allows you to add annotations, stamps, or additional information on top of the existing content without altering the original PDF.

What methods are available for modifying PDF backgrounds and overlays?

IronPDF provides methods such as AddBackgroundPdf for adding backgrounds and AddForegroundOverlayPdf for overlaying content on PDFs. These methods allow users to customize PDFs by adding visual layers.

How can I apply a background or overlay to specific pages in a PDF?

You can apply a background or overlay to specific pages in a PDF using methods like AddBackgroundPdfToPage and AddForegroundOverlayPdfToPage in IronPDF. These methods allow targeting single pages, while page range methods can be used for multiple pages.

Is it possible to use an existing PDF as a background in another PDF?

Yes, you can use an existing PDF as a background in another PDF by specifying its file path and using IronPDF's AddBackgroundPdf method. This feature allows for seamless integration of existing documents as backgrounds.

How do I specify which page of a PDF to use as a background?

To specify which page of a PDF to use as a background, you can provide the page index as a parameter in the AddBackgroundPdf method in IronPDF. This allows you to select the exact page you want to use from the background PDF.

Can I use the same method to apply backgrounds and foregrounds?

No, IronPDF uses different methods for applying backgrounds and foregrounds. Use AddBackgroundPdf for backgrounds and AddForegroundOverlayPdf for foregrounds to achieve the desired effect on your PDF.

How can I enhance a PDF's appearance with decorative elements?

You can enhance a PDF's appearance with decorative elements by using IronPDF's AddBackgroundPdf method to add images or PDFs as backgrounds, or overlaying text and images on the foreground with AddForegroundOverlayPdf.

Chaknith Bin
Software Engineer
Chaknith works on IronXL and IronBarcode. He has deep expertise in C# and .NET, helping improve the software and support customers. His insights from user interactions contribute to better products, documentation, and overall experience.