繁体   English   中英

使用 C# 和 Word Interop 将 Word 导出到 PDF 和 Word Interop 不会导出文档中书签的链接

[英]Export Word to PDF with C# and Word Interop don't export links to bookmarks in document

我在 C# 中有一个程序,我在其中使用 Word Interop 在.docx 中创建报告。 该文档完美地创建了几个“标题”,并带有指向文档内页面上的书签的链接,然后保存。 然后我尝试使用 ExportAsFixedFormat 将其导出到 pdf 并且导出的文件包括那些“标题”但没有指向书签的链接,但带有蓝色和下划线。

我用于导出的设置:

doc.ExportAsFixedFormat(
                newPdfPath,                                             //output file name
                WdExportFormat.wdExportFormatPDF,                       //export format
                false,                                                  //open after export
                WdExportOptimizeFor.wdExportOptimizeForPrint,           //optimize for
                WdExportRange.wdExportAllDocument,                      //export range
                1,                                                      //from
                1,                                                      //to
                WdExportItem.wdExportDocumentWithMarkup,                //export item - only text or include text with markups
                true,                                                   //include dox properties
                true,                                                   //copy information rights management
                WdExportCreateBookmarks.wdExportCreateWordBookmarks,    //export bookmarks and which types
                true,                                                   //include extra data to help screen readers (info about flow and logical organization of content
                true,                                                   //bitmap missing fonts
                false);                                                  //use ISO 19005-1

知道我哪里出错了吗?

PS:如果我用MS word打开那个word文档并手动将其导出为PDF,那么导出的版本应该是应该的,里面有所有的链接,所以问题一定是在使用ExportAsFixedFormat。

根据我的测试,我认为我们应该使用WdSaveFormat.wdFormatPDF而不是WdExportFormat.wdExportFormatPDF

这是您可以参考的代码示例。

        using Microsoft.Office.Interop.Word;
        object oMissing = System.Reflection.Missing.Value;
        string filename = "D:\\1.docx";
        Application app = new Application();
        Document doc = app.Documents.Open(filename);
        doc.Activate();
        object fullname = filename.Replace(".docx", ".pdf");
        object fileFormat = WdSaveFormat.wdFormatPDF;
        doc.SaveAs(ref fullname,ref fileFormat, ref oMissing, ref oMissing,ref oMissing, ref oMissing, ref oMissing, ref oMissing,ref oMissing, ref oMissing, ref oMissing, ref oMissing,ref oMissing, ref oMissing, ref oMissing, ref oMissing);
        object saveChanges = WdSaveOptions.wdDoNotSaveChanges;
        ((_Document)doc).Close(ref saveChanges, ref oMissing, ref oMissing);
        ((_Application)app).Quit(ref oMissing, ref oMissing, ref oMissing);

结果: 在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM