Merge PDF

Merge PDF

It is possible to merge PDFs in .NET using TallPDF. In this code sample we will create a new PDF by stitching two existing PDF documents.

C# merge PDF code sample

using System;
using System.IO;

using TallComponents.PDF.Layout;
using TallComponents.PDF.Layout.Paragraphs;
using TallComponents.PDF.Layout.Shapes;

namespace PageShapeSample
{
   class Program
   {
      static void Main(string[] args)
      {
         using ( FileStream fileA = new FileStream(
            "SuccesStory_Gems.pdf", FileMode.Open, FileAccess.Read))
         using ( FileStream fileB = new FileStream(
            "SuccesStory_Netco.pdf", FileMode.Open, FileAccess.Read))
         {
            Document document = new Document();
            addPages(document, fileA);
            addPages(document, fileB);

            using (FileStream file = new FileStream(
               "out.pdf", FileMode.Create, FileAccess.Write))
            {
               document.Write(file);
            }
         }
      }

      static void addPages(Document document, FileStream file)
      {
         PageShape pageShape = new PageShape(file, 0, "");
         int n = pageShape.PageCount;
         for (int i = 0; i < n; i++)
         {
            pageShape.PageIndex = i;
            Section section = new Section();
            section.StartOnNewPage = true;
            section.PageSize = new PageSize(
               pageShape.Width, pageShape.Height);
            section.Margin.Left = 0;
            section.Margin.Right = 0;
            section.Margin.Top = 0;
            section.Margin.Bottom = 0;
            document.Sections.Add(section);
            Drawing drawing = new Drawing(
               pageShape.Width, pageShape.Height);
            drawing.Shapes.Add(pageShape.Clone());
            section.Paragraphs.Add(drawing);
         }
      }
   }
}
    Sub Main()
        ' Find all pdf documents in input folder.
        Dim directoryInfo As New DirectoryInfo("..\..\../inputdocuments")
        Dim allPDFs As FileInfo() = directoryInfo.GetFiles("*.pdf")

        ' Create a document for the merged result.
        Dim mergedDocument As New Document()

        ' Keep a list of input streams to close when done.
        Dim streams As New List(Of FileStream)()
        For Each fileInfo As FileInfo In allPDFs
            ' Open input stream and add to list of streams.
            Dim stream As New FileStream(fileInfo.FullName, FileMode.Open, FileAccess.Read)
            streams.Add(stream)

            ' Open input document.
            Dim document As New Document(stream)

            ' Append all pages to target document.
            ' The target document holds references to data in the input stream.
            ' For efficiency it does not copy this data so the input streams
            ' should not be closed before the merged document is saved.
            mergedDocument.Pages.AddRange(document.Pages.CloneToArray())
        Next

        ' Save merged document.
        Using output As New FileStream("..\..\output.pdf", FileMode.Create, FileAccess.Write)
            mergedDocument.Write(output)
        End Using

        ' Close all input streams.
        For Each stream As FileStream In streams
            stream.Close()
        Next

    End Sub