Skip to main content
Skip table of contents

Superimposing content from one PDF into another PDF

Example written in answer to the question Click How to superimpose pages from existing documents into another document?


superimpose

JAVA

JAVA
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2023 Apryse Group NV
    Authors: Apryse Software.

    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
package com.itextpdf.samples.sandbox.stamper;

import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.kernel.pdf.xobject.PdfFormXObject;

import java.io.File;

public class SuperImpose {
    public static final String DEST = "./target/sandbox/stamper/super_impose.pdf";
    public static final String[] EXTRA = {
            "./src/main/resources/pdfs/hello.pdf",
            "./src/main/resources/pdfs/base_url.pdf",
            "./src/main/resources/pdfs/state.pdf"
    };
    public static final String SRC = "./src/main/resources/pdfs/primes.pdf";

    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();

        new SuperImpose().manipulatePdf(DEST);
    }

    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
        PdfCanvas canvas = new PdfCanvas(pdfDoc.getFirstPage().newContentStreamBefore(),
                pdfDoc.getFirstPage().getResources(), pdfDoc);

        for (String path : EXTRA) {
            PdfDocument srcDoc = new PdfDocument(new PdfReader(path));
            PdfFormXObject page = srcDoc.getFirstPage().copyAsFormXObject(pdfDoc);
            canvas.addXObjectAt(page, 0, 0);
            srcDoc.close();
        }

        pdfDoc.close();
    }
}

C#

C#
using System;
using System.IO;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas;
using iText.Kernel.Pdf.Xobject;

namespace iText.Samples.Sandbox.Stamper 
{
    public class SuperImpose 
    {
        public static readonly String DEST = "results/sandbox/stamper/super_impose.pdf";
        public static readonly String SRC = "../../../resources/pdfs/primes.pdf";
        public static readonly String[] EXTRA =
        {       "../../../resources/pdfs/hello.pdf", 
                "../../../resources/pdfs/base_url.pdf", 
                "../../../resources/pdfs/state.pdf"
                
        };
        
        public static void Main(String[] args) 
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            
            new SuperImpose().ManipulatePdf(DEST);
        }

        protected void ManipulatePdf(String dest) 
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
            PdfCanvas canvas = new PdfCanvas(pdfDoc.GetFirstPage().NewContentStreamBefore(), 
                    pdfDoc.GetFirstPage().GetResources(), pdfDoc);
            
            foreach (String path in EXTRA) 
            {
                PdfDocument srcDoc = new PdfDocument(new PdfReader(path));
                PdfFormXObject page = srcDoc.GetFirstPage().CopyAsFormXObject(pdfDoc);
                canvas.AddXObjectAt(page, 0, 0);
                srcDoc.Close();
            }
            
            pdfDoc.Close();
        }
    }
}
JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.