Fondos y primeros planos
Para agregar elementos específicos de fondo o primer plano a sus documentos PDF, IronPDF proporciona los métodos addBackground
y addForeground
. Estos métodos permiten a los desarrolladores utilizar el contenido de un PDF como fondo o primer plano de otro PDF. Estos métodos son especialmente útiles para generar grupos de PDF basados en una plantilla de diseño común.
addBackground(PdfDocument backgroundPdf);
addForeground(PdfDocument foregroundPdf);
addBackground(PdfDocument backgroundPdf);
addForeground(PdfDocument foregroundPdf);
Dado que estos métodos funcionan con objetos PdfDocument
, los desarrolladores pueden obtenerlos de archivos existentes utilizando el método fromFile
, o generarlos de nuevo utilizando uno de los métodos de renderización de PDF disponibles.
addBackground
y addForeground
utilizarán la primera página de documentos PDF de varias páginas como fondo o primer plano por defecto. Para utilizar una página diferente de los PDF que contienen varias páginas, añada el índice de la página deseada como segundo argumento de la llamada al método.
// Use the third page of the background PDF as the background of every page
// in the working PDF
pdf.addBackground(backgroundPdf, 2);
// Use the second page of the foreground PDF as the foreground of every page
// of the working PDF
pdf.addForeground(foregroundPdf, 1);
// Use the third page of the background PDF as the background of every page
// in the working PDF
pdf.addBackground(backgroundPdf, 2);
// Use the second page of the foreground PDF as the foreground of every page
// of the working PDF
pdf.addForeground(foregroundPdf, 1);
Para superponer un PDF como fondo o primer plano en páginas específicas de un PDF existente, especifique las páginas a superponer utilizando un objeto PageSelection
. El ejemplo siguiente muestra cómo hacerlo para una sola página y para un intervalo de páginas de un documento PDF.
// Add the background to page 5 of the working PDF
pdf.addBackground(backgroundPdf, PageSelection.singlePage(6));
// Add a different background on pages 7 through 16 of the working PDF
pdf.addBackground(backgroundPdf, PageSelection.pageRange(6, 15));
// Add another background to just the first page.
pdf.addBackground(backgroundPdf, PageSelection.firstPage());
// Add the background to page 5 of the working PDF
pdf.addBackground(backgroundPdf, PageSelection.singlePage(6));
// Add a different background on pages 7 through 16 of the working PDF
pdf.addBackground(backgroundPdf, PageSelection.pageRange(6, 15));
// Add another background to just the first page.
pdf.addBackground(backgroundPdf, PageSelection.firstPage());
Para añadir marcas de agua a tus documentos PDF, utiliza el método addWatermark
como alternativa al uso de addBackground
para tener un control más sencillo sobre la posición y opacidad del fondo.
Para obtener más información sobre la manipulación de PDF, visite Características y Documentación de IronPDF.