1

我正在创建一个应用程序,它可以从Oracle数据库中获取数千个word文档,并且需要将它们转换为pdf并将它们发送回数据库。我已经拥有了所有支持机制(数据库交互,多任务和数据库和配置的可插入方法)并运行。尽管所有关于在服务器端使用办公自动化的警告我第一种方法是使用它(事实是,我的客户要求使用它)。但是我对c#(.Net 4.0)和word 2007之间的交互感到愤怒。我已经尝试了SaveAs和ExportAsFixedFormat。两者都运行良好,但当我试图关闭这个词...我得到了一个错误(弹出窗口,说这个词发现了一个问题,将被关闭)。然后,我试图在退出应用程序之前包含此内容:c#4.0和word 2007自动化

wordApplication.NormalTemplate.Saved = true; 

但它仍然抛出错误。我无法将超过一百个文档转换为无误。你知道一些方法来实现这种转换而不使用办公自动化吗?或者另一方面,你是否知道如何通过办公自动化进行这种转换而不出错?任何帮助将非常感激。

编辑: 宅男,这里是我使用(WARING测试提前代码!)

if (wordApplication == null) 
     { 
      try 
      { 
       wordApplication = (ApplicationClass)System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application") ?? 
          new ApplicationClass(); 
      } 
      catch (COMException) 
      { 
       Type type = Type.GetTypeFromProgID("Word.Application"); 
       wordApplication = (ApplicationClass)System.Activator.CreateInstance(type); 
      } 

     } 


     wordApplication.DisplayAlerts = WdAlertLevel.wdAlertsNone; 
     wordApplication.DisplayRecentFiles = false; 
     wordApplication.Visible = false; 
     wordApplication.ScreenUpdating = false; 

     Document wordDocument = null; 

     object paramSourceDocPath = Path.Combine(TempFolder, sourceFilename); 
     var paramExportFilePath = Path.Combine(TempFolder, sourceFilename + ".pdf"); 
     var paramMissing = Type.Missing; 

     const WdExportFormat paramExportFormat = WdExportFormat.wdExportFormatPDF; 
     const bool paramOpenAfterExport = false; 
     const WdExportOptimizeFor paramExportOptimizeFor = WdExportOptimizeFor.wdExportOptimizeForPrint; 
     const WdExportRange paramExportRange = WdExportRange.wdExportAllDocument; 
     const int paramStartPage = 0; 
     const int paramEndPage = 0; 
     const WdExportItem paramExportItem = WdExportItem.wdExportDocumentContent; 
     const bool paramIncludeDocProps = true; 
     const bool paramKeepIrm = true; 
     const WdExportCreateBookmarks paramCreateBookmarks = WdExportCreateBookmarks.wdExportCreateWordBookmarks; 
     const bool paramDocStructureTags = true; 
     const bool paramBitmapMissingFonts = true; 
     const bool paramUseIso190051 = false; 

     try 
     { 
      // Open the source document. 
      wordDocument = wordApplication.Documents.Open(
       ref paramSourceDocPath, ref paramMissing, ref paramMissing, 
       ref paramMissing, ref paramMissing, ref paramMissing, 
       ref paramMissing, ref paramMissing, ref paramMissing, 
       ref paramMissing, ref paramMissing, ref paramMissing, 
       ref paramMissing, ref paramMissing, ref paramMissing, 
       ref paramMissing); 

      // Export it in the specified format. 
      if (wordDocument != null) 
      { 

       //DocumentSaveAs(wordDocument); 

       Logger.Write("Open document" + sourceFilename, "info"); 
       wordDocument.ExportAsFixedFormat(paramExportFilePath, 
               paramExportFormat, paramOpenAfterExport, 
               paramExportOptimizeFor, paramExportRange, paramStartPage, 
               paramEndPage, paramExportItem, paramIncludeDocProps, 
               paramKeepIrm, paramCreateBookmarks, paramDocStructureTags, 
               paramBitmapMissingFonts, paramUseIso190051, 
               ref paramMissing); 
      } 
     } 
     catch (Exception ex) 
     { 
      Logger.Write(ex.Message); 
      throw; 
     } 
     catch 
     { 
      Logger.Write("Empty catch."); 
      throw; 
     } 
     finally 
     { 
      try 
      { 

      object saveChanges = WdSaveOptions.wdDoNotSaveChanges; 
      // Close and release the Document object. 
      if (wordDocument != null) 
      { 

       wordDocument.Close(ref saveChanges, ref paramMissing, 
            ref paramMissing); 
       Thread.Sleep(2000); 
       wordDocument = null; 
      } 

      // Quit Word and release the ApplicationClass object. 

      foreach (Document document in wordApplication.Documents) 
      { 
       document.Close(saveChanges, paramMissing, paramMissing); 
      } 
      wordApplication.NormalTemplate.Saved = true; 
      wordApplication.Quit(ref saveChanges, ref paramMissing, 
           ref paramMissing); 
      //Thread.Sleep(1000); 
      wordApplication = null; 


      GC.Collect(); 
      GC.WaitForPendingFinalizers(); 
      GC.Collect(); 
      GC.WaitForPendingFinalizers(); 


      Logger.Write("Deleting word file " + sourceFilename, "info"); 
      File.Delete(paramSourceDocPath.ToString()); 
      Logger.Write("File deleted " + sourceFilename, "info"); 

      Logger.Write("Reading pdf data " + paramExportFilePath, "info"); 
      ret = File.ReadAllBytes(paramExportFilePath); 
      Logger.Write("Data read " + sourceFilename + ".pdf", "info"); 
      File.Delete(paramExportFilePath); 
      Logger.Write("Pdf file deleted " + paramExportFilePath, "info"); 
      } 
      catch (Exception e) 
      { 
       Logger.Write(e,"info"); 
       throw; 
      } 
+0

这可能太多了。首先向我们展示您的转换例程代码。此外,你是否在桌面上尝试过这种转换,如果是这样,你是否得到相同的错误? – 2011-03-14 01:32:50

回答

1

我们使用一个被阅读Aspose我们所有Office集成的商业产品(因为它没有代码的例子实际上需要Office,速度非常快,而且不是Interop)。我不是100%确定你的确切情况是否会得到支持,但他们在他们的网站上有很多样本,如果你的项目支持购买这对夫妇的许可证,这可以让你的生活变得更加轻松。