Tiling and N-upping

Tags: tilingclippingN-upiText 5

Examples written in answer to questions such as:

Files: 
/**
 * Example written by Bruno Lowagie.
 */
package sandbox.merge;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.File;
 
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
 
@WrapToTest
public class TilingHero {
 
    /** The original PDF file. */
    public static final String SRC
        = "resources/pdfs/hero.pdf";
 
    /** The resulting PDF file. */
    public static final String DEST
        = "results/merge/superman.pdf";
 
    /**
     * Manipulates a PDF file src with the file dest as result
     * @param src the original PDF
     * @param dest the resulting PDF
     * @throws IOException
     * @throws DocumentException
     */
    public void manipulatePdf(String src, String dest)
        throws IOException, DocumentException {
    	// Creating a reader
        PdfReader reader = new PdfReader(src);
        Rectangle pagesize = reader.getPageSizeWithRotation(1);
        float width = pagesize.getWidth();
        float height = pagesize.getHeight();
        // step 1
        Rectangle mediabox = new Rectangle(0, 3 * height, width, 4 * height);
        Document document = new Document(mediabox);
        // step 2
        PdfWriter writer
            = PdfWriter.getInstance(document, new FileOutputStream(dest));
        // step 3
        document.open();
        // step 4
        PdfContentByte content = writer.getDirectContent();
        PdfImportedPage page = writer.getImportedPage(reader, 1);
        // adding the same page 16 times with a different offset
        for (int i = 0; i < 16; ) {
            content.addTemplate(page, 4, 0, 0, 4, 0, 0);
            i++;
            mediabox = new Rectangle(
                    (i % 4) * width, (4 - (i / 4)) * height,
                    ((i % 4) + 1) * width, (3 - (i / 4)) * height);
            document.setPageSize(mediabox);
            document.newPage();
        }
        // step 4
        document.close();
        reader.close();
    }
 
    /**
     * Main method.
     * @param    args    no arguments needed
     * @throws DocumentException 
     * @throws IOException
     */
    public static void main(String[] args)
        throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new TilingHero().manipulatePdf(SRC, DEST);
    }
}
/**
 * Example written by Bruno Lowagie in answer to:
 * http://stackoverflow.com/questions/32769493/tiling-with-itext-and-adding-margins
 */
package sandbox.stamper;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfWriter;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
 
/**
 * @author Bruno Lowagie (iText Software)
 */
@WrapToTest
public class TileClipped {
 
    public static final String SRC = "resources/pdfs/hero.pdf";
    public static final String DEST = "results/stamper/hero_tiled_clipped.pdf";
 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new TileClipped().manipulatePdf(SRC, DEST);
    }
 
    public void manipulatePdf(String src, String dest)
        throws IOException, DocumentException {
        float margin = 30;
    	// Creating a reader
        PdfReader reader = new PdfReader(src);
        Rectangle rect = reader.getPageSizeWithRotation(1);
        Rectangle pagesize = new Rectangle(rect.getWidth() + margin * 2, rect.getHeight() + margin * 2);
        // step 1
        Document document = new Document(pagesize);
        // step 2
        PdfWriter writer
            = PdfWriter.getInstance(document, new FileOutputStream(dest));
        // step 3
        document.open();
        // step 4
        PdfContentByte content = writer.getDirectContent();
        PdfImportedPage page = writer.getImportedPage(reader, 1);
        // 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.addTemplate(page, 4, 0, 0, 4, x, y);
            document.newPage();
        }
        // step 4
        document.close();
        reader.close();
    }
}
/*
 * 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 sandbox.merge;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfWriter;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
 
@WrapToTest
public class TileInTwo {
 
    /** The original PDF file. */
    public static final String SRC
        = "resources/pdfs/united_states.pdf";
 
    /** The resulting PDF file. */
    public static final String DEST
        = "results/merge/unitedstates_tiled.pdf";
 
    public static void main(String[] args) throws DocumentException, IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new TileInTwo().manipulatePdf(SRC, DEST);
    }
 
    public void manipulatePdf(String src, String dest)
        throws IOException, DocumentException {
    	// Creating a reader
        PdfReader reader = new PdfReader(src);
        int n = reader.getNumberOfPages();
        // step 1
        Rectangle mediabox = new Rectangle(getHalfPageSize(reader.getPageSizeWithRotation(1)));
        Document document = new Document(mediabox);
        // step 2
        PdfWriter writer
            = PdfWriter.getInstance(document, new FileOutputStream(dest));
        // step 3
        document.open();
        // step 4
        PdfContentByte content = writer.getDirectContent();
        PdfImportedPage page;
        int i = 1;
        while (true) {
            page = writer.getImportedPage(reader, i);
            content.addTemplate(page, 0, -mediabox.getHeight());
            document.newPage();
            content.addTemplate(page, 0, 0);
            if (++i > n)
                break;
            mediabox = new Rectangle(getHalfPageSize(reader.getPageSizeWithRotation(i)));
            document.setPageSize(mediabox);
            document.newPage();
        }
        // step 5
        document.close();
        reader.close();
    }
 
    public Rectangle getHalfPageSize(Rectangle pagesize) {
        float width = pagesize.getWidth();
        float height = pagesize.getHeight();
        return new Rectangle(width, height / 2);
    }
}
/*
 * Example written by Bruno Lowagie in answer to a question on StackOverflow:
 * http://stackoverflow.com/questions/27600809/divide-pdf-exact-equal-half-using-itextsharp
 */
package sandbox.merge;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfWriter;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
 
@WrapToTest
public class TileInTwo2 {
 
    /** The original PDF file. */
    public static final String SRC
        = "resources/pdfs/united_states.pdf";
 
    /** The resulting PDF file. */
    public static final String DEST
        = "results/merge/unitedstates_tiled2.pdf";
 
    public static void main(String[] args) throws DocumentException, IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new TileInTwo2().manipulatePdf(SRC, DEST);
    }
 
    public void manipulatePdf(String src, String dest)
        throws IOException, DocumentException {
    	// Creating a reader
        PdfReader reader = new PdfReader(src);
        int n = reader.getNumberOfPages();
        // step 1
        Rectangle mediabox = new Rectangle(getHalfPageSize(reader.getPageSizeWithRotation(1)));
        Document document = new Document(mediabox);
        // step 2
        PdfWriter writer
            = PdfWriter.getInstance(document, new FileOutputStream(dest));
        // step 3
        document.open();
        // step 4
        PdfContentByte content = writer.getDirectContent();
        PdfImportedPage page;
        int i = 1;
        while (true) {
            page = writer.getImportedPage(reader, i);
            content.addTemplate(page, 0, 0);
            document.newPage();
            content.addTemplate(page, -mediabox.getWidth(), 0);
            if (++i > n)
                break;
            mediabox = new Rectangle(getHalfPageSize(reader.getPageSizeWithRotation(i)));
            document.setPageSize(mediabox);
            document.newPage();
        }
        // step 5
        document.close();
        reader.close();
    }
 
    public Rectangle getHalfPageSize(Rectangle pagesize) {
        float width = pagesize.getWidth();
        float height = pagesize.getHeight();
        return new Rectangle(width / 2, height);
    }
}
/**
 * Example written by Bruno Lowagie in answer to a question on StackOverflow
 * 
 * When concatenating documents, we add a named destination every time
 * a new document is started. After we've finished merging, we add an extra
 * page with the table of contents and links to the named destinations.
 */
package sandbox.merge;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.PdfAnnotation;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfWriter;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import sandbox.WrapToTest;
 
@WrapToTest
public class NUpWithLink {
    public static final String SRC1 = "resources/pdfs/links1.pdf";
    public static final String SRC2 = "resources/pdfs/links2.pdf";
    public static final String DEST = "results/merge/nup_links.pdf";
 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new NUpWithLink().createPdf(DEST);
    }
 
    public void createPdf(String filename) throws IOException, DocumentException {
         Document document = new Document();
         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
         float W = PageSize.A4.getWidth() / 2;
         float H = PageSize.A4.getHeight() / 2;
         document.open();
         int firstPage = 1;
         String[] files = new String[]{SRC1, SRC2};
         PdfContentByte cb = writer.getDirectContent();
         for (int i = 0; i < files.length; i++) {
            PdfReader currentReader = new PdfReader(files[i]);
            currentReader.consolidateNamedDestinations();
            for (int page = 1; page <= currentReader.getNumberOfPages(); page++) {
                PdfImportedPage importedPage = writer.getImportedPage(currentReader, page);
                float a = 0.5f;
                float e = (page % 2 == 0) ? W : 0;
                float f = (page % 4 == 1 || page % 4 == 2) ? H : 0;
                ArrayList<PdfAnnotation.PdfImportedLink> links = currentReader.getLinks(page);
                cb.addTemplate(importedPage, a, 0, 0, a, e, f);
                for (int j = 0; j < links.size(); j++) {
                    PdfAnnotation.PdfImportedLink link = (PdfAnnotation.PdfImportedLink)links.get(j);
                    if (link.isInternal()) {
                        int dPage = link.getDestinationPage();
                        int newDestPage = (dPage-1)/4 + firstPage;
                        float ee = (dPage % 2 == 0) ? W : 0;
                        float ff = (dPage % 4 == 1 || dPage % 4 == 2) ? H : 0;
                        link.setDestinationPage(newDestPage);
                        link.transformDestination(a, 0, 0, a, ee, ff);
                    }
                    link.transformRect(a, 0, 0, a, e, f);
                    writer.addAnnotation(link.createAnnotation(writer));
                }
                if (page % 4 == 0)
                document.newPage();
            }
            if (i < files.length - 1)
                document.newPage();
            firstPage += (currentReader.getNumberOfPages()+3)/4;
         }
         document.close();
    }
}
File nameRaw URLUpdated
TilingHero.javaTilingHero.java2015-11-07 6:03 pm
TileClipped.javaTileClipped.java2015-11-08 3:11 pm
TileInTwo.javaTileInTwo.java2015-11-07 6:03 pm
TileInTwo2.javaTileInTwo2.java2015-11-07 6:03 pm
NUpWithLink.javaNUpWithLink.java2015-11-07 6:03 pm
Resources: 
File nameRaw URLUpdated
hero.pdfhero.pdf2015-10-11 3:03 pm
united_states.pdfunited_states.pdf2015-10-30 6:41 pm
links1.pdflinks1.pdf2015-10-11 3:52 pm
links2.pdflinks2.pdf2015-10-11 3:52 pm