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;
        }
相关推荐
小小小小宇4 小时前
虚拟列表兼容老DOM操作
前端
悦悦子a啊4 小时前
Python之--基本知识
开发语言·前端·python
安全系统学习5 小时前
系统安全之大模型案例分析
前端·安全·web安全·网络安全·xss
涛哥码咖5 小时前
chrome安装AXURE插件后无效
前端·chrome·axure
OEC小胖胖5 小时前
告别 undefined is not a function:TypeScript 前端开发优势与实践指南
前端·javascript·typescript·web
行云&流水6 小时前
Vue3 Lifecycle Hooks
前端·javascript·vue.js
Sally璐璐6 小时前
零基础学HTML和CSS:网页设计入门
前端·css
老虎06276 小时前
JavaWeb(苍穹外卖)--学习笔记04(前端:HTML,CSS,JavaScript)
前端·javascript·css·笔记·学习·html
灿灿121386 小时前
CSS 文字浮雕效果:巧用 text-shadow 实现 3D 立体文字
前端·css
烛阴6 小时前
Babel 完全上手指南:从零开始解锁现代 JavaScript 开发的超能力!
前端·javascript