Word转PDF时,对于文献引用问题,可能会出现”错误!无法找到引用源“,在转换时可以Ctrl+F11锁定域,那么在C#中如何设置呢?参考如下代码:wordDoc.Fields.Locked = -1;这一行可以锁定域更新。
/// <summary>
/// Word文件转成PDF文件
/// </summary>
/// <param name="wordFilePath"></param>
/// <param name="pdfFilePath"></param>
public static void ConvertWordToPDF(string wordFilePath, string pdfFilePath)
{
// 创建一个Word应用实例
Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
wordApp.Visible = false;
// Make the process invisible to the user
wordApp.ScreenUpdating = false;
// 创建一个Word文档对象,并打开Word文件
Microsoft.Office.Interop.Word.Document wordDoc = wordApp.Documents.Open(wordFilePath);
try
{
// 锁定域更新
wordDoc.Fields.Locked = -1;
// 导出为PDF,保留结构标签(如目录)
wordDoc.ExportAsFixedFormat(pdfFilePath,
WdExportFormat.wdExportFormatPDF,
Item: WdExportItem.wdExportDocumentWithMarkup,
CreateBookmarks: WdExportCreateBookmarks.wdExportCreateHeadingBookmarks);
}
catch
{
throw;
}
finally
{
// 关闭Word文档
wordDoc.Close(WdSaveOptions.wdDoNotSaveChanges);
// 退出Word应用
wordApp.Quit();
// 释放COM对象
System.Runtime.InteropServices.Marshal.ReleaseComObject(wordDoc);
System.Runtime.InteropServices.Marshal.ReleaseComObject(wordApp);
wordDoc = null;
wordApp = null;
GC.Collect();
}
}