最近由于工作需求,需要将office系列文件(world、excel、ppt)pdf转换为图片格式,经过调研C++处理不依赖微软官方office的库有类似aspose
,但是需要收费,所以这里选择用C#封装office(非常方便),然后用C++(原始工程是C++)调用之。
调用方式为,将C#类库开发为COM,注册后,C++像调用其它COM组件一样,调用Net类库中的方法
优点: 编写代码简单,调用方便
缺点: 需要注册output,发布不够简单
C#常规编写类,生产assembly,C++使用CLR编译既可直接引用托管类。
优点: 编写代码简单,调用方便
缺点: 需要了解C++ CLR语法(既不像C++,又不像C#,总之很奇怪)
C#常规编写类,生产assembly,C++使用SDK提供的CLR非托管接口(CLRCreateInstance)进行调用。
优点: 传统C#编程,传统C++编程
缺点: 暂时还没发现
根据实际情况我选择第二种方式,C#封装库-》C++通过 /CLR调用C#库封装成C++的DLL-》主程序调用C++DLL
修改输出目录为C++项目可执行程序目录
右键属性-》生成-》输出路径
导入office库
using System.Runtime.InteropServices;
using Office = Microsoft.Office.Core;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Word = Microsoft.Office.Interop.Word;
using Excel = Microsoft.Office.Interop.Excel;
实现转换功能
public int PowerPointConvertPdf(String pptPath,String pdfPath)
{
PowerPoint.Application pptApplication;
pptApplication = new PowerPoint.Application();
PowerPoint.Presentation document = pptApplication.Presentations.Open(pptPath, ReadOnly: Office.MsoTriState.msoTrue, Office.MsoTriState.msoFalse, WithWindow: Office.MsoTriState.msoFalse);
if (document == null)
{
return 0;
}
document.ExportAsFixedFormat(pdfPath, PowerPoint.PpFixedFormatType.ppFixedFormatTypePDF);
document.Close();
document = null;
pptApplication.Quit();
pptApplication = null;
return 1;
}
public int WordConvertPdf(String WordPath, String pdfPath)
{
Word.Application wordApplication;
wordApplication = new Word.Application();
Word.Document document = wordApplication.Documents.Open(WordPath, System.Reflection.Missing.Value, true);
if (document == null)
{
return 0;
}
document.Activate();
document.ExportAsFixedFormat(pdfPath, Word.WdExportFormat.wdExportFormatPDF);
document.Close();
document = null;
wordApplication.Quit();
wordApplication = null;
return 1;
}
public int ExcelConvertPdf(String ExcelPath, String pdfPath)
{
Excel.Application excelApplication;
excelApplication = new Excel.Application();
Excel.Workbook document = excelApplication.Workbooks.Open(ExcelPath, System.Reflection.Missing.Value, true);
if (document == null)
{
return 0;
}
document.ExportAsFixedFormat(Excel.XlFixedFormatType.xlTypePDF,pdfPath);
document.Close();
document = null;
excelApplication.Quit();
excelApplication = null;
return 1;
}
引入库
#using "../x64/Debug/OfficeConvertPdfLibrary.dll"
using namespace OfficeConvertPdfLibrary;
调用库函数
int main()
{
ClassOfficeConvertPdfLibrary^ pConvertClass = gcnew ClassOfficeConvertPdfLibrary();
//路径一定要注意 错误示范
//D:\sourcecode\OfficeConvertPdfLibrary\x64\Debug\test.xlsx
//D:/sourcecode/OfficeConvertPdfLibrary/x64/Debug/test.xlsx
int nlsxResult = pConvertClass->ExcelConvertPdf("D:\\sourcecode\\OfficeConvertPdfLibrary\\x64\\Debug\\test.xlsx","D:\\sourcecode\\OfficeConvertPdfLibrary\\x64\\Debug\\test_xlsx.pdf");
if (nlsxResult == 0)
{
std::cout << "test.xlsx 转换失败" << std::endl;
}
else
{
std::cout << "test.xlsx 转换成功" << std::endl;
}
int wordResult = pConvertClass->WordConvertPdf("D:\\sourcecode\\OfficeConvertPdfLibrary\\x64\\Debug\\test.docx","D:\\sourcecode\\OfficeConvertPdfLibrary\\x64\\Debug\\test_word.pdf");
if (wordResult == 0)
{
std::cout << "test.docx 转换失败" << std::endl;
}
else
{
std::cout << "test.docx 转换成功" << std::endl;
}
int pptResult = pConvertClass->PowerPointConvertPdf("D:\\sourcecode\\OfficeConvertPdfLibrary\\x64\\Debug\\test.pptx","D:\\sourcecode\\OfficeConvertPdfLibrary\\x64\\Debug\\test_ppt.pdf");
if (pptResult == 0)
{
std::cout << "test.pptx 转换失败" << std::endl;
}
else
{
std::cout << "test.pptx 转换成功" << std::endl;
}
}
注意:注释掉dllmain.cpp中的main函数,否则debug调试时报错(网上有解决方法 CTRL+ALT+E LoaderLock取消打勾,本人试了没有效果)
引入C#库
#using "../x64/Debug/OfficeConvertPdfLibrary.dll"
using namespace OfficeConvertPdfLibrary;
C++封装代码
bool CCppOfficeConvertPdf::ConvertPdf(std::string type, std::string inPath, std::string outPath)
{
System::String^ strType = gcnew System::String(type.c_str());
System::String^ strInPath = gcnew System::String(inPath.c_str());
System::String^ strOutPath = gcnew System::String(outPath.c_str());
ClassOfficeConvertPdfLibrary^ pConvertClass = gcnew ClassOfficeConvertPdfLibrary();
if (strType == "xlsx")
{
int nlsxResult = pConvertClass->ExcelConvertPdf(strInPath, strOutPath);
if (nlsxResult == 0)
{
return false;
}
else
{
return true;
}
}
else if(strType == "docx")
{
int wordResult = pConvertClass->WordConvertPdf(strInPath, strOutPath);
if (wordResult == 0)
{
return false;
}
else
{
return true;
}
}else if (strType == "pptx")
{
int pptResult = pConvertClass->PowerPointConvertPdf(strInPath, strOutPath);
if (pptResult == 0)
{
return false;
}
else
{
return true;
}
}
}
此时的C++项目不需要设置别的配置,正常调用C++Dll就可以了
#include
#include "../CppOfficeConvertPdf/CppOfficeConvertPdf.h"
#pragma comment (lib,"../x64/Debug/CppOfficeConvertPdf.lib")
int main()
{
CCppOfficeConvertPdf *pConvert = new CCppOfficeConvertPdf();
pConvert->ConvertPdf("xlsx", "D:\\sourcecode\\OfficeConvertPdfLibrary\\x64\\Debug\\test.xlsx", "D:\\sourcecode\\OfficeConvertPdfLibrary\\x64\\Debug\\cpp_xlsx.pdf");
pConvert->ConvertPdf("docx", "D:\\sourcecode\\OfficeConvertPdfLibrary\\x64\\Debug\\test.docx","D:\\sourcecode\\OfficeConvertPdfLibrary\\x64\\Debug\\cpp_word.pdf");
pConvert->ConvertPdf("pptx", "D:\\sourcecode\\OfficeConvertPdfLibrary\\x64\\Debug\\test.pptx", "D:\\sourcecode\\OfficeConvertPdfLibrary\\x64\\Debug\\cpp_ppt.pdf");
}
完整代码 https://mp.csdn.net/mp_download/manage/download/UpDetailed