Skip to main content
Skip table of contents

Create booklet from A4 document

This example was written in response to questions such as:


makea3booklet

JAVA

JAVA
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2023 Apryse Group NV
    Authors: Apryse Software.

    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
package com.itextpdf.samples.sandbox.merge;

import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.kernel.pdf.xobject.PdfFormXObject;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class MakeA3Booklet {
    public static final String DEST = "./target/sandbox/merge/make_a3_booklet.pdf";

    public static final String SRC = "./src/main/resources/pdfs/primes.pdf";

    public static void main(String[] args) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();

        new MakeA3Booklet().manipulatePdf(DEST);
    }

    protected void manipulatePdf(String dest) throws IOException {
        PdfDocument srcDoc = new PdfDocument(new PdfReader(SRC));
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        pdfDoc.setDefaultPageSize(PageSize.A3.rotate());

        PdfCanvas canvas = new PdfCanvas(pdfDoc.addNewPage());
        float a4Width = PageSize.A4.getWidth();
        int numberOfPages = srcDoc.getNumberOfPages();
        int i = 0;
        while (i++ < numberOfPages) {
            PdfFormXObject page = srcDoc.getPage(i).copyAsFormXObject(pdfDoc);
            if (i % 2 == 1) {
                canvas.addXObjectAt(page, 0, 0);
            } else {
                canvas.addXObjectAt(page, a4Width, 0);
                canvas = new PdfCanvas(pdfDoc.addNewPage());
            }
        }

        pdfDoc.close();
        srcDoc.close();
    }
}

C#

C#
using System;
using System.IO;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas;
using iText.Kernel.Pdf.Xobject;

namespace iText.Samples.Sandbox.Merge
{
    public class MakeA3Booklet
    {
        public static readonly String DEST = "results/sandbox/merge/make_a3_booklet.pdf";

        public static readonly String SRC = "../../../resources/pdfs/primes.pdf";

        public static void Main(String[] args)
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();

            new MakeA3Booklet().ManipulatePdf(DEST);
        }

        protected void ManipulatePdf(String dest)
        {
            PdfDocument srcDoc = new PdfDocument(new PdfReader(SRC));
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
            pdfDoc.SetDefaultPageSize(PageSize.A3.Rotate());

            PdfCanvas canvas = new PdfCanvas(pdfDoc.AddNewPage());
            float a4Width = PageSize.A4.GetWidth();
            int numberOfPages = srcDoc.GetNumberOfPages();
            int i = 0;
            while (i++ < numberOfPages)
            {
                PdfFormXObject page = srcDoc.GetPage(i).CopyAsFormXObject(pdfDoc);
                if (i % 2 == 1)
                {
                    canvas.AddXObjectAt(page, 0, 0);
                }
                else
                {
                    canvas.AddXObjectAt(page, a4Width, 0);
                    canvas = new PdfCanvas(pdfDoc.AddNewPage());
                }
            }

            pdfDoc.Close();
            srcDoc.Close();
        }
    }
}


makebooklet

JAVA

JAVA
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2023 Apryse Group NV
    Authors: Apryse Software.

    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
package com.itextpdf.samples.sandbox.merge;

import com.itextpdf.kernel.geom.AffineTransform;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.kernel.pdf.xobject.PdfFormXObject;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class MakeBooklet {
    public static final String DEST = "./target/sandbox/merge/make_booklet.pdf";

    public static final String SRC = "./src/main/resources/pdfs/primes.pdf";

    public static void main(String[] args) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();

        new MakeBooklet().manipulatePdf(DEST);
    }

    protected void manipulatePdf(String dest) throws IOException {
        PdfDocument srcDoc = new PdfDocument(new PdfReader(SRC));
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));

        float a4Width = PageSize.A4.getWidth();
        float a4Height = PageSize.A4.getHeight();
        PageSize pagesize = new PageSize(a4Width * 4, a4Height * 2);
        pdfDoc.setDefaultPageSize(pagesize);

        int numberOfPages = srcDoc.getNumberOfPages();
        int p = 1;
        PdfCanvas canvas = new PdfCanvas(pdfDoc.addNewPage());
        while ((p - 1) <= numberOfPages) {
            copyPageToDoc(canvas, srcDoc, pdfDoc, p + 3, 0);
            copyPageToDoc(canvas, srcDoc, pdfDoc, p + 12, a4Width);
            copyPageToDoc(canvas, srcDoc, pdfDoc, p + 15, a4Width * 2);
            copyPageToDoc(canvas, srcDoc, pdfDoc, p, a4Width * 3);
            canvas.saveState();

            // Rotate on 180 degrees and copy pages to the top row.
            AffineTransform at = AffineTransform.getRotateInstance((float) -Math.PI);
            at.concatenate(AffineTransform.getTranslateInstance(0, -a4Height * 2));
            canvas.concatMatrix(at);
            copyPageToDoc(canvas, srcDoc, pdfDoc, p + 4, -a4Width);
            copyPageToDoc(canvas, srcDoc, pdfDoc, p + 11, -a4Width * 2);
            copyPageToDoc(canvas, srcDoc, pdfDoc, p + 8, -a4Width * 3);
            copyPageToDoc(canvas, srcDoc, pdfDoc, p + 7, -a4Width * 4);
            canvas.restoreState();

            canvas = new PdfCanvas(pdfDoc.addNewPage());
            copyPageToDoc(canvas, srcDoc, pdfDoc, p + 1, 0);
            copyPageToDoc(canvas, srcDoc, pdfDoc, p + 14, a4Width);
            copyPageToDoc(canvas, srcDoc, pdfDoc, p + 13, a4Width * 2);
            copyPageToDoc(canvas, srcDoc, pdfDoc, p + 2, a4Width * 3);
            canvas.saveState();

            // Rotate on 180 degrees and copy pages to the top row.
            canvas.concatMatrix(at);
            copyPageToDoc(canvas, srcDoc, pdfDoc, p + 6, -a4Width);
            copyPageToDoc(canvas, srcDoc, pdfDoc, p + 9, -a4Width * 2);
            copyPageToDoc(canvas, srcDoc, pdfDoc, p + 10, -a4Width * 3);
            copyPageToDoc(canvas, srcDoc, pdfDoc, p + 5, -a4Width * 4);
            canvas.restoreState();

            if ((p - 1) / 16 < numberOfPages / 16) {
                canvas = new PdfCanvas(pdfDoc.addNewPage());
            }

            p += 16;
        }

        pdfDoc.close();
        srcDoc.close();
    }

    private static void copyPageToDoc(PdfCanvas canvas, PdfDocument srcDoc, PdfDocument pdfDoc,
            int pageNumber, float offsetX) throws IOException {
        if (pageNumber > srcDoc.getNumberOfPages()) {
            return;
        }

        PdfFormXObject page = srcDoc.getPage(pageNumber).copyAsFormXObject(pdfDoc);
        canvas.addXObjectAt(page, offsetX, 0);
    }
}

C#

C#
using System;
using System.IO;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas;
using iText.Kernel.Pdf.Xobject;

namespace iText.Samples.Sandbox.Merge
{
    public class MakeBooklet
    {
        public static readonly String DEST = "results/sandbox/merge/make_booklet.pdf";

        public static readonly String SRC = "../../../resources/pdfs/primes.pdf";

        public static void Main(String[] args)
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();

            new MakeBooklet().ManipulatePdf(DEST);
        }

        protected void ManipulatePdf(String dest)
        {
            PdfDocument srcDoc = new PdfDocument(new PdfReader(SRC));
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));

            float a4Width = PageSize.A4.GetWidth();
            float a4Height = PageSize.A4.GetHeight();
            PageSize pagesize = new PageSize(a4Width * 4, a4Height * 2);
            pdfDoc.SetDefaultPageSize(pagesize);

            int numberOfPages = srcDoc.GetNumberOfPages();
            int p = 1;
            PdfCanvas canvas = new PdfCanvas(pdfDoc.AddNewPage());
            while ((p - 1) <= numberOfPages)
            {
                CopyPageToDoc(canvas, srcDoc, pdfDoc, p + 3, 0);
                CopyPageToDoc(canvas, srcDoc, pdfDoc, p + 12, a4Width);
                CopyPageToDoc(canvas, srcDoc, pdfDoc, p + 15, a4Width * 2);
                CopyPageToDoc(canvas, srcDoc, pdfDoc, p, a4Width * 3);
                canvas.SaveState();

                // Rotate on 180 degrees and copy pages to the top row.
                AffineTransform at = AffineTransform.GetRotateInstance((float) -Math.PI);
                at.Concatenate(AffineTransform.GetTranslateInstance(0, -a4Height * 2));
                canvas.ConcatMatrix(at);
                CopyPageToDoc(canvas, srcDoc, pdfDoc, p + 4, -a4Width);
                CopyPageToDoc(canvas, srcDoc, pdfDoc, p + 11, -a4Width * 2);
                CopyPageToDoc(canvas, srcDoc, pdfDoc, p + 8, -a4Width * 3);
                CopyPageToDoc(canvas, srcDoc, pdfDoc, p + 7, -a4Width * 4);
                canvas.RestoreState();

                canvas = new PdfCanvas(pdfDoc.AddNewPage());
                CopyPageToDoc(canvas, srcDoc, pdfDoc, p + 1, 0);
                CopyPageToDoc(canvas, srcDoc, pdfDoc, p + 14, a4Width);
                CopyPageToDoc(canvas, srcDoc, pdfDoc, p + 13, a4Width * 2);
                CopyPageToDoc(canvas, srcDoc, pdfDoc, p + 2, a4Width * 3);
                canvas.SaveState();

                // Rotate on 180 degrees and copy pages to the top row.
                canvas.ConcatMatrix(at);
                CopyPageToDoc(canvas, srcDoc, pdfDoc, p + 6, -a4Width);
                CopyPageToDoc(canvas, srcDoc, pdfDoc, p + 9, -a4Width * 2);
                CopyPageToDoc(canvas, srcDoc, pdfDoc, p + 10, -a4Width * 3);
                CopyPageToDoc(canvas, srcDoc, pdfDoc, p + 5, -a4Width * 4);
                canvas.RestoreState();

                if ((p - 1) / 16 < numberOfPages / 16)
                {
                    canvas = new PdfCanvas(pdfDoc.AddNewPage());
                }

                p += 16;
            }

            pdfDoc.Close();
            srcDoc.Close();
        }

        private static void CopyPageToDoc(PdfCanvas canvas, PdfDocument srcDoc, PdfDocument pdfDoc,
            int pageNumber, float offsetX)
        {
            if (pageNumber > srcDoc.GetNumberOfPages())
            {
                return;
            }

            PdfFormXObject page = srcDoc.GetPage(pageNumber).CopyAsFormXObject(pdfDoc);
            canvas.AddXObjectAt(page, offsetX, 0);
        }
    }
}


makebookleta3

JAVA

JAVA
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2023 Apryse Group NV
    Authors: Apryse Software.

    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
package com.itextpdf.samples.sandbox.merge;

import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.kernel.pdf.xobject.PdfFormXObject;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class MakeBookletA3 {
    public static final String DEST = "./target/sandbox/merge/make_booklet_a3.pdf";

    public static final String SRC = "./src/main/resources/pdfs/primes.pdf";

    public static void main(String[] args) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();

        new MakeBookletA3().manipulatePdf(DEST);
    }

    protected void manipulatePdf(String dest) throws IOException {
        PdfDocument srcDoc = new PdfDocument(new PdfReader(SRC));
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));

        float a4_width = PageSize.A4.getWidth();
        PageSize pageSize = new PageSize(a4_width * 2, PageSize.A4.getHeight());
        pdfDoc.setDefaultPageSize(pageSize);

        int numberOfPages = srcDoc.getNumberOfPages();
        int p = 1;
        PdfCanvas canvas = new PdfCanvas(pdfDoc.addNewPage());
        while ((p - 1) <= numberOfPages) {
            copyPage(canvas, srcDoc, pdfDoc, p + 3, 0);
            copyPage(canvas, srcDoc, pdfDoc, p, a4_width);

            canvas = new PdfCanvas(pdfDoc.addNewPage());
            copyPage(canvas, srcDoc, pdfDoc, p + 1, 0);
            copyPage(canvas, srcDoc, pdfDoc, p + 2, a4_width);

            if ((p - 1) / 4 < numberOfPages / 4) {
                canvas = new PdfCanvas(pdfDoc.addNewPage());
            }

            p += 4;
        }

        pdfDoc.close();
        srcDoc.close();
    }

    private static void copyPage(PdfCanvas canvas, PdfDocument srcDoc, PdfDocument pdfDoc,
            int pageNumber, float offsetX) throws IOException {
        if (pageNumber > srcDoc.getNumberOfPages()) {
            return;
        }

        PdfFormXObject page = srcDoc.getPage(pageNumber).copyAsFormXObject(pdfDoc);
        canvas.addXObjectAt(page, offsetX, 0);
    }
}

C#

C#
using System;
using System.IO;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas;
using iText.Kernel.Pdf.Xobject;

namespace iText.Samples.Sandbox.Merge
{
    public class MakeBookletA3
    {
        public static readonly String DEST = "results/sandbox/merge/make_booklet_a3.pdf";

        public static readonly String SRC = "../../../resources/pdfs/primes.pdf";

        public static void Main(String[] args)
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();

            new MakeBookletA3().ManipulatePdf(DEST);
        }

        protected void ManipulatePdf(String dest)
        {
            PdfDocument srcDoc = new PdfDocument(new PdfReader(SRC));
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));

            float a4_width = PageSize.A4.GetWidth();
            PageSize pageSize = new PageSize(a4_width * 2, PageSize.A4.GetHeight());
            pdfDoc.SetDefaultPageSize(pageSize);

            int numberOfPages = srcDoc.GetNumberOfPages();
            int p = 1;
            PdfCanvas canvas = new PdfCanvas(pdfDoc.AddNewPage());
            while ((p - 1) <= numberOfPages)
            {
                CopyPage(canvas, srcDoc, pdfDoc, p + 3, 0);
                CopyPage(canvas, srcDoc, pdfDoc, p, a4_width);

                canvas = new PdfCanvas(pdfDoc.AddNewPage());
                CopyPage(canvas, srcDoc, pdfDoc, p + 1, 0);
                CopyPage(canvas, srcDoc, pdfDoc, p + 2, a4_width);

                if ((p - 1) / 4 < numberOfPages / 4)
                {
                    canvas = new PdfCanvas(pdfDoc.AddNewPage());
                }

                p += 4;
            }

            pdfDoc.Close();
            srcDoc.Close();
        }

        private static void CopyPage(PdfCanvas canvas, PdfDocument srcDoc, PdfDocument pdfDoc,
            int pageNumber, float offsetX)
        {
            if (pageNumber > srcDoc.GetNumberOfPages())
            {
                return;
            }

            PdfFormXObject page = srcDoc.GetPage(pageNumber).CopyAsFormXObject(pdfDoc);
            canvas.AddXObjectAt(page, offsetX, 0);
        }
    }
}
JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.