Create / impose PDF 2-up

Shapes, Manipulate PDF
11/3/2011

Downloads

This code sample shows how to do 2-up imposition using the PageShape class.

Create-pdf-2-up.png

Creating a PageShape from a specific page is like taking a screen shot of the page - but preserving vector graphics. We can translate, rotate or transform it in any way and place it on a page like an image. The following code sample implements the following steps:
1) Create a page shape of every page in the document.
2) Scale each page shape so two fit on a single page of the target document.
3) Rotate and place each page shape on the target document.

C# Code sample to impose PDF 2-up

1 using (FileStream inFile = new FileStream(@"..\..\..\inputDocuments\PackingLightBrochure.pdf", FileMode.Open, FileAccess.Read)) 2 { 3 // open the source document 4 Document documentIn = new Document(inFile); 5 6 // create the target document 7 Document documentOut = new Document(); 8 9 // assume all pages have the same size and width < height (portrait) 10 double width = documentIn.Pages[0].Width; 11 double height = documentIn.Pages[0].Height; 12 13 // enumerate the pages in the source document 14 Page page = null; 15 16 for (int i = 0; i < documentIn.Pages.Count; i++) 17 { 18 double bottom = height; 19 // every 2 pages a new page is created 20 if (i % 2 == 0) 21 { 22 // create a new page that will hold 2 source pages 23 page = new Page(width, height); 24 documentOut.Pages.Add(page); 25 } 26 else 27 { 28 bottom = height / 2; 29 } 30 31 // append a rotated version of the original page to the target document 32 // because the pageShape rotates clockwise with the rotation point at the lower left 33 // corner, we must apply a translation in order to get the pageShape in view again 34 PageShape pageShape = new PageShape(documentIn.Pages[i]); 35 36 pageShape.Height = page.Width; 37 pageShape.Width = page.Height / 2; 38 39 TransformCollection transforms = new TransformCollection(); 40 pageShape.Transform = transforms; 41 42 transforms.Add(new RotateTransform(90)); 43 transforms.Add(new TranslateTransform(0, bottom)); 44 45 page.VisualOverlay.Add(pageShape); 46 } 47 48 // write the target document to disk 49 using (FileStream outFile = new FileStream(@"..\..\create2up.pdf", FileMode.Create, FileAccess.Write)) 50 { 51 documentOut.Write(outFile); 52 } 53 }

VB.NET Code sample to impose PDF 2-up

1 Using inFile As New FileStream("..\..\..\inputDocuments\PackingLightBrochure.pdf", FileMode.Open, FileAccess.Read) 2 ' open the source document 3 Dim documentIn As New Document(inFile) 4 5 ' create the target document 6 Dim documentOut As New Document() 7 8 ' assume all pages have the same size and width < height (portrait) 9 Dim width As Double = documentIn.Pages(0).Width 10 Dim height As Double = documentIn.Pages(0).Height 11 12 ' enumerate the pages in the source document 13 Dim page As Page = Nothing 14 15 For i As Integer = 0 To documentIn.Pages.Count - 1 16 Dim bottom As Double = height 17 ' every 2 pages a new page is created 18 If i Mod 2 = 0 Then 19 ' create a new page that will hold 2 source pages 20 page = New Page(width, height) 21 documentOut.Pages.Add(page) 22 Else 23 bottom = height / 2 24 End If 25 26 ' append a rotated version of the original page to the target document 27 ' because the pageShape rotates clockwise with the rotation point at the lower left 28 ' corner, we must apply a translation in order to get the pageShape in view again 29 Dim pageShape As New PageShape(documentIn.Pages(i)) 30 31 pageShape.Height = page.Width 32 pageShape.Width = page.Height / 2 33 34 Dim transforms As New TransformCollection() 35 pageShape.Transform = transforms 36 37 transforms.Add(New RotateTransform(90)) 38 transforms.Add(New TranslateTransform(0, bottom)) 39 40 page.VisualOverlay.Add(pageShape) 41 Next 42 43 ' write the target document to disk 44 Using outFile As New FileStream("..\..\create2up.pdf", FileMode.Create, FileAccess.Write) 45 documentOut.Write(outFile) 46 End Using 47 End Using