Использование слова с помощью OLE-автоматизации (Примечание: слово COM не является входящим - убедитесь, что это не вызывается одновременно из разных потоков):
/// <summary>
/// Interacts directly with Word via COM to convert a document to PDF
/// Must have the Microsoft enhancement installed to support Save As PDF
/// in the word file menu
/// "2007 Microsoft Office Add-in: Microsoft Save as PDF or XPS" - SaveAsPDFandXPS.exe
/// </summary>
private void ExportToPDFUsingWord(object sourceDoc, string destPDF)
{
object m = System.Reflection.Missing.Value;
object readOnly = true;
object myFalse = false;
object isVisible = false;
object matchCase = false;
object matchWholeWord = true;
object saveChangesWhenQuitting = false;
//Debug("Connecting to word");
_Application oWordApp = new Application();
_Document oWordDoc = null;
Documents oWordDocs = null;
try
{
// load the source file
//Debug("Opening " + sourceDoc);
oWordDocs = oWordApp.Documents;
oWordDoc = oWordDocs.Open(ref sourceDoc, ref m, ref readOnly, ref m, ref m, ref m, ref m, ref m, ref m, ref m, ref m, ref isVisible, ref m, ref m, ref m, ref m);
if (oWordDoc != null)
{
try
{
WdExportFormat paramExportFormat = WdExportFormat.wdExportFormatPDF;
bool paramOpenAfterExport = false;
WdExportOptimizeFor paramExportOptimizeFor = WdExportOptimizeFor.wdExportOptimizeForPrint;
WdExportRange paramExportRange = WdExportRange.wdExportAllDocument;
int paramStartPage = 0;
int paramEndPage = 0;
WdExportItem paramExportItem = WdExportItem.wdExportDocumentContent;
bool paramIncludeDocProps = true;
bool paramKeepIRM = true;
WdExportCreateBookmarks paramCreateBookmarks = WdExportCreateBookmarks.wdExportCreateWordBookmarks;
bool paramDocStructureTags = true;
bool paramBitmapMissingFonts = true;
bool paramUseISO19005_1 = false;
//
// Export the file to PDF.
//Debug("Exporting to " + destPDF);
oWordDoc.ExportAsFixedFormat(destPDF,
paramExportFormat, paramOpenAfterExport,
paramExportOptimizeFor, paramExportRange, paramStartPage,
paramEndPage, paramExportItem, paramIncludeDocProps,
paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,
paramBitmapMissingFonts, paramUseISO19005_1,
ref m);
}
finally
{
//Debug("Closing " + sourceDoc);
oWordDoc.Close(ref saveChangesWhenQuitting, ref m, ref m);
}
}
else
throw new FileLoadException("Documents.Open returned null for file \'" + sourceDoc + "\', perhaps use comexp.msc and change DCOM->Word97-2003 Identity to Interactive user"); // if you are running from within a service then Word behaves differently - it is designed as a user application which must have a user interface
}
finally
{
// close word and cleanup
//Debug("Quitting Word");
oWordApp.Quit(ref saveChangesWhenQuitting, ref m, ref m);
//Debug("Releasing RCWs");
Release(oWordDocs);
oWordDocs = null;
Release(oWordDoc);
oWordDoc = null;
Release(oWordApp);
oWordApp = null;
//Debug("Collecting garbage");
GC.Collect(); // forces the garbage collector to run and might release any references that the RCW still has
GC.WaitForPendingFinalizers();
//Debug("Word objects finished");
}
}