Cómo agregar fondos y primeros planos en PDFs

How to Add Background and Overlay Foreground on PDFs

This article was translated from English: Does it need improvement?
Translated
View the article in English

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.

Quickstart: Add Background to Your PDF Documents

Easily enhance your PDF documents by adding a background with IronPDF. This quick guide shows you how to insert a PDF as a background layer, perfect for letterheads or watermarks. Follow the simple code snippet to get started quickly and effortlessly, and transform your PDF presentations with ease.

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronPDF with NuGet Package Manager

    PM > Install-Package IronPdf

  2. Copy and run this code snippet.

    var pdf = new IronPdf.PdfDocument("input.pdf");
    pdf.AddBackgroundPdf("background.pdf");
    pdf.SaveAs("output.pdf");
  3. Deploy to test on your live environment

    Start using IronPDF in your project today with a free trial
    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.

ConsejosAll 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.

ConsejosAll 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

Preguntas Frecuentes

¿Cuáles son los beneficios de añadir un fondo a un PDF?

Agregar un fondo a un PDF puede mejorar su apariencia al incorporar imágenes u otros PDFs como capas de fondo. Esta función es ideal para crear documentos con un aspecto profesional con elementos como membretes, marcas de agua o diseños decorativos usando IronPDF.

¿Cómo puedo superponer texto en un documento PDF existente?

Para superponer texto en un documento PDF existente, puedes usar el método AddForegroundOverlayPdf de IronPDF. Esto te permite agregar anotaciones, sellos o información adicional sobre el contenido existente sin alterar el PDF original.

¿Qué métodos están disponibles para modificar fondos y superposiciones de PDFs?

IronPDF ofrece métodos como AddBackgroundPdf para añadir fondos y AddForegroundOverlayPdf para superponer contenido en PDFs. Estos métodos permiten a los usuarios personalizar PDFs añadiendo capas visuales.

¿Cómo puedo aplicar un fondo o superposición a páginas específicas en un PDF?

Puedes aplicar un fondo o superposición a páginas específicas en un PDF usando métodos como AddBackgroundPdfToPage y AddForegroundOverlayPdfToPage en IronPDF. Estos métodos permiten dirigirse a páginas individuales, mientras que los métodos de rango de páginas pueden usarse para múltiples páginas.

¿Es posible usar un PDF existente como fondo en otro PDF?

Sí, puedes usar un PDF existente como fondo en otro PDF especificando su ruta de archivo y usando el método AddBackgroundPdf de IronPDF. Esta función permite una integración fluida de documentos existentes como fondos.

¿Cómo especifico qué página de un PDF usar como fondo?

Para especificar qué página de un PDF usar como fondo, puedes proporcionar el índice de la página como parámetro en el método AddBackgroundPdf en IronPDF. Esto te permite seleccionar la página exacta que quieres usar del PDF de fondo.

¿Puedo usar el mismo método para aplicar fondos y primeros planos?

No, IronPDF utiliza métodos diferentes para aplicar fondos y primeros planos. Usa AddBackgroundPdf para fondos y AddForegroundOverlayPdf para primeros planos para lograr el efecto deseado en tu PDF.

¿Cómo puedo mejorar la apariencia de un PDF con elementos decorativos?

Puedes mejorar la apariencia de un PDF con elementos decorativos utilizando el método AddBackgroundPdf de IronPDF para añadir imágenes o PDFs como fondos, o superponiendo texto e imágenes en el primer plano con AddForegroundOverlayPdf.

¿IronPDF es totalmente compatible con .NET 10 para utilizar métodos de fondo y primer plano?

Sí. IronPDF es totalmente compatible con .NET 10 e incluye funciones como AddBackgroundPdf , AddForegroundOverlayPdf y métodos de fondo/superposición específicos de la página. Incorpora las mejoras de rendimiento y tiempo de ejecución introducidas en .NET 10, manteniendo el comportamiento de la API.

Chaknith Bin
Ingeniero de Software
Chaknith trabaja en IronXL e IronBarcode. Tiene un profundo conocimiento en C# y .NET, ayudando a mejorar el software y apoyar a los clientes. Sus conocimientos derivados de las interacciones con los usuarios contribuyen a mejores productos, documentación y experiencia en general.
¿Listo para empezar?
Nuget Descargas 16,154,058 | Versión: 2025.11 recién lanzado