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:

1 using System; 2 using System.IO; 3 4 using TallComponents.PDF.Layout; 5 using TallComponents.PDF.Layout.Paragraphs; 6 using TallComponents.PDF.Layout.Shapes; 7 using TallComponents.PDF.Layout.Fonts; 8 9 namespace PageShapeSample 10 { 11 class Program 12 { 13 static void Main(string[] args) 14 { 15 //open two existing PDFs 16 using (FileStream fileA = new FileStream( 17 "input.pdf", FileMode.Open, FileAccess.Read)) 18 using (FileStream fileB = new FileStream( 19 "input.pdf", FileMode.Open, FileAccess.Read)) 20 { 21 //create a new document and add new content to it 22 Document document = new Document(); 23 addNewContent(document); 24 25 //add the content of the two PDFs 26 addPages(document, fileA); 27 addPages(document, fileB); 28 29 //save new PDF to disk 30 using (FileStream file = new FileStream( 31 "out.pdf", FileMode.Create, FileAccess.Write)) 32 { 33 document.Write(file); 34 } 35 } 36 } 37 38 //adds some filler text to a pdf section 39 static void addNewContent(Document document) 40 { 41 Section section = new Section(); 42 document.Sections.Add(section); 43 44 for (int i = 0; i < 10; i++) 45 { 46 TextParagraph text = new TextParagraph(); 47 text.SpacingAfter = 18; 48 section.Paragraphs.Add(text); 49 50 Fragment fragment = new Fragment(); 51 fragment.Font = Font.Helvetica; 52 fragment.FontSize = 18; 53 fragment.Text = "Filler text..."; 54 text.Fragments.Add(fragment); 55 } 56 } 57 58 //copy content from input filestream to the document 59 static void addPages(Document document, FileStream file) 60 { 61 PageShape pageShape = new PageShape(file, 0, ""); 62 int n = pageShape.PageCount; 63 for (int i = 0; i < n; i++) 64 { 65 pageShape.PageIndex = i; 66 Section section = new Section(); 67 section.StartOnNewPage = true; 68 section.PageSize = new PageSize( 69 pageShape.Width, pageShape.Height); 70 section.Margin.Left = 0; 71 section.Margin.Right = 0; 72 section.Margin.Top = 0; 73 section.Margin.Bottom = 0; 74 document.Sections.Add(section); 75 //clone the page to the new document 76 Drawing drawing = new Drawing( 77 pageShape.Width, pageShape.Height); 78 drawing.Shapes.Add(pageShape.Clone()); 79 section.Paragraphs.Add(drawing); 80 } 81 } 82 } 83 }