Beispiel #1
0
        public static void ConvertPDF2Image(string pdfInputPath, string imageOutputPath, string imageName, int startPageNum, int endPageNum, ImageFormat imageFormat, Definition definition)
        {
            PDFWrapper pdfWrapper = new PDFWrapper();

            pdfWrapper.LoadPDF(pdfInputPath);
            if (!System.IO.Directory.Exists(imageOutputPath))
            {
                Directory.CreateDirectory(imageOutputPath);
            }
            // validate pageNum
            if (startPageNum <= 0)
            {
                startPageNum = 1;
            }
            if (endPageNum > pdfWrapper.PageCount)
            {
                endPageNum = pdfWrapper.PageCount;
            }
            if (startPageNum > endPageNum)
            {
                int tempPageNum = startPageNum;
                startPageNum = endPageNum;
                endPageNum   = startPageNum;
            }
            // start to convert each page
            for (int i = startPageNum; i <= endPageNum; i++)
            {
                pdfWrapper.ExportJpg(imageOutputPath + imageName, i, i, 300, 90);//这里可以设置输出图片的页数、大小和图片质量从默认的150--》300
                while (pdfWrapper.IsJpgBusy == true)
                {
                    System.Threading.Thread.Sleep(500);
                }
            }
            pdfWrapper.Dispose();
        }
        /// <summary>
        /// Set image for PictureBox
        /// </summary>
        private void SetPictureBoxImage()
        {
            try
            {
                // 一時フォルダ存在チェック
                if (!System.IO.Directory.Exists(Path.GetTempPath() + @"PDFViewer\"))
                {
                    System.IO.Directory.CreateDirectory(Path.GetTempPath() + @"PDFViewer\");
                }

                string jpgFileName = Path.GetTempPath() + @"PDFViewer\" + "temp" + DateTime.Now.Millisecond;

                //Create current page
                pdfDoc.ExportJpg(jpgFileName, currentPage, currentPage, imageDPI, imageQuality);
                if (pdfDoc.IsJpgBusy)
                {
                    System.Threading.Thread.Sleep(50);
                }

                do
                {
                    System.Threading.Thread.Sleep(100);
                }while (!File.Exists(jpgFileName));

                System.Threading.Thread.Sleep(100);
                Stream stream = new FileStream(jpgFileName, FileMode.Open, FileAccess.Read, FileShare.Read);
                img = new Bitmap(stream);
                img = ResizeImage((Bitmap)img, img.Width, img.Height);

                int   w      = Convert.ToInt32(img.Width * zoomRate[currentZoom]);
                int   h      = Convert.ToInt32(img.Height * zoomRate[currentZoom]);
                Image imgNew = ResizeImage((Bitmap)img, w, h);
                imgNew         = SetCurrentRatationImage(imgNew);
                pbViewer.Image = imgNew;

                stream.Close();
                stream.Dispose();
                System.Threading.Thread.Sleep(100);
                File.Delete(jpgFileName);

                FitPnlViewerSize(imgNew);
                SetPnlViewerLocation();
            }
            catch (Exception ex)
            {
                throw;
                //MessageBox.Show(ex.ToString());
            }
        }
 /// <summary>
 /// pdf分页转图片
 /// </summary>
 /// <param name="attachmentId">附件id</param>
 /// <param name="sourcePath">文件原始路劲</param>
 /// <param name="fileInputPath">文件路劲(临时文件路劲)</param>
 /// <param name="fileSavePath">文件要保存的路劲</param>
 /// <param name="imageFormat">图片格式</param>
 /// <param name="DPI">图片分辨率(数值越大质量越高)</param>
 /// <param name="definition">清晰度(数值越大质量越高)</param>
 /// <returns></returns>
 public bool PdfToImg(int attachmentId, string sourcePath, string fileInputPath, string fileSavePath, ImageFormat imageFormat, int DPI, int definition)
 {
     try
     {
         List <int> list    = new List <int>();
         PDFWrapper wrapper = new PDFWrapper();
         wrapper.LoadPDF(fileInputPath);
         if (!Directory.Exists(fileSavePath))
         {
             Directory.CreateDirectory(fileSavePath);
         }
         var           pageCount = wrapper.PageCount;
         StringBuilder sb        = new StringBuilder();
         if (pageCount > 0)
         {
             sb.Append(string.Format("delete from PreviewFile where AttachmentId={0};", attachmentId));
             for (int num = 1; num <= pageCount; num++)
             {
                 string filename = fileSavePath + "\\" + num.ToString() + "." + imageFormat.ToString();
                 if (File.Exists(filename))
                 {
                     File.Delete(filename);
                 }
                 wrapper.ExportJpg(filename, num, num, (double)DPI, definition);
                 sb.Append(string.Format("insert into PreviewFile(AttachmentId,SavePath,CreateTime)values({0},'{1}','{2}');", attachmentId, fileSavePath.Replace(sourcePath, "").Replace("\\", "/"), DateTime.Now));
                 Thread.Sleep(1000);
             }
             wrapper.Dispose();
             var result = SqlHelper.ExecteNonQueryText(sb.ToString(), null);
             return(result > 0);
         }
         else
         {
             return(true);
         }
     }
     catch (Exception exception)
     {
         throw exception;
     }
 }
        /// <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)
        {
            PDFWrapper pdfWrapper = new PDFWrapper();

            pdfWrapper.LoadPDF(pdfInputPath);

            if (!System.IO.Directory.Exists(imageOutputPath))
            {
                Directory.CreateDirectory(imageOutputPath);
            }

            // validate pageNum
            if (startPageNum <= 0)
            {
                startPageNum = 1;
            }

            if (endPageNum > pdfWrapper.PageCount)
            {
                endPageNum = pdfWrapper.PageCount;
            }

            if (startPageNum > endPageNum)
            {
                int tempPageNum = startPageNum;
                startPageNum = endPageNum;
                endPageNum = startPageNum;
            }

            // start to convert each page
            for (int i = startPageNum; i <= endPageNum; i++)
            {
                pdfWrapper.ExportJpg(imageOutputPath + imageName + i.ToString() + ".jpg", i, i, 150, 90);//这里可以设置输出图片的页数、大小和图片质量
                if (pdfWrapper.IsJpgBusy) { System.Threading.Thread.Sleep(50); }
            }

            pdfWrapper.Dispose();
        }
Beispiel #5
0
        /// <summary>
        /// 调用PDFLibNet生成图片(如果pdf文件有特殊字体,无法转化)
        /// </summary>
        private static void GetImgFromPDFLibNet()
        {
            string      pdfInputPath = AppDomain.CurrentDomain.BaseDirectory + "test.pdf";
            string      imageOutputPath = AppDomain.CurrentDomain.BaseDirectory + @"\Pdf2Img\";
            ImageFormat imageFormat = ImageFormat.Png;
            int         DPI = 200; int definition = 30;

            try
            {
                List <int> list    = new List <int>();
                PDFWrapper wrapper = new PDFWrapper();
                wrapper.LoadPDF(pdfInputPath);
                if (!Directory.Exists(imageOutputPath))
                {
                    Directory.CreateDirectory(imageOutputPath);
                }
                if (list.Count == 0)
                {
                    for (int i = 1; i <= wrapper.PageCount; i++)
                    {
                        list.Add(i);
                    }
                }
                foreach (int num in list)
                {
                    string filename = imageOutputPath + num.ToString() + "." + imageFormat.ToString();

                    wrapper.ExportJpg(filename, num, num, (double)DPI, definition);
                    Thread.Sleep(1000);
                    Console.WriteLine("生成图片" + filename);
                }
                wrapper.Dispose();
            }
            catch (Exception exception)
            {
                Console.WriteLine("异常" + exception.ToString());
            }
        }