0

在我的 Asp.net Web 应用程序中,我必须以编程方式将一个 docx 文件转换为 pdf 文件。我使用了 microsoft interoperability word package 并使用方法 saveAs() 方法。

这是我的 C# 代码...

var TheDocument = wdApp.Documents.Open("sample.docx"); //control stopped here...

TheDocument.ExportAsFixedFormat("sample.pdf", Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF,
               OptimizeFor: Microsoft.Office.Interop.Word.WdExportOptimizeFor.wdExportOptimizeForOnScreen,
               BitmapMissingFonts: true, DocStructureTags: false);

((Microsoft.Office.Interop.Word._Document)TheDocument).Close();

但是在执行这一行时,var TheDocument = wdApp.Documents.Open("sample.docx");控件停在了这一行上,没有进一步的反应。浏览器符号好像正在加载,正在加载...

我不知道这里有什么问题......

请指导我摆脱这个问题......

4

2 回答 2

0

检查此链接->在 c# 中将 doc 转换为 pdf

它使用 Microsoft.Office.Interop。

private Microsoft.Office.Interop.Word.ApplicationClass MSdoc;       

        //Use for the parameter whose type are not known or say Missing
        object Unknown = Type.Missing;

  private void word2PDF(object Source, object Target)
        {   //Creating the instance of Word Application          
       if (MSdoc == null)MSdoc = new Microsoft.Office.Interop.Word.ApplicationClass();

            try
            {  
                MSdoc.Visible = false;               
                MSdoc.Documents.Open(ref Source, ref Unknown,
                     ref Unknown, ref Unknown, ref Unknown,
                     ref Unknown, ref Unknown, ref Unknown,
                     ref Unknown, ref Unknown, ref Unknown,
                     ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown);
                MSdoc.Application.Visible = false;
                MSdoc.WindowState = Microsoft.Office.Interop.Word.WdWindowState.wdWindowStateMinimize;               

                object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF;

                MSdoc.ActiveDocument.SaveAs(ref Target, ref format,
                        ref Unknown, ref Unknown, ref Unknown,
                        ref Unknown, ref Unknown, ref Unknown,
                        ref Unknown, ref Unknown, ref Unknown,
                        ref Unknown, ref Unknown, ref Unknown,
                       ref Unknown, ref Unknown);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
            finally
            {
                if (MSdoc != null)
                {
                    MSdoc.Documents.Close(ref Unknown, ref Unknown, ref Unknown);
                    //WordDoc.Application.Quit(ref Unknown, ref Unknown, ref Unknown);
                }               
                // for closing the application
                WordDoc.Quit(ref Unknown, ref Unknown, ref Unknown);
            }
        }
于 2012-11-23T09:13:48.320 回答
0
Object missing = Type.Missing;

Globals.ThisAddIn.Application.ActiveDocument.ExportAsFixedFormat(@"D:\MyPDF.pdf", 

Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF, false, 

Microsoft.Office.Interop.Word.WdExportOptimizeFor.wdExportOptimizeForPrint, 

Microsoft.Office.Interop.Word.WdExportRange.wdExportAllDocument, 1, 1, 

Microsoft.Office.Interop.Word.WdExportItem.wdExportDocumentWithMarkup, true, true, 

Microsoft.Office.Interop.Word.WdExportCreateBookmarks.wdExportCreateNoBookmarks, 

true, true, false,ref missing);
于 2015-11-18T11:09:26.983 回答