注意:实现word转换为PDF文件,本人安装Office为2013;
word以后缀为.doc为例实现文件类型转换,具体方式如下所示:
实现步骤:
1.添加命名空间引用——using Microsoft.Office.Interop.Word;
2.添加WordConvertPdf方法——方法实现请阅读文件后续内容
3.WordConvertPdf方法的使用
详细如下所示;
2.添加WordConvertPdf方法==》
==》
private bool WordConvertPdf(string sourcePath, string targetPath, WdExportFormat exportFormat)
{
bool result = false;
object paramMissing = Type.Missing;
Microsoft.Office.Interop.Word.ApplicationClass wordApplication = new Microsoft.Office.Interop.Word.ApplicationClass();
Document wordDocument = null;
try
{
object paramSourceDocPath = sourcePath;
string paramExportFilePath = targetPath;
WdExportFormat paramExportFormat = exportFormat;
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;
wordDocument = wordApplication.Documents.OpenNoRepairDialog(
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);
if (wordDocument != null)
wordDocument.ExportAsFixedFormat(paramExportFilePath,
paramExportFormat, paramOpenAfterExport,
paramExportOptimizeFor, paramExportRange, paramStartPage,
paramEndPage, paramExportItem, paramIncludeDocProps,
paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,
paramBitmapMissingFonts, paramUseISO19005_1,
ref paramMissing);
result = true;
}
catch
{
return false;
}
finally
{
if (wordDocument != null)
{
wordDocument.Close(ref paramMissing, ref paramMissing, ref paramMissing);
wordDocument = null;
}
if (wordApplication != null)
{
wordApplication.Quit(ref paramMissing, ref paramMissing, ref paramMissing);
wordApplication = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
return result;
}
3.WordConvertPdf方法的使用
==》
OpenFileDialog opg = new OpenFileDialog();
opg.Filter = "word(*.doc)|*.doc";
if (opg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
WordConvertPdf(opg.FileName, pp, WdExportFormat.wdExportFormatPDF);
}
注意:
如果出现如下图所示异常:
解决方式如下: