How to Add Background and Overlay Foreground on PDFs

by Chaknith Bin

Adding 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 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 background and overlaying 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")
VB   C#

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 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")
VB   C#

Output PDF


Select Pages for Background or Foreground

It is possible to choose which page of the PDF to use as our 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.

Tips
All 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")
VB   C#

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.

Tips
All 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")
VB   C#

Use the AddBackgroundPdfToPageRange and AddForegroundOverlayPdfToPageRange methods to apply background and foreground to multiple pages respectively.

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")
VB   C#

Output PDF

Chaknith related to Output PDF

Chaknith Bin

Software Engineer

Chaknith is the Sherlock Holmes of developers. It first occurred to him he might have a future in software engineering, when he was doing code challenges for fun. His focus is on IronXL and IronBarcode, but he takes pride in helping customers with every product. Chaknith leverages his knowledge from talking directly with customers, to help further improve the products themselves. His anecdotal feedback goes beyond Jira tickets and supports product development, documentation and marketing, to improve customer’s overall experience.When he isn’t in the office, he can be found learning about machine learning, coding and hiking.