当前位置: 首页 > news >正文

PDF另存为图片的一个方法

说明

有时需要把PDF的每一页另存为图片。用Devexpress可以很方便的完成这个功能。

窗体上放置一个PdfViewer。

然后循环每一页 for (int i = 1; i <= pdfViewer1.PageCount; i++)

调用 chg_pdf_to_bmp函数获得图片并保存

chg_pdf_to_bmp中调用了PdfViewer的CreateBitmap函数

全部代码

        private string pdf_fn = "";
        private void button1_Click(object sender, EventArgs e)
        {
            pdf_fn = textBox1.Text.Trim();
            if (! System.IO.File.Exists(pdf_fn))
                return;
            button1.Enabled = false;
            string dir = System.IO.Path.GetDirectoryName(pdf_fn) + @"\" + System.IO.Path.GetFileNameWithoutExtension(pdf_fn) + ".files";
            if (!System.IO.Directory.Exists(dir))
                System.IO.Directory.CreateDirectory(dir);
            pdfViewer1.LoadDocument(pdf_fn);
            progressBar1.Value = 0;
            progressBar1.Maximum = pdfViewer1.PageCount;
            Application.DoEvents();
            for (int i = 1; i <= pdfViewer1.PageCount; i++)
            {
                Bitmap bmp = chg_pdf_to_bmp(pdfViewer1,i);

                bmp.Save(dir+@"\"+i.ToString()+".jpg", ImageFormat.Jpeg ); 

                bmp.Dispose();
                progressBar1.Value = progressBar1.Value +1;
                Application.DoEvents();
            }
            MessageBox.Show("保存在"+ dir+"下");
            button1.Enabled = true;

        }

        private string pdf_fn = "";
        private void button1_Click(object sender, EventArgs e)
        {
            pdf_fn = textBox1.Text.Trim();
            if (! System.IO.File.Exists(pdf_fn))
                return;
            button1.Enabled = false;
            string dir = System.IO.Path.GetDirectoryName(pdf_fn) + @"\" + System.IO.Path.GetFileNameWithoutExtension(pdf_fn) + ".files";
            if (!System.IO.Directory.Exists(dir))
                System.IO.Directory.CreateDirectory(dir);
            pdfViewer1.LoadDocument(pdf_fn);
            progressBar1.Value = 0;
            progressBar1.Maximum = pdfViewer1.PageCount;
            Application.DoEvents();
            for (int i = 1; i <= pdfViewer1.PageCount; i++)
            {
                Bitmap bmp = chg_pdf_to_bmp(pdfViewer1,i);

                bmp.Save(dir+@"\"+i.ToString()+".jpg", ImageFormat.Jpeg ); 

                bmp.Dispose();
                progressBar1.Value = progressBar1.Value +1;
                Application.DoEvents();
            }
            MessageBox.Show("保存在"+ dir+"下");
            button1.Enabled = true;

        }

        public Bitmap chg_pdf_to_bmp(DevExpress.XtraPdfViewer.PdfViewer VW_PDF, int i_page_no, int max_w = 210 * 4 * 3)
        {
            DevExpress.Pdf.PdfDocument _pdf_document = null;
            VW_PDF.CurrentPageNumber = i_page_no;
            VW_PDF.Refresh();
            System.Reflection.PropertyInfo fiDocument = VW_PDF.GetType().GetProperty("Document", BindingFlags.Instance | BindingFlags.NonPublic);
            _pdf_document = fiDocument.GetValue(VW_PDF, null) as DevExpress.Pdf.PdfDocument;

            PdfPage pdfpage = _pdf_document.Pages[i_page_no - 1];
            SizeF page_sf = VW_PDF.GetPageSize(i_page_no);
            int pw = (int)(page_sf.Width / pdfpage.UserUnit * 500);
            int ph = (int)(page_sf.Height / pdfpage.UserUnit * 500);
            int bmp_w = pw;                                            //pw * 3 / 2;
            int bmp_h = ph;                                            //ph * 3 / 2; 
            if (bmp_w > max_w)
                bmp_w = max_w;
            Bitmap bmp = VW_PDF.CreateBitmap(i_page_no, bmp_w);
            return bmp;
        }

        public Bitmap chg_pdf_to_bmp(DevExpress.XtraPdfViewer.PdfViewer VW_PDF, int i_page_no, int max_w = 210 * 4 * 3)
        {
            DevExpress.Pdf.PdfDocument _pdf_document = null;
            VW_PDF.CurrentPageNumber = i_page_no;
            VW_PDF.Refresh();
            System.Reflection.PropertyInfo fiDocument = VW_PDF.GetType().GetProperty("Document", BindingFlags.Instance | BindingFlags.NonPublic);
            _pdf_document = fiDocument.GetValue(VW_PDF, null) as DevExpress.Pdf.PdfDocument;

            PdfPage pdfpage = _pdf_document.Pages[i_page_no - 1];
            SizeF page_sf = VW_PDF.GetPageSize(i_page_no);
            int pw = (int)(page_sf.Width / pdfpage.UserUnit * 500);
            int ph = (int)(page_sf.Height / pdfpage.UserUnit * 500);
            int bmp_w = pw;                                            //pw * 3 / 2;
            int bmp_h = ph;                                            //ph * 3 / 2; 
            if (bmp_w > max_w)
                bmp_w = max_w;
            Bitmap bmp = VW_PDF.CreateBitmap(i_page_no, bmp_w);
            return bmp;
        }

相关文章:

  • 深入解析 PCIe 的 iATU(Internal Address Translation Unit)及其工作原理
  • 从二维到三维3D工业相机如何改变机器视觉检测
  • 《open3d+pyqt》第二章——体素采样-open3d自带显示
  • 微信小程序~电器维修系统小程序
  • 【Pico】使用Pico进行无线串流搜索不到电脑
  • DeepSeekApi对接流式输出异步聊天功能:基于Spring Boot和OkHttp的SSE应用实现
  • Python函数参数参数逐步进阶250214
  • 基于Multi-Runtime的云原生多态微服务:解耦基础设施与业务逻辑的革命性实践
  • 【工业安全】-CVE-2022-35555- Tenda W6路由器 命令注入漏洞
  • PHP防伪溯源查询系统小程序
  • Mysql之主从复制
  • 对接 PayPal 支付平台流程详解
  • 单调栈及相关题解
  • Unity3D 可视化脚本框架设计详解
  • 线程池处理异常
  • 应对DeepSeek总是服务器繁忙的解决方法
  • 服务器linux操作系统安全加固
  • 深度求索(DeepSeek)的AI革命:NLP、CV与智能应用的技术跃迁
  • 第一章嵌入式系统概论考点07数字音频与数字视频
  • Python数据可视化 - Matplotlib教程
  • 晋江免费网站建设/梅花seo 快速排名软件
  • 手机客户端网站怎么做/软文代写是什么
  • 软件技术开发合同/网站怎么做优化排名
  • 网站中的横幅怎么做/外链seo服务
  • 做黄色网站的人不犯法吗/厦门seo小谢
  • ps网站轮播图怎么做的/seo快速排名案例