因近期公司项目要做一些pdf文件处理,在比较多个开源库后,感觉mupdf还不错,就选择了它。
https://mupdf.com/downloads/archive/mupdf-1.0-source.tar.gz(源码下载路径)
网上也有很多windows和linux下编译的方法,其实在windows下只需要打开platformwin32 下的vs工程编译生成lib即可。
这里我用的是1.10版本,下面大概的介绍下windows下怎样对mupdf做简单的封装。(vs2017企业版)
1.源码编译编译成功后会生成一下文件
添加现有项 引入
把源码中的头文件拷贝过来
把源码生成的lib文件拷贝到工程中
这里还有一个lib要拷贝,当初我被这个问题坑的不轻,查看分析源码工程废了老大的劲才发现
下面为添加好的工程目录截图 lib放到了一个专门的lib文件夹
其中第三方库头文件只要mujs
新建头文件和c文件 自己可以写代码封装功能
我这里写了两个简单的方法,一个获取pdf页数,一个把pdf转换成图片
c文件
老外写的吗,所以碰到一些中文路径还是有些麻烦的,需要unicode转utf-8编码 所以写了一个方法
包含头文件目录 .include;.thirdpartymujs;%(AdditionalIncludeDirectories)
引入lib .liblibfonts.lib;.liblibglfw.lib;.liblibmupdf.lib;.liblibthirdparty.lib;%(AdditionalDependencies)
因晕源码中 指针没有初始化 这里需要修改设置
至此工程创建完成
添加一个测试工程
最好路径个 dll工程同一级
为了方便调试 我把测试exe工程 和 dll工程生成目录放到了同一个地方
exe工程..Debug_dll
dll 工程 ....Debug_dll
把exe工程设为启动项
运行 进入dll函数 实现pdf操作(docs文件夹下也有demo)
MuPDF是一个用于查看和转换PDF,XPS和电子书文档的开源软件框架。支持多种平台。
其中关于mutool 和mupdf 查看器 这两个平时能用到的较多。源码中也可以编译出来mutool.exe 和mupdf.exe等可执行文件,直接传参调用就能实现多种功能。我这里是把mutool的源码摘取了出来,在dll中可以自己写方法,同样也可以直接调用mutool.exe的方法 ,在mutool.c中 有个main函数接口,这里就是接受指令的地方,你也可以在dll中写个方法调用这个main,这样很多功能就不需要自己再写了,接收了外部参数直接调用。
由于本人也只是个初级码农,研究了几天,现在只是看了mutool中的一小部分,后续还有mupdf查看器等很多东西要慢慢研究,希望各位大神能多多指点。(目前碰到了一个问题,pdf转图片的时候dpi设置,jpg文件dpi一直是96,源码设置dpi也只是把缩放比例调高,但是网上的在线传图片dpi值是变了的,请知道的大神教一下)。
下面是 一些参数指令,可在原工程中模拟传入这些指令追踪源码。
如在源码mutool中可加入这个指令 并吧mutool设成启动项 运行即可
《mupdf mutool功能简介》
版本1.13
*******************************************************************************
1 mutool draw
This is the more customizable tool, but also has a more difficult set of command line options. It is primarily used for rendering a document to image files.
这是更具可定制性的工具,但也有一组更加困难的命令行选项。它主要用于将文档渲染为图像文件。
2 mutool convert
This tool is used for converting documents into other formats, and is easier to use.
该工具用于将文档转换为其他格式,并且更易于使用。
3 mutool trace
This is a debugging tool used for printing a trace of the graphics device calls on a page.
这是一个用于在页面上打印图形设备调用跟踪的调试工具。
还有几种专门用于处理PDF文件的工具:
There are also several tools specifically for working with PDF files:
4 mutool show
A tool for displaying the internal objects in a PDF file.
用于在PDF文件中显示内部对象的工具。
5 mutool extract
Extract images and embedded font resources.
提取图像和嵌入的字体资源。
6 mutool clean
Rewrite PDF file. Used to fix broken files, or to make a PDF file human editable.
重写PDF文件。用于修复损坏的文件,或者使PDF文件可编辑。
7 mutool merge
Merge pages from multiple input files into a new PDF.
将来自多个输入文件的页面合并到一个新的PDF中。
8 mutool create
Create a new PDF file from a text file with graphics commands.
使用图形命令从文本文件创建一个新的PDF文件。
9 mutool portfolio
Manipulate PDF portfolios.
操作PDF组合。
And finally, there is a tool for doing anything you can imagine:
最后,有一种工具可以做你能想象的任何事情:
10 mutool run
A tool for running Javascript programs with access to the MuPDF library functions.
运行Javascript程序并访问MuPDF库函数的工具。
*******************************************************************************
《mupdf pdf查看器命令》
版本1.13
*******************************************************************************
mupdf-gl [options] document [page]
-p password
The password needed to open a password protected PDF file.
打开受密码保护的PDF文件所需的密码。
-r resolution
Set the initial yxdmht level, specified as DPI. The default value is 72.
设置初始缩放级别,指定为DPI。默认值是72。
-W width
Set the page width in points for EPUB layout.
为EPUB布局设置页面宽度。
-H height
Set the page height in points for EPUB layout.
为EPUB布局设置页面高度。
-S size
Set the default font size in points for EPUB layout.
为EPUB布局设置默认字体大小。
-U stylesheet
Specify a CSS file containing user styles to load for EPUB layout.
指定包含要加载EPUB布局的用户样式的CSS文件。
-X
Ignore publisher styles for EPUB layout.
忽略EPUB布局的发布者样式。
Mouse Bindings
鼠标绑定
The middle mouse button (scroll wheel button) pans the document view.
鼠标中键(滚轮按钮)平移文档视图。
The right mouse button selects a region and copies the marked text to the clipboard.
鼠标右键选择一个区域并将标记的文本复制到剪贴板。
Key Bindings
几个命令可以在键之前输入一个数字参数来修改命令。例如,要缩放至150 dpi,请输入“150z”
Several commands can take a number argument entered before the key, to modify the command. For example, to yxdmht to 150 dpi, type "150z".
F1 Display help. 显示帮助
i Display document metadata. 显示文档元数据
o Toggle display of outline.切换大纲的显示
L Toggle display of links.切换显示链接
r Reload document.重新载入文件
q Quit viewer.退出查看器
f Toggle fullscreen.切换全屏
w Shrinkwrap window to fit page.缩小窗口以适合页面
W Fit page width to window. 将页面宽度调整为窗口
H Fit page height to window.将页面高度调整为窗口
Z Fit page size to window.将页面大小适合窗口
[number] z Set yxdmht resolution in DPI.在DPI中设置缩放分辨率
+ Zoom in.放大
- Zoom out. 缩小
[ Rotate counter-clockwise.逆时针旋转
] Rotate clockwise.顺时针旋转
[arrows] or h, j, k, l Pan page in small increments.以小增量平移页面
b Smart move one screenful backward.智能向后移动一个屏幕
[space] Smart move one screenful forward.智能向前移动一个屏幕
[comma] or [page up] Go one page backward.向后翻一页
[period] or [page down] Go one page forward.前进一页
< Go 10 pages backward.向后走10页
> Go 10 pages forward.前进10页
[number] g Go to page number.转到页码
G Go to last page.转到最后一页
m Save current page to navigation history.保存当前页面到导航历史记录
t Go back in navigation history.返回导航历史记录
T Go forward in navigation history.在导航历史中前进
[number] m Save current page in numbered bookmark.将当前页面保存在编号书签中
[number] t Go to numbered bookmark.转到编号书签
/ Start searching forward.开始向前搜索
? Start searching backward.开始向后搜索
n Continue searching forward.继续向前搜索
N Continue searching backward.s继续向后搜索
*******************************************************************************
《mudraw》
版本1.10
*******************************************************************************
draw -o 1.jpg -r 200 E:wj项目dllDebug_dll1.pdf 1
用法:mudraw [选项] 文件 [页号]
-p - 密码
-o - 输出文件名(%d为页码)
-F - 输出格式(从输出文件名中推断出的默认值)
栅格:png,tga,pnm,pam,pbm,pkm,pwg,pcl,ps
矢量:svg,pdf,跟踪
文本:txt,html,stext
-s - 显示额外信息:
m - 显示内存使用情况
t - 显示时间
f - 显示页面功能
5 - 显示渲染图像的md5校验和
-R - 顺时针旋转(默认:0度)
-r - 以dpi为单位的分辨率(默认值:72)
-w - 宽度(以像素为单位)(如果指定了-r,则为最大宽度)
-h - 高度(以像素为单位)(如果指定了-r,则为最大高度)
-f - 准确地确定宽度和/或高度; 忽略原始宽高比
-B - 最大band_height(仅限pgm,ppm,pam,png输出)
-T - 用于渲染的线程数(仅限带状模式)
-W - EPUB布局的页面宽度
-H - EPUB布局的页面高度
-S - EPUB布局的字体大小
-U - 用于EPUB布局的用户样式表的文件名
-c - colorspace(单色,灰色,grayalpha,rgb,rgba,cmyk,cmykalpha)
-G - 应用伽马校正
-I 反转颜色
-A - 抗混叠位数(0到8)
-A - / - 抗混叠位数(0到8)(图形,文本)
-l - 最小描边线宽(以像素为单位)
-D 禁用显示列表
-i 忽略错误
-L 低内存模式(避免缓存,每页之后清除对象)
-P 平行翻译/渲染
-y l 将图层配置列出到stderr
-y - 选择图层配置(按编号)
-y - {, - } *选择图层配置(按编号),并切换列出的条目
页面逗号分隔页码和范围列表
第一次写博客,请大家多多指教。
我做了vs2013 和2017两种版本的 简单例子,等两天上传,谢谢!