How to tile a document and add margins to the tiles?

I am tiling a document as is done in the TilingHero example. That works well, but now I'd also like to add margins to each page. Is that possible?

Posted on StackOverflow on Sep 24, 2015 by lagnat

Please take a look at the TileClipped example. It is based on the TilingHero example, but it has a twist:

public void manipulatePdf(String src, String dest) throws IOException {
    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 

Do you see how we make a distinction between rect and pagesize? We define rect as the size of the original pages and we define pagesize as a size that is slightly bigger (depending on the value of margin).

We use rect when we define the offset x and y, but we add margin to slightly change that offset. We change the offset because we clip the pagesize. Clipping is done by defining a clipping path:

content.rectangle(margin, margin, rect.getWidth(), rect.getHeight());
content.clip();
content.newPath();

Everything added after these three lines will be clipped by the rectangle we define in the rectangle() method. You may want to add extra saveState()/restoreState() methods if you also want to add other content, especially if that content needs to be added outside the clipping path.

Click this link if you want to see how to answer this question in iText 5.



Ready to use iText?

Try our iText 7 Library and add-ons FREE for 30 days. Test your proof of concept, and see if our solution is right for you.

Get my FREE trial
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