Office文档转pdf文档的方法有多种,整理两种方法。
一、用office软件另存为功能,适用于无编程基础的人们,而通过编程的方法如下:
1、添加引用
using Microsoft.Office.Core; using Word = Microsoft.Office.Interop.Word; using Excel = Microsoft.Office.Interop.Excel; using PowerPoint = Microsoft.Office.Interop.PowerPoint;
2、以下代码未测试
/// <summary>
/// 将word文档转换成PDF格式
/// </summary>
/// <param name="sourcePath"></param>
/// <param name="targetPath"></param>
/// <param name="exportFormat"></param>
/// <returns></returns>
private bool ConvertWordToPdf(string sourcePath, string targetPath, Word.WdExportFormat exportFormat)
{
bool result;
object paramMissing = Type.Missing;
Word.ApplicationClass wordApplication = new Word.ApplicationClass();
Word.Document wordDocument = null;
try
{
object paramSourceDocPath = Server.MapPath(sourcePath);
string paramExportFilePath = Server.MapPath(targetPath);
Word.WdExportFormat paramExportFormat = exportFormat;
bool paramOpenAfterExport = false;
Word.WdExportOptimizeFor paramExportOptimizeFor =
Word.WdExportOptimizeFor.wdExportOptimizeForPrint;
Word.WdExportRange paramExportRange = Word.WdExportRange.wdExportAllDocument;
int paramStartPage = 0;
int paramEndPage = 0;
Word.WdExportItem paramExportItem = Word.WdExportItem.wdExportDocumentContent;
bool paramIncludeDocProps = true;
bool paramKeepIRM = true;
Word.WdExportCreateBookmarks paramCreateBookmarks =
Word.WdExportCreateBookmarks.wdExportCreateWordBookmarks;
bool paramDocStructureTags = true;
bool paramBitmapMissingFonts = true;
bool paramUseISO19005_1 = false;
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);
if (wordDocument != null)
wordDocument.ExportAsFixedFormat(paramExportFilePath,
paramExportFormat, paramOpenAfterExport,
paramExportOptimizeFor, paramExportRange, paramStartPage,
paramEndPage, paramExportItem, paramIncludeDocProps,
paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,
paramBitmapMissingFonts, paramUseISO19005_1,
ref paramMissing);
result = true;
}
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;
}
/// <summary>
/// 将excel文档转换成PDF格式
/// </summary>
/// <param name="sourcePath"></param>
/// <param name="targetPath"></param>
/// <param name="targetType"></param>
/// <returns></returns>
private bool ConvertExcelToPdf(string sourcePath, string targetPath,Excel.XlFixedFormatType targetType)
{
bool result;
object missing = Type.Missing;
Excel.ApplicationClass application = null;
Excel.Workbook workBook = null;
try
{
application = new Excel.ApplicationClass();
object target = Server.MapPath(targetPath);
object type = targetType;
workBook = application.Workbooks.Open(Server.MapPath(sourcePath), missing, missing, missing, missing, missing,
missing, missing, missing, missing, missing, missing, missing, missing, missing);
workBook.ExportAsFixedFormat(targetType, target,Excel.XlFixedFormatQuality.xlQualityStandard, true, false, missing, missing, missing, missing);
result = true;
}
catch
{
result = false;
}
finally
{
if (workBook != null)
{
workBook.Close(true, missing, missing);
workBook = null;
}
if (application != null)
{
application.Quit();
application = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
return result;
}
/// <summary>
/// 将ppt文档转换成PDF格式
/// </summary>
/// <param name="sourcePath"></param>
/// <param name="targetPath"></param>
/// <param name="targetFileType"></param>
/// <returns></returns>
private bool ConvertPowerPointToPdf(string sourcePath, string targetPath,PowerPoint.PpSaveAsFileType targetFileType)
{
bool result;
object missing = Type.Missing;
PowerPoint.ApplicationClass application = null;
PowerPoint.Presentation persentation = null;
try
{
application = new PowerPoint.ApplicationClass();
persentation = application.Presentations.Open(Server.MapPath(sourcePath), MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse);
persentation.SaveAs(Server.MapPath(targetPath), targetFileType, MsoTriState.msoTrue);
result = true;
}
catch
{
result = false;
}
finally
{
if (persentation != null)
{
persentation.Close();
persentation = null;
}
if (application != null)
{
application.Quit();
application = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
return result;
}
二、用PDFMakerAPI,这种方法个人觉得更方便;
1、获得PDFMakerAPI.dll
该dll是软件Acrobat的一部分,该示例用的是Acrobat 7.0版本,dll路径:...\Adobe\Acrobat 7.0\PDFMaker\Common\PDFMakerAPI.dll
2、注册PDFMakerAPI.dll
将dll复制到程序适当的路径下,注册
方法:命令行 regsvr32 路径+PDFMakerAPI.dll, 反注册regsvr32 路径+PDFMakerAPI.dll /u
3、添加引用
添加 PDFMakerAPI 1.0 Type Library ,之后在引用中出现PDFMAKERAPILib
3、函数
private static int OfficeToPDF()
{
string sourcefilePath ="......"; //office文件路径
string targetfilePath ="......"; //转换后的pdf文件保存路径,后缀为pdf
int i = 0;
try
{
PDFMAKERAPILib.PDFMakerApp app = new PDFMAKERAPILib.PDFMakerApp();
i = app.CreatePDF(sourcefilePath, targetfilePath,PDFMAKERAPILib.PDFMakerSettings.kConvertAllPages, false, true, true, Type.Missing);
return i;
}
catch (Exception eee)
{
return i;
}
}