I have PDF file with one page and I want to use it like a background for all my pages in second PDF file with some information. I've tried to do it with CopyPagesTo but it just copy PDF every second page.
private void ApplyBackground(string sourceFilename, string backgroundPdf, int pageNumber) {
PdfDocument srcDocument = new PdfDocument(new PdfReader(sourceFilename));
PdfDocument bgDocument = new PdfDocument(new PdfReader(backgroundPdf));
PdfDocument destDocument = new PdfDocument(new PdfWriter(@"C:\Desktop\result.pdf").SetSmartMode(true));
int pagesCount = srcDocument.GetNumberOfPages();
for (int i = 1; i <= pagesCount; i++) {
srcDocument.CopyPagesTo(i, i, destDocument);
bgDocument.CopyPagesTo(1, 1, destDocument);
}
srcDocument.Close();
bgDocument.Close();
destDocument.Close();
}
Is it possible to use one PDF file like a background and put it into other PDF file every page behind text.
Here is the iText 7 code. Please note that it assumes equal page sizes for the page with the background and the pages of the document being processed.
PdfDocument backgroundDocument = new PdfDocument(new PdfReader(@"path/to/background_doc.pdf"));
PdfDocument pdfDocument = new PdfDocument(new PdfReader(@"path/to/source.pdf"),
new PdfWriter(@"path/to/target.pdf"));
PdfFormXObject backgroundXObject = backgroundDocument.GetPage(1).CopyAsFormXObject(pdfDocument);
for (int i = 1; i <= pdfDocument.GetNumberOfPages(); i++) {
PdfPage page = pdfDocument.GetPage(i);
PdfStream stream = page.NewContentStreamBefore();
new PdfCanvas(stream, page.GetResources(), pdfDocument).AddXObject(backgroundXObject, 0, 0);
}
pdfDocument.Close();
backgroundDocument.Close();