Create / impose PDF 2-up

Create / impose PDF 2-up

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

Create Pdf 2 Up

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.
using (FileStream inFile = new FileStream(@"..\..\..\inputDocuments\PackingLightBrochure.pdf", FileMode.Open, FileAccess.Read))
{
    // open the source document
    Document documentIn = new Document(inFile);

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

    // assume all pages have the same size and width < height (portrait)
    double width = documentIn.Pages[0].Width;
    double height = documentIn.Pages[0].Height;

    // enumerate the pages in the source document
    Page page = null;

    for (int i = 0; i < documentIn.Pages.Count; i++)
    {
        double bottom = height;
        // every 2 pages a new page is created
        if (i % 2 == 0)
        {
            // create a new page that will hold 2 source pages
            page = new Page(width, height);
            documentOut.Pages.Add(page);
        }
        else
        {
            bottom = height / 2;
        }

        // append a rotated version of the original page to the target document
        // because the pageShape rotates clockwise with the rotation point at the lower left
        // corner, we must apply a translation in order to get the pageShape in view again
        PageShape pageShape = new PageShape(documentIn.Pages[i]);

        pageShape.Height = page.Width;
        pageShape.Width = page.Height / 2;

        TransformCollection transforms = new TransformCollection();
        pageShape.Transform = transforms;

        transforms.Add(new RotateTransform(90));
        transforms.Add(new TranslateTransform(0, bottom));

        page.VisualOverlay.Add(pageShape);
    }

    // write the target document to disk
    using (FileStream outFile = new FileStream(@"..\..\create2up.pdf", FileMode.Create, FileAccess.Write))
    {
        documentOut.Write(outFile);
    }
}
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()

	' assume all pages have the same size and width < height (portrait)
	Dim width As Double = documentIn.Pages(0).Width
	Dim height As Double = documentIn.Pages(0).Height

	' enumerate the pages in the source document
	Dim page As Page = Nothing

	For i As Integer = 0 To documentIn.Pages.Count - 1
		Dim bottom As Double = height
		' every 2 pages a new page is created
		If i Mod 2 = 0 Then
			' create a new page that will hold 2 source pages
			page = New Page(width, height)
			documentOut.Pages.Add(page)
		Else
			bottom = height / 2
		End If

		' append a rotated version of the original page to the target document
		' because the pageShape rotates clockwise with the rotation point at the lower left
		' corner, we must apply a translation in order to get the pageShape in view again
		Dim pageShape As New PageShape(documentIn.Pages(i))

		pageShape.Height = page.Width
		pageShape.Width = page.Height / 2

		Dim transforms As New TransformCollection()
		pageShape.Transform = transforms

		transforms.Add(New RotateTransform(90))
		transforms.Add(New TranslateTransform(0, bottom))

		page.VisualOverlay.Add(pageShape)
	Next

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