QT开发中文网

how to Load the PDF into a QPixmap c++

To load a PDF into a QPixmap in C++, you can use the Poppler library (which is already installed if you followed the previous answer). Here’s an example code:

#include <QApplication>
#include <QLabel>
#include <poppler-qt5.h>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    // Load PDF document
    Poppler::Document* doc = Poppler::Document::load("path/to/your/pdf/file.pdf");
    if (!doc || doc->isLocked()) {
        qDebug() << "Error loading PDF";
        return 1;
    }

    // Get first page of the document
    Poppler::Page* page = doc->page(0);
    if (!page) {
        qDebug() << "Error getting page";
        delete doc;
        return 1;
    }

    // Render page to image
    QImage image = page->renderToImage();

    // Convert QImage to QPixmap and display it in a QLabel
    QPixmap pixmap = QPixmap::fromImage(image);
    QLabel label;
    label.setPixmap(pixmap);
    label.show();

    // Clean up resources
    delete page;
    delete doc;

    return app.exec();
}

In this example, we first load a PDF document using Poppler::Document::load() method. Then we get the first page of the document using Poppler::Document::page() method. We render this page to an image using Poppler::Page::renderToImage() method. Finally, we convert this QImage to a QPixmap and display it in a QLabel.

Note that you need to link your project with the Poppler library by adding LIBS += -lpoppler-qt5 to your .pro file.

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:http://qt.0voice.com/?id=2882

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?