Backgrounds & Foregrounds

import com.ironsoftware.ironpdf.*;  
import java.io.IOException;  
import java.nio.file.Paths;

// Load background and foreground PDFs from the filesystem (or create them programmatically)  
PdfDocument backgroundPdf = PdfDocument.fromFile(Paths.get("assets/MyBackground.pdf"));  
PdfDocument foregroundPdf = PdfDocument.fromFile(Paths.get("assets/MyForeground.pdf"));  
  
// Render content (HTML, URL, etc) as a PDF Document  
PdfDocument pdf = PdfDocument.renderUrlAsPdf("https://www.nuget.org/packages/IronPdf");  
  
// Add the background and foreground PDFs to the newly-rendered document.  
pdf.addBackgroundPdf(backgroundPdf);  
pdf.addForegroundPdf(foregroundPdf);  
  
pdf.saveAs(Paths.get("assets/BackgroundForegroundPdf.pdf"));
mvn install
Backgrounds & Foregrounds

Backgrounds & Foregrounds

For adding specific background or foreground elements to your PDF documents, IronPDF provides the addBackground and addForeground methods. These methods enable developers to use the content of one PDF as the background or the foreground of another PDF. These methods are particularly useful for generating groups of PDFs based on a common design template.

Since these methods work with PdfDocument objects, developers can source them from existing files using the fromFile method, or generate them anew using one of the available PDF rendering methods.

addBackground and addForeground will utilize the first page of multipage PDF documents as the background or foreground by default. To use a different page from PDFs containing multiple pages, add the index of the desired page as the second argument to the method call.

For overlaying a PDF as the background or foreground on specific pages of a working PDF, specify the pages to be overlaid using a PageSelection object. The example below shows how to do this for a single page and for a range of pages of a PDF document.

For watermarking your PDF documents, use the addWatermark method as an alternative to using addBackground for easier control over background positioning and opacity.

For more information on PDF manipulation, visit IronPDF's Features and Documentation.

Learn to Edit PDF Backgrounds & Foregrounds in Java
View on GitHub

Ready to Get Started?

Version:2026.10just released