public class OfficeHelper
{
static Word.Application wordApplication = new Word.Application();
static Excel.Application excelApplication = new Excel.Application();
static PowerPoint.Application pptApplication = new PowerPoint.Application();
/// <summary>
/// 将Word文档转换成PDF格式
/// </summary>
/// <param name="sourcePath">源文件路径</param>
/// <param name="targetPath">目标文件路径</param>
/// <returns></returns>
public static bool WordConvertPDF(string sourcePath, string targetPath)
{
bool result;
Word.Document wordDocument = null;
try
{
wordDocument = wordApplication.Documents.Open(ref sourcePath);
if (wordDocument != null)
{
wordDocument.SaveAs2(targetPath, WdExportFormat.wdExportFormatPDF);
//wordDocument.ExportAsFixedFormat(targetPath, WdExportFormat.wdExportFormatPDF);
result = true;
}
}
catch (Exception ex)
{
result = false;
LogHelper.Log($"文件:{sourcePath} 生成失败,原因:{ex.Message}", ex.StackTrace);
}
finally
{
if (wordDocument != null)
{
wordDocument.Close();
wordDocument = null;
}
}
return result;
}
/// <summary>
/// 将Excel文档转换成PDF格式
/// </summary>
/// <param name="sourcePath">源文件路径</param>
/// <param name="targetPath">目标文件路径</param>
/// <returns></returns>
public static bool ExcelConvertPDF(string sourcePath, string targetPath)
{
bool result;
Workbook workBook = null;
try
{
workBook = excelApplication.Workbooks.Open(sourcePath);
if (workBook != null)
{
workBook.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, targetPath);
result = true;
}
}
catch (Exception ex)
{
result = false;
LogHelper.Log($"文件:{sourcePath} 生成失败,原因:{ex.Message}", ex.StackTrace);
}
finally
{
if (workBook != null)
{
workBook.Close();
workBook = null;
}
}
return result;
}
/// <summary>
/// 将PPT文档转换成pdf格式
/// </summary>
/// <param name="sourcePath">源文件路径</param>
/// <param name="targetPath">目标文件路径</param>
/// <returns></returns>
public static bool PPTConvertPDF(string sourcePath, string targetPath)
{
bool result;
object missing = Type.Missing;
Presentation persentation = null;
try
{
persentation = pptApplication.Presentations.Open(sourcePath, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse);
if (persentation != null)
{
persentation.SaveAs(targetPath, PpSaveAsFileType.ppSaveAsPDF, Microsoft.Office.Core.MsoTriState.msoTrue);
//persentation.ExportAsFixedFormat(targetPath, PpFixedFormatType.ppFixedFormatTypePDF);
result = true;
}
}
catch (Exception ex)
{
result = false;
LogHelper.Log($"文件:{sourcePath} 生成失败,原因:{ex.Message}", ex.StackTrace);
}
finally
{
if (persentation != null)
{
persentation.Close();
persentation = null;
}
}
return result;
}
}
========================
这个和基于Office的解决方案一样,通过代码调用COM接口,实现文件的转换。当然需要提前在服务器上安装WPS软件。
在本地的WPS安装目录中,找到以下几个dll文件,并将其引用到项目中,
wpsapi.dll
wpsapiex.dll
代码:
public static void WordConvertPDF(string sourcePath, string targetPath)
{
var app = new Word.Application();
var doc = app.Documents.Open(sourcePath,Visible: MsoTriState.msoFalse);
doc.SaveAs2(targetPath, Word.WdExportFormat.wdExportFormatPDF);
doc.Close();
app.Close();
}
其中Word是wpsapi.dll添加到程序中后,程序集命名空间名称。
调用WPS转换文档我也只是在本机运行成功了,并没有实际的应用,只能说是多做了一下了解和尝试吧。
评论0