Overlay one PDF's graphics onto another

PDF Read Write Graphics Container

This sample shows how to overlay the vector content of one PDF file, such as a seal or logo, onto every page of another PDF file. It reads the stamp from "Seal.pdf" and the page content from "GraphicsContainer.pdf" with PdfReader, shrinks the stamp's GraphicsContainer to a fixed size with Scale, then draws the page content and the stamp together onto each output page with PdfWriter and Graphics.DrawContainer.

Сode Snippet

const float stampSize = 90f; // Target size, in pixels, regardless of the stamp's native size.
const float margin = 20f;    // Distance from the page edge, in pixels.

using (var stampReader = new PdfReader("Seal.pdf"))
using (var stamp = stampReader.Frames[0].GetContent())
{
    var scale = stampSize / Math.Max(stamp.Width, stamp.Height);
    stamp.Scale(scale, scale);

    using (var contentReader = new PdfReader("GraphicsContainer.pdf"))
    using (var writer = new PdfWriter("OverlaidDocument.pdf"))
    using (var gr = writer.GetGraphics())
    {
        // The same stamp container is drawn onto every page of the content file.
        foreach (var frame in contentReader.Frames.Cast<PdfFrame>())
        {
            using (var pageContent = frame.GetContent())
            {
                writer.AddPage(pageContent.Width, pageContent.Height, pageContent.DpiX, pageContent.DpiY);

                // The original page content goes first, the stamp on top of it.
                gr.DrawContainer(pageContent, 0, 0);

                var stampX = pageContent.Width - stamp.Width - margin;
                var stampY = pageContent.Height - stamp.Height - margin;
                gr.DrawContainer(stamp, stampX, stampY);
            }
        }
    }
}

Input

GraphicsContainer.pdf

Download

Seal.pdf

Download

Output

OverlaidDocument.pdf

Download

OverlaidDocument.png

For AI-assisted development: Download Graphics Mill Code Samples XML Catalog