'--------------------------------------------------------------------------------------- ' Copyright (c) 2001-2008 by PDFTron Systems Inc. All Rights Reserved. ' Consult legal.txt regarding legal and license information. '--------------------------------------------------------------------------------------- Imports System Imports System.Drawing Imports PDFTRON Imports PDFTRON.Common Imports PDFTRON.SDF Imports PDFTRON.PDF ' '--------------------------------------------------------------------------------------- ' The following sample illustrates how to convert PDF documents to various raster image ' formats (such as PNG, JPEG, BMP, TIFF), as well as how to convert a PDF page to GDI+ Bitmap ' for further manipulation and/or display in WinForms applications. '--------------------------------------------------------------------------------------- ' Module Module1 ' The main entry point for the application. Sub Main() ' The first step in every application using PDFNet is to initialize the ' library and set the path to common PDF resources. The library is usually ' initialized only once, but calling Initialize() multiple times is also fine. PDFNet.Initialize() PDFNet.SetResourcesPath("../../../../resources") ' Try ' Optional: Set ICC color profiles to fine tune color conversion ' for PDF 'device' color spaces. You can use your own ICC profiles. ' Standard Adobe color profiles can be download from Adobes site: ' http://www.adobe.com/support/downloads/iccprofiles/iccprofiles_win.html ' ' Simply drop all *.icc files in PDFNet resource folder or you specify ' the full pathname. '--- ' PDFNet.SetColorManagement(true) ' PDFNet.SetDefaultDeviceCMYKProfile("USWebCoatedSWOP.icc") ' will search in PDFNet resource folder. ' PDFNet.SetDefaultDeviceRGBProfile("AdobeRGB1998.icc") ' Optional: Set predefined font mappings to override default font ' substitution for documents with missing fonts. For example: '--- ' PDFNet.AddFontSubst("StoneSans-Semibold", "C:/WINDOWS/Fonts/comic.ttf") ' PDFNet.AddFontSubst("StoneSans", "comic.ttf") ' search for 'comic.ttf' in PDFNet resource folder. ' PDFNet.AddFontSubst(PDFNet.CharacterOrdering.e_Identity, "C:/WINDOWS/Fonts/arialuni.ttf") ' PDFNet.AddFontSubst(PDFNet.CharacterOrdering.e_Japan1, "C:/Program Files/Adobe/Acrobat 7.0/Resource/CIDFont/KozMinProVI-Regular.otf") ' PDFNet.AddFontSubst(PDFNet.CharacterOrdering.e_Japan2, "c:/myfonts/KozMinProVI-Regular.otf") ' ' If fonts are in PDFNet resource folder, it is not necessary to specify ' the full path name. For example, '--- ' PDFNet.AddFontSubst(PDFNet.CharacterOrdering.e_Korea1, "AdobeMyungjoStd-Medium.otf") ' PDFNet.AddFontSubst(PDFNet.CharacterOrdering.e_CNS1, "AdobeSongStd-Light.otf") ' PDFNet.AddFontSubst(PDFNet.CharacterOrdering.e_GB1, "AdobeMingStd-Light.otf") ' Catch e As PDFNetException ' Console.WriteLine(e.Message) ' End Try ' Relative path to the folder containing test files. Dim input_path As String = "../../../TestFiles/" Dim output_path As String = "../../../TestFiles/Output/" Dim draw As PDFDraw = New PDFDraw ' PDFDraw class is used to rasterize PDF pages. '-------------------------------------------------------------------------------- ' Example 1) Convert the first PDF page to PNG at 92 DPI. ' A three step tutorial to convert PDF page to an image. Try ' A) Open the PDF document. Dim doc As PDFDoc = New PDFDoc(input_path + "tiger.pdf") ' Initialize the security handler, in case the PDF is encrypted. doc.InitSecurityHandler() ' B) The output resolution is set to 92 DPI. draw.SetDPI(92) ' C) Rasterize the first page in the document and save the result as PNG. Dim pg As Page = doc.GetPage(1) draw.Export(pg, output_path + "tiger_92dpi.png") Console.WriteLine("Example 1: {0}. Done.", output_path + "tiger_92dpi.png") ' Export the same page as TIFF draw.Export(pg, output_path + "tiger_92dpi.tif", "TIFF") Catch e As PDFNetException Console.WriteLine(e.Message) End Try '-------------------------------------------------------------------------------- ' Example 2) Convert the all pages in a given document to JPEG at 72 DPI. Console.WriteLine("Example 2:") Dim hint_set As ObjSet = New ObjSet ' A collection of rendering 'hits'. Try Dim doc As PDFDoc = New PDFDoc(input_path + "newsletter.pdf") ' Initialize the security handler, in case the PDF is encrypted. doc.InitSecurityHandler() draw.SetDPI(72) ' Set the output resolution is to 72 DPI. ' Use optional encoder parameter to specify JPEG quality. Dim encoder_param As SDF.Obj = hint_set.CreateDict() encoder_param.PutNumber("Quality", 80) ' Traverse all pages in the document. Dim itr As PageIterator = doc.GetPageIterator() While itr.HasNext() Dim outname As String = String.Format("{0}newsletter{1:d}.jpg", output_path, itr.GetPageNumber()) Console.WriteLine(outname) draw.Export(itr.Current(), outname, "JPEG", encoder_param) itr.Next() End While Console.WriteLine("Done") Catch e As PDFNetException Console.WriteLine(e.Message) End Try ' Examples(3 - 5) Try ' Common code for remaining samples. Dim tiger_doc As PDFDoc = New PDFDoc(input_path + "tiger.pdf") ' Initialize the security handler, in case the PDF is encrypted. tiger_doc.InitSecurityHandler() Dim page As Page = tiger_doc.GetPage(1) '-------------------------------------------------------------------------------- ' Example 3) Convert the first page to GDI+ Bitmap. Also, rotate the ' page 90 degrees and save the result as TIFF. draw.SetDPI(100) ' Set the output resolution is to 100 DPI. draw.SetRotate(page.Rotate.e_90) ' Rotate all pages 90 degrees clockwise. Dim bmp As System.Drawing.Bitmap = draw.GetBitmap(page) bmp.Save(output_path + "tiger_100dpi_rot90.tif", System.Drawing.Imaging.ImageFormat.Tiff) Console.WriteLine("Example 3: {0}. Done.", output_path + "tiger_100dpi_rot90.tif") draw.SetRotate(page.Rotate.e_0) ' Disable image rotation for remaining samples. '-------------------------------------------------------------------------------- ' Example 4) Convert PDF page to a fixed image size. Also illustrates some ' other features in PDFDraw class such as rotation, image stretching, exporting ' to grayscale, or monochrome. ' Initialize render 'gray_hint' parameter, that is used to control the ' rendering process. In this case we tell the rasterizer to export the image as ' 1 Bit Per Component (BPC) image. Dim mono_hint As Obj = hint_set.CreateDict() mono_hint.PutNumber("BPC", 1) ' SetImageSize can be used instead of SetDPI() to adjust page scaling ' dynamically so that given image fits into a buffer of given dimensions. draw.SetImageSize(1000, 1000) ' Set the output image to be 1000 wide and 1000 pixels tall draw.Export(page, output_path + "tiger_1000x1000.png", "PNG", mono_hint) Console.WriteLine("Example 4: {0}. Done.", output_path + "tiger_1000x1000.png") draw.SetImageSize(200, 400) ' Set the output image to be 200 wide and 300 pixels tall draw.SetRotate(page.Rotate.e_180) ' Rotate all pages 90 degrees clockwise. ' 'gray_hint' tells the rasterizer to export the image as grayscale. Dim gray_hint As Obj = hint_set.CreateDict() gray_hint.PutName("ColorSpace", "Gray") draw.Export(page, output_path + "tiger_200x400_rot180.png", "PNG", gray_hint) Console.WriteLine("Example 4: {0}. Done.", output_path + "tiger_200x400_rot180.png") draw.SetImageSize(400, 200, False) ' The third parameter sets 'preserve-aspect-ratio' to false. draw.SetRotate(page.Rotate.e_0) ' Disable image rotation. draw.Export(page, output_path + "tiger_400x200_stretch.jpg", "JPEG") Console.WriteLine("Example 4: {0}. Done.", output_path + "tiger_400x200_stretch.jpg") '-------------------------------------------------------------------------------- ' Example 5) Zoom into a specific region of the page and rasterize the ' area at 200 DPI and as a thumbnail (i.e. a 50x50 pixel image). page.SetCropBox(New Rect(216, 522, 330, 600)) ' Set the page crop box. ' Select the crop region to be used for drawing. draw.SetPageBox(page.Box.e_crop) draw.SetDPI(900) ' Set the output image resolution to 900 DPI. draw.Export(page, output_path + "tiger_zoom_900dpi.png", "PNG") Console.WriteLine("Example 5: {0}. Done.", output_path + "tiger_zoom_900dpi.png") draw.SetImageSize(50, 50) ' Set the thumbnail to be 50x50 pixel image. draw.Export(page, output_path + "tiger_zoom_50x50.png", "PNG") Console.WriteLine("Example 5: {0}. Done.", output_path + "tiger_zoom_50x50.png") Catch e As PDFNetException Console.WriteLine(e.Message) End Try ' Explicitly clean-up allocated memory draw.Dispose() End Sub End Module