Products
PDFKit.NET 4.0
Created
7/24/2014
Tags
Manipulate PDF Shapes

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

translate-pdf-content.png

const string inputFile = @"..\..\inputDocuments\PackingLightBrochure.pdf";
using (var inFile = new FileStream(inputFile, FileMode.Open, FileAccess.Read))
{

     // open the source document
     Document documentIn = new Document(inFile);

     // create the target document
     Document documentOut = new Document();

     // move to right and down 1 inch (2.54 cm) or 72 points
     // the origin of the page is in the bottom left corner, so move vertically downwards
     const double tx = 72;
     const double ty = -72;

     // enumerate the pages in the source document
     foreach(Page original in documentIn.Pages)
     {
         // append a translated version of the original page to the target document

         // create a new page that has the same width and height
         Page newPage = new Page(original.Width, original.Height);
         documentOut.Pages.Add(newPage);

         // translate by tx, ty (origin is at the lower left corner - units are in points)
         PageShape shape = new PageShape(original, tx, ty, original.Width, original.Height);
         newPage.VisualOverlay.Add(shape);
     }

     // write the target document to disk
     const string fileName = @"..\..\translatepage.pdf";

     using (FileStream output = new FileStream(fileName, FileMode.Create, FileAccess.Write))
     {
         documentOut.Write(output);
     }
}
Using inFile As New FileStream("..\..\..\inputDocuments\PackingLightBrochure.pdf", FileMode.Open, FileAccess.Read)
    ' open the source document
    Dim documentIn As New Document(inFile)

    ' create the target document
    Dim documentOut As New Document()

    Dim tx As Double = 0
    Dim ty As Double = -72
    For i As Integer = 0 To documentIn.Pages.Count - 1
        ' move down 1 inch (2.54 cm)
        ' enumerate the pages in the source document
        ' append a translated version of the original page to the target document

        ' create a new page that has the same width and height
        Dim page As New Page(documentIn.Pages(i).Width, documentIn.Pages(i).Height)
        documentOut.Pages.Add(page)

        ' translate by tx, ty (origin is at the lower left corner - units are in points)
        Dim pageShape As New PageShape(documentIn.Pages(i), tx, ty, page.Width, page.Height)
        page.VisualOverlay.Add(pageShape)
    Next

    ' write the target document to disk
    Using outFile As New FileStream("..\..\translatepage.pdf", FileMode.Create, FileAccess.Write)
    documentOut.Write(outFile)
    End Using
End Using