首页 > 其他分享 >Qt + mupdf 显示PDF,支持翻页

Qt + mupdf 显示PDF,支持翻页

时间:2023-09-18 16:37:48浏览次数:36  
标签:mupdf Qt 翻页 fz num ui listWidget PDF page

使用Qt + mupdf 实现PDF阅读器,支持翻页(上一页、下一页)

 

思路:

PDF阅读器:使用mupdf,将PDF文件拆分成单个page页,将page页转为QImage图像,使用QListWidget来装载所有的QImage,这样Qt就可以显示出所有图像(完整的PDF)。

翻页:根据滚动条,确认当前是哪一页,然后实现【上一页、下一页】功能

 

代码:

// pro文件设置
INCLUDEPATH += mupdf
LIBS += -L$$PWD/lib -llibextract -llibmupdf -llibmuthreads -llibthirdparty

QMAKE_CFLAGS_DEBUG += -MTd
QMAKE_CXXFLAGS_DEBUG += -MTd

 

// 显示PDF文件--PDF阅读器
#include <QListWidget>
#include <QListWidgetItem>
#include <QImage>
#include <QIcon>
#include <QSize>
#include "pdf.h"
#include "fitz.h"


// 对控件  QListWidget 属性进行设置
/**
*    1. 使用Qt Create编写的demo,所以 QListWidget 控件是直接在ui文件拉进入的
*    2. 需要对每个 QListWidgetItem 加10px的间隔,不然翻页计算不准,或者是 px_num+1 也行
*/
ui->listWidget->setViewMode(QListView::IconMode);
ui->listWidget->setIconSize(QSize(1050, 1485));        
ui->listWidget->setSpacing(10);  
ui->listWidget->setResizeMode(QListView::Adjust);
ui->listWidget->setMovement(QListView::Static);


// 显示 PDF
fz_context* ctx =  fz_new_context(NULL, NULL, FZ_STORE_DEFAULT);
fz_register_document_handlers(ctx);    // *:这里报错,就需要设置 MTD 或者 MDD
fz_document* doc =  fz_open_document(ctx, "F:\\1.pdf");

int pageCount = fz_count_pages(ctx, doc);

float zoom = (float)300 / (float)72;
fz_matrix ctm = fz_scale(zoom, zoom);

for(int i=0; i<pageCount; i++)
{
    fz_pixmap* pix = fz_new_pixmap_from_page_number(ctx, doc, i, ctm, fz_device_rgb(ctx), 0);

    float b = (float)pix->w / (float)1050;
    int t_h = pix->h / b;
    QSize t_size(1050, t_h);

    QImage img(pix->samples, pix->w, pix->h, pix->stride, QImage::Format_RGB888);
    img = img.scaled(1050, t_h);
    
    QListWidgetItem *tempImageItem = new QListWidgetItem;
    tempImageItem->setIcon(QIcon(QPixmap::fromImage(img)));
    tempImageItem->setSizeHint(t_size);
    ui->listWidget->addItem(tempImageItem);

    fz_drop_pixmap(ctx, pix);
}

fz_drop_document(ctx, doc);
fz_drop_context(ctx);

 

// 获取当前是哪一页
int getShowPageIndex()
{
    // image_height 是 1485,在最开始设置 QListWidget 时就有用到
    int px_num = ui->listWidget->verticalScrollBar()->value();
    double page_num = ceil(px_num / image_height);
    return page_num;
}

// 上一页
int page_num = getShowPageIndex();
if(page_num > 0)
    ui->listWidget->setCurrentRow(page_num-1, QItemSelectionModel::Current);

// 下一页
int page_num = getShowPageIndex();
int page_count = ui->listWidget->count();

if(page_num < page_count-1)
    ui->listWidget->setCurrentRow(page_num+1, QItemSelectionModel::Current);

 

 

 

代码比较简单,但是研究mupdf这个库还是花了点时间的,实属不易

另:公司使用的时候,可能还需要讲签名图片贴到PDF上,请参考另一篇文章:mupdf实用操作

 

标签:mupdf,Qt,翻页,fz,num,ui,listWidget,PDF,page
From: https://www.cnblogs.com/shiyixirui/p/17712206.html

相关文章

  • Qt控制键中打印特殊字符
    一、特殊字符在哪里右键输入法->符号大全二、显示特殊字符1)查找特殊字符对应的进制编码,两个网站都可以https://www.qqxiuzi.cn/bianma/erjinzhi.phphttp://www.kreativekorp.com/charset/whatis/?q=©2)程序内显示copyright=QString("%1%2%3%4").arg("Copyrigh......
  • Qt设置窗口显示到扩展屏
      #include<QDesktopWidget>voidDialog::setDisplayPos(){QDesktopWidget*desktop=QApplication::desktop();//获取显示器个数intnum=desktop->screenCount();//设置窗口显示到扩展屏上//如果有两个显示器,num=2,默认主屏inde......
  • VS2015 QT5.9.4 联合编译报错:提示找不到rc.exe
    解决方案:参考链接1、在C:\ProgramFiles(x86)\WindowsKits\10\bin\10.0.19041.0\x64路径下,找到rc.exe和rcdll.dll两个文件,并复制;2、粘贴到MicrosoftVisualStudio14.0\VC\bin对应目录下;3、重新编译运行程序即可。......
  • 【日常收支账本】【Day02】通过PyCharm集成QtDesigner和PyUIC快速创建界面
    一、集成QtDesigner和PyUICPyCharm集成QtDesigner和PyUIC教程二、在QtDesigner中画出窗体1.主界面编辑账本:新增、修改或删除记录可视化账本:通过不同角度查看收支情况全局配置:根据自身实际情况定义配置2.编辑账本界面三、创建项目项目结构将UI文件与窗体文件分......
  • 2021-11-14-MQTT的python应用
    layout:posttitle:MQTT的python应用categories:日志tags:-日志-大二BGImage:'https://github.xutongxin.me/https://raw.githubusercontent.com/xutongxin1/PictureBed/master/img0/20220310123346.png'jekyll-theme-WuK:musicid:'34367899......
  • 二十五、QT的BLE蓝牙操作,连接ECB02蓝牙模块收发信息
    1.注意事项(1)pro文件中引入bluetooth模块(2)安卓端运行时,需要同时打开蓝牙和定位才能获取到附近的蓝牙设备(3)mingw套件不能在Windows上运行,需要使用MSVC套件编译才能在Windows上运行2.操作步骤(1)使用QBluetoothLocalDevice类对设备蓝牙进行操作,判断设备蓝牙是否开启,开启和关闭设备......
  • Qt/C++音视频开发54-视频监控控件的极致设计
    一、前言跌跌撞撞摸爬滚打一步步迭代完善到今天,这个视频监控控件的设计,在现阶段水平上个人认为是做的最棒的(稍微自恋一下),理论上来说应该可以用5年不用推翻重写,推翻重写当然也是程序员爱干的事情,这个就要考验个人的功底,设计的好框架搭建的好,可以很多年不用变,只需要在现有框架小修......
  • Qt 6.5.2安装教程
    1先去下载在线安装包,例如,因为有时候这个软件会更新,老的会安装不了。  一、先去官方网站下载安装包https://download.qt.io/official_releases/online_installers/二、安装之前请指向国内的镜像,这样下载速度很快例如我下载的qt-unified-windows-x64-x.x.x-online.exe存放在d......
  • Qt和ffmpeg结合制作全能解码播放器
    #include<QCoreApplication>#include<QApplication>#include<QWidget>#include<QVBoxLayout>#include<QVideoWidget>#include<QAudioOutput>#include<QDebug>extern"C"{#include<libavformat/avfor......
  • PyQt中绘制折线图
    在PyQt中,可以使用matplotlib库来绘制折线图。 importsysfromPyQt5.QtWidgetsimportQApplication,QMainWindow,QVBoxLayout,QWidgetfrommatplotlib.figureimportFigurefrommatplotlib.backends.backend_qt5aggimportFigureCanvasQTAggasFigureCanvasimport......