Console: Convert an extra large PDF page to a TIFF image.

Code samples for VintaSoft Imaging .NET SDK. Here you can request a code sample.

Moderator: Alex

Post Reply
Site Admin
Posts: 1445
Joined: Thu Jul 10, 2008 2:21 pm

Console: Convert an extra large PDF page to a TIFF image.

Post by Alex » Tue Nov 22, 2016 7:04 pm

This topic contains a code sample that shows how to convert an extra large PDF page to a TIFF image:

Code: Select all

using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using Vintasoft.Imaging;
using Vintasoft.Imaging.Codecs.Decoders;
using Vintasoft.Imaging.Codecs.Encoders;
using Vintasoft.Imaging.Codecs.ImageFiles;
using Vintasoft.Imaging.ImageRendering;

namespace ConsoleApplication1
    /// <summary>
    /// This example shows how to convert PDF page of unlimited size to a TIFF file.<br />
    /// Size of PDF page is not limited because image renderer divides PDF page into tiles and
    /// process each tile separately.<br />
    /// Size of TIFF image is not limited because TIFF encoder divides TIFF image into tiles and
    /// process each tile separately.
    /// </summary>
    class Program
        static void Main(string[] args)
            // create an image collection
            using (ImageCollection images = new ImageCollection())
                // add PDF document to the image collection
                // set the rendering resolution for PDF document
                images.SetRenderingSettings(new RenderingSettings(new Resolution(300, 300)));

                // crop the first image and save the cropped image as a TIFF file
                CropPdfPageAndSaveAsTiff(images[0], new Rectangle(219, 250, 23031, 10406), 4096, "output.tif");

                // clear image collection and dispose images

        /// <summary>
        /// Crops image and saves the cropped image as a TIFF file.
        /// </summary>
        /// <param name="image">Source image.</param>
        /// <param name="imageCropRect">Rectangle that defines how image must be cropped.</param>
        /// <param name="imageTileSize">Size (width/height) of image tile.</param>
        /// <param name="outputTiffFilename">Outpur TIFF file.</param>
        static void CropPdfPageAndSaveAsTiff(
            VintasoftImage image,
            Rectangle imageCropRect,
            int imageTileSize,
            string outputTiffFilename)
            // create an empty image with necessary size
            using (VintasoftImage pageImage = new VintasoftImage(imageCropRect.Width, imageCropRect.Height, PixelFormat.Bgr24))
                pageImage.Resolution = image.Resolution;

                // create a TIFF encoder
                TiffEncoder tiffEncoder = new TiffEncoder();
                // specify that TIFF image must be divided into tiles
                tiffEncoder.Settings.UseTiles = true;
                // specify the tile size
                tiffEncoder.Settings.TileSize = new Size(imageTileSize, imageTileSize);
                // save image to a destination TIFF file
                pageImage.Save(outputTiffFilename, tiffEncoder);

            // open destination TIFF file
            using (FileStream stream = new FileStream(outputTiffFilename, FileMode.Open, FileAccess.ReadWrite))
                // create TIFF decoder
                using (TiffDecoder decoder = new TiffDecoder(stream))
                    // get raster grid editor
                    IRasterGridEditor rasterGridEditor = decoder.GetRasterGridEditor();
                    // get grid of raster regions
                    Rectangle[] grid = rasterGridEditor.GetRasterEditorGrid(0);
                    if (grid == null || grid.Length == 0)
                        throw new Exception("Image tiles cannot be changed.");

                    // create image renderer
                    ImageRenderer renderer = new ImageRenderer(image);

                    // get image tile rectangles
                    Rectangle[] imageTileRects = GetImageTileRects(image, imageCropRect, imageTileSize);

                    // for each image tile rectangle
                    for (int i = 0; i < imageTileRects.Length; i++)
                        // create an rendering task for rendering image tile
                        ImageRenderingTask renderingTask = ImageRenderingTask.CreateImageRegion(imageTileRects[i]);

                        // render image
                        using (VintasoftImage imagePart = renderer.ExecuteRendering(renderingTask))
                            // set the tile image as a new tile in the destination TIFF file
                            rasterGridEditor.SetImageRect(0, i, imagePart, null);

                            // save changes to the destination TIFF file

                        Console.Write(string.Format("{0} ", i));

        /// <summary>
        /// Returns the image tile rectangles.
        /// </summary>
        /// <param name="image">Source image.</param>
        /// <param name="imageCropRect">Rectangle that defines how image must be cropped.</param>
        /// <param name="imageTileSize">Size (width/height) of image tile.</param>
        /// <returns>An array of image tile rectangles.</returns>
        static Rectangle[] GetImageTileRects(VintasoftImage image, Rectangle imageCropRect, int imageTileSize)
            if ((imageTileSize % 16) != 0)
                throw new ApplicationException("Image tile size must be a multiple of 16.");

            List<Rectangle> imageTiles = new List<Rectangle>();

            // calculate tile count
            int xTileCount = imageCropRect.Width / imageTileSize;
            if ((imageCropRect.Width % imageTileSize) != 0)
            int yTileCount = imageCropRect.Height / imageTileSize;
            if ((imageCropRect.Height % imageTileSize) != 0)

            for (int y = 0; y < yTileCount; y++)
                for (int x = 0; x < xTileCount; x++)
                    // calculate coordinates of image tile region on the source image
                    int imageTileX = imageCropRect.Left + x * imageTileSize;
                    int imageTileY = imageCropRect.Top + y * imageTileSize;
                    int imageTileWidth = imageTileSize;
                    if ((imageTileX + imageTileWidth) > imageCropRect.Right)
                        imageTileWidth = imageCropRect.Right - imageTileX;
                    int imageTileHeight = imageTileSize;
                    if ((imageTileY + imageTileHeight) > imageCropRect.Bottom)
                        imageTileHeight = imageCropRect.Bottom - imageTileY;

                    // add new tile region to a list of regions
                    imageTiles.Add(new Rectangle(imageTileX, imageTileY, imageTileWidth, imageTileHeight));

            return imageTiles.ToArray();


VintaSoft Imaging .NET SDK (Standard) (Vintasoft.Imaging.dll) and VintaSoft PDF .NET Plug-in (Reader) (Vintasoft.Pdf.dll) are necessary for executing this sample.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest