我正在创建一个应用程序,它从 Oracle 数据库中获取数千个 word 文档,需要将它们转换为 pdf 并将它们发送回数据库。我已经启动并运行了所有支持机制(数据库交互、多任务处理和可插入的数据库和配置方法)。尽管有关于在服务器端使用办公自动化的所有警告,但我的第一个方法是使用它(事实是我的客户要求使用它)。但是我对 c# (.Net 4.0) 和 word 2007 之间的交互感到很生气。我已经尝试过 SaveAs 和 ExportAsFixedFormat。两者都工作得很好,但是当我尝试关闭这个词时......我得到了一个错误(弹出窗口说那个词发现了一个问题并将被关闭)。然后我尝试在退出应用程序之前将其包含在内:
wordApplication.NormalTemplate.Saved = true;
但它仍然抛出错误。我无法转换超过一百个文档而不会出错。你知道在不使用办公自动化的情况下实现这种转换的方法吗?或者另一方面,您知道如何通过办公自动化进行这种转换而不会出错吗?任何帮助将不胜感激。
编辑:Otaku,这是我正在使用的代码示例(警告!提前测试代码)
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;
}