Quantcast
Channel: PDF tips & tricks
Viewing all articles
Browse latest Browse all 125

Convert PDF to image (C# .NET sample)

$
0
0

Welcome, it’s finally out and we proudly present it to you.


Our .NET component Apitron PDF Rasterizer for.NET has been recently released and I’m going to show you how it can be used for PDF processing. 

STEP I

Download component package from company website http://www.apitron.com/Downloads and unzip to desired folder.

STEP II

Except folders for each framework version (it also has a specific build that works with Xamarin.Android – I plan additional post on how to use that) there is a Samples folder in unzipped directory containing several example projects. Go ahead and open ConvertPdfToImage.csproj.

I decided to go with my own file instead of default test PDF file provided with package and downloaded one from adobe website http://acroeng.adobe.com/Test_Files/viewing//3BigPreview.pdf and put in Samples\Documents folder.

The code is very simple but clearly demonstrates the process:

namespace ConvertPDFtoBitmap{
    using System.Diagnostics;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.IO;

    using Apitron.PDF.Rasterizer;
    using Apitron.PDF.Rasterizer.Configuration;

    internalclassProgram
    {
        privatestaticvoid Main(string[] args)
        {
            // open and load the file
            using (FileStreamfs = newFileStream(@"..\..\..\Documents\3BigPreview.pdf", FileMode.Open))
            {
                // this object represents a PDF document
                Document document = newDocument(fs);
               
                // default rendering settings
                RenderingSettings settings = newRenderingSettings();

                // process and save pages one by one
                for (inti = 0; i < document.Pages.Count; i++)
                {
                    Page currentPage = document.Pages[i];

                    // we use original page's width and height for image as well as default rendering settings
                    using (Bitmapbitmap = currentPage.Render((int)currentPage.Width, (int)currentPage.Height,settings))
                    {
                        bitmap.Save(string.Format("{0}.png", i), ImageFormat.Png);
                    }
                }
                // preview first rendered page
                Process.Start("0.png");
            }
        }
    }
}


STEP III

Enjoy the result:


This component can also be used for PDF to TIFF conversion, as well as reading PDF document properties like Author, Title or creation date.As you may see from the image above, advanced  CJK text rendering is nicely supported and many other drawing features that PDF specification describes.Patterns, shadings, images in various colorspaces, transparency..etc.




Viewing all articles
Browse latest Browse all 125

Trending Articles