Translate PDF page content

Manipulate PDF, Shapes
7/24/2014

Downloads

This code sample shows how to apply a single translation to all the content on a PDF page.

translate-pdf-content.png

C# code sample

1 const string inputFile = @"..\..\inputDocuments\PackingLightBrochure.pdf"; 2 using (var inFile = new FileStream(inputFile, FileMode.Open, FileAccess.Read)) 3 { 4 5 // open the source document 6 Document documentIn = new Document(inFile); 7 8 // create the target document 9 Document documentOut = new Document(); 10 11 // move to right and down 1 inch (2.54 cm) or 72 points 12 // the origin of the page is in the bottom left corner, so move vertically downwards 13 const double tx = 72; 14 const double ty = -72; 15 16 // enumerate the pages in the source document 17 foreach(Page original in documentIn.Pages) 18 { 19 // append a translated version of the original page to the target document 20 21 // create a new page that has the same width and height 22 Page newPage = new Page(original.Width, original.Height); 23 documentOut.Pages.Add(newPage); 24 25 // translate by tx, ty (origin is at the lower left corner - units are in points) 26 PageShape shape = new PageShape(original, tx, ty, original.Width, original.Height); 27 newPage.VisualOverlay.Add(shape); 28 } 29 30 // write the target document to disk 31 const string fileName = @"..\..\translatepage.pdf"; 32 33 using (FileStream output = new FileStream(fileName, FileMode.Create, FileAccess.Write)) 34 { 35 documentOut.Write(output); 36 } 37 }

VB.NET code sample

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 Dim tx As Double = 0 9 Dim ty As Double = -72 10 For i As Integer = 0 To documentIn.Pages.Count - 1 11 ' move down 1 inch (2.54 cm) 12 ' enumerate the pages in the source document 13 ' append a translated version of the original page to the target document 14 15 ' create a new page that has the same width and height 16 Dim page As New Page(documentIn.Pages(i).Width, documentIn.Pages(i).Height) 17 documentOut.Pages.Add(page) 18 19 ' translate by tx, ty (origin is at the lower left corner - units are in points) 20 Dim pageShape As New PageShape(documentIn.Pages(i), tx, ty, page.Width, page.Height) 21 page.VisualOverlay.Add(pageShape) 22 Next 23 24 ' write the target document to disk 25 Using outFile As New FileStream("..\..\translatepage.pdf", FileMode.Create, FileAccess.Write) 26 documentOut.Write(outFile) 27 End Using 28 End Using