当前位置: 首页 > 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;}

http://www.lryc.cn/news/534907.html

相关文章:

  • HTML之JavaScript运算符
  • 借助 ListWise 提升推荐系统精排效能:技术、案例与优化策略
  • C++中什么时候用. 什么时候用->
  • 从云原生到 AI 原生,谈谈我经历的网关发展历程和趋势
  • 【Python深入浅出】Python3正则表达式:开启高效字符串处理大门
  • Vue.js Vue CLI 安装与使用
  • 科技的尽头:在有限与永恒的夹缝中寻找文明的真谛
  • 【牛客】动态规划专题一:斐波那契数列
  • java8、9新特性
  • 作业:zuoye
  • redis底层数据结构——链表
  • 问题解决 4S 法
  • SQL-leetcode—1407. 排名靠前的旅行者
  • 机器学习(李宏毅)——Transformer
  • React进阶之React状态管理CRA
  • 攻克AWS认证机器学习工程师(AWS Certified Machine Learning Engineer) - 助理级别认证:我的成功路线图
  • 前端开发环境
  • Web自动化测试—测试用例流程设计
  • HTML全局属性与Meta元信息详解:优化网页的灵魂
  • day001 折半查找/二分查找
  • Linux 资源监控:优化与跟踪系统性能
  • java安全中的类加载
  • Node.js调用DeepSeek Api 实现本地智能聊天的简单应用
  • 分布式服务框架 如何设计一个更合理的协议
  • Unity使用iTextSharp导出PDF-02基础结构及设置中文字体
  • Kafka因文件句柄数过多导致挂掉的排查与解决
  • 【LeetCode Hot100 多维动态规划】最小路径和、最长回文子串、最长公共子序列、编辑距离
  • PRC框架-Dubbo
  • 智能检测摄像头模块在客流统计中的应用
  • [LLM面试题] 指示微调(Prompt-tuning)与 Prefix-tuning区别