使用策略标签在 c# (.net) 中将 docx 保存为 pdf

to save docx as pdf in c# (.net) with policy label

我正在使用 C# (.Net Framework 4.6.2) 中的 Microsoft.Office.Interop.Word 库将 .docx 文件转换为 .pdf

我的代码是:

    public static void DOCtoPDF(string docFullPath, string pdfFullPath)
    {
        Application appWord = new Application();
        var wordDocument = appWord.Documents.Open(docFullPath);

        wordDocument.SaveAs2(pdfFullPath, WdSaveFormat.wdFormatPDF);
        wordDocument.Close();
        appWord.Quit();
    }

我在保存文档文件时遇到了这个错误:

如何解决这个问题?

作为 Microsoft.Office.Interop.Word 库的替代方法,您可能需要查看 GemBox.Document library

例如像这样:

public static void DOCtoPDF(string docFullPath, string pdfFullPath)
{
    DocumentModel wordDocument = DocumentModel.Load(docFullPath);
    wordDocument.Save(pdfFullPath);
}

我的问题解决了..

它正在尝试保存 wordDocument,所以它会向我显示该对话框..

我写了

wordDocument.Close(false);

而不是

wordDocument.Close();

现在,它不会尝试保存我的原始 word 文档。 它只能转换为pdf。 :)