/// <summary> /// Free memory consumed by graphics resources /// </summary> public override void Dispose() { if (FPDFFile != null) { FPDFFile.Dispose(); FPDFFile = null; } base.Dispose(); }
//选择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); }
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> 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 void CloseFile() { //this.FileName = string.Empty; this.CurrentPage = 0; this.PageScale = 1; this.HScroll = false; this.vScrollBar1.Value = 0; this.vScrollBar1.Maximum = this.panelContent.Height; if (pdfFile != null) { pdfFile.Dispose(); pdfFile = null; } foreach (var page in this.Pages) { if (page.PageControl != null) { this.panelContent.Controls.Remove(page.PageControl); page.PageControl.Dispose(); page.Dispose(); } } this.Pages.Clear(); }
private int printShow(string url) { int isOK = 0; PDFFile file = PDFFile.Open(url); PrinterSettings settings = new PrinterSettings(); System.Drawing.Printing.PrintDocument pd = new System.Drawing.Printing.PrintDocument(); settings.PrinterName = "\\\\192.168.2.93\\Brother DCP-7030 Printer"; settings.PrintToFile = false; //设置纸张大小(可以不设置,取默认设置)3.90 in, 8.65 in //PaperSize ps = new PaperSize("test", 4, 9); //ps.RawKind = 9; //如果是自定义纸张,就要大于118,(A4值为9,详细纸张类型与值的对照请看http://msdn.microsoft.com/zh-tw/library/system.drawing.printing.papersize.rawkind(v=vs.85).aspx) O2S.Components.PDFRender4NET.Printing.PDFPrintSettings pdfPrintSettings = new O2S.Components.PDFRender4NET.Printing.PDFPrintSettings(settings); //pdfPrintSettings.PaperSize = ps; pdfPrintSettings.PageScaling = O2S.Components.PDFRender4NET.Printing.PageScaling.FitToPrinterMarginsProportional; pdfPrintSettings.PrinterSettings.Copies = 1; try { file.Print(pdfPrintSettings); isOK = 1; } catch (Exception) { isOK = -1; throw; } finally { file.Dispose(); } return(isOK); }
/// <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 string SilentPrint(string Url, string PostData, string cookieStr, string printerParams, string charset) { string fileName = null; try { Stream stream = HttpUtils.SendRequest(Url, PostData, charset, cookieStr); //创建临时目录 string temp = System.Environment.GetEnvironmentVariable("TEMP"); fileName = Guid.NewGuid().ToString(); fileName = temp + "\\" + fileName + ".pdf"; saveFile(stream, fileName); //读取PDF文件 if (PrintEngine.Equals("exe")) { string exePath = Assembly.GetExecutingAssembly().Location; string exeFile = new FileInfo(exePath).DirectoryName + "\\FR.exe"; // MessageBox.Show(exeFile); Process p = new Process(); ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.CreateNoWindow = true; startInfo.WindowStyle = ProcessWindowStyle.Hidden; startInfo.UseShellExecute = true; startInfo.FileName = exeFile; //startInfo.Verb = "FR.exe"; string cmd = "/p \"" + fileName + "\" \"" + GetPrinter() + "\""; startInfo.Arguments = cmd; p.StartInfo = startInfo; p.Start(); p.WaitForExit(); } else { PDFFile pdf = PDFFile.Open(fileName); // Create a default printer settings to print on the default printer. PrinterSettings settings = new PrinterSettings(); PDFPrintSettings pdfPrintSettings = new PDFPrintSettings(settings); pdfPrintSettings.PageScaling = PageScaling.FitToPrinterMargins; pdf.Print(pdfPrintSettings); pdf.Dispose(); } } catch (Exception ex) { return("{status:'error',msg:'" + ex.Message + "'}"); } finally { try { if (fileName != null) { new FileInfo(fileName).Delete(); } } catch (Exception ee) { } } return("{status:'success'}"); }
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); }
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(); } }
/*打印pdf*/ // <param name="url">要打印的PDF路径</param> private void printPDF(string url) { //打开pdf文件 PDFFile file = PDFFile.Open(url); PrinterSettings settings = new PrinterSettings(); System.Drawing.Printing.PrintDocument pd = new System.Drawing.Printing.PrintDocument(); //设置打印机的名称 settings.PrinterName = "HP Officejet Pro X551dw Printer PCL 6 (网络)"; settings.PrintToFile = false; //设置打印的颜色为彩色 if (PrintColor.Equals("1")) { settings.DefaultPageSettings.Color = true; } else { settings.DefaultPageSettings.Color = false; } //设置打印的单双面 if (PrintType.Equals("0")) { settings.Duplex = Duplex.Simplex; } else { //双面 settings.Duplex = Duplex.Vertical; } //设置纸张大小(可以不设置取,取默认设置)3.90 in, 8.65 in // PaperSize ps = new PaperSize("Your Paper Name", config.Width, config.Height); // ps.RawKind = 150; //如果是自定义纸张,就要大于118,(A4值为9,详细纸张类型与值的对照请看http://msdn.microsoft.com/zh-tw/library/system.drawing.printing.papersize.rawkind(v=vs.85).aspx) O2S.Components.PDFRender4NET.Printing.PDFPrintSettings pdfPrintSettings = new O2S.Components.PDFRender4NET.Printing.PDFPrintSettings(settings); //设置打印纸张的大小 //pdfPrintSettings.PaperSize = ps; pdfPrintSettings.PageScaling = O2S.Components.PDFRender4NET.Printing.PageScaling.FitToPrinterMarginsProportional; //设置打印份数 pdfPrintSettings.PrinterSettings.Copies = 1; //打印pdf file.Print(pdfPrintSettings); //关闭文件 file.Dispose(); }
public bool OpenPdfFile(Stream stream) { //if (this.FileName == fileName) //{ // return true; //} CloseFile(); pdfFile = PDFFile.Open(stream); if (pdfFile == null) { return(false); } if (pdfFile.PageCount == 0) { pdfFile.Dispose(); pdfFile = null; return(false); } //this.FileName = fileName; double s = 96.0 / 72.0; for (int i = 0; i < pdfFile.PageCount; i++) { PDFSize size = pdfFile.GetPageSize(i); PdfPageInfo page = new PdfPageInfo(i, (int)(size.Width * s), (int)(size.Height * s)); this.Pages.Add(page); } double scale = (double)this.Height / (double)this.Width; double pdfScale = this.Pages[0].PdfHeight / this.Pages[0].PdfWidth; if (scale < pdfScale) { SetScale((double)this.Height / this.Pages[0].PdfHeight); } else { SetScale((double)this.Width / this.Pages[0].PdfWidth); } MoveToPage(0); return(true); }
/// <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 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());; }
/// <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(); }
private bool pdf(String filename) { PDFFile file = PDFFile.Open(filename); try { file.Print(this.PrinterSettings()); } catch { return(false); } finally { file.Dispose(); } 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> 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); }
void PrintPDF(string pdfPath, int copies) { PDFFile file = PDFFile.Open(pdfPath); PrinterSettings setting = new PrinterSettings(); PrintDocument pd = new PrintDocument(); setting.PrinterName = "FUJI XEROX DocuCentre S2110"; setting.Duplex = Duplex.Vertical; setting.PrintToFile = false; setting.Copies = (short)copies; setting.Collate = true; PDFPrintSettings pdfps = new PDFPrintSettings(setting); try { file.Print(pdfps); } finally { file.Dispose(); } }
private void PrintButton_Click(object sender, RoutedEventArgs e) { // Load the PDF file. PDFFile file = PDFFile.Open(this.pdfViewer.Source); file.SerialNumber = "PDFVW-6ATTA-DK6XD-A2XTO-AIQUM-3ECYE"; // Create a default printer settings to print on the default printer. PrinterSettings settings = new PrinterSettings(); PDFPrintSettings pdfPrintSettings = new PDFPrintSettings(settings); pdfPrintSettings.PageScaling = PageScaling.FitToPrinterMargins; // Create the print dialog object and set options PrintDialog pDialog = new PrintDialog(); pDialog.PageRangeSelection = PageRangeSelection.AllPages; pDialog.UserPageRangeEnabled = true; // Display the dialog. This returns true if the user presses the Print button. Nullable <Boolean> print = pDialog.ShowDialog(); if (print == true) { // Get the settings from the PrintDialog and manually set each one. // If there are more option that need to be set the we need to identify them and set them manually. pdfPrintSettings.PrinterSettings.PrinterName = pDialog.PrintQueue.FullName; pdfPrintSettings.PrinterSettings.Copies = (short)pDialog.PrintTicket.CopyCount; pdfPrintSettings.PrinterSettings.FromPage = pDialog.PageRange.PageFrom; pdfPrintSettings.PrinterSettings.ToPage = pDialog.PageRange.PageTo; // Print the PDF file. file.Print(pdfPrintSettings); } file.Dispose(); }
///// <summary> ///// 将字符串发送到打印机方法 ///// </summary> ///// <param name="szPrinterName">打印机名称</param> ///// <param name="szString">打印的字符串</param> ///// <returns></returns> //public static bool SendStringToPrinter(string szPrinterName, string szString) //{ // bool flag = false; // try // { // IntPtr pBytes; // Int32 dwCount; // // 获取字符串长度 // dwCount = szString.Length; // // 将字符串复制到非托管 COM 任务分配的内存非托管内存块,并转换为 ANSI 文本 // pBytes = Marshal.StringToCoTaskMemAnsi(szString); // // 将已转换的 ANSI 字符串发送到打印机 // flag = SendBytesToPrinter(szPrinterName, pBytes, dwCount); // // 释放先前分配的非托管内存 // Marshal.FreeCoTaskMem(pBytes); // } // catch (Win32Exception ex) // { // Helper.WriteLog(ex.Message); // flag = false; // } // return flag; //} /// <summary> /// PDFRender4Net打印pdf /// </summary> /// <param name="filePath">pdf文件路径</param> /// <param name="printerName">打印机名称</param> /// <param name="paper">纸张大小</param> /// <param name="isVertical">是否竖打</param> /// <param name="copies">份数</param> public static string UsePDFRender4NetToPrintPdf(string filePath, string printerName, PaperSize paper, bool isVertical, int copies) { string errorStr = ""; if (!File.Exists(filePath)) { errorStr += "打印的文件不存在\r\n"; return(errorStr); } PDFFile file = PDFFile.Open(filePath); PrinterSettings settings = new PrinterSettings(); settings.PrinterName = printerName; settings.PrintToFile = false; settings.DefaultPageSettings.Landscape = !isVertical; PDFPrintSettings pdfPrintSettings = new PDFPrintSettings(settings); pdfPrintSettings.PaperSize = paper; pdfPrintSettings.PageScaling = PageScaling.FitToPrinterMarginsProportional; pdfPrintSettings.PrinterSettings.Copies = (short)copies; try { file.Print(pdfPrintSettings); } catch (Exception ex) { errorStr = ex.Message; Helper.WriteLog(ex.Message); } finally { file.Dispose(); } return(errorStr); }
/// <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="srcPath"></param> /// <param name="destDir"></param> /// <param name="fileGUID"></param> /// <param name="pageIndex">转换的页码,-1表示转换所有页</param> /// <param name="imgQuality"></param> /// <param name="combineImages"></param> /// <returns></returns> public string Convert(string srcPath, string destDir, string fileGUID, Boolean combineImages, int pageIndex = -1, int imgQuality = 0) { string imgPaths = destDir + fileGUID;//Guid.NewGuid().ToString(); string result = string.Empty; if (pageIndex < 0) { if (combineImages) { result = string.Format(@"{0}\{1}.jpg", imgPaths, fileGUID); } else { result = string.Format(@"{0}\0.jpg", imgPaths); } } else { result = string.Format(@"{0}\{1}.jpg", imgPaths, pageIndex); } //如果已经存在,则直接返回 if (File.Exists(result)) { return(result); } if (!Directory.Exists(imgPaths)) { Directory.CreateDirectory(imgPaths); } PDFFile pdf = PDFFile.Open(srcPath); try { int pageCount = pdf.PageCount; //根据页索取 if (pageIndex >= 0 && pageIndex < pageCount) { return(ProcessPage(pdf, destDir, fileGUID, pageIndex, pageCount, imgQuality)); } //获取所有页 else if (pageIndex == -1) { string[] ImgPaths = new string[pdf.PageCount]; //全部取的情况 for (int i = 0; i < pdf.PageCount; i++) { ImgPaths[i] = ProcessPage(pdf, destDir, fileGUID, i, pageCount, imgQuality); } //拼接图片 if (combineImages) { int imgCount = ImgPaths.Count(); if (imgCount > 0) { Bitmap resultImg = new Bitmap(_imageWidth, _imageHeight * imgCount); Graphics resultGraphics = Graphics.FromImage(resultImg); Image tempImage; for (int i = 0; i < ImgPaths.Length; i++) { tempImage = Image.FromFile(ImgPaths[i]); if (i == 0) { resultGraphics.DrawImage(tempImage, 0, 0); } else { resultGraphics.DrawImage(tempImage, 0, _imageHeight * i); } tempImage.Dispose(); } ImageUtility.CompressAsJPG(resultImg, result, imgQuality); resultGraphics.Dispose(); } } return(result); } return(""); } finally { pdf.Dispose(); } }
private void CreatePDF(string hphm, string vin, string business, string carType, string chekItem, string weight, string makeDate, byte[] qrcode) { PdfHelper pdfHelper = new PdfHelper(); Document doc = pdfHelper.CreateDocument(500, 500); MemoryStream memoryStream = new MemoryStream(); PdfWriter writer = PdfWriter.GetInstance(doc, memoryStream); doc.Open(); doc.NewPage(); BaseFont ArialFont = BaseFont.CreateFont("STZHONGS.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); iTextSharp.text.Font BoldFont = new iTextSharp.text.Font(ArialFont, 11, iTextSharp.text.Font.NORMAL, new BaseColor(System.Drawing.Color.Black)); iTextSharp.text.Font HeadFont = new iTextSharp.text.Font(ArialFont, 11, iTextSharp.text.Font.BOLD, new BaseColor(System.Drawing.Color.Red)); PdfPTable tableVIN = pdfHelper.CreateTable(new float[] { 1, 1, 1, 1, 1 }, doc); tableVIN.AddCell(pdfHelper.CreateCell("号牌号码", BoldFont, Element.ALIGN_RIGHT)); tableVIN.AddCell(pdfHelper.CreateCell(hphm, HeadFont, Element.ALIGN_CENTER)); tableVIN.AddCell(pdfHelper.CreateCell("VIN", BoldFont, Element.ALIGN_RIGHT)); tableVIN.AddCell(pdfHelper.CreateCell(vin, HeadFont, Element.ALIGN_CENTER, 2)); doc.Add(tableVIN); PdfPTable tableMsg = pdfHelper.CreateTable(new float[] { 1, 1, 1, 1 }, doc); tableMsg.AddCell(pdfHelper.CreateCell("业务类型", BoldFont, Element.ALIGN_RIGHT)); tableMsg.AddCell(pdfHelper.CreateCell(business, BoldFont, Element.ALIGN_CENTER)); tableMsg.AddCell(pdfHelper.CreateCell("车辆类型", BoldFont, Element.ALIGN_RIGHT)); tableMsg.AddCell(pdfHelper.CreateCell(carType, BoldFont, Element.ALIGN_CENTER)); tableMsg.AddCell(pdfHelper.CreateCell("检测项目", BoldFont, Element.ALIGN_RIGHT)); tableMsg.AddCell(pdfHelper.CreateCell(chekItem, BoldFont, Element.ALIGN_CENTER)); tableMsg.AddCell(pdfHelper.CreateCell("总质量", BoldFont, Element.ALIGN_RIGHT)); tableMsg.AddCell(pdfHelper.CreateCell(weight, BoldFont, Element.ALIGN_CENTER)); tableMsg.AddCell(pdfHelper.CreateCell("环保登记", BoldFont, Element.ALIGN_RIGHT)); tableMsg.AddCell(pdfHelper.CreateCell("", BoldFont, Element.ALIGN_CENTER)); tableMsg.AddCell(pdfHelper.CreateCell("出厂日期", BoldFont, Element.ALIGN_RIGHT)); tableMsg.AddCell(pdfHelper.CreateCell(makeDate, BoldFont, Element.ALIGN_CENTER)); PdfPCell qrCell = null; if (qrcode != null) { qrCell = new PdfPCell(pdfHelper.CreateImage(qrcode, 100f, 100f)); } else { qrCell = pdfHelper.CreateCell("", BoldFont, Element.ALIGN_CENTER); } //qrCell.Rowspan = 3; qrCell.Padding = 2; tableMsg.AddCell(qrCell); //tableMsg.AddCell(pdfHelper.CreateCell(" ", BoldFont, Element.ALIGN_LEFT, false, 3, false)); //tableMsg.AddCell(pdfHelper.CreateCell("Admin", BoldFont, Element.ALIGN_LEFT,false,3,false)); tableMsg.AddCell(pdfHelper.CreateCell("Admin\r\n\r\n" + DateTime.Now.ToString("yyyy年MM月dd日 HH:mm:ss"), BoldFont, Element.ALIGN_LEFT, false, 3, false)); doc.Add(tableMsg); doc.Close(); MemoryStream pdf = new MemoryStream(memoryStream.ToArray()); PDFFile pdfFile = PDFFile.Open(pdf); //Bitmap pageImage = pdfFile.GetPageImage(0,56 * (int)PdfHelper.Definition.Six); PrinterSettings settings = new PrinterSettings(); PrintDocument pd = new PrintDocument(); settings.PrinterName = "Adobe PDF"; settings.PrintToFile = false; //设置纸张大小(可以不设置,取默认设置)3.90 in, 8.65 in //PaperSize ps = new PaperSize("test", 4, 9); // ps.RawKind = 9; //如果是自定义纸张,就要大于118,(A4值为9,详细纸张类型与值的对照请看http://msdn.microsoft.com/zh-tw/library/system.drawing.printing.papersize.rawkind(v=vs.85).aspx) PDFPrintSettings pdfPrintSettings = new PDFPrintSettings(settings); //pdfPrintSettings.PaperSize = ps; pdfPrintSettings.PageScaling = O2S.Components.PDFRender4NET.Printing.PageScaling.MultiplePagesPerSheetProportional; pdfPrintSettings.PrinterSettings.Copies = 1; pdfFile.Print(pdfPrintSettings); // MemoryStream pic = new MemoryStream(); //pageImage.Save(pic, ImageFormat.Png); //pageImage.Save("测试.png", ImageFormat.Png); pdf.Close(); pdf.Dispose(); //pageImage.Dispose(); pdfFile.Dispose(); // picQRCode.BackgroundImage = System.Drawing.Image.FromStream(pic); }