python pdf转图片 poppler_如何使用python-poppler-qt4显示PDF?

I have downloaded and installed python-poppler-qt4 and I am now trying out a simple Qt application to display a PDF page. I've followed what I've been able to get from the web, i.e. convert the PDF to a QImage, then to a QPixMap, but it doesn't work (all I get is a small window with no visible content).

I may have failed at some point (QImage.width() returns the width I have input, QPixMap.width() returns 0).

Here is the code:

#!/usr/bin/env python

import sys

from PyQt4 import QtGui, QtCore

import popplerqt4

class Application(QtGui.QApplication):

def __init__(self):

QtGui.QApplication.__init__(self, sys.argv)

self.main = MainWindow()

self.main.show()

class MainWindow(QtGui.QFrame):

def __init__(self, parent=None):

QtGui.QWidget.__init__(self, parent)

self.layout = QtGui.QVBoxLayout()

self.doc = popplerqt4.Poppler.Document.load('/home/benjamin/test.pdf')

self.page = self.doc.page(1)

# here below i entered almost random dpi, position and size, just to test really

self.image = self.page.renderToImage(150, 150, 0, 0, 210, 297)

self.pixmap = QtGui.QPixmap()

self.pixmap.fromImage(self.image)

self.label = QtGui.QLabel(self)

self.label.setPixmap(self.pixmap)

self.layout.addWidget(self.label)

self.setLayout(self.layout)

if __name__ == "__main__":

application = Application()

sys.exit(application.exec_())

Where does it go wrong here? Thanks.

解决方案

I'm not familiar with python, so this might not apply directly, but QPixmap::fromImage is a static function that returns a QPixmap. So your code should read something like:

self.pixmap = QtGui.QPixmap.fromImage(self.image)

In other words, self.pixmap.fromImage doesn't change self.pixmap, it returns a new pixmap generated from the image you give it as a parameter.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
文件时,如何实现搜索关键词并高亮显示? 您好!感谢您的问题。要实现在Qt中使用poppler读取pdf文件并搜索关键词并高亮显示,您可以使用以下步骤: 1. 首先,使用poppler库打开要读取的pdf文件并获取页面数量: ``` Poppler::Document *pdfDoc = Poppler::Document::load(filePath); int pageCount = pdfDoc->numPages(); ``` 2. 接着,使用poppler库获取每一页PDF文件的Render,从而允许您在其上绘制高亮: ``` Poppler::Page *page = pdfDoc->page(pageNum); Poppler::PageRender *render = new Poppler::PageRender(); render->setPage(page); render->setDpi(dpi, dpi); QImage img = render->renderToImage().convertToFormat(QImage::Format_ARGB32); ``` 其中,`pageNum`为当前页面的页数,`dpi`为分辨率。 3. 在PDF页面上使用QPainter绘制高亮。您可以使用`QPainter::drawRect()`方法绘制矩形框,用所需的颜色调用`QPainter::setBrush()`方法,并使用`QPainter::setFont()`方法定义字体。例如: ``` QPainter painter(&img); painter.setRenderHint(QPainter::Antialiasing); painter.setFont(QFont("Times", 16, QFont::Bold)); painter.setPen(QPen(Qt::red, 2)); painter.setBrush(QColor(255, 255, 0, 128)); painter.drawRect(rectangle); ``` 其中,`rectangle`为要高亮显示的矩形框的坐标。 4. 最后,将渲染后的图像渲染到您的界面控件上。例如: ``` QLabel *pdfLabel = new QLabel; pdfLabel->setPixmap(QPixmap::fromImage(img)); ``` 这样,您就可以实现在Qt中使用poppler库读取pdf文件并搜索关键词并高亮显示了。希望以上步骤可以对您有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值