Stitch PDF documents

Generate PDF
11/16/2010

Downloads

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);
            }
        }
    }
}