I am creating an application that gets thousands of word documents from an Oracle database and need to transform them into pdf and send them back to database. I already have all the support mechanism (database interaction, multitasking and a plugable approach to database and configuration) up and running. In spite of all warnings about using office automation in the server side my first approach was to use it (the truth is that my customer asked to use it). But I'm going mad with the interaction between c# (.Net 4.0) and word 2007. I already tried the SaveAs and the ExportAsFixedFormat. Both worked fine but when I try to close the word... I got an erro (pop up window saying that word found a problem and will be closed). Then I tried to include this before quitting the application:
wordApplication.NormalTemplate.Saved = true;
But it still throwing the errror. I'm unable to convert more than one hundred docs without an error. Do you know some way to achieve this conversion without using office automation? Or in the other hand, do you know how to do this conversion through office automation without errors? Any help will be very appreciated.
EDIT: Otaku, here is an example of the code I am using (WARING! testing code ahead)
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 = WdExportOptimize开发者_Python百科For.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;
}
We use a commercial product called Aspose for all our Office integrations (as it does not actually require Office, is very fast, and is not Interop). I am not 100% sure if your exact scenario would be supported, but they have many samples on their website and if your project supports buying the couple licenses this can make your life a lot easier.
精彩评论