//选择PDF预览文件 private void comboPDFlist_SelectionChangeCommitted(object sender, EventArgs e) { pdfInputPath = comboPDFlist.SelectedValue.ToString(); PDFFile pdfFile = PDFFile.Open(pdfInputPath); Bitmap pageImage = pdfFile.GetPageImage(0, 56 * 1); pictureBox1.Image = pageImage; imgStartPage = 1; imgPageCount = pdfFile.PageCount; pdfFile.Dispose(); labelPage.Text = imgStartPage + "/" + imgPageCount; float px, py; DataRow[] arrRow = dtPos.Select("Path = '" + pdfInputPath + "' and Page = " + imgStartPage); if (arrRow == null || arrRow.Length == 0) { px = Convert.ToSingle(textPx.Text); //这里根据比例来定位 py = Convert.ToSingle(textPy.Text); //这里根据比例来定位 } else { DataRow dr = arrRow[0]; px = Convert.ToSingle(dr["X"].ToString()); py = Convert.ToSingle(dr["Y"].ToString()); } int X = Convert.ToInt32(pictureBox1.Width * px); int Y = Convert.ToInt32(pictureBox1.Height * py); Point pt1 = pictureBox1.Location; pictureBox2.Location = new Point(pt1.X + X - 10, pt1.Y + Y - 10); }
/// <summary> /// 将Pdf转换成图片存放在指定的文件夹下面并且返回转换了多少张图片(插件:O2S.Components.PDFRender4NETd) /// (使用:调用此方法传递参数,最后一个参数为模糊度(参考传递30)) /// </summary> /// <param name="pdfInputPath">源PDF路径</param> /// <param name="imageOutpPutPath">PDF转化成图片之后存放的路径</param> /// <param name="imageFormat">转换的图片格式</param> /// <param name="difinition">传递参数模糊度</param> /// <returns>返回转换了多少张图片信息</returns> public static int PdfConvertImageInfo(string pdfInputPath, string imageOutpPutPath, ImageFormat imageFormat, float difinition) { try { //第一步:判断需要转换的PDF是否存在 if (!File.Exists(pdfInputPath)) { throw new System.Exception("PDF的路径不存在"); } //第二步:判断存放转换后图片的地址是否存在 if (!Directory.Exists(imageOutpPutPath)) { throw new System.Exception("存放图片的路径不存在"); } //第三步:进行转换(使用插件O2S.Components.PDFRender4NET) PDFFile pdfFile = PDFFile.Open(pdfInputPath); int pageCount = pdfFile.PageCount; for (int i = 0; i < pageCount; i++) { Bitmap bitmap = pdfFile.GetPageImage(i, difinition); bitmap.Save(Path.Combine(imageOutpPutPath, i + "." + imageFormat), imageFormat); bitmap.Dispose(); } pdfFile.Dispose(); //第四步:返回PDF转换了多少张图片的个数 return(pageCount); } catch (System.Exception exception) { throw new System.Exception("PDF转换图片出现错误了,错误原因:" + exception.Message); } }
/// <summary> /// 将PDF文档转换为图片的方法 /// </summary> /// <param name="pdfInputPath">PDF文件路径</param> /// <param name="imageName">生成图片的名字</param> /// <param name="startPageNum">从PDF文档的第几页开始转换</param> /// <param name="endPageNum">从PDF文档的第几页开始停止转换</param> /// <param name="imageFormat">设置所需图片格式</param> /// <param name="definition">设置图片的清晰度,数字越大越清晰</param> public static Stream ConvertPDF2Image(Stream stream, string imageName, int startPageNum, int endPageNum, ImageFormat imageFormat, Definition definition = Definition.Ten) { PDFFile pdfFile = PDFFile.Open(stream); // validate pageNum if (startPageNum <= 0) { startPageNum = 1; } if (endPageNum > pdfFile.PageCount) { endPageNum = pdfFile.PageCount; } if (startPageNum > endPageNum) { int tempPageNum = startPageNum; startPageNum = endPageNum; endPageNum = startPageNum; } var ms = new MemoryStream(); // start to convert each page for (int i = startPageNum; i <= endPageNum; i++) { Bitmap pageImage = pdfFile.GetPageImage(i - 1, 40 * (int)definition); pageImage.Save(ms, imageFormat); pageImage.Dispose(); } pdfFile.Dispose(); return(ms); }
/// <summary> /// 将PDF文档转换为图片的方法 /// </summary> /// <param name="pdfInputPath">PDF文件路径</param> /// <param name="imageOutputPath">图片输出路径</param> /// <param name="imageName">生成图片的名字</param> /// <param name="startPageNum">从PDF文档的第几页开始转换</param> /// <param name="endPageNum">从PDF文档的第几页开始停止转换</param> /// <param name="imageFormat">设置所需图片格式</param> /// <param name="definition">设置图片的清晰度,数字越大越清晰</param> public static void ConvertPDF2Image(string pdfInputPath, string imageOutputPath, string imageName, int startPageNum, int endPageNum, ImageFormat imageFormat, Definition definition) { PDFFile pdfFile = PDFFile.Open(pdfInputPath); if (!Directory.Exists(imageOutputPath)) { Directory.CreateDirectory(imageOutputPath); } // validate pageNum if (startPageNum <= 0) { startPageNum = 1; } if (endPageNum > pdfFile.PageCount) { endPageNum = pdfFile.PageCount; } if (startPageNum > endPageNum) { int tempPageNum = startPageNum; startPageNum = endPageNum; endPageNum = startPageNum; } // start to convert each page for (int i = startPageNum; i <= endPageNum; i++) { Bitmap pageImage = pdfFile.GetPageImage(i - 1, 56 * (int)definition); pageImage.Save(imageOutputPath + imageName + i.ToString() + "." + imageFormat.ToString(), imageFormat); pageImage.Dispose(); } pdfFile.Dispose(); }
public bool GetFilePath(int page, UnityAction <byte[]> fileAction) { if (pdfFile == null) { return(false); } Debug.Log(1); Bitmap map; map = pdfFile.GetPageImage(page, picureConfig.definition); Debug.Log(2); if (map == null) { return(false); } Debug.Log(3); map = SetPictureAlpha(map, (int)picureConfig.transparent); Debug.Log(4); byte[] bytes = BitMapToArray(map); Debug.Log(5); if (fileAction != null) { fileAction(bytes); } return(true); }
public static bool ToImage(string pdfInputPath, string imageOutputPath, string imageName, ImageFormat imageFormat) { PDFFile pdfFile = PDFFile.Open(pdfInputPath); Data.pageCount = pdfFile.PageCount; int startPageNum = 1; int endPageNum = Data.pageCount; Console.WriteLine(imageOutputPath); if (!Directory.Exists(Data.sourceImageDirpath)) { Directory.CreateDirectory(Data.sourceImageDirpath); } else { Directory.Delete(Data.sourceImageDirpath, true); Directory.CreateDirectory(Data.sourceImageDirpath); } // validate pageNum if (startPageNum <= 0) { startPageNum = 1; } if (endPageNum > pdfFile.PageCount) { endPageNum = pdfFile.PageCount; } if (startPageNum > endPageNum) { startPageNum = endPageNum; endPageNum = startPageNum; } Data.OriginalImages = new List <Bitmap>(); // start to convert each page for (int i = startPageNum; i <= endPageNum; i++) { float pageWidth = (float)(pdfFile.GetPageSize(i - 1).Width / 72); //float pageHeight = (float)(pdfFile.GetPageSize(i - 1).Height / 72); float resolution = Data.panelWidth / pageWidth; //Bitmap pageImage = pdfFile.GetPageImage(i - 1, 56 * (int)definition); Bitmap pageImage = pdfFile.GetPageImage(i - 1, resolution); //if (!Directory.Exists(imageOutputPath)) // Directory.CreateDirectory(imageOutputPath + "\\images"); pageImage.Save(imageOutputPath + "\\" + imageName + i.ToString() + "." + imageFormat.ToString(), imageFormat); string imgPath = imageOutputPath + "\\" + imageName + i.ToString() + "." + imageFormat.ToString(); Data.OriginalImages.Add(new Bitmap(GetCopyImage(imgPath))); //File.Delete(imgPath); //Console.WriteLine($"page {i} converted, width:{Data.OriginalImages[i - 1].Width} height:{Data.OriginalImages[i - 1].Height}"); } pdfFile.Dispose(); //Data.OriginalImages.Clear(); //File.Delete(imageOutputPath + "\\" + imageName + "1." + imageFormat.ToString()); return(true); }
/// <summary> /// 将PDF文档转换为图片的方法 /// </summary> /// <param name="pdfInputPath">PDF文件路径</param> /// <param name="imageOutputPath">图片输出路径</param> /// <param name="imageName">生成图片的名字</param> /// <param name="startPageNum">从PDF文档的第几页开始转换</param> /// <param name="endPageNum">从PDF文档的第几页开始停止转换</param> /// <param name="imageFormat">设置所需图片格式</param> /// <param name="definition">设置图片的清晰度,数字越大越清晰</param> private void ConvertPDF2ImageO2S(string pdfInputPath) { // Is NULL if (pdfInputPath == null) { m_nConvertStatus = EErrorType.PTJ_FILENAME_EMPTY; } PDFFile pdfFile = PDFFile.Open(pdfInputPath); try { m_nTotalPage = pdfFile.PageCount; PrintLog("ConvertStatus:" + (int)ConvertStatus.START + " " + 0 + " " + m_nTotalPage); // start to convert each page for (int i = 0; i < m_nTotalPage; i++) { PrintLog("ConvertStatus:" + (int)ConvertStatus.ING + " " + (i + 1).ToString() + " " + m_nTotalPage); string strJPGName = m_strOutDir + "\\" + Path.GetFileNameWithoutExtension(pdfInputPath) + "_" + m_nTotalPage + "_" + (i + 1).ToString() + ".jpg"; Bitmap pageImage = pdfFile.GetPageImage(i, 56 * (int)m_dQuality); pageImage.Save(strJPGName, ImageFormat.Jpeg); pageImage.Dispose(); } } catch (Exception ex) { PrintLog(ex.Message.ToString()); m_nConvertStatus = EErrorType.PTJ_EXCEPTION_FAILED; } finally { pdfFile.Dispose(); } }
public static int LeafletPdf2JPG(string pdfPath, string fileID, string taskID) { //Aspose.Pdf.Document pdf = new Aspose.Pdf.Document(pdfPath); curIndex = 1; int highHeightUnit = 0; string root = Path.Combine(OfficeHelper.GetFolderPath(taskID, "Office"), fileID); imgHeight = 0; listBitmaps = new Dictionary <string, Bitmap>(); if (!Directory.Exists(@root)) { Directory.CreateDirectory(@root); } /*O2S.Components.PDFRender4NET.dll方式*/ PDFFile pdfFile = PDFFile.Open(pdfPath); int pageCount = pdfFile.PageCount; List <Bitmap> listImage = new List <Bitmap>(); for (int i = 1; i <= pageCount; i++) { Bitmap bitmap = pdfFile.GetPageImage(i - 1, 56 * (int)Definition.Two); if (bitmap != null) { listImage.Add(bitmap); } } highHeightUnit = MergerImageAndCut(listImage, root, true); /*Aspose.Pdf方式,比较慢*/ //for (int pageCount = 1; pageCount <= pdf.Pages.Count; pageCount++) //{ // string fileName = root + "\\" + pageCount + ".jpg"; // using (FileStream imageStream = new FileStream(fileName, FileMode.Create)) // { // Aspose.Pdf.Devices.Resolution resolution = new Aspose.Pdf.Devices.Resolution(300); // Aspose.Pdf.Devices.PngDevice pngDevice = new Aspose.Pdf.Devices.PngDevice(resolution); // pngDevice.Process(pdf.Pages[pageCount], imageStream); // Image image = Image.FromStream(imageStream); // imageStream.Close(); // if (image != null) // ImgCutSave(image, root, pageCount); //代码截图 // } // if (File.Exists(fileName)) // File.Delete(fileName); //} foreach (KeyValuePair <string, Bitmap> bitmaps in listBitmaps) { Bitmap bitmap = bitmaps.Value; if (bitmap != null) { ImageFormat imageFormat = ImageFormat.Jpeg; bitmap.Save(bitmaps.Key, imageFormat); } } return(highHeightUnit); }
Bitmap GetCachedBmp(int pageIndex) { if (this.Pages[pageIndex].CachedMap == null) { this.Pages[pageIndex].CachedMap = pdfFile.GetPageImage(pageIndex, 300); } return(this.Pages[pageIndex].CachedMap); }
/// <summary> /// 将PDF转换为图片的方法 /// </summary> /// <param name="pdfInputPath">PDF文件路径</param> /// <param name="imageOutputPath">图片输出路径</param> /// <param name="imageName">生成图片的名字</param> /// <param name="startPageNum">从PDF文档的第几页开始转换</param> /// <param name="endPageNum">从PDF文档的第几页开始停止转换</param> /// <param name="imageFormat">设置所需图片格式</param> /// <param name="definition">设置图片的清晰度,数字越大越清晰</param> public string[] PdfToPng(string pdfInputPath, string imageOutputPath, string imageName, int startPageNum, int endPageNum, ImageFormat imageFormat, int definition) { List <string> outFileList = new List <string>(); PDFFile pdfFile = PDFFile.Open(pdfInputPath); if (!Directory.Exists(imageOutputPath)) { Directory.CreateDirectory(imageOutputPath); } // validate pageNum if (startPageNum <= 0) { startPageNum = 1; } if (endPageNum > pdfFile.PageCount) { endPageNum = pdfFile.PageCount; } if (startPageNum > endPageNum) { int tempPageNum = startPageNum; startPageNum = endPageNum; endPageNum = startPageNum; } // start to convert each page if (endPageNum == 1) { Bitmap pageImage = pdfFile.GetPageImage(1 - 1, 56 * (int)definition); pageImage.Save(imageOutputPath + imageName + "." + imageFormat, imageFormat); pageImage.Dispose(); outFileList.Add(imageOutputPath + imageName + "." + imageFormat); } else { for (int i = startPageNum; i <= endPageNum; i++) { Bitmap pageImage = pdfFile.GetPageImage(i - 1, 56 * (int)definition); pageImage.Save(imageOutputPath + imageName + i + "." + imageFormat, imageFormat); pageImage.Dispose(); outFileList.Add(imageOutputPath + imageName + i + "." + imageFormat); } } pdfFile.Dispose(); return(outFileList.ToArray());; }
public List <List <Bitmap> > ConvertPDF2ListImage(string pdfInputPath, string imageOutputPath, string imageName, int startPageNum, int endPageNum, System.Drawing.Imaging.ImageFormat imageFormat, Definition definition) { List <List <Bitmap> > xList = new List <List <Bitmap> >(); PDFFile pdfFile = PDFFile.Open(pdfInputPath); if (!Directory.Exists(imageOutputPath)) { Directory.CreateDirectory(imageOutputPath); } // validate pageNum if (startPageNum <= 0) { startPageNum = 1; } if (endPageNum > pdfFile.PageCount) { endPageNum = pdfFile.PageCount; } if (startPageNum > endPageNum) { int tempPageNum = startPageNum; startPageNum = endPageNum; endPageNum = startPageNum; } // start to convert each page for (int i = startPageNum; i <= endPageNum; i++) { Bitmap pageImage = pdfFile.GetPageImage(i - 1, 56 * (int)definition); var x = _提取表格类.ConvertImage2form(pageImage); //pageImage.Save(imageOutputPath + imageName + i.ToString() + "." + imageFormat.ToString(), imageFormat); xList.Add(x); pageImage.Dispose(); } pdfFile.Dispose(); return(xList); }
/// <summary> /// 将PDF文档转换为图片的方法 /// </summary> /// <param name="pdfInputPath">PDF文件路径</param> /// <param name="imageOutputPath">图片输出路径</param> /// <param name="imageName">生成图片的名字</param> /// <param name="startPageNum">从PDF文档的第几页开始转换</param> /// <param name="endPageNum">从PDF文档的第几页开始停止转换</param> /// <param name="imageFormat">设置所需图片格式</param> /// <param name="definition">设置图片的清晰度,数字越大越清晰</param> public static int ConvertPDF2Image(string pdfInputPath, string imageOutputPath, string imageName, int?startPageNum, int?endPageNum, ImageFormat imageFormat, Definition definition = Definition.Five) { pdfInputPath = FileBasePath == null ? pdfInputPath : Path.Combine(FileBasePath, pdfInputPath); PDFFile pdfFile = PDFFile.Open(pdfInputPath); if (!Directory.Exists(imageOutputPath)) { Directory.CreateDirectory(imageOutputPath); } // validate pageNum if (startPageNum <= 0) { startPageNum = 1; } if (endPageNum > pdfFile.PageCount) { endPageNum = pdfFile.PageCount; } if (startPageNum == null) { startPageNum = 1; } if (endPageNum == null) { endPageNum = pdfFile.PageCount; } if (startPageNum > endPageNum) { int tempPageNum = (int)startPageNum; startPageNum = endPageNum; endPageNum = startPageNum; } // start to convert each page for (int i = (int)startPageNum; i <= endPageNum; i++) { Bitmap pageImage = pdfFile.GetPageImage(i - 1, 56 * (int)definition); pageImage.Save(imageOutputPath + imageName + i.ToString() + "." + imageFormat.ToString(), imageFormat); pageImage.Dispose(); } int pagecount = pdfFile.PageCount; pdfFile.Dispose(); return(pagecount); }
/// <summary> /// 生成缩略图,只生成第一张页面的缩略图 /// </summary> /// <param name="fileBuffer">文件的字节数组</param> /// <param name="fileType">文件类型,目前支持:Word、Excel、PDF</param> public static byte[] ConvertToSnap(byte[] fileBuffer, string fileType) { var snapStream = new MemoryStream(); if (fileType == "xls" || fileType == "xlsx") { var book = new Aspose.Cells.Workbook(new MemoryStream(fileBuffer)); var sheet = book.Worksheets[0]; var imgOptions = new Aspose.Cells.Rendering.ImageOrPrintOptions { OnePagePerSheet = true, VerticalResolution = 400, HorizontalResolution = 300, ImageFormat = System.Drawing.Imaging.ImageFormat.Jpeg }; var sr = new Aspose.Cells.Rendering.SheetRender(sheet, imgOptions); sr.ToImage(0, snapStream); } else if (fileType == "doc" || fileType == "docx") { var doc = new Aspose.Words.Document(new MemoryStream(fileBuffer)); var imgOptions = new Aspose.Words.Saving.ImageSaveOptions(Aspose.Words.SaveFormat.Jpeg) { Resolution = 400, }; doc.Save(snapStream, imgOptions); } else if (fileType == "pdf") { //aspose有性能问题 //var converter = new Aspose.Pdf.Facades.PdfConverter(); //converter.BindPdf(new Aspose.Pdf.Document(new MemoryStream(fileBuffer))); //converter.DoConvert(); //converter.GetNextImage(snapStream, new Aspose.Pdf.PageSize(200, 150), System.Drawing.Imaging.ImageFormat.Jpeg,30); //换用ghostscript //GhostscriptRasterizer rasterizer = new GhostscriptRasterizer(); //rasterizer.Open(new MemoryStream(fileBuffer)); //Image page = rasterizer.GetPage(96, 96, 1); //page.Save(snapStream, ImageFormat.Jpeg); PDFFile pdfFile = PDFFile.Open(new MemoryStream(fileBuffer)); Bitmap bitmap = pdfFile.GetPageImage(0, 56 * (int)Definition.Two); bitmap.Save(snapStream, ImageFormat.Jpeg); } var result = snapStream.ToArray(); snapStream.Close(); return(result); }
private static bool convertPDFToImage(Log4netUtil.LogAppendToForms logAppendToForms, string pdfInputPath, string imageOutputPath, string imageName, int startPageNum, int endPageNum, ImageFormat imageFormat, Definition definition, bool isDebug) { PDFFile pdfFile = PDFFile.Open(pdfInputPath); try { if (!Directory.Exists(imageOutputPath)) { Directory.CreateDirectory(imageOutputPath); } if (startPageNum <= 0) { startPageNum = 1; } if (endPageNum > pdfFile.PageCount) { endPageNum = pdfFile.PageCount; } if (startPageNum > endPageNum) { int tempPageNum = startPageNum; startPageNum = endPageNum; endPageNum = startPageNum; } for (int i = startPageNum; i <= endPageNum; i++) { Bitmap pageImage = pdfFile.GetPageImage(i - 1, 56 * (int)definition); string extension = imageFormat.ToString(); if (string.Equals(extension.ToLower(), "jpeg")) { extension = "jpg"; } pageImage.Save(imageOutputPath + imageName + "." + extension, imageFormat); //pageImage.Save(imageOutputPath + imageName + i.ToString() + "." + extension, imageFormat); pageImage.Dispose(); } return(true); } catch (Exception ex) { string logMessage = string.Format("【随货同行下载任务】 PdfInputPath {0} 转换图片失败!原因,{1}", pdfInputPath, ex.Message); Log4netUtil.Log4NetHelper.LogMessage(logAppendToForms, isDebug, logMessage, @"KJJ\DownloadDataBusiness"); return(false); } finally { pdfFile.Dispose(); } }
/// <summary> /// 转换PDF为指定格式的图片 /// </summary> /// <param name="pdfInputStream"></param> /// <param name="imageFormat"></param> /// <param name="resolution">清晰度,分为10级(数字1-10)</param> /// <returns></returns> public static IList <Stream> ConvertPdfToImage(Stream inputStream, ImageFormat imageFormat, int resolution) { PDFFile pdfFile = PDFFile.Open(inputStream); IList <Stream> result = new List <Stream>(); for (int i = 0; i < pdfFile.PageCount; i++) { Bitmap pageImage = pdfFile.GetPageImage(i, 56 * (int)resolution); result.Add(ConvertBitmap.ToMemoryStream(pageImage, imageFormat)); } pdfFile.Dispose(); return(result); }
/// <summary> /// pdf转bitmap /// </summary> /// <param name="pdfPath">pdf文件路径</param> /// <param name="usedAllPage">全部转换</param> /// <param name="startPageNum">开始页数</param> /// <param name="endPageNum">结束页数</param> /// <param name="definition">转换质量(1~10)</param> /// <returns></returns> private List <Bitmap> PdfConvertBitmap(string pdfPath, bool usedAllPage, int startPageNum, int endPageNum, int definition) { List <Bitmap> bitmaps = new List <Bitmap>(); //打开pdf文件 PDFFile pdfFile = PDFFile.Open(pdfPath); //页数处理 if (usedAllPage) { startPageNum = 1; endPageNum = pdfFile.PageCount; } else { if (startPageNum <= 0) { startPageNum = 1; } if (endPageNum > pdfFile.PageCount) { endPageNum = pdfFile.PageCount; } if (startPageNum > endPageNum) { int tempPageNum = startPageNum; startPageNum = endPageNum; endPageNum = startPageNum; } } //转换质量处理 if (definition < 0) { definition = 1; } if (definition > 10) { definition = 10; } //转换图像 for (int i = startPageNum; i <= endPageNum; i++) { Bitmap pageImage = pdfFile.GetPageImage(i - 1, 56 * definition); bitmaps.Add(pageImage); } pdfFile.Dispose(); return(bitmaps); }
private void ConvertPDF2Image(string pdfInputPath, string imageOutputPath, string imageName, int startPageNum, int endPageNum, ImageFormat imageFormat, Definition definition) { PDFFile pdfFile = PDFFile.Open(pdfInputPath); if (!Directory.Exists(imageOutputPath)) { Directory.CreateDirectory(imageOutputPath); } for (int i = startPageNum; i <= endPageNum; i++) { Bitmap pageImage = pdfFile.GetPageImage(i - 1, 56 * (int)definition); pageImage.Save(imageOutputPath + imageName + "." + imageFormat.ToString(), imageFormat); pageImage.Dispose(); } pdfFile.Dispose(); }
/// <summary> /// 将PDF文档转换为图片的方法 /// </summary> /// <param name="pdfInputPath">PDF文件路径</param> /// <param name="imageOutputPath">图片输出路径</param> /// <param name="imageName">生成图片的名字</param> /// <param name="startPageNum">从PDF文档的第几页开始转换</param> /// <param name="endPageNum">从PDF文档的第几页开始停止转换</param> /// <param name="imageFormat">设置所需图片格式</param> /// <param name="definition">设置图片的清晰度,数字越大越清晰</param> public static void ConvertPDF2Image(string pdfInputPath, string imageOutputPath, string imageName, int startPageNum, int endPageNum, ImageFormat imageFormat, int definition) //Definition definition) { PDFFile pdfFile = PDFFile.Open(pdfInputPath); if (!Directory.Exists(imageOutputPath)) { Directory.CreateDirectory(imageOutputPath); } // validate pageNum if (startPageNum <= 0) { startPageNum = 1; } if (endPageNum > pdfFile.PageCount) { endPageNum = pdfFile.PageCount; } if (startPageNum > endPageNum) { int tempPageNum = startPageNum; startPageNum = endPageNum; endPageNum = startPageNum; } // start to convert each page for (int i = startPageNum; i <= endPageNum; i++) { Bitmap pageImage = pdfFile.GetPageImage(i - 1, 56 * (int)definition); string savefile = imageOutputPath + "\\" + imageName + "_" + i.ToString().PadLeft(3, '0') + "." + imageFormat.ToString(); System.Console.WriteLine("savefile:" + savefile); if (!File.Exists(savefile)) { pageImage.Save(savefile, imageFormat); //pageImage.Save(imageOutputPath + imageName , imageFormat); } pageImage.Dispose(); } pdfFile.Dispose(); }
/// <summary> /// 将PDF文档转换为图片的方法 /// </summary> /// <param name="pdfInputPath">PDF文件路径</param> /// <param name="imageOutputPath">图片输出路径</param> /// <param name="imageName">生成图片的名字</param> /// <param name="startPageNum">从PDF文档的第几页开始转换</param> /// <param name="endPageNum">从PDF文档的第几页开始停止转换</param> /// <param name="imageFormat">设置所需图片格式</param> /// <param name="definition">设置图片的清晰度,数字越大越清晰</param> public static void ConvertPdf2Image(string pdfInputPath, string imageOutputPath, string imageName, int startPageNum, int endPageNum, ImageFormat imageFormat, Definition definition) { if (File.Exists(pdfInputPath)) { PDFFile pdfFile = PDFFile.Open(pdfInputPath); if (!Directory.Exists(imageOutputPath)) { Directory.CreateDirectory(imageOutputPath); } if (startPageNum <= 0) { startPageNum = 1; } if (endPageNum > pdfFile.PageCount) { endPageNum = pdfFile.PageCount; } if (startPageNum > endPageNum) { startPageNum = endPageNum; endPageNum = startPageNum; } for (int i = startPageNum; i <= endPageNum; i++) { Bitmap pageImage = pdfFile.GetPageImage(i - 1, 56 * (int)definition); int canKao = pageImage.Width > pageImage.Height ? pageImage.Height : pageImage.Width; int newHeight = canKao > 1080 ? pageImage.Height / 2 : pageImage.Height; int newWidth = canKao > 1080 ? pageImage.Width / 2 : pageImage.Width; Bitmap newPageImage = new Bitmap(newWidth, newHeight); Graphics g = Graphics.FromImage(newPageImage); g.InterpolationMode = InterpolationMode.Default; g.DrawImage(pageImage, new Rectangle(0, 0, newWidth, newHeight), new Rectangle(0, 0, pageImage.Width, pageImage.Height), GraphicsUnit.Pixel); newPageImage.Save(imageOutputPath + imageName + "." + imageFormat, imageFormat); g.Dispose(); newPageImage.Dispose(); pageImage.Dispose(); } pdfFile.Dispose(); } }
public int ConvertPDF2Image(string pdfInputPath, string imageOutputPath, string imageName, int startPageNum, int endPageNum, ImageFormat imageFormat, Definition definition) { PDFFile pdfFile = PDFFile.Open(pdfInputPath); int fileCount = pdfFile.PageCount; if (!Directory.Exists(imageOutputPath)) { Directory.CreateDirectory(imageOutputPath); } // validate pageNum if (startPageNum <= 0) { startPageNum = 1; } if (endPageNum > pdfFile.PageCount) { endPageNum = pdfFile.PageCount; } if (startPageNum > endPageNum) { int tempPageNum = startPageNum; startPageNum = endPageNum; endPageNum = startPageNum; } // start to convert each page for (int i = startPageNum; i <= pdfFile.PageCount; i++) { Bitmap pageImage = pdfFile.GetPageImage(i - 1, 56 * (int)definition); Bitmap pageImageNew = ZoomAuto(pageImage, 1280, 1024);//KiResizeImage(pageImage, 1280, 1024); pageImageNew.Save(imageOutputPath + "\\" + imageName + i.ToString() + "." + imageFormat.ToString(), imageFormat); pageImageNew.Dispose(); } pdfFile.Dispose(); return(fileCount); }
/// <summary> /// 将PDF文档转换为图片的方法 /// </summary> /// <param name="pdfInputPath">PDF文件路径</param> /// <param name="imageOutputPath">图片输出路径</param> /// <param name="imageName">生成图片的名字</param> /// <param name="startPageNum">从PDF文档的第几页开始转换</param> /// <param name="endPageNum">从PDF文档的第几页开始停止转换</param> /// <param name="imageFormat">设置所需图片格式</param> /// <param name="definition">设置图片的清晰度,数字越大越清晰</param> public DataTable ConvertPDF2Image(string pdfInputPath, int startPageNum, int endPageNum, ImageFormat imageFormat, Definition definition) { // pdfInputPath = "\\\\192.168.23.75\\web\\TEMPPDF\\1.pdf"; PDFFile pdfFile = PDFFile.Open(pdfInputPath); DataTable dtImage = new DataTable("ImageData"); dtImage.Columns.Add("PageCount", typeof(System.Int32)); DataColumn ImagePrint = new DataColumn("ImagePrint", typeof(Image)); dtImage.Columns.Add(ImagePrint); if (startPageNum <= 0) { startPageNum = 1; } if (endPageNum > pdfFile.PageCount) { endPageNum = pdfFile.PageCount; } if (startPageNum > endPageNum) { int tempPageNum = startPageNum; startPageNum = endPageNum; endPageNum = startPageNum; } for (int i = startPageNum; i <= endPageNum; i++) { Bitmap pageImage = pdfFile.GetPageImage(i - 1, 56 * (int)definition); MemoryStream ms = new MemoryStream(); pageImage.Save(ms, imageFormat); Image datatableImage = Image.FromStream(ms); DataRow newRow = dtImage.NewRow(); newRow["PageCount"] = i; newRow["ImagePrint"] = datatableImage; dtImage.Rows.Add(newRow); pageImage.Dispose(); } pdfFile.Dispose(); return(dtImage); }
private string ProcessPage(PDFFile pdf, string destDir, string guid, int pageIndex, int pageCount, int imgQuality) { string imgPaths = destDir + guid;//Guid.NewGuid().ToString(); if (!Directory.Exists(imgPaths)) { Directory.CreateDirectory(imgPaths); } if (imgQuality == 0) { imgQuality = 100; } Bitmap oriBmp = pdf.GetPageImage(pageIndex, 96); Bitmap bmp = ImageUtility.CutAsBmp(oriBmp, CutBorderWidth, CutTopHeight, oriBmp.Width - 2 * CutBorderWidth, oriBmp.Height - CutTopHeight - CutBottomHeight); string result = string.Format(@"{0}\{1}.jpg", imgPaths, pageIndex); if (bmp.Width >= 700) { _imageHeight = (int)bmp.Height; // / 2; _imageWidth = (int)bmp.Width; // / 2; ImageUtility.ThumbAsJPG(bmp, result, _imageWidth, _imageHeight, imgQuality); //tempImg = bmp.GetThumbnailImage((int)bmp.Width / 2, (int)bmp.Height / 2, new Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero); //tempImg.Save(string.Format(@"{0}\{1}-ori.jpg", imgPaths, i), System.Drawing.Imaging.ImageFormat.Jpeg); } else { _imageHeight = bmp.Height; _imageWidth = bmp.Width; ImageUtility.CompressAsJPG(bmp, result, imgQuality); } return(result); //bmp.Save(string.Format(@"{0}\{1}.jpg",imgPaths,i), System.Drawing.Imaging.ImageFormat.Jpeg); }
/// <summary> /// 将PDF文档转换为图片的方法【官方方法】 /// </summary> /// <param name="pdfInputPath">PDF文件路径</param> /// <param name="imageOutputPath">图片输出路径</param> /// <param name="imageName">生成图片的名字</param> /// <param name="startPageNum">从PDF文档的第几页开始转换</param> /// <param name="endPageNum">从PDF文档的第几页开始停止转换</param> /// <param name="imageFormat">设置所需图片格式</param> /// <param name="resolution">清晰度,分为10级(数字1-10)</param> public static void ConvertPdfToImage(string pdfInputPath, string imageOutputPath, string imageName, int startPageNum, int endPageNum, ImageFormat imageFormat, int resolution) { PDFFile pdfFile = PDFFile.Open(pdfInputPath); if (!Directory.Exists(imageOutputPath)) { Directory.CreateDirectory(imageOutputPath); } // validate pageNum if (startPageNum <= 0) { startPageNum = 1; } if (endPageNum > pdfFile.PageCount) { endPageNum = pdfFile.PageCount; } if (startPageNum > endPageNum) { int tempPageNum = startPageNum; startPageNum = endPageNum; endPageNum = startPageNum; } // start to convert each page for (int i = startPageNum; i <= endPageNum; i++) { Bitmap pageImage = pdfFile.GetPageImage(i - 1, 56 * (int)resolution); pageImage.Save(imageOutputPath + imageName + i.ToString() + "." + imageFormat.ToString(), imageFormat); pageImage.Dispose(); } pdfFile.Dispose(); /* * 官方调用方式 * ConvertPdfToImage("F:\\Events.pdf", "F:\\", "A", 1, 5, ImageFormat.Jpeg, 1); */ }
/// <summary> /// 将PDF文档转换为图片的方法 /// </summary> /// <param name="pdfInputPath">PDF文件路径</param> /// <param name="startPageNum">从PDF文档的第几页开始转换</param> /// <param name="endPageNum">从PDF文档的第几页开始停止转换</param> /// <param name="definition">设置图片的清晰度,数字越大越清晰</param> public static List <Bitmap> Convert(string pdfInputPath, int startPageNum = -1, int endPageNum = -1, Definition definition = Definition.Ten) { using (PDFFile pdfFile = PDFFile.Open(pdfInputPath)) { if (startPageNum <= 1) { startPageNum = 1; } // validate pageNum if (startPageNum <= 0) { startPageNum = 1; } if (endPageNum > pdfFile.PageCount || endPageNum < 1) { endPageNum = pdfFile.PageCount; } if (startPageNum > endPageNum) { int tempPageNum = startPageNum; startPageNum = endPageNum; endPageNum = startPageNum; } List <Bitmap> bmps = new List <Bitmap>(); // start to convert each page for (int i = startPageNum; i <= endPageNum; i++) { Bitmap pageImage = pdfFile.GetPageImage(i - 1, 56 * (int)definition); bmps.Add(pageImage); } return(bmps); } }
/// <summary> /// 将PDF文档转换为图片的方法 /// </summary> /// <param name="pdfInputPath">PDF文件路径</param> /// <param name="imageOutputPath">图片输出完整路径(包括文件名)</param> /// <param name="startPageNum">从PDF文档的第几页开始转换</param> /// <param name="endPageNum">从PDF文档的第几页开始停止转换</param> /// <param name="imageFormat">设置所需图片格式</param> /// <param name="definition">设置图片的清晰度,数字越大越清晰</param> /// <returns></returns> public static void ConvertPdf2Image(string pdfInputPath, string imageOutputPath, string imageName, int startPageNum, int endPageNum, ImageFormat imageFormat, Definition definition, ref string logContent) { try { using (PDFFile pdfFile = PDFFile.Open(pdfInputPath)) { logContent += "成功打开" + pdfInputPath + "文件"; if (startPageNum <= 0) { startPageNum = 1; } if (endPageNum > pdfFile.PageCount) { endPageNum = pdfFile.PageCount; } if (startPageNum > endPageNum) { int tempPageNum = startPageNum; startPageNum = endPageNum; endPageNum = startPageNum; } var bitMap = new Bitmap[endPageNum]; //设置保存路径,如果没有文件夹的话就重新创建一个 imageOutputPath += "\\" + imageName + "\\"; if (!Directory.Exists(imageOutputPath)) { Directory.CreateDirectory(imageOutputPath); logContent += "成功保存" + imageOutputPath + "文件夹"; } for (int i = startPageNum; i <= endPageNum; i++) { Bitmap pageImage = pdfFile.GetPageImage(i - 1, 56 * (int)definition); Bitmap newPageImage = new Bitmap(pageImage.Width / 4, pageImage.Height / 4); Graphics g = Graphics.FromImage(newPageImage); g.InterpolationMode = InterpolationMode.HighQualityBicubic; //重新画图的时候Y轴减去130,高度也减去130 这样水印就看不到了 g.DrawImage(pageImage, new System.Drawing.Rectangle(0, 0, pageImage.Width / 4, pageImage.Height / 4), new System.Drawing.Rectangle(0, 130, pageImage.Width, pageImage.Height - 130), GraphicsUnit.Pixel); bitMap[i - 1] = newPageImage; string saveFilePath = imageOutputPath + i.ToString() + "." + imageFormat.ToString(); bitMap[i - 1].Save(saveFilePath, imageFormat); bitMap[i - 1].Dispose(); g.Dispose(); pageImage.Dispose(); newPageImage.Dispose(); //图片保存的大小 ImageUtil.SetImgSize(saveFilePath, 1180, 1572); } logContent += "成功保存完所有图片"; //把所有的图片保存到文件夹中 // SaveImgToFolder(imageOutputPath, imageName, imageFormat, bitMap); } } catch (Exception ex) { logContent += ex.ToString(); throw ex; } finally { GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers(); } }
/// <summary> /// 将PDF文档转换为图片的方法 /// </summary> /// <param name="pdfInputPath">PDF文件路径</param> /// <param name="imageOutputPath">图片输出路径</param> /// <param name="imageName">生成图片的名字</param> /// <param name="startPageNum">从PDF文档的第几页开始转换</param> /// <param name="endPageNum">从PDF文档的第几页开始停止转换</param> /// <param name="imageFormat">设置所需图片格式</param> /// <param name="definition">设置图片的清晰度,数字越大越清晰</param> public string ConvertPDF2Image(Stream stream, string imageOutputPath, string imageName, int startPageNum, int endPageNum, ImageFormat imageFormat, Definition definition) { string uploadResult; string FileNames = ""; PDFFile pdfFile = PDFFile.Open(stream); //if (!Directory.Exists(imageOutputPath)) //{ // Directory.CreateDirectory(imageOutputPath); //} // validate pageNum if (startPageNum <= 0) { startPageNum = 1; } if (endPageNum > pdfFile.PageCount) { endPageNum = pdfFile.PageCount; } if (startPageNum > endPageNum) { int tempPageNum = startPageNum; startPageNum = endPageNum; endPageNum = startPageNum; } // start to convert each page for (int i = startPageNum; i <= endPageNum; i++) { Bitmap pageImage = pdfFile.GetPageImage(i - 1, 56 * (int)definition); ImgUploadFromBase64Req entity = new ImgUploadFromBase64Req(); entity.Id = 10; entity.Type = (int)ImgType.CartPic; entity.FileName = imageName + i.ToString() + ".png"; entity.Base64Img = GetBase64FromImage(pageImage);; //NetLog.WriteTextLog("生成base64编码", "Base64-" + i + "=" + entity.Base64Img, DateTime.Now); uploadResult = ImgClient.UploadImg(entity); //获取上传后的返回信息 ImgUploadRes uploadRes = XpShop.Common.XpShopJSONHelper.Deserialize <XpShop.Client.Entity.ImgUploadRes>(uploadResult); if (uploadRes.ErrorFlag == 0) { FileNames = FileNames + uploadRes.Img1 + "|"; } else { Response.Write("上传失败,原因:" + uploadRes.ErrorMsg); } //pageImage.Save(imageOutputPath + imageName + i.ToString() + "." + imageFormat.ToString(), imageFormat); pageImage.Dispose(); } Response.Write("上传成功,文件名:" + FileNames); pdfFile.Dispose(); return(FileNames); }
/// <summary> /// 获取生产计划所需图纸文件 /// </summary> /// <returns></returns> public static ResultModel DownLoadPlanNeedFile() { string url = uri + "GetPlanNeedFile?pRouteID=" + StoreInfo.CurrentWorkingTicket.ProcessRouteID + "&productID=" + StoreInfo.CurrentWorkingTicket.ProductID; var result = HttpHelper.GetTByUrl <ResultModel <List <FileInfoEntity> > >(url); if (result.Result) { int errNum = 0; string[] fileType = new string[] { ".jpg", ".png", ".dwg", ".pdf", }; StoreInfo.CurrentPlanFiles.Clear(); foreach (var item in result.Data) { if (fileType.Contains(Path.GetExtension(item.FileName).ToLower())) { if (!File.Exists($"{ConfigInfo.DocDirName}\\{item.FileName}")) { if (!FtpHelper.DownLoad(item)) { errNum++; continue; } } if (Path.GetExtension(item.FileName).ToLower().Equals(".pdf")) { using (FileStream fs = new FileStream(string.Format(@"{0}\{1}", ConfigInfo.DocDirName, item.FileName), FileMode.Open)) { PDFFile pdfFile = PDFFile.Open(fs); for (int i = 0; i < pdfFile.PageCount; i++) { string filePath = $"{ConfigInfo.DocDirName}\\{Path.GetFileNameWithoutExtension(item.FileName)}_{(i + 1)}.png"; if (!File.Exists(filePath)) { Bitmap pageImage = pdfFile.GetPageImage(i, 56 * 10); pageImage.Save(filePath, ImageFormat.Png); } StoreInfo.CurrentPlanFiles.Add(new FileInfoEntity() { DocName = item.DocName, FileName = Path.GetFileName(filePath), FileAddress = filePath, WebAddressType = WebAddressTypeEnum.Local }); } pdfFile.Dispose(); } } else { StoreInfo.CurrentPlanFiles.Add(item); } } } bool isOK = errNum.Equals(0); return(new ResultModel() { Result = isOK, Msg = isOK ? @"下载成功!" : $"{errNum}个文件下载失败!" }); } return(new ResultModel() { Result = result.Result, Msg = result.Msg }); }
public MemoryStream GetPdfImagePageStream(string pdfInputPath, int pageIndex, ImageFormat format, int width = 1600, int height = 2560, int quality = 10) { try { //pdf处理插件 PDFFile pdfFile = PDFFile.Open(pdfInputPath); int total = pdfFile.PageCount; #region 防止异常参数 if (pageIndex < 0) { pageIndex = 0; } if (pageIndex > total) { pageIndex = total - 1; } if (quality < 1) { quality = 1; } if (quality > 10) { quality = 10; } if (width <= 0) { width = 1; } if (height <= 0) { height = 1; } #endregion //pdf转换图片 SizeF pageSize = pdfFile.GetPageSize(pageIndex); Bitmap pageImage = pdfFile.GetPageImage(pageIndex, 56 * quality); MemoryStream ms = new MemoryStream(); pageImage.Save(ms, format); //原图 Image img = Image.FromStream(ms, true); double ratio = (double)width / (double)height; double oRatio = (double)img.Width / (double)img.Height; int sbWidth = 0; int sbHeight = 0; int outX = 0; int outY = 0; if (oRatio < ratio) { sbWidth = (int)(img.Width * ((double)height / (double)(img.Height))); sbHeight = height; outX = (width - sbWidth) / 2; } else { sbHeight = (int)(img.Height * ((double)width / (double)(img.Width))); sbWidth = width; outY = (height - sbHeight) / 2; } //缩放 Image sbImg = new Bitmap(sbWidth, sbHeight); Graphics sbGra = Graphics.FromImage(sbImg); sbGra.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; sbGra.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; sbGra.Clear(Color.White); sbGra.DrawImage(img, new System.Drawing.Rectangle(0, 0, sbWidth, sbHeight), new System.Drawing.Rectangle(0, 0, img.Width, img.Height), System.Drawing.GraphicsUnit.Pixel); //补白 Image outImg = new System.Drawing.Bitmap(width, height); Graphics outGra = System.Drawing.Graphics.FromImage(outImg); outGra.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; outGra.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; outGra.Clear(Color.White); outGra.DrawImage(sbImg, new System.Drawing.Rectangle(outX, outY, sbWidth, sbHeight), new System.Drawing.Rectangle(0, 0, sbWidth, sbHeight), System.Drawing.GraphicsUnit.Pixel); MemoryStream outMs = new MemoryStream(); outImg.Save(outMs, format); sbImg.Dispose(); outImg.Dispose(); img.Dispose(); return(outMs); } catch (Exception ex) { } return(new MemoryStream()); }
/// <summary> /// 绑定PDF用于显示 /// </summary> /// 根据病理号和住院号获得病人的诊断PDF /// PDFLJ1常规报告路径 PDFLJ2 补充报告路径 private void BindPDF(string blh, string zyh) { try { string[] pathinfo = DS_SqlService.GetConfigValueByKey("PathologicPathInfo").Split(','); string ippath = string.Empty; string username = string.Empty; string pwd = string.Empty; if (pathinfo.Length >= 3) { ippath = pathinfo[0].ToString().Trim(); username = pathinfo[1].ToString().Trim(); pwd = pathinfo[2].ToString().Trim(); } DataTable PathDT = new DataTable(); PathDT = DS_SqlHelper.ExecuteDataTable(string.Format("select PDFLJ1,PDFLJ2 from PATHOLOGICREPORT where F_BLH='{0}' and F_BRBH='{1}'", blh, zyh)); string cgportpath = string.Empty; //常规报告路径 string bcportpath = string.Empty; //补充报告路径 if (PathDT != null && PathDT.Rows.Count > 0) { cgportpath = PathDT.Rows[0]["PDFLJ1"].ToString(); bcportpath = PathDT.Rows[0]["PDFLJ2"].ToString(); } if (!Directory.Exists("C:\\PatBLReport")) { Directory.CreateDirectory("C:\\PatBLReport"); } #region 绑定常规报告图片 if (!string.IsNullOrEmpty(cgportpath)) { try { WebClient request = new WebClient(); request.Credentials = new NetworkCredential(username, pwd); byte[] newFileData = request.DownloadData(cgportpath.ToString()); newcgpath = "C:\\PatBLReport\\" + m_App.CurrentPatientInfo.NoOfHisFirstPage + Guid.NewGuid().ToString().Substring(0, 10) + "changgui.pdf"; using (FileStream fs = new FileStream(newcgpath, FileMode.OpenOrCreate)) { fs.Write(newFileData, 0, newFileData.Length); fs.Close(); } PDFFile pdfFile = PDFFile.Open(newcgpath); pageImage = pdfFile.GetPageImage(0, 56 * 10); this.picCG.Image = pageImage; pdfFile.Dispose(); } catch (Exception) { } } #endregion #region 绑定补充报告图片 if (!string.IsNullOrEmpty(bcportpath)) { try { WebClient request1 = new WebClient(); request1.Credentials = new NetworkCredential(username, pwd); byte[] newFileDatabc = request1.DownloadData(bcportpath.ToString()); //声明GUID标识,防止访问时正在使用中 newbcpath = "C:\\PatBLReport\\" + m_App.CurrentPatientInfo.NoOfHisFirstPage + Guid.NewGuid().ToString().Substring(0, 10) + "buchong.pdf"; using (FileStream fs1 = new FileStream(newbcpath, FileMode.OpenOrCreate)) { fs1.Write(newFileDatabc, 0, newFileDatabc.Length); //foreach (Byte b in newFileDatabc) //{ // fs1.WriteByte(b);//报内存溢出估计就是这里循环的在写入字节 //edit by ywk 2013年9月2日 15:20:25 //} //fs1.Write(newFileDatabc, 0, newFileData.Length); fs1.Close(); } PDFFile pdfFileBC = PDFFile.Open(newbcpath); Bitmap pageBCImage = pdfFileBC.GetPageImage(0, 56 * 10); this.picBC.Image = pageBCImage; //this.picBC.Focus(); //pageBCImage.Save("c:\\myBitmap2.bmp"); pdfFileBC.Dispose(); //pageBCImage.Dispose(); } catch (Exception) { } } #endregion } catch (Exception ex) { MyMessageBox.Show(1, ex); //if (this.picBC.Image != null) //{ // this.picBC.Image.Dispose(); this.picBC.Image = null; //} //if (this.picCG.Image != null) //{ // this.picCG.Image.Dispose(); this.picCG.Image = null; //} //DeletePDF(); } }
// convert to image /// <summary> /// /// </summary> /// <param name="srcFilePath"></param> /// <param name="destFileDir"></param> /// <param name="destFileName"></param> /// <param name="startPageNum"></param> /// <param name="endPageNum"></param> /// <param name="imgFormat"></param> /// <param name="imgResolution"></param> /// <param name="imgQuality"></param> /// <returns></returns> private int ConvertPDF2Image(string srcFilePath, string destFileDir, string destFileName, int startPageNum, int endPageNum, ImageFormat imgFormat, int imgResolution, int imgQuality) { // validate path ValidateHelper.Begin().NotNullAndEmpty(srcFilePath).FileExist(srcFilePath).NotNullAndEmpty(destFileDir) .NotNull(imgFormat).InRange(imgResolution, 1, 512).InRange(imgQuality, 1, 100); string imageExtention = "." + imgFormat.ToString(); if (imgFormat == ImageFormat.Jpeg) { imageExtention = ".jpg"; } if (!string.IsNullOrEmpty(destFileName)) { destFileName = destFileName + "_"; } if (!Directory.Exists(destFileDir)) { Directory.CreateDirectory(destFileDir); } // open pdf file using (PDFFile pdfFile = PDFFile.Open(srcFilePath)) { if (startPageNum <= 0) { startPageNum = 1; } if (endPageNum > pdfFile.PageCount || endPageNum <= 0) { endPageNum = pdfFile.PageCount; } if (startPageNum > endPageNum) { int tempPageNum = startPageNum; startPageNum = endPageNum; endPageNum = tempPageNum; } // init encoder parameter EncoderParameter encoderParam = new EncoderParameter(Encoder.Quality, (long)imgQuality); EncoderParameters encoderParams = new EncoderParameters(1); encoderParams.Param[0] = encoderParam; ImageCodecInfo codecinfo = GetEncoderInfo("image/jpeg"); // start to convert each page try { for (int i = startPageNum - 1; i < endPageNum; i++) { using (Bitmap pdfBitmap = pdfFile.GetPageImage(i, (float)imgResolution)) { pdfBitmap.Save(Path.Combine(destFileDir, destFileName + i.ToString() + imageExtention), codecinfo, encoderParams); } } } catch (Exception ex) { throw ex; } return(endPageNum - startPageNum + 1); } }