源代码网络上有很多,但是使用过程中发现问题还是有的,尤其是在文档属性配置不同的时候,出现的问题就是不能转换。在开发端没有问题,但在不同于开发端电脑的配置情况下,会出现意向不到的问题。此处对解决方案进行记录。
using Word = Microsoft.Office.Interop.Word;
public static bool WordToPdf(object sourcePath, string targetPath)
{
bool result = false;
Word.WdExportFormat wdExportFormatPDF = Word.WdExportFormat.wdExportFormatPDF;
object missing = Type.Missing;
Microsoft.Office.Interop.Word.ApplicationClass applicationClass = null;
Microsoft.Office.Interop.Word.Document document = null;
try
{
applicationClass = new Microsoft.Office.Interop.Word.ApplicationClass();
document = applicationClass.Documents.Open(ref sourcePath, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
if (document != null)
{
document.ExportAsFixedFormat(targetPath, wdExportFormatPDF, false, Word.WdExportOptimizeFor.wdExportOptimizeForPrint, Word.WdExportRange.wdExportAllDocument, 0, 0, Word.WdExportItem.wdExportDocumentContent, true, true, Word.WdExportCreateBookmarks.wdExportCreateWordBookmarks, true, true, false, ref missing);
}
result = true;
}
catch
{
result = false;
}
finally
{
if (document != null)
{
document.Close(ref missing, ref missing, ref missing);
document = null;
}
if (applicationClass != null)
{
applicationClass.Quit(ref missing, ref missing, ref missing);
applicationClass = null;
}
}
return result;
}
在启动程序中输入dcomcnfg命令,打开组件服务,选定
设置其标识为启动用户,可以快速解决很多问题。
文章来源:https://www.toymoban.com/news/detail-448226.html
文章来源地址https://www.toymoban.com/news/detail-448226.html
到了这里,关于Asp.net Core使用Microsoft.Office.Interop.Word转换文档的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!