/* * Class constructor. * * Initiates the Accusoft.PdfXpressSdk.PdfXpress font and character map path. * * Please note that this program assumes the existance of the "library" folder * with the Font and CMap copied from the PDFXpress DotNet SDK directory located * under: * * [PDFXpress DotNet install directory]\PDFXpress\V7.0\Support * * */ public PDFXpressConverter() { pdfXpress = new Accusoft.PdfXpressSdk.PdfXpress(); string resourcePath = Environment.CurrentDirectory + @"\library\"; System.Console.WriteLine(resourcePath); string fontPath = resourcePath + "Font"; string cmapPath = resourcePath + "CMap"; //init the PDFXPress object pdfXpress.Initialize(fontPath, cmapPath); }
/* * ConvertPDF2Image * * Uses the Accusoft.PdfXpressSdk.PdfXpress to load a PDF file named "PDFFileName". It then parses * through the pages starting at pageStart to pageEnd. Each PDF page parsed is saved * as the format specified (bmp, png, gif, with jpg as the default) with the following naming convention: * * PDFFileName[CurrentPageNumber].format. * * CurrentPageNumber is the page number being converted, with trailing 0s if the number is less * than a 4 digit number. * * Example: * * Page 389 of file "TestFile.pdf" as jpg would become: * * "TestFile0389.jpg". * * * There is error correction to verify that: * * pageStart is not less than 0, or greater than the last page or pageEnd. * pageEnd is not less than 0, less than pageStart or greater than the last page in the PDF document. * * * Parameters: * * PDFFileName: String, name of PDF file to be read (does not verify that the file exists). * format: String, default "jpg", format to save images as * pageStart: System.Int32, default 0, page to start from (with starting page as 0). * pageEnd: System.Int32, default -1, last page to print. -1 means "print to the last page." * * Please note that starting page is 0, so a file with 500 pages the pageEnd would be 499. * * Returns: * * 0 (failure) * 1 (success) * * */ public int ConvertPDF2Image(string PDFFileName, String format = "jpg", System.Int32 pageStart = 0, System.Int32 pageEnd = -1) { try { // load the PDF document System.Int32 index = pdfXpress.Documents.Add(PDFFileName); // initialize the variables we need to write out the files System.String outputFileBase = System.IO.Path.GetFileNameWithoutExtension(PDFFileName); // Please note: If the DIB is being passed to an ImagXpress control // then the ProduceDibSection property of the RenderOptions class should be set = False. RenderOptions renderOpts = new RenderOptions(); renderOpts.ProduceDibSection = false; renderOpts.ResolutionX = 300; renderOpts.ResolutionY = 300; //if they give us -1 as the parameter, do all the pages if (pageEnd == -1) { pageEnd = pdfXpress.Documents[index].PageCount - 1; } /* * Error checking * * These could be converted to exceptions in the future * */ //starting page can't be less than 0, or greater than total pages if (pageStart < 0 || pageStart > pdfXpress.Documents[index].PageCount - 1 || pageStart > pageEnd) { System.Console.WriteLine("Invalid starting page. Must be greater than 0 and less than total page count."); return(0); } //ending page can not be less than the start page or more than the total number of pages if (pageEnd < pageStart || pageEnd > pdfXpress.Documents[index].PageCount - 1) { System.Console.WriteLine("Invalid end page. Must be more than the starting page and not greater than the total number of pages."); return(0); } //allowed image types //convert everything to lower case first System.Drawing.Imaging.ImageFormat imageType; switch (format.ToLower()) { case "bmp": imageType = System.Drawing.Imaging.ImageFormat.Bmp; break; case "png": imageType = System.Drawing.Imaging.ImageFormat.Png; break; case "gif": imageType = System.Drawing.Imaging.ImageFormat.Gif; break; //if it's unrecognized, just do JPEG case "jpg": case "jpeg": default: imageType = System.Drawing.Imaging.ImageFormat.Jpeg; break; } System.Int32 totalPages = pageEnd + 1 - pageStart; //setting up the string format for leading zeros before any number less than 4 digits long String numberFormat = "0000"; //for (pageIndex = 0; pageIndex < pdfXpress.Documents[index].PageCount; pageIndex++) for (int pageIndex = 0; pageStart <= pageEnd; pageStart++) { using (Bitmap bp = pdfXpress.Documents[index].RenderPageToBitmap(pageStart, renderOpts)) { bp.Save(outputFileBase + pageStart.ToString(numberFormat) + "." + format, imageType); } //show current progress Progress(++pageIndex, totalPages); //ending the progress line } System.Console.WriteLine(); //properly dispose of our PDF object if (pdfXpress != null) { pdfXpress.Dispose(); pdfXpress = null; } } catch (System.Exception ex) { System.Console.WriteLine(ex.Message); } //success - return 1 return(1); }