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.
// Declaration of addBackground and addForeground methods
addBackground(PdfDocument backgroundPdf);
addForeground(PdfDocument foregroundPdf);
// Declaration of addBackground and addForeground methods
addBackground(PdfDocument backgroundPdf);
addForeground(PdfDocument foregroundPdf);
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.
// Import a necessary library for using PdfDocument
import com.example.PdfDocument;
import com.example.PageSelection;
// Assume working PDF, background PDF, and foreground PDF are already defined
PdfDocument pdf = new PdfDocument();
PdfDocument backgroundPdf = PdfDocument.fromFile("background.pdf");
PdfDocument foregroundPdf = PdfDocument.fromFile("foreground.pdf");
// Use the third page of the background PDF as the background of every page
pdf.addBackground(backgroundPdf, 2);
// Use the second page of the foreground PDF as the foreground of every page
pdf.addForeground(foregroundPdf, 1);
// Import a necessary library for using PdfDocument
import com.example.PdfDocument;
import com.example.PageSelection;
// Assume working PDF, background PDF, and foreground PDF are already defined
PdfDocument pdf = new PdfDocument();
PdfDocument backgroundPdf = PdfDocument.fromFile("background.pdf");
PdfDocument foregroundPdf = PdfDocument.fromFile("foreground.pdf");
// Use the third page of the background PDF as the background of every page
pdf.addBackground(backgroundPdf, 2);
// Use the second page of the foreground PDF as the foreground of every page
pdf.addForeground(foregroundPdf, 1);
For overlaying a PDF as the background or foreground on specific pages of a working PDF, specify the pages to be overlayed 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.
// Apply a background to a specific page of the working PDF
pdf.addBackground(backgroundPdf, PageSelection.singlePage(5));
// Add a different background on a range of pages in the working PDF
pdf.addBackground(backgroundPdf, PageSelection.pageRange(6, 15));
// Add another background to just the first page
pdf.addBackground(backgroundPdf, PageSelection.firstPage());
// Apply a background to a specific page of the working PDF
pdf.addBackground(backgroundPdf, PageSelection.singlePage(5));
// Add a different background on a range of pages in the working PDF
pdf.addBackground(backgroundPdf, PageSelection.pageRange(6, 15));
// Add another background to just the first page
pdf.addBackground(backgroundPdf, PageSelection.firstPage());
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.