iText pdf library
Website search

Tiling and N-upping

Examples written in answer to questions such as:

TileInTwo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2020 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
/**
 * Example written by Bruno Lowagie in answer to a question on StackOverflow:
 * http://stackoverflow.com/questions/27011829/divide-one-page-pdf-file-in-two-pages-pdf-file
 */
package com.itextpdf.samples.sandbox.merge;
 
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.geom.Rectangle;
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.IOException;
 
public class TileInTwo {
    public static final String DEST = "./target/sandbox/merge/tile_in_two.pdf";
 
    public static final String SRC = "./src/main/resources/pdfs/united_states.pdf";
 
    public static void main(String[] args) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
 
        new TileInTwo().manipulatePdf(DEST);
    }
 
    protected void manipulatePdf(String dest) throws IOException {
        PdfDocument srcDoc = new PdfDocument(new PdfReader(SRC));
        PdfDocument resultDoc = new PdfDocument(new PdfWriter(dest));
 
        int numberOfPages = srcDoc.getNumberOfPages();
        for (int i = 1; i <= numberOfPages; i++) {
            PageSize mediaBox = getHalfHeightPageSize(srcDoc.getPage(i).getPageSizeWithRotation());
            resultDoc.setDefaultPageSize(mediaBox);
            PdfFormXObject page = srcDoc.getPage(i).copyAsFormXObject(resultDoc);
 
            PdfCanvas canvas = new PdfCanvas(resultDoc.addNewPage());
            canvas.addXObject(page, 0, -mediaBox.getHeight());
 
            canvas = new PdfCanvas(resultDoc.addNewPage());
            canvas.addXObject(page, 0, 0);
        }
 
        resultDoc.close();
        srcDoc.close();
    }
 
    private static PageSize getHalfHeightPageSize(Rectangle pageSize) {
        float width = pageSize.getWidth();
        float height = pageSize.getHeight();
        return new PageSize(width, height / 2);
    }
}
TileInTwo2.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2020 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
/**
 * Example written by Bruno Lowagie in answer to a question on StackOverflow:
 * http://stackoverflow.com/questions/27011829/divide-one-page-pdf-file-in-two-pages-pdf-file
 */
package com.itextpdf.samples.sandbox.merge;
 
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.geom.Rectangle;
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.IOException;
 
public class TileInTwo2 {
    public static final String DEST = "./target/sandbox/merge/tile_in_two2.pdf";
 
    public static final String SRC = "./src/main/resources/pdfs/united_states.pdf";
 
    public static void main(String[] args) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
 
        new TileInTwo2().manipulatePdf(DEST);
    }
 
    protected void manipulatePdf(String dest) throws IOException {
        PdfDocument srcDoc = new PdfDocument(new PdfReader(SRC));
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
 
        int numberOfPages = srcDoc.getNumberOfPages();
        for (int i = 1; i <= numberOfPages; i++) {
            PageSize mediaBox = getHalfWidthPageSize(srcDoc.getPage(i).getPageSizeWithRotation());
            pdfDoc.setDefaultPageSize(mediaBox);
            PdfFormXObject page = srcDoc.getPage(i).copyAsFormXObject(pdfDoc);
 
            PdfCanvas canvas = new PdfCanvas(pdfDoc.addNewPage());
            canvas.addXObject(page, 0, 0);
 
            canvas = new PdfCanvas(pdfDoc.addNewPage());
            canvas.addXObject(page, -mediaBox.getWidth(), 0);
        }
 
        pdfDoc.close();
        srcDoc.close();
    }
 
    private static PageSize getHalfWidthPageSize(Rectangle pageSize) {
        float width = pageSize.getWidth();
        float height = pageSize.getHeight();
        return new PageSize(width / 2, height);
    }
}
TilingHero.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2020 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
/**
 * Example written by Bruno Lowagie.
 */
package com.itextpdf.samples.sandbox.merge;
 
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfPage;
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.IOException;
 
public class TilingHero {
    public static final String DEST = "./target/sandbox/merge/tiling_hero.pdf";
 
    public static final String RESOURCE = "./src/main/resources/pdfs/hero.pdf";
 
    public static void main(String[] args) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
 
        new TilingHero().manipulatePdf(DEST);
    }
 
    protected void manipulatePdf(String dest) throws IOException {
        PdfDocument srcDoc = new PdfDocument(new PdfReader(RESOURCE));
        PdfDocument resultDoc = new PdfDocument(new PdfWriter(dest));
        PdfPage srcFirstPage = srcDoc.getFirstPage();
 
        Rectangle pageSize = srcFirstPage.getPageSizeWithRotation();
        float width = pageSize.getWidth();
        float height = pageSize.getHeight();
 
        // The top left rectangle of the tiled pdf picture
        Rectangle mediaBox = new Rectangle(0, 3 * height, width, height);
        resultDoc.setDefaultPageSize(new PageSize(mediaBox));
 
        PdfFormXObject page = srcFirstPage.copyAsFormXObject(resultDoc);
        for (int i = 1; i <= 16; i++) {
            PdfCanvas canvas = new PdfCanvas(resultDoc.addNewPage());
            canvas.addXObject(page, 4, 0, 0, 4, 0, 0);
 
            float xCoordinate = (i % 4) * width;
            float yCoordinate = (4 - (i / 4)) * height;
            mediaBox = new Rectangle(xCoordinate, yCoordinate, width, -height);
            resultDoc.setDefaultPageSize(new PageSize(mediaBox));
        }
 
        srcDoc.close();
        resultDoc.close();
    }
}
TileClipped.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2020 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
*/
 
/**
 * Example written by Bruno Lowagie in answer to:
 * http://stackoverflow.com/questions/32769493/tiling-with-itext-and-adding-margins
 */
 
package com.itextpdf.samples.sandbox.stamper;
 
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.geom.Rectangle;
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;
 
public class TileClipped {
    public static final String DEST = "./target/sandbox/stamper/tile_clipped.pdf";
    public static final String SRC = "./src/main/resources/pdfs/hero.pdf";
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
 
        new TileClipped().manipulatePdf(DEST);
    }
 
    protected void manipulatePdf(String dest) throws Exception {
        float margin = 30;
        PdfDocument srcDoc = new PdfDocument(new PdfReader(SRC));
        Rectangle rect = srcDoc.getFirstPage().getPageSizeWithRotation();
        Rectangle pageSize = new Rectangle(rect.getWidth() + margin * 2, rect.getHeight() + margin * 2);
 
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
 
        // The functionality below will work only for the pages, added after the method is called
        pdfDoc.setDefaultPageSize(new PageSize(pageSize));
 
        PdfCanvas content = new PdfCanvas(pdfDoc.addNewPage());
        PdfFormXObject page = srcDoc.getFirstPage().copyAsFormXObject(pdfDoc);
 
        // Adding the same page 16 times with a different offset
        for (int i = 0; i < 16; i++) {
            float x = -rect.getWidth() * (i % 4) + margin;
            float y = rect.getHeight() * (i / 4 - 3) + margin;
            content.rectangle(margin, margin, rect.getWidth(), rect.getHeight());
            content.clip();
            content.endPath();
            content.addXObject(page, 4, 0, 0, 4, x, y);
            if (15 != i) {
                content = new PdfCanvas(pdfDoc.addNewPage());
            }
        }
 
        pdfDoc.close();
    }
}

Click the following link to see the legacy example for iText 5. Except for security fixes, iText 5 is no longer being developed.



Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now