Ktas
Ktas

Reputation: 63

Add background to every page in PDF using iText 7

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.

Upvotes: 0

Views: 1541

Answers (2)

Alexey Subach
Alexey Subach

Reputation: 12312

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();

Upvotes: 3

Vikas Garg
Vikas Garg

Reputation: 200

Based on my understanding you are looking for below solution. If I missed something then please let me know.

  1. Create reader for Original PDF for which you want to create background.
  2. Create PDF reader for background PDF
  3. Create PDF stamper where you want to generate final PDF.
  4. Get background using GetImportedPage method for Stamper.
  5. Loop on all pages of original PDF pages and add background.

Below is the code:

static void CreatePdfwithBackGround(string originalPdf, string backgroundPdf, string destPdf)
    {
        PdfReader originalPdfReader = new PdfReader(originalPdf);
        PdfReader backgroundPdfReader = new PdfReader(backgroundPdf);
        // Create the stamper for Destination pdf
        PdfStamper stamper = new PdfStamper(originalPdfReader, new FileStream(destPdf, FileMode.Create));
        // Add the backgroundPdf to each page of original PDF
        PdfImportedPage page = stamper.GetImportedPage(backgroundPdfReader, 1);
        int pageCount = originalPdfReader.NumberOfPages;
        PdfContentByte background;
        for (int i = 1; i <= pageCount; i++)
        {
            background = stamper.GetUnderContent(i);
            background.AddTemplate(page, 0, 0);
        }
        // Close the Destination stamper
        stamper.Close();
    }

And example call is:

CreatePdfwithBackGround(@"C:\TEST\MainPDF.pdf", @"C:\TEST\BackGroundTemplate.pdf", @"C:\TEST\FinalPDFOutput.pdf");

Upvotes: 2

Related Questions