如何以编程方式将Word 2007文档保存为asp.net中的PDF文档?

时间:2012-11-23 09:11:31

标签: c# asp.net c#-4.0 ms-word office-interop

在我的Asp.net Web应用程序中,我必须以编程方式将一个docx文件转换为pdf文件。我使用了microsoft互操作性word包并使用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");控件在此行停止并且没有进一步的响应。浏览器符号似乎正在加载,加载......

我不知道这里的问题是什么......

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

2 个答案:

答案 0 :(得分:0)

检查此链接 - > convert doc to pdf in c#

  

它使用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);
            }
        }

答案 1 :(得分: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);