//-------------------------------------------------------------------------------- // Copyright (c) 2001-2008 by PDFTron Systems Inc. All Rights Reserved. //-------------------------------------------------------------------------------- using System; using System.Runtime.InteropServices; using System.Collections; namespace test { class Class1 { public delegate Int32 PDF2ImageCallback(int mode, string msg, IntPtr user_data); // The following three lines import the PDF2Image functions from pdf2image.dll (via PInvoke). [DllImport("pdf2image.dll")] public static extern int PDF2ImageInit(string user_name, string company, string license_key, string res_path); [DllImport("pdf2image.dll")] public static extern int PDF2ImageRun(string command_str, PDF2ImageCallback funct, IntPtr user_data); // Return codes for PDF2ImageRun function. static int PDF2IMAGE_OK = 0; // No errors static int PDF2IMAGE_ERR = 1; // Unspecified error static int PDF2IMAGE_ERR_BADKEY = 2; // Bad license key static int PDF2IMAGE_ERR_DIRCREATE = 3; // Failed to create the output file/directory static int PDF2IMAGE_ERR_READINGPDF = 4; // Failed to read input document static int PDF2IMAGE_ERR_PASSWORD = 5; // The password required to open PDF is incorrect static int PDF2IMAGE_ERR_CONVERT = 6; // A conversion error // You can modify the following lines with your registration information. static string username = "John Doe"; static string company = "My Company"; static string lic_key = "my license key"; // 'mode' identifier passed in PDF2ImageCallback. static int PDF2IMAGE_ERROR = 1; // Show the error message static int PDF2IMAGE_MSG = 2; // Report the message static int PDF2IMAGE_GETPASS = 3; // Get the password static int PDF2IMAGE_OUT_FILENAME = 4; //Get the output filenames // A custom callback that can be used to report errors and other messages. public static Int32 MyCallback(int mode, string msg, IntPtr user_data) { if (mode == PDF2IMAGE_ERROR) { Console.WriteLine("Error: {0}", msg); } else if (mode == PDF2IMAGE_MSG) { Console.Write("{0}", msg); } else if (mode == PDF2IMAGE_GETPASS) { //gl_pass = Console.ReadLine(); // return gl_pass.c_str(); } else if(mode == PDF2IMAGE_OUT_FILENAME) { //check whether the IntPtr is valid if(user_data.ToInt32() != 0) { //cast it to a GCHandle to //obtain the ArrayList GCHandle gch = (GCHandle)user_data; ArrayList list = (ArrayList)gch.Target; //add the newly created file's path list.Add(msg); } } return 0; } static void Main(string[] args) { PDF2ImageInit(username, company, lic_key, null); PDF2ImageCallback mycallback = new PDF2ImageCallback(MyCallback); // Variable used to switch between different code samples. int example = 1; if (example == 1) { // The simplest PDF2Image program: Pass an explicit command-line string. // // This example converts 'tiger.pdf' to 'tiger.svg' // To convert all PDF documents in 'TestFiles' folder simply delete 'tiger.pdf' // from the command string. // // For a detailed explanation of all commands, please refer to PDF2Image SDK User // Manual. Also, PDF2Image Command-Line application is a great tool to experiment // with the available options. PDF2ImageRun("-f png -o TestOutput/test1 TestFiles/tiger.pdf", mycallback, new IntPtr(0)); } else if (example == 2) { // An example of how to use PDF2Image SDK to implement a command-line application // for converting PDF pages to images. String s = ""; foreach (string arg in args) { s += arg + " "; } PDF2ImageRun(s, mycallback, new IntPtr(0)); } else if (example == 3) { // An example of how to dynamically build a command string based on // user settings... // // Please note that this sample does not cover all possible options offered by // PDF2Image. For a detailed explanation of all commands, please refer to // PDF2Image SDK User Manual. string output_folder = "TestOutput/test2"; string open_password = "secret"; string output_format = "png"; // or png, png8, gif, tif, bmp, raw, etc. // Controls for the output name of rasterized/converted images... string filename_prefix = ""; int filename_digits = 4; bool multipage = false; // used if the output format is 'tiff'. bool grayscale = false; // the output image resolution (DPI := Dots Pet Inch). int dpi = 96; // dimensions of the output image in pixels // Used if dpi is 0. int hres = 500, vres=500; // compression_quality is used to control lossy compression // algorithms (e.g. jpeg) int compression_quality = 60; int rotate = 0; // rotates the page 90/180/270 degrees bool antialias = true; bool printmode = true; // -------------------------------------------------- // Given the above settings build a command string. String s = ""; if (output_folder != "") s += "-o " + output_folder + " "; if (open_password != "") s += "-p " + open_password + " "; if (output_format != "") s += "-f " + output_format + " "; if (filename_prefix != "") s += "--prefix " + filename_prefix + " "; if (filename_digits>0) s += "--digits " + filename_digits + " "; if (multipage) s += "--multipage "; if (grayscale) s += "--gray "; if (dpi>0) { s += "--dpi " + dpi + " "; } else { // use explicit image resolution s += "--hres " + hres + " --vres " + vres + " "; } if (compression_quality > 0) { s += "-q " + compression_quality + " "; } if (antialias == false) s += "--nosmooth "; if (printmode) s += "--printmode "; if (rotate == 90) s += "--rotate 90 "; if (rotate == 180) s += "--rotate 180 "; if (rotate == 270) s += "--rotate 270 "; // specify input PDF files and folders... s += "TestFiles/tiger.pdf "; s += "TestFiles/tai962.pdf "; // s += "c:/my_pdf_folder "; etc ... //an array list to store the saved file path(s) ArrayList saved_files=new ArrayList(); //create a GCHandle to allow MyCallback to access //the object saved_files GCHandle gch = GCHandle.Alloc(saved_files); // Execute the command string. PDF2ImageRun(s, mycallback, (IntPtr)gch); gch.Free(); //print the saved file(s) Console.WriteLine("The Saved Files Are:"); for(int i=0; i