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();
        }
Beispiel #2
0
        public static void ConvertPDFtoJPG(string name)
        {
            using (var fs = new FileStream(name, FileMode.OpenOrCreate))
            {
                var _pdfDoc = new PDFWrapper();
                _pdfDoc.LoadPDF(fs);

                for (int i = 0; i < _pdfDoc.PageCount; i++)
                {
                    var fileName = Path.Combine(Path.GetDirectoryName(name).Replace(@"D:\temp\New folder\", ""), $"{Path.GetFileNameWithoutExtension(name)}-page-{i + 1}.jpg");

                    Image img = RenderPage(_pdfDoc, i);
                    var   tn  = img.GetThumbnailImage(150, 200, () => { return(true); }, IntPtr.Zero);
                    using (var ms = new MemoryStream())
                    {
                        tn.Save(ms, ImageFormat.Jpeg);
                        ms.Seek(0, SeekOrigin.Begin);
                        SCS(ms, fileName, i);
                    }
                    Console.WriteLine($"saved: {fileName}");
                }
                Console.WriteLine(name);
                _pdfDoc.Dispose();
            }
        }
Beispiel #3
0
 public bool UnloadPdf()
 {
     if (mypdfDoc != null)
     {
         //MessageBox.Show("UnloadPDF");
         mypdfDoc.Dispose();
         mypdfDoc = null;
         return(true);
     }
     return(false);
 }
Beispiel #4
0
        protected override void Dispose(bool disposing)
        {
            if (!_disposed && disposing)
            {
                if (_document != null)
                {
                    _document.Dispose();
                    _document = null;
                }

                if (_dummyControl != null)
                {
                    _dummyControl.Dispose();
                    _dummyControl = null;
                }

                _disposed = true;
            }
        }
 /// <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 #7
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());
            }
        }
        /// <summary>
        /// Form initialization
        /// </summary>
        /// <param name="fileName">fileName</param>
        private void FormInitial(string fileName)
        {
            rmsUserpEncrypt = true;

            if (outPutStream != null)
            {
                outPutStream.Close();
                outPutStream.Dispose();
            }

            if (!tempFile.Equals(string.Empty))
            {
                if (File.Exists(tempFile + ".tmp"))
                {
                    WipeFile(tempFile + ".tmp", 1);
                }
                tempFile = string.Empty;
            }

            if (pdfDoc != null)
            {
                pdfDoc.Dispose();
                pdfDoc = null;
            }
            pdfDoc = new PDFWrapper();

            if (!LoadFileByStream(fileName))
            {
                return;
            }
            //set zoomRate
            if (zoomRate.Count == 0)
            {
                zoomRate.Add(-3, 0.25);
                zoomRate.Add(-2, 0.5);
                zoomRate.Add(-1, 0.75);
                zoomRate.Add(0, 1);
                zoomRate.Add(1, 1.25);
                zoomRate.Add(2, 1.5);
                zoomRate.Add(3, 2);
                zoomRate.Add(4, 4);
            }

            foreach (string item in ddlZoom.Items)
            {
                if (item.Equals((zoomRate[currentZoom] * 100).ToString() + "%"))
                {
                    ddlZoom.SelectedIndex = currentZoom - (-3);
                }
            }

            currentPage        = pdfDoc.CurrentPage;
            pageCount          = pdfDoc.PageCount;
            tslblPageAll.Text  = "/" + pageCount.ToString();
            tstbPageIndex.Text = currentPage.ToString();

            SetPictureBoxImage();

            this.WindowState = FormWindowState.Maximized;
            this.pnlForm.Focus();
        }
Beispiel #9
0
        private void tsbOpen_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog dlg = new OpenFileDialog();
                dlg.Filter = "Portable Document Format (*.pdf)|*.pdf";
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    if (_pdfDoc != null)
                    {
                        _pdfDoc.Dispose();
                        _pdfDoc = null;
                    }
                    //if (_pdfDoc == null)
                    //{
                    _pdfDoc = new PDFWrapper();
                    _pdfDoc.RenderNotifyFinished += new RenderNotifyFinishedHandler(_pdfDoc_RenderNotifyFinished);
                    _pdfDoc.PDFLoadCompeted      += new PDFLoadCompletedHandler(_pdfDoc_PDFLoadCompeted);
                    _pdfDoc.PDFLoadBegin         += new PDFLoadBeginHandler(_pdfDoc_PDFLoadBegin);
                    //}
                    //xPDFParams.ErrorQuiet =true;
                    //xPDFParams.ErrorFile = "C:\\stderr.log";
                    //}
                    int ts = Environment.TickCount;
                    using (StatusBusy sb = new StatusBusy(Resources.UIStrings.StatusLoadingFile))
                    {
                        if (LoadFile(dlg.FileName, _pdfDoc))
                        {
                            Text = string.Format(Resources.UIStrings.StatusFormCaption, _pdfDoc.Author, _pdfDoc.Title);
                            FillTree();
                            _pdfDoc.CurrentPage = 1;
                            UpdateText();

                            _pdfDoc.FitToWidth(pageViewControl1.Handle);
                            _pdfDoc.RenderPage(pageViewControl1.Handle);

                            Render();

                            PDFPage pg = _pdfDoc.Pages[1];
                            listView2.TileSize = new Size(134, (int)(128 * pg.Height / pg.Width) + 10);
                            listView2.BeginUpdate();
                            listView2.Clear();
                            for (int i = 0; i < _pdfDoc.PageCount; ++i)
                            {
                                listView2.Items.Add((i + 1).ToString());
                            }
                            listView2.EndUpdate();

                            //pg.LoadThumbnail(128, (int)(128 * pg.Height / pg.Width));
                        }
                    }
                }
            }
            catch (System.IO.IOException ex)
            {
                MessageBox.Show(ex.Message, "IOException");
            }
            catch (System.Security.SecurityException ex)
            {
                MessageBox.Show(ex.Message, "SecurityException");
            }
            catch (System.IO.InvalidDataException ex)
            {
                MessageBox.Show(ex.Message, "InvalidDataException");
            }
        }