public static void wordConvertPdf(string wordPath, string pdfPath)
{ oWord.Application wordApplication =new Microsoft.Office.Interop.Word.Application ();
oWord.Document wordDocument = null;
object paramSourceDocPath = wordPath;//eg.@"C:\Temp\Test.docx";
object paramMissing = Type.Missing;
string paramExportFilePath =pdfPath;//eg.@"C:\Temp\Test.xps";
oWord.WdExportFormat paramExportFormat = oWord.WdExportFormat.wdExportFormatPDF;
bool paramOpenAfterExport = false;
oWord.WdExportOptimizeFor paramExportOptimizeFor =
oWord.WdExportOptimizeFor.wdExportOptimizeForPrint;
oWord.WdExportRange paramExportRange = oWord.WdExportRange.wdExportAllDocument;
int paramStartPage = 0;
int paramEndPage = 0;
oWord.WdExportItem paramExportItem = oWord.WdExportItem.wdExportDocumentContent;
bool paramIncludeDocProps = true;
bool paramKeepIRM = true;
oWord.WdExportCreateBookmarks paramCreateBookmarks =oWord.WdExportCreateBookmarks.wdExportCreateWordBookmarks;
bool paramDocStructureTags = true;
bool paramBitmapMissingFonts = true;
bool paramUseISO19005_1 = 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)
wordDocument.ExportAsFixedFormat(paramExportFilePath,
paramExportFormat, paramOpenAfterExport,
paramExportOptimizeFor, paramExportRange, paramStartPage,
paramEndPage, paramExportItem, paramIncludeDocProps,
paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,
paramBitmapMissingFonts, paramUseISO19005_1,
ref paramMissing);
}
catch (Exception ex)
{}
finally
{
// Close and release the Document object.
if (wordDocument != null)
{
wordDocument.Close(ref paramMissing, ref paramMissing,
ref paramMissing);
wordDocument = null;
}
// Quit Word and release the ApplicationClass object.
if (wordApplication != null)
{
wordApplication.Quit(ref paramMissing, ref paramMissing,
ref paramMissing);
wordApplication = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
原帖地址:http://msdn.microsoft.com/en-us/library/bb412305.aspx