最近需要把PPT文件转换成PDF文件,我发现网上好像关于这方面的教程很少,我这里来弥补一下,首先我的环境是windows 10 64bit,qt 5.9.7,安装了office 2010.
然后转换的核心代码为:
void MainWindow::openPPT(){
QFileDialog _dialog;
_dialog.setFileMode(QFileDialog::ExistingFile);
_dialog.setViewMode(QFileDialog::Detail);
_dialog.setOption(QFileDialog::ReadOnly, true);
_dialog.setDirectory(QString("./"));
_dialog.setNameFilter(QString("ppt(*.pptx *.ppt)"));
if (_dialog.exec()){
QStringList files = _dialog.selectedFiles();
for (auto fileName : files){
if (fileName.endsWith(".pptx")||fileName.endsWith(".ppt"))
{
// Open parameter
QAxObject *_powerPointAxObj = new QAxObject("Powerpoint.Application", 0);
if (!_powerPointAxObj){
continue;
}
_powerPointAxObj->dynamicCall("SetVisible(bool)", false);
QAxObject *presentations = _powerPointAxObj->querySubObject("Presentations");
QList<QVariant> paramList;
paramList.push_back(QVariant(fileName));
paramList.push_back(0);
paramList.push_back(0);
paramList.push_back(0);
//FileName Required String. The name of the file to open
//ReadOnly Optional Long. True to open the file with read-only status. If this argument is omitted, the file is opened with read/write status.
//Untitled Optional Long. True to open the file without a title. This is equivalent to creating a copy of the file. If this argument isn't specified, the file name automatically becomes the title of the opened presentation.
//WithWindow Optional Long. True to open the file in a visible window. False to hide the opened presentation. The default value is True.
QAxObject *presentation = presentations->querySubObject("Open(const QString&,int,int,int)",paramList);
if (presentation != nullptr){
paramList.clear();
QString application_path = QApplication::applicationDirPath();
application_path.replace("/", "\\");
application_path += "\\ShowFile.pdf";
paramList.push_back(application_path);
paramList.push_back(32);
paramList.push_back(0);
presentation->dynamicCall("SaveAs(const QString&,int,int)", paramList);
presentations->dynamicCall("Close()"); //到这里就已经转换成功了,转换的文件名为ShowFile.pdf,保存在你的debug或者release目录里,可以自行查看
delete presentations;
// this->OpenPdf(application_path);
}
}
}
}
参考文献
[1].关于使用QAxObject自动播放ppt的问题.https://bbs.csdn.net/topics/392037375