原创

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

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://shazhenyu.blog.csdn.net/article/details/53286042

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

相关链接:qt显示pdf——poppler-qt问题Qt 生成word、pdf文档Qt使用Poppler-qt5操作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,然后把每一页解析成一个图片。

源码下载地址:

https://download.csdn.net/download/u014597198/10947614

文章最后发布于: 2016-11-22 14:59:36
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 代码科技 设计师: Amelia_0503

分享到微信朋友圈

×

扫一扫,手机浏览