使用 Microsoft.Office.Interop.Word 将word文档转为PDF
在NuGet中搜索 Microsoft.Office.Interop.Word 安装
方法 参数参考 微软官网地址文章来源:https://www.toymoban.com/news/detail-537701.html
文章来源地址https://www.toymoban.com/news/detail-537701.html
/// <summary>
/// 将word转成PDF office
/// </summary>
/// <param name="sourcePath"></param>
/// <param name="targetPath"></param>
/// <returns></returns>
public static bool WordToPDFWithOffice(string sourcePath, string targetPath, int fromPage = 1, int toPage = 1)
{
bool result = false;
Microsoft.Office.Interop.Word.Application application = new Microsoft.Office.Interop.Word.Application();
Document document = null;
try
{
application.Visible = false;
document = application.Documents.Open(sourcePath);
/*
参数参考 https://docs.microsoft.com/zh-cn/office/vba/api/visio.document.exportasfixedformat
*/
document.ExportAsFixedFormat(targetPath, WdExportFormat.wdExportFormatPDF, false, WdExportOptimizeFor.wdExportOptimizeForPrint, WdExportRange.wdExportFromTo, fromPage, toPage);
result = true;
}
catch (Exception e)
{
//Console.WriteLine(e.Message);
result = false;
}
finally
{
document.Close();
}
return result;
}
到了这里,关于C#将word文档转为PDF的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!