Tiling and N-upping

Tags: tilingclippingN-upiText 7

Examples written in answer to questions such as:

Files: 
/*
 
    This file is part of the iText (R) project.
    Copyright (c) 1998-2016 iText Group NV
 
*/
 
/**
 * 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 com.itextpdf.samples.GenericTest;
import com.itextpdf.test.annotations.type.SampleTest;
import org.junit.experimental.categories.Category;
 
import java.io.File;
import java.io.IOException;
 
@Category(SampleTest.class)
public class TileInTwo extends GenericTest {
    public static final String DEST
            = "./target/test/resources/sandbox/merge/tile_in_two.pdf";
    public static final String SRC
            = "./src/test/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);
    }
 
    public static PageSize getHalfPageSize(Rectangle pagesize) {
        float width = pagesize.getWidth();
        float height = pagesize.getHeight();
        return new PageSize(width, height / 2);
    }
 
    public void manipulatePdf(String dest) throws IOException {
        PdfDocument srcDoc = new PdfDocument(new PdfReader(SRC));
        PageSize mediaBox = getHalfPageSize(srcDoc.getFirstPage().getPageSizeWithRotation());
 
        PdfDocument resultDoc = new PdfDocument(new PdfWriter(DEST));
        resultDoc.setDefaultPageSize(mediaBox);
 
        PdfCanvas canvas;
        int n = srcDoc.getNumberOfPages();
        int i = 1;
        while (true) {
            PdfFormXObject page = srcDoc.getPage(i).copyAsFormXObject(resultDoc);
            canvas = new PdfCanvas(resultDoc.addNewPage());
            canvas.addXObject(page, 0, -mediaBox.getHeight());
            canvas = new PdfCanvas(resultDoc.addNewPage());
            canvas.addXObject(page, 0, 0);
            if (++i > n) {
                break;
            }
            mediaBox = getHalfPageSize(srcDoc.getPage(i).getPageSizeWithRotation());
            resultDoc.setDefaultPageSize(mediaBox);
        }
 
        resultDoc.close();
        srcDoc.close();
    }
}
/*
 
    This file is part of the iText (R) project.
    Copyright (c) 1998-2016 iText Group NV
 
*/
 
/**
 * 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 com.itextpdf.samples.GenericTest;
import com.itextpdf.test.annotations.type.SampleTest;
import org.junit.experimental.categories.Category;
 
import java.io.File;
import java.io.IOException;
 
@Category(SampleTest.class)
public class TileInTwo2 extends GenericTest {
    public static final String DEST
            = "./target/test/resources/sandbox/merge/tile_in_two2.pdf";
    public static final String SRC
            = "./src/test/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);
    }
 
    public static PageSize getHalfPageSize(Rectangle pageSize) {
        float width = pageSize.getWidth();
        float height = pageSize.getHeight();
        return new PageSize(width / 2, height);
    }
 
    public void manipulatePdf(String dest) throws IOException {
        PdfDocument srcDoc = new PdfDocument(new PdfReader(SRC));
        PageSize mediaBox = getHalfPageSize(srcDoc.getFirstPage().getPageSizeWithRotation());
 
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(DEST));
        pdfDoc.setDefaultPageSize(mediaBox);
 
        PdfCanvas canvas;
        int n = srcDoc.getNumberOfPages();
        int i = 1;
        while (true) {
            PdfFormXObject page = srcDoc.getPage(i).copyAsFormXObject(pdfDoc);
            canvas = new PdfCanvas(pdfDoc.addNewPage());
            canvas.addXObject(page, 0, 0);
            canvas = new PdfCanvas(pdfDoc.addNewPage());
            canvas.addXObject(page, -mediaBox.getWidth(), 0);
            if (++i > n) {
                break;
            }
            mediaBox = getHalfPageSize(srcDoc.getPage(i).getPageSizeWithRotation());
            pdfDoc.setDefaultPageSize(mediaBox);
        }
        pdfDoc.close();
        srcDoc.close();
    }
}
/*
 
    This file is part of the iText (R) project.
    Copyright (c) 1998-2016 iText Group NV
 
*/
 
/**
 * 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.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.kernel.pdf.xobject.PdfFormXObject;
import com.itextpdf.samples.GenericTest;
import com.itextpdf.test.annotations.type.SampleTest;
import org.junit.experimental.categories.Category;
 
import java.io.File;
import java.io.IOException;
 
@Category(SampleTest.class)
public class TilingHero extends GenericTest {
    public static final String DEST
            = "./target/test/resources/sandbox/merge/tiling_hero.pdf";
    public static final String RESOURCE
            = "./src/test/resources/pdfs/hero.pdf";
 
    public static void main(String[] args) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new TilingHero().manipulatePdf(DEST);
    }
 
    public void manipulatePdf(String dest) throws IOException {
        PdfDocument srcDoc = new PdfDocument(new PdfReader(RESOURCE));
 
        Rectangle pageSize = srcDoc.getFirstPage().getPageSizeWithRotation();
        float width = pageSize.getWidth();
        float height = pageSize.getHeight();
        Rectangle mediaBox = new Rectangle(0, 3 * height, width, height);
 
        PdfDocument resultDoc = new PdfDocument(new PdfWriter(DEST));
        resultDoc.setDefaultPageSize(new PageSize(mediaBox));
        PdfFormXObject page = srcDoc.getFirstPage().copyAsFormXObject(resultDoc);
        PdfCanvas canvas;
        for (int i = 0; i < 16; ) {
            canvas = new PdfCanvas(resultDoc.addNewPage());
            canvas.addXObject(page, 4, 0, 0, 4, 0, 0);
            i++;
            mediaBox = new Rectangle(
                    (i % 4) * width, (4 - (i / 4)) * height,
                    width, - height);
            resultDoc.setDefaultPageSize(new PageSize(mediaBox));
        }
 
        srcDoc.close();
        resultDoc.close();
    }
}
/*
 
    This file is part of the iText (R) project.
    Copyright (c) 1998-2016 iText Group NV
 
*/
 
/**
 * 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 com.itextpdf.samples.GenericTest;
import com.itextpdf.test.annotations.type.SampleTest;
import org.junit.experimental.categories.Category;
 
import java.io.File;
 
@Category(SampleTest.class)
public class TileClipped extends GenericTest {
    public static final String DEST =
            "./target/test/resources/sandbox/stamper/tile_clipped.pdf";
    public static final String SRC =
            "./src/test/resources/pdfs/hero.pdf";
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new TileClipped().manipulatePdf(DEST);
    }
 
    @Override
    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));
        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
        float x, y;
        for (int i = 0; i < 16; i++) {
            x = -rect.getWidth() * (i % 4) + margin;
            y = rect.getHeight() * (i / 4 - 3) + margin;
            content.rectangle(margin, margin, rect.getWidth(), rect.getHeight());
            content.clip();
            content.newPath();
            content.addXObject(page, 4, 0, 0, 4, x, y);
            if (15 != i) {
                content = new PdfCanvas(pdfDoc.addNewPage());
            }
        }
        srcDoc.close();
        pdfDoc.close();
    }
}
File nameRaw URLUpdated
TileInTwo.javaTileInTwo.java2016-08-09 12:10 pm
TileInTwo2.javaTileInTwo2.java2016-08-09 12:10 pm
TilingHero.javaTilingHero.java2016-08-09 12:10 pm
TileClipped.javaTileClipped.java2016-08-09 12:11 pm
Resources: 
File nameRaw URLUpdated
hero.pdfhero.pdf2016-08-09 12:12 pm
links1.pdflinks1.pdf2016-08-09 12:12 pm
links2.pdflinks2.pdf2016-08-09 12:12 pm
united_states.pdfunited_states.pdf2016-08-09 10:03 am
Results: 
File nameRaw URLUpdated
cmp_tile_in_two.pdfcmp_tile_in_two.pdf2016-08-09 12:16 pm
cmp_tile_in_two2.pdfcmp_tile_in_two2.pdf2016-08-09 12:16 pm
cmp_tiling_hero.pdfcmp_tiling_hero.pdf2016-08-09 12:16 pm
cmp_tile_clipped.pdfcmp_tile_clipped.pdf2016-08-09 12:17 pm