Overlay Documents

This example demonstrates how to draw PDF templates.

First PDF file:
Second PDF file:

Overlay a pdf document on to another pdf

First we choose the primary document on which we have to overlay

Second we choose which document will be overlaying .

After clicking generate we have the overlayed pdf a single pdf file

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Text;

using OfficeComponent.Pdf;
using OfficeComponent.Pdf.Graphics;

namespace OfficeComponent.Samples
{
    class OverlayDocumentsExample : ExampleBase
#if WEB
, IUIExample
#endif
    {
        #region Input

        public string FirstFile { get; set; }
        public string SecondFile { get; set; }


        #endregion

        public OverlayDocumentsExample(string commonDataPath, string outputDir)
            : base(commonDataPath, outputDir)
        {

        }

        public OverlayDocumentsExample(string commonDataPath, string outputDir, string xmlFile) : base(commonDataPath, outputDir, xmlFile)
        {

        }

        public override string Execute()
        {
#if WEB
            ProcessForm();
#endif
            if (string.IsNullOrWhiteSpace(FirstFile))
            {
                ShowError("Please specify the first file.");
                return null;
            }

            if (string.IsNullOrWhiteSpace(SecondFile))
            {
                ShowError("Please specify the second file.");
                return null;
            }

            // Create a new instance of PdfDocument class.
            PdfDocument doc = new PdfDocument();
            
            PdfImportedDocument ldDoc1 = new PdfImportedDocument(FirstFile);
            PdfImportedDocument ldDoc2 = new PdfImportedDocument(SecondFile);

            for (int i = 0, count = ldDoc2.Pages.Count; i < count; ++i)
            {
                PdfPage page = doc.Pages.Add();
                PdfGraphics g = page.Graphics;

                PdfPageBase lpage = ldDoc2.Pages[i];
                PdfTemplate template = lpage.CreateTemplate();

                g.DrawPdfTemplate(template, PointF.Empty, page.GetClientSize());

                lpage = ldDoc1.Pages[0];
                template = lpage.CreateTemplate();

                g.DrawPdfTemplate(template, PointF.Empty, page.GetClientSize());
            }

            // Save the document
            var outputPath = Path.Combine(OutputDir, this.GetType().Name + "_" + Guid.NewGuid().ToString() + ".pdf");
            doc.Save(outputPath);
            doc.Close();

            return outputPath;
        }

        public override string ActionTitle
        {
            get { return "Create Overlay"; }
        }

#if WEB
        void ProcessForm()
        {
            FirstFile = GetPostFile("File1");
            SecondFile = GetPostFile("File2");
        }
#endif
    }
}
Imports System.Drawing.Imaging
Imports System.IO
Imports System.Text

Imports OfficeComponent.Pdf
Imports OfficeComponent.Pdf.Graphics

Namespace OfficeComponent.Samples
#If WEB Then
	Friend Class OverlayDocumentsExample
		Inherits ExampleBase
		Implements IUIExample
#Else
	Friend Class OverlayDocumentsExample
		Inherits ExampleBase
#End If
		#Region "Input"

		Private privateFirstFile As String
		Public Property FirstFile() As String
			Get
				Return privateFirstFile
			End Get
			Set(ByVal value As String)
				privateFirstFile = value
			End Set
		End Property
		Private privateSecondFile As String
		Public Property SecondFile() As String
			Get
				Return privateSecondFile
			End Get
			Set(ByVal value As String)
				privateSecondFile = value
			End Set
		End Property


		#End Region

		Public Sub New(ByVal commonDataPath As String, ByVal outputDir As String)
			MyBase.New(commonDataPath, outputDir)

		End Sub

		Public Sub New(ByVal commonDataPath As String, ByVal outputDir As String, ByVal xmlFile As String)
			MyBase.New(commonDataPath, outputDir, xmlFile)

		End Sub

		Public Overrides Function Execute() As String
#If WEB Then
			ProcessForm()
#End If
			If String.IsNullOrWhiteSpace(FirstFile) Then
				ShowError("Please specify the first file.")
				Return Nothing
			End If

			If String.IsNullOrWhiteSpace(SecondFile) Then
				ShowError("Please specify the second file.")
				Return Nothing
			End If

			' Create a new instance of PdfDocument class.
			Dim doc As New PdfDocument()

			Dim ldDoc1 As New PdfImportedDocument(FirstFile)
			Dim ldDoc2 As New PdfImportedDocument(SecondFile)

			Dim i As Integer = 0
			Dim count As Integer = ldDoc2.Pages.Count
			Do While i < count
				Dim page As PdfPage = doc.Pages.Add()
				Dim g As PdfGraphics = page.Graphics

				Dim lpage As PdfPageBase = ldDoc2.Pages(i)
				Dim template As PdfTemplate = lpage.CreateTemplate()

				g.DrawPdfTemplate(template, PointF.Empty, page.GetClientSize())

				lpage = ldDoc1.Pages(0)
				template = lpage.CreateTemplate()

				g.DrawPdfTemplate(template, PointF.Empty, page.GetClientSize())
				i += 1
			Loop

			' Save the document
			Dim outputPath = Path.Combine(OutputDir, Me.GetType().Name & "_" & Guid.NewGuid().ToString() & ".pdf")
			doc.Save(outputPath)
			doc.Close()

			Return outputPath
		End Function

		Public Overrides ReadOnly Property ActionTitle() As String
			Get
				Return "Create Overlay"
			End Get
		End Property

#If WEB Then
		Private Sub ProcessForm()
			FirstFile = GetPostFile("File1")
			SecondFile = GetPostFile("File2")
		End Sub
#End If
	End Class
End Namespace