Merge Documents

This example demonstrates how to merge two PDF documents.

First PDF file:
Second PDF file:

Sometimes we came across a situation where we have two pdf document and we need to merge them together

First file explorer is for the primary pdf

Second file explorer is for the pdf document that is to be appended to the existing

So here first we choose the primary document into which we want it to merge another pdf.

Click generate will launch the merged pdf

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 MergeDocumentsExample : ExampleBase
#if WEB
, IUIExample
#endif
    {
        #region Input

        public string[] Files
        {
            get;
            set;
        }

        #endregion

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

        }

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

        }

        public override string Execute()
        {
#if WEB
            ProcessForm();
#endif
            if (Files.Length <= 1)
            {
                ShowError("Please select at least 2 PDF file to merge.");
                return null;
            }

            int i;
            for (i = 0; i < Files.Length; i++)
            {
                var f = Files[i];
                if (string.IsNullOrWhiteSpace(f))
                {
                    ShowError("Please specify the #" + (i + 1) + " file.");
                    return null;
                }
            }

            // Create a new instance of PdfDocument class.
            PdfDocument doc = new PdfDocument();
            
            // Merge documents
            doc = PdfDocument.Merge(Files);
            
            // 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 "Merge Documents"; }
        }

#if WEB
        void ProcessForm()
        {
            var file1 = GetPostFile("File1");
            var file2 = GetPostFile("File2");

            if (file1 != null && file2 != null)
                Files = new[] {file1, 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 MergeDocumentsExample
		Inherits ExampleBase
		Implements IUIExample
#Else
	Friend Class MergeDocumentsExample
		Inherits ExampleBase
#End If
		#Region "Input"

		Private privateFiles As String()
		Public Property Files() As String()
			Get
				Return privateFiles
			End Get
			Set(ByVal value As String())
				privateFiles = 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 Files.Length <= 1 Then
				ShowError("Please select at least 2 PDF file to merge.")
				Return Nothing
			End If

			Dim i As Integer
			For i = 0 To Files.Length - 1
				Dim f = Files(i)
				If String.IsNullOrWhiteSpace(f) Then
					ShowError("Please specify the #" & (i + 1) & " file.")
					Return Nothing
				End If
			Next i

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

			' Merge documents
			doc = PdfDocument.Merge(Files)

			' 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 "Merge Documents"
			End Get
		End Property

#If WEB Then
		Private Sub ProcessForm()
			Dim file1 = GetPostFile("File1")
			Dim file2 = GetPostFile("File2")

			If file1 IsNot Nothing AndAlso file2 IsNot Nothing Then
				Files = New String() {file1, file2}
			End If
		End Sub
#End If
	End Class
End Namespace