C#将PDF保存为位图

这里主要用到mupdf库

https://mupdf.com/

库是由C写的

mupdf有一个.NET的移植版本mupdf-converter

项目地址: https://github.com/dwaleke/mupdf-converter

mupdf-converter可以将PDF转换成Bitmap,也可以直接转换成WPF使用的BitmapSource。

 

使用方法如下:

1、下载 mupdf-converter Release

2、引用 MuPDF.dll,将MuPDFLib-x64.dll(MuPDFLib-x86.dll)放到运行路径下。

 1             int width = 0;
 2             int height = 0;
 3             int rotation = 0;
 4             int page = 1;
 5             int dpi = 96;
 6             MuPDFLib.RenderType RenderType = RenderType.RGB;
 7             bool rotateAuto = false;
 8             string file = @"test.pdf";
 9             string password = "";
10 
11             MuPDFLib.MuPDF pdfDoc = new MuPDFLib.MuPDF(file, password);
12 
13             pdfDoc.Page = page;
14             Bitmap bitmap = pdfDoc.GetBitmap(width, height, dpi, dpi, rotation, RenderType,
15                                          rotateAuto, false, 0);
16 
17             var bitmapSource = pdfDoc.GetBitmapSource(width, height, dpi, dpi, rotation, RenderType,rotateAuto, false, 0);
18             pdfDoc.Dispose();  

 

注意:

mupdf不支持中文路径,如果有中文路径,程序会闪退。因为是P/Invoke,所以是看不到异常信息的。

posted @ 2023-05-04 16:58  zhaotianff  阅读(212)  评论(0编辑  收藏  举报