pdf reader


import fitz
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QScrollArea, QVBoxLayout, \
    QHBoxLayout, QPushButton, QLineEdit,QFileDialog
from PyQt5.QtGui import QImage, QPixmap

class Window(QWidget):  # 定义类继承自
    def __init__(self):
        super().__init__()  # 调用父类init方法
        self.label = None
        self.page_num = 0
        self.file_path = str(QFileDialog.getOpenFileUrl()[0]).replace("PyQt5.QtCore.QUrl('file:///","").replace("')","")
        print(self.file_path)
        self.doc = fitz.open(self.file_path)
        # self.doc =self.file_path
        self.setWindowTitle("Rogabet PDF reader")
        self.resize(1250, 800)
        self.setup_ui()
        self.generatePDFView()

    def setup_ui(self):
        vLayout = QVBoxLayout(self)
        hLayout = QHBoxLayout(self)
        self.nextpageBtn = QPushButton("下一页")
        self.prevpageBtn = QPushButton("上一页")
        self.pageLineEdit = QLineEdit()
        self.pageLineEdit.setEnabled(False)
        self.pageLineEdit.setMaximumWidth(80)
        self.pageLineEdit.setAlignment(Qt.AlignCenter)
        self.pageLineEdit.setText(str(self.page_num) + "/" + str(self.doc.pageCount))
        hLayout.addStretch(30)
        hLayout.addWidget(self.prevpageBtn)
        hLayout.addWidget(self.pageLineEdit)
        hLayout.addWidget(self.nextpageBtn)
        hLayout.addStretch(30)
        self.scrollarea = QScrollArea(self)
        self.label = QLabel("标签显示文字")
        self.tocDict = {}
        self.scrollarea.setWidget(self.label)
        vLayout.addWidget(self.scrollarea)
        vLayout.addLayout(hLayout)

        self.nextpageBtn.clicked.connect(self.nextpage)
        self.prevpageBtn.clicked.connect(self.prevpage)

    def prevpage(self):
        if self.page_num <= 0:
            self.page_num = self.doc.pageCount
        self.page_num -= 1
        # if self.page_num >= self.doc.pageCount:
        #     self.page_num -= self.doc.pageCount
        self.pageLineEdit.setText(str(self.page_num) + "/" + str(self.doc.pageCount))
        self.updatePdfView()

    def nextpage(self):
        self.page_num += 1
        if self.page_num >= self.doc.pageCount:
            self.page_num -= self.doc.pageCount
        self.pageLineEdit.setText(str(self.page_num) + "/" + str(self.doc.pageCount))
        self.updatePdfView()

    def updatePdfView(self):
        self.scrollarea.verticalScrollBar().setValue(0)
        self.generatePDFView()

    def generatePDFView(self):
        self.doc = fitz.open(self.file_path)
        trans_a = 200
        trans_b = 200
        trans = fitz.Matrix(trans_a / 100, trans_b / 100).prerotate(0)
        pix = self.doc[self.page_num].get_pixmap(matrix=trans)
        fmt = QImage.Format_RGBA8888 if pix.alpha else QImage.Format_RGB888
        pageImage = QImage(pix.samples, pix.width, pix.height, pix.stride, fmt)
        pixmap = QPixmap()
        pixmap.convertFromImage(pageImage)
        self.label.setPixmap(QPixmap(pixmap))
        self.label.resize(pixmap.size())


if __name__ == "__main__":
    import sys

    app = QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

rogabet-note

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值