How to mirror PDF pages and other shapes

Manipulate PDF, Shapes
6/30/2015

In this codesample we will mirror the pages in an existing PDF document

In short, this is done by getting the PageShape from each page, and applying a mirror transformation to it and finally add it to a new document. Note that widgets and annotations cannot be flipped; however it is possible to move and rotate them. Although we only look at PageShapes in this sample, mirroring other Shapes is done in the same way.

mirrored_page.PNG

1 using (FileStream fs = new FileStream(@"..\..\../inputdocuments/packinglightbrochure.pdf",FileMode.Open,FileAccess.Read)) 2 { 3 //open pdf source 4 Document document = new Document(fs); 5 //create the new document to add the mirrored pages 6 Document mirrored = new Document(); 7 8 foreach(Page page in document.Pages) 9 { 10 //create a pageshape to apply the transformations to 11 PageShape pageShape = new PageShape(page); 12 13 MatrixTransform transform = new MatrixTransform(); 14 //mirror horizontally 15 transform.Scale(-1, 1); 16 //because the page is out of view after horizontal mirroring, we need to shift it into view again 17 transform.Translate(page.Width, 0); 18 19 pageShape.Transform = transform; 20 21 //create a new page from the transformed pageshape and add it to new document 22 Page mirroredPage = new Page(page.Width, page.Height); 23 mirroredPage.VisualOverlay.Add(pageShape); 24 mirrored.Pages.Add(mirroredPage); 25 } 26 27 //write the mirrored document to disk 28 using (FileStream output = new FileStream(@"..\../out.pdf",FileMode.Create,FileAccess.Write)) 29 { 30 mirrored.Write(output); 31 } 32 }