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;

}

cs 复制代码
        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;

}

cs 复制代码
        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;
        }
相关推荐
d3soft1 小时前
deepseek清华大学第二版 如何获取 DeepSeek如何赋能职场应用 PDF文档 电子档(附下载)
ai·pdf·教程·deepseek·赋能职场
hello_simon1 小时前
【Word转PDF】在线Doc/Docx转换为PDF格式 免费在线转换 功能强大好用
职场和发展·pdf·word·学习方法·word转pdf·石墨文档·word转换
anyup_前端梦工厂2 小时前
了解几个 HTML 标签属性,实现优化页面加载性能
前端·html
周杰伦fans2 小时前
C#中修饰符
开发语言·c#
前端御书房2 小时前
前端PDF转图片技术调研实战指南:从踩坑到高可用方案的深度解析
前端·javascript
2301_789169542 小时前
angular中使用animation.css实现翻转展示卡片正反两面效果
前端·css·angular.js
风口上的猪20153 小时前
thingboard告警信息格式美化
java·服务器·前端
avi91113 小时前
[AI相关]Unity的C#代码如何简写
unity·c#·语法糖
程序员黄同学3 小时前
请谈谈 Vue 中的响应式原理,如何实现?
前端·javascript·vue.js
快乐的二进制鸭4 小时前
uniapp实现app的pdf预览
pdf·uni-app