How to Add Background and Overlay Foreground on PDFs
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.
How to Add Background and Overlay Foreground on PDFs
- Download the C# library to add a background and overlay a foreground
- Use a freshly rendered or existing PDF as a background or foreground
- Use the
AddBackgroundPdf
method to add a background - Use the
AddForegroundOverlayPdf
method to overlay a foreground - Specify which pages to apply the background or foreground
Install with NuGet
Install-Package IronPdf
Download DLL
Manually install into your project
Install with NuGet
Install-Package IronPdf
Download DLL
Manually install into your project
Start using IronPDF in your project today with a free trial.
Check out IronPDF on Nuget for quick installation and deployment. With over 8 million downloads, it's transforming PDF with C#.
Install-Package IronPdf
Consider installing the IronPDF DLL directly. Download and manually install it for your project or GAC form: IronPdf.zip
Manually install into your project
Download DLLAdd 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")
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")
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
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")
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
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")
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")