How to mirror PDF pages and other shapes
How to mirror PDF pages and other shapes
In this code sample 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.
using (FileStream fs = new FileStream(@"..\..\../inputdocuments/packinglightbrochure.pdf",FileMode.Open,FileAccess.Read))
{
//open pdf source
Document document = new Document(fs);
//create the new document to add the mirrored pages
Document mirrored = new Document();
foreach(Page page in document.Pages)
{
//create a pageshape to apply the transformations to
PageShape pageShape = new PageShape(page);
MatrixTransform transform = new MatrixTransform();
//mirror horizontally
transform.Scale(-1, 1);
//because the page is out of view after horizontal mirroring, we need to shift it into view again
transform.Translate(page.Width, 0);
pageShape.Transform = transform;
//create a new page from the transformed pageshape and add it to new document
Page mirroredPage = new Page(page.Width, page.Height);
mirroredPage.VisualOverlay.Add(pageShape);
mirrored.Pages.Add(mirroredPage);
}
//write the mirrored document to disk
using (FileStream output = new FileStream(@"..\../out.pdf",FileMode.Create,FileAccess.Write))
{
mirrored.Write(output);
}
}