原文链接:https://www.cnblogs.com/zhaotianff/p/17371809.html
这里主要用到mupdf库
MuPDF: The ultimate library for managing PDF documents
库是由C写的
mupdf有一个.NET的移植版本mupdf-converter
项目地址: GitHub - dwaleke/mupdf-converter: Automatically exported from code.google.com/p/mupdf-converter
mupdf-converter可以将PDF转换成Bitmap,也可以直接转换成WPF使用的BitmapSource。
使用方法如下:
1、下载 mupdf-converter Release
2、引用 MuPDF.dll,将MuPDFLib-x64.dll(MuPDFLib-x86.dll)放到运行路径下。
int width = 0;
int height = 0;
int rotation = 0;
int page = 1;
int dpi = 96;
MuPDFLib.RenderType RenderType = RenderType.RGB;
bool rotateAuto = false;
string file = @"test.pdf";
string password = "";
MuPDFLib.MuPDF pdfDoc = new MuPDFLib.MuPDF(file, password);
pdfDoc.Page = page;
Bitmap bitmap = pdfDoc.GetBitmap(width, height, dpi, dpi, rotation, RenderType,
rotateAuto, false, 0);
var bitmapSource = pdfDoc.GetBitmapSource(width, height, dpi, dpi, rotation, RenderType,rotateAuto, false, 0);
pdfDoc.Dispose();
注意:
mupdf不支持中文路径,如果有中文路径,程序会闪退。因为是P/Invoke,所以是看不到异常信息的。