Link Search Menu Expand Document

Use PDF as background to another PDF

Yes, it is possible to use a PDF as background to another PDF using our PDF SDK. Please see the sample code below:

using System;
using Bytescout.PDF;

namespace Sample
{
   class Program
    {
        public static void Main(string[] args)
        {
            Document templateDoc = new Document { RegistrationName = "demo", RegistrationKey = "demo" };
            Document mainDoc = new Document { RegistrationName = "demo", RegistrationKey = "demo" };
            Document resultDoc = new Document { RegistrationName = "demo", RegistrationKey = "demo" };

            templateDoc.Load(@"c:\temp\template.pdf");
            mainDoc.Load(@"c:\temp\sample.pdf");

            // Create GraphicsTemplate object from the first page of the template document
            GraphicsTemplate template = templateDoc.Pages[0].SaveAsTemplate();

            for (int i = 0; i < mainDoc.Pages.Count; i++)
            {
                // Create empty page 
                Page resultPage = new Page(mainDoc.Pages[i].Width, mainDoc.Pages[i].Height);

                // Draw the template page as a background before the main content
                resultPage.Canvas.DrawTemplate(template, 0, 0);

                // Draw main content
                GraphicsTemplate mainContentAsTemplate = mainDoc.Pages[i].SaveAsTemplate();
                resultPage.Canvas.DrawTemplate(mainContentAsTemplate, 0, 0);
                
                // Add the created page to the result document
                resultDoc.Pages.Add(resultPage);
            }

            // Save result document
            resultDoc.Save(@"c:\temp\result.pdf");

            resultDoc.Dispose();
            templateDoc.Dispose();
            mainDoc.Dispose();
        }
    }
}