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;
        }
相关推荐
魔云连洲17 小时前
深入解析:Object.prototype.toString.call() 的工作原理与实战应用
前端·javascript·原型模式
JinSo17 小时前
alien-signals 系列 —— 认识下一代响应式框架
前端·javascript·github
开心不就得了17 小时前
Glup 和 Vite
前端·javascript
szial17 小时前
React 快速入门:菜谱应用实战教程
前端·react.js·前端框架
西洼工作室17 小时前
Vue CLI为何不显示webpack配置
前端·vue.js·webpack
黄智勇18 小时前
xlsx-handlebars 一个用于处理 XLSX 文件 Handlebars 模板的 Rust 库,支持多平台使
前端
mysolisoft18 小时前
Avalonia+ReactiveUI实现记录自动更新
c#·avalonia·reactiveui·sourcegenerator
pc大老19 小时前
PDF文件翻译新方法:轻松多语言转换!
pdf·pdf文件·#pdf设置·pdf转换多国语言
心疼你的一切19 小时前
使用Unity引擎开发Rokid主机应用的模型交互操作
游戏·ui·unity·c#·游戏引擎·交互
brzhang19 小时前
为什么 OpenAI 不让 LLM 生成 UI?深度解析 OpenAI Apps SDK 背后的新一代交互范式
前端·后端·架构