当前位置:网站首页>Qt笔记-利用QAxObject将文件夹内的所有docx转pdf
Qt笔记-利用QAxObject将文件夹内的所有docx转pdf
2022-07-29 15:24:00 【IT1995】
这里用Qt提供的AXContainer库就可以了,但这个只能在Windows平台上用。
首先要先了解几个知识点:
ExportAsFixedFormat是将文件导出为指定格式:这里关键的有2个参数:
FileName: 文件名;
FileType:为17代表是pdf
整体代码如下:
DocxToPdfDemo.pro
QT += axcontainer gui widgets
CONFIG += c++11 console
CONFIG -= app_bundle
# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
main.cpp
#include <QAxObject>
#include <QAxWidget>
#include <QDebug>
#include <QApplication>
#include <QFile>
#include <QDir>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QDir dir("D:\\数学");
if(!dir.exists()){
qDebug() << "dir error";
return 0;
}
dir.setFilter(QDir::Files | QDir::NoSymLinks);
QFileInfoList list = dir.entryInfoList();
for(int i = 0; i < list.size(); i++){
QFileInfo fileInfo = list.at(i);
QString fileName = fileInfo.fileName();
QString filePath = fileInfo.absoluteFilePath();
QString dirName = dir.path();
QAxWidget word("Word.Application");
word.setProperty("Visible", false);
QAxObject * documents = word.querySubObject("Documents");
QAxObject * document = documents->querySubObject("Open(QString)", filePath);
QVariant OutputFileName(dirName + "/" + fileName.mid(0, fileName.lastIndexOf(".")) + ".pdf");
QVariant ExportFormat(17);
QVariant OpenAfterExport(false);
document->querySubObject("ExportAsFixedFormat(const QVariant&,const QVariant&,const QVariant&)",
OutputFileName,
ExportFormat,
OpenAfterExport);
document->dynamicCall("Close(boolean)",false);
word.dynamicCall("Quit (void)");
qDebug() << filePath + " to pdf success";
}
return a.exec();
}
程序运行截图如下:

源码打包下载地址:
Qt/DocxToPdfDemo at master · fengfanchen/Qt · GitHub
边栏推荐
猜你喜欢

教程篇(7.0) 07. 诊断和故障排除 * FortiClient EMS * Fortinet 网络安全专家 NSE 5

一文看懂分布式存储架构

网络知识大集合(最详细)与网络通信过程

广汽本田安全驾驶体验营,老司机的必修课

LeetCode·739.每日温度·递减栈

电子元器件行业B2B交易系统:规范企业交易流程,提升销售管理效率

双线机房、多线机房、BGP机房的关系和对比

Floating point memory storage problem

ES6 from entry to master # 11: the Map data type

Unable to open the source file in qt vs2015 "QtWidgets" solution
随机推荐
NLP自然语言处理-机器学习和自然语言处理介绍(三)
又一位AI大佬辞职进体制内!AI的尽头是编制?
zabbix邮件发送告警信息
理解 Web3 的权威指南
深圳见!云原生加速应用构建专场:来看云原生 FinOps、SRE、高性能计算场景最佳实践
深度卷积生成对抗网络
Alibaba 开源内网高并发编程手册
微机原理之指令系统和汇编程序设计
[MySQL] 排序与分页
网络知识大集合(最详细)与网络通信过程
Unable to open the source file in qt vs2015 "QtWidgets" solution
这个保护数据隐私的赛道,人人都想插一脚,互联网大厂挤破头,连甲方都下场自研了...
Ribbon自定义修改负载均衡
NDK 系列(5):JNI 从入门到实践,爆肝万字详解!
Shell script programming - operation
UFLO:5、启动任务并自动完成第一个人工任务
nacos实现基本的服务跨进程调用和使用OpenFeign进行服务跨进程调用
奇怪,为什么ArrayList初始化容量大小为10?
CAN报文:数据帧详解
药品研发--质理研究人员绩效办法