Skip to main content
Skip table of contents

Tiling images

This example was written in answer to the question Click How to show an image with large dimensions across multiple pages?


tiledimage

TiledImage 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.images;

import com.itextpdf.io.image.ImageData;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;

import java.io.File;

public class TiledImage {
    public static final String DEST = "./target/sandbox/images/tiled_image.pdf";

    public static final String IMAGE = "./src/main/resources/img/bruno_ingeborg.jpg";

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

        new TiledImage().manipulatePdf(DEST);
    }

    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        ImageData image = ImageDataFactory.create(IMAGE);
        float width = image.getWidth();
        float height = image.getHeight();
        PageSize pageSize = new PageSize(width / 2, height / 2);
        pdfDoc.setDefaultPageSize(pageSize);

        PdfCanvas canvas = new PdfCanvas(pdfDoc.addNewPage());
        canvas.addImageWithTransformationMatrix(image, width, 0, 0, height, 0, -height / 2, false);

        canvas = new PdfCanvas(pdfDoc.addNewPage());
        canvas.addImageWithTransformationMatrix(image, width, 0, 0, height, 0, 0, false);

        canvas = new PdfCanvas(pdfDoc.addNewPage());
        canvas.addImageWithTransformationMatrix(image, width, 0, 0, height, -width / 2, -height / 2, false);

        canvas = new PdfCanvas(pdfDoc.addNewPage());
        canvas.addImageWithTransformationMatrix(image, width, 0, 0, height, -width / 2, 0, false);

        pdfDoc.close();
    }
}

TiledImage C#

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

namespace iText.Samples.Sandbox.Images
{
    public class TiledImage
    {
        public static readonly String DEST = "results/sandbox/images/tiled_image.pdf";

        public static readonly String IMAGE = "../../../resources/img/bruno_ingeborg.jpg";

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

            new TiledImage().ManipulatePdf(DEST);
        }

        protected void ManipulatePdf(String dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
            ImageData image = ImageDataFactory.Create(IMAGE);
            float width = image.GetWidth();
            float height = image.GetHeight();
            PageSize pageSize = new PageSize(width / 2, height / 2);
            pdfDoc.SetDefaultPageSize(pageSize);
            
            PdfCanvas canvas = new PdfCanvas(pdfDoc.AddNewPage());
            canvas.AddImageWithTransformationMatrix(image, width, 0, 0, height, 0, -height / 2, false);

            canvas = new PdfCanvas(pdfDoc.AddNewPage());
            canvas.AddImageWithTransformationMatrix(image, width, 0, 0, height, 0, 0, false);

            canvas = new PdfCanvas(pdfDoc.AddNewPage());
            canvas.AddImageWithTransformationMatrix(image, width, 0, 0, height, -width / 2, -height / 2, false);

            canvas = new PdfCanvas(pdfDoc.AddNewPage());
            canvas.AddImageWithTransformationMatrix(image, width, 0, 0, height, -width / 2, 0, false);

            pdfDoc.Close();
        }
    }
}

Resources


JavaScript errors detected

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

If this problem persists, please contact our support.