使用c#和Interop另存为PDF时,无法保存嵌入在Word文档中的PDF。

8

背景:我正在尝试使用C#和Interop生成Word文档。我成功地插入了表格、页眉、页脚、图表等,大部分功能都正常工作。接下来,我希望将其转换为PDF。

采用的方法:由于我使用的是Word 2011和Word 2007(安装了saveaspdf插件),因此我使用“另存为”将文档保存为PDF,并选择文件类型为PDF。

出现的问题:我成功地嵌入了PDF到我的文档中,使用以下代码:

        Object oIconLabel = new FileInfo(file_path).Name;
        Object oFileDesignInfo = file_path;
        Object oClassType = "AcroExch.Document.7";
        Object oTrue = true;
        Object oFalse = false;
        Object oMissing = System.Reflection.Missing.Value;
        Object index = 0;
        Range r = d.Bookmarks.get_Item(ref eod).Range;

        r.InlineShapes.AddOLEObject(
            ref oClassType, ref oFileDesignInfo, ref oFalse, ref oTrue, ref oMissing,
            ref index, ref oIconLabel, ref oMissing);

这里的d是Document对象。现在,当我尝试使用以下方法将此Word文档保存为PDF时:

 d.SaveAs(ref filename, ref filetype, 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);

正在生成PDF文档。但是我嵌入的文档并没有嵌入到生成的PDF中,只显示了一个图标。

如何将文档嵌入到PDF中?我们通常如何处理这种情况?如果问题不清楚,请告诉我。

3个回答

11

我选择的API是错误的,应该不是SaveAs,而是ExportAsFixedFormat。

文档可以在这里找到。

使用示例:

d.ExportAsFixedFormat(filename.ToString(), WdExportFormat.wdExportFormatPDF,false,WdExportOptimizeFor.wdExportOptimizeForOnScreen,
                    WdExportRange.wdExportAllDocument,1,1,WdExportItem.wdExportDocumentContent,true,true,
                    WdExportCreateBookmarks.wdExportCreateHeadingBookmarks,true,true,false,ref oMissing);

5

这段代码对我来说,在Word文档中适用于文本、表格、图形和图片。
需要在服务器上安装Word。
我正在使用Word 2013,一切正常。

Microsoft.Office.Interop.Word.Application wordApplication = new Microsoft.Office.Interop.Word.Application();

                    object oMissing = System.Reflection.Missing.Value;
                    Object oFalse = false;
                    Object filename = (Object)path;

                    Microsoft.Office.Interop.Word.Document doc = wordApplication.Documents.Open(ref filename, ref oMissing,
                        ref oFalse, 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);

                    doc.Activate();

                    var pathfilename = Path.Combine(Server.MapPath("~/UploadDocuments/PDF"), fileName).Replace(".docx", ".pdf"); ;
                    Object filename2 = (Object)pathfilename;

                    doc.SaveAs(ref filename2, WdSaveFormat.wdFormatPDF,
                        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);


                    // close word doc and word app.
                    object saveChanges = WdSaveOptions.wdDoNotSaveChanges;

                    ((_Document)doc).Close(ref saveChanges, ref oMissing, ref oMissing);

                    ((_Application)wordApplication).Quit(ref oMissing, ref oMissing, ref oMissing);

                    wordApplication = null;
                    doc = null;

1
"WdSaveOptions.wdDoNotSaveChanges" 正是我需要解决问题的方法,谢谢! - Nicolas Régnier

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接