pdf links

PDF Rendering
Convert PDF to Image (.NET)
Convert PDF to image on Android (Xamarin)
Convert PDF to image on iOS (Xamarin)
Convert PDF to image in Windows Store apps (.NET)
Convert PDF to image in Windows Phone apps (.NET)
PDF to image in Universal Windows Store apps (.NET)
Free PDF Viewer control for Windows Forms (.NET)
How to integrate PDF Viewer control in WPF app (.NET)
Creating WPF PDF Viewer supporting bookmarks (.NET)
Cross-platform PDF Viewer using GTK# (MONO)
Silverlight PDF viewer control (Silverlight 5)
Multithreaded PDF rendering (.NET)
Convert pdf to image in Silverlight app (C# sample)
How to set fallback fonts for PDF rendering (C#)
Avoiding the out-of-memory exception on rendering (C#)
PDF viewer single page application (WebAPI, AngularJS)
PDF viewer control for Windows 10 universal applications
Use custom ICC profile for CMYK to RGB conversion
PDF layers - separate images, text, annotations, graphics

PDF Forms Creation PDF Security
Conversion to PDF/A
Other topics
PDF Document Manipulation PDF Content Generation
Fixed and Flow layout document API (.NET)
Creation of grids and tables in PDF (C# sample)
How to create interactive documents using Actions (C# sample)
Text flow effects in PDF (C# sample)
How to generate ordered and bulleted lists in PDF (C# sample)
Convert HTML to PDF using flow layout API (C# sample)
How to use custom fonts for PDF generation (.NET)
Create document with differently sized pages (C#)
Create PDF documents using MONO (C#/MONO/Windows/OSX)
How to use background images for content elements (C#/PDF Kit/FlowLayout)
Add transparent images to PDF document (C#)
Draw round rect borders in PDF documents(C#)
ICC color profiles and and ICC based colors in PDF (C#)
How to use bidirectional and right to left text in PDF (C#)
Create PDF documents from XML templates (C# sample)
How to resize PDF pages and use custom stamps (C#)
Add header and footer to PDF page (.NET sample)
How to use clipping mask for drawing on PDF page
Fill graphics path with gradient brushes in PDF (Shadings)
Apitron PDF Kit and Rasterizer engine settings
Add layers to PDF page (optional content, C# sample)
How to create free text annotation with custom appearance
PDF Content Extraction PDF Navigation
PDF to TIFF conversion
Contact us if you have a PDF related question and we'll cover it in our blog.

2016-10-20

Convert PDF to TIFF using custom rendering settings

Introduction


The task to convert from PDF to TIFF format can sometimes be tricky and quite challenging if you don’t have the right tool for that. Our Apitron PDF Rasterizer for .NET component has been offering this feature for years and now we’ve tweaked it a bit to fit into more demanding workflows. From now on, you’ll be able to control the conversion of the individual page by changing its DPI, rotation, crop and other parameters.

The complete code sample can be downloaded from our github repo.

 

The code


We’ll start with the following PDF document having one rotated page:

Pic. 1 Source PDF file before conversion to TIFF

Pic. 1 Source PDF file before conversion to TIFF


Using the custom page conversion handling code in the handler of BeforeRenderPage event, we’ll correct the rotation for the second page, set the crop and remove the 3d page.

internal class Program
{
    const int DPI = 144;

    private static void Main(string[] args)
    {
        // open and load the file
        using (FileStream fs = new FileStream(@"..\..\map.pdf", FileMode.Open),
             fsOut = File.Create("out.tiff"))
        {
            // this objects represents a PDF document
            using (Document document = new Document(fs))
            {
                // save to tiff using CCIT4 compression, black and white tiff.
                // set the DPI to 144.0 for this sample, so twice more than default PDF dpi setting.
               TiffRenderingSettings tiffRenderingSettings = 
                    new TiffRenderingSettings(TiffCompressionMethod.LZW, DPI, DPI);

                tiffRenderingSettings.RenderMode = RenderMode.HighQuality;
                tiffRenderingSettings.ScaleMode = ScaleMode.PreserveAspectRatio;
                tiffRenderingSettings.Compression = TiffCompressionMethod.LZW;

                // subscribe to before rendering event and adjust rendering settings 
                // for each page as you want 
                tiffRenderingSettings.BeforeRenderPage += TiffRenderingSettings_BeforeRenderPage;

                document.SaveToTiff(fsOut, tiffRenderingSettings);
            }

            System.Diagnostics.Process.Start("out.tiff");
        }
    }
         
    private static void TiffRenderingSettings_BeforeRenderPage(BeforeRenderPageEventArgs args)
    {
        // skip all pages expect first two
        if (args.PageIndex > 1)
        {
            args.IsPageSkipped = true;
            return;
        }

        // force the portrait mode for all pages if needed
        if (args.DesiredHeight < args.DesiredWidth)
        {
            args.RenderingSettings.RotationAngle = RotationAngle.Rotate270;
        }

        // crop from the all sides by cutting out existing margins
        double margin = 40;
        args.CropBox = new Rectangle(margin, margin, args.DesiredWidth - margin, 
              args.DesiredHeight - margin);
    }
}

The complete code sample can be found in our github repo and the resulting image is shown below:

Pic. 2 Cropped first page of the resulting TIFF file (gray area outlines its original dimensions)

Pic. 2 Cropped first page of the resulting TIFF file
(gray area outlines its original dimensions)

Produced tiff file will have only two cropped pages left as we ignored everything starting from the second one, and additionally the second page will be rotated for a better viewing.

Summary


The Apitron PDF Rasterizer for .NET component is a reliable and highly effective solution for those who need the ultimate quality and control over the PDF to image conversion. It can also be used for creation of cross-platform solutions targeting many platforms at once being available (including plain vanilla .NET) for Xamarin, Mono and Windows store based applications.

No comments:

Post a Comment