首页 > 其他分享 >Qt用Poppler库解析PDF成图片的简单示例

Qt用Poppler库解析PDF成图片的简单示例

时间:2022-10-31 21:04:54浏览次数:90  
标签:pdfPage return Qt 示例 Poppler PWD document poppler


解析PDF这里用的是Poppler库,与之相关的库还有MuPDF库,参考了这个链接:​​https://people.freedesktop.org/~aacid/docs/qt5/​

相关链接:​​qt显示pdf——poppler-qt问题​​、​​Qt 生成word、pdf文档​​、​​Qt使用Poppler-qt5操作PDF文件  ​​

 

效果图:

Qt用Poppler库解析PDF成图片的简单示例_图片

 

pro文件:

############################
# author: zhenyu.sha
############################
QT += widgets

INCLUDEPATH += $$PWD/poppler

HEADERS += \
poppler\poppler-qt5.h
SOURCES += \
main.cpp \

CONFIG(debug, debug|release) {
DESTDIR = $$PWD/bin_debug
}
CONFIG(release, debug|release) {
DESTDIR = $$PWD/bin_release
}

win32: LIBS += -L$$PWD/lib -llibpoppler
win32: LIBS += -L$$PWD/lib -llibpoppler-qt5

INCLUDEPATH += $$PWD/poppler
DEPENDPATH += $$PWD/poppler

 

简单代码:

 

/*****************************
* author: zhenyu.sha
*****************************/
#include <QApplication>
#include <QStandardPaths>
#include <QWidget>
#include "poppler\poppler-qt5.h"

int main(int argc, char*argv[])
{
QApplication app(argc, argv);
QString filename;
filename = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation)
+ "/test.pdf";//桌面上的test文件
Poppler::Document* document = Poppler::Document::load(filename);
if (!document || document->isLocked()) {
delete document;
return 1;
}
if (document == 0) {
return 1;
}
document->setRenderHint(Poppler::Document::TextAntialiasing);

for(int i = 0; i<document->numPages(); i++){
Poppler::Page* pdfPage = document->page(i);
if (pdfPage == 0) {
return 1;
}
int xres = 72.0, yres = 72.0;
QImage image = pdfPage->renderToImage(xres*2, yres*2, 0, 0, pdfPage->pageSizeF().width()*2,
pdfPage->pageSizeF().height()*2);
if (image.isNull()) {
return 1;
}else{
image.save(QStandardPaths::writableLocation(QStandardPaths::DesktopLocation)
+ QString("/pdf-page%1.png").arg(i), "png");
}
delete pdfPage;
}
delete document;
exit(0);

return app.exec();
}

代码的目的就是简单的从桌面上获取一个叫test的pdf,然后把每一页解析成一个图片。


标签:pdfPage,return,Qt,示例,Poppler,PWD,document,poppler
From: https://blog.51cto.com/u_15854865/5811261

相关文章