Stitch PDF documents

This code sample shows how to stich PDF pages from an existing PDF document to a newly generated PDF using TallPDF.NET.

Code sample to stitch PDF documents

using System;
using System.IO;

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

namespace PageShapeSample
{
    class Program
    {
        static void Main(string[] args)
        {
            //open two existing PDFs
            using (FileStream fileA = new FileStream(
               "input.pdf", FileMode.Open, FileAccess.Read))
            using (FileStream fileB = new FileStream(
               "input.pdf", FileMode.Open, FileAccess.Read))
            {
                //create a new document and add new content to it
                Document document = new Document();
                addNewContent(document);

                //add the content of the two PDFs
                addPages(document, fileA);
                addPages(document, fileB);

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

        //adds some filler text to a pdf section
        static void addNewContent(Document document)
        {
            Section section = new Section();
            document.Sections.Add(section);

            for (int i = 0; i < 10; i++)
            {
                TextParagraph text = new TextParagraph();
                text.SpacingAfter = 18;
                section.Paragraphs.Add(text);

                Fragment fragment = new Fragment();
                fragment.Font = Font.Helvetica;
                fragment.FontSize = 18;
                fragment.Text = "Filler text...";
                text.Fragments.Add(fragment);
            }
        }

        //copy content from input filestream to the document
        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);
                //clone the page to the new document
                Drawing drawing = new Drawing(
                   pageShape.Width, pageShape.Height);
                drawing.Shapes.Add(pageShape.Clone());
                section.Paragraphs.Add(drawing);
            }
        }
    }
}
Download TallPDF.NET 4.0
We will send you a download link
Why do we ask your email address?
We send tips that speed up your evaluation
We let you know about bug fixes
You can always unsubscribe with one click
We never share your address with a 3rd party
Thank you for your download

We have sent an email with a download link. Alternatively, you may want to use the NuGet package manager to install our library.

Nuget ID

Use the NugetID and start right away, or download the package and install it handmatically