本文适用于VS2013 项目中的Word转换为PDF、Excel转换为PDF、PPT转换为PDF

0.一种更加简单方便的方法


1.本页所用的方法在本机测试时基本不会出现问题,只是偶尔PPT转PDF失败,需要重新添加dll,但是在服务器环境就会出现各种错误,需要各种配置。

2.http://www.cnblogs.com/moonache/p/4991459.html 请参考此文,更简单实用。

1.添加Using


1.在后台添加using

  1. using Microsoft.Office.Interop.Word;
    using Microsoft.Office.Interop.Excel;
    using Microsoft.Office.Core;
    using Microsoft.Office.Interop.PowerPoint;

2.添加引用


1.添加引用

3.添加代码


1.在后台添加代码

  1. protected void Page_Load(object sender, EventArgs e)
    {
    string wordSource,wordTarget,excelSource,excelTarget,pptSource,pptTarget;
    wordSource = @"E:\FileDownload\Explorer\意向 0.3.docx";
    wordTarget = @"E:\FileDownload\Explorer\W2P.pdf";
    excelSource = @"E:\FileDownload\Explorer\xlsx.xlsx";
    excelTarget = @"E:\FileDownload\Explorer\E2P.pdf";
    pptSource = @"E:\FileDownload\Explorer\质量月活动1509.pptx";
    pptTarget = @"E:\FileDownload\Explorer\P2P.pdf"; WordToPdf(wordSource,wordTarget);
    ExcelToPdf(excelSource,excelTarget);
    PPTConvertToPDF(pptSource, pptTarget); } public static bool WordToPdf(string sourcePath, string targetPath)
    {
    bool result = false;
    WdExportFormat wdExportFormatPDF = WdExportFormat.wdExportFormatPDF;//转换格式1.wdExportFormatPDF转换成pdf格式 2.wdExportFormatXPS转换成xps格式
    object missing = Type.Missing;
    Microsoft.Office.Interop.Word.ApplicationClass applicationClass = null;
    Document document = null;
    try
    {
    applicationClass = new Microsoft.Office.Interop.Word.ApplicationClass();
    object inputfileName = sourcePath;//需要转格式的文件路径
    string outputFileName = targetPath;//转换完成后PDF或XPS文件的路径和文件名名称
    WdExportFormat exportFormat = wdExportFormatPDF;//导出文件所使用的格式
    bool openAfterExport = false;//转换完成后是否打开
    WdExportOptimizeFor wdExportOptimizeForPrint = WdExportOptimizeFor.wdExportOptimizeForPrint;//导出方式1.wdExportOptimizeForPrint针对打印进行导出,质量较高,生成的文件大小较大。2.wdExportOptimizeForOnScreen 针对屏幕显示进行导出,质量较差,生成的文件大小较小。
    WdExportRange wdExportAllDocument = WdExportRange.wdExportAllDocument;//导出全部内容(枚举)
    int from = ;//起始页码
    int to = ;//结束页码
    WdExportItem wdExportDocumentContent = WdExportItem.wdExportDocumentContent;//指定导出过程中是否只包含文本或包含文本的标记.1.wdExportDocumentContent:导出文件没有标记,2.导出文件有标记
    bool includeDocProps = true;//指定是否包含新导出的文件在文档属性
    bool keepIRM = true;//
    WdExportCreateBookmarks wdExportCreateWordBookmarks = WdExportCreateBookmarks.wdExportCreateWordBookmarks;//1.wdExportCreateNoBookmarks:不要在导出文件中创建书签,2.wdExportCreateHeadingBookmarks:标题和文本框导出的文件中创建一个书签,3.wdExportCreateWordBookmarks每个字的书签,其中包括除包含页眉和页脚中的所有书签导出的文件中创建一个书签。
    bool docStructureTags = true;
    bool bitmapMissingFonts = true;
    bool UseISO19005_1 = false;//生成的文档是否符合 ISO 19005-1 (PDF/A)
    document = applicationClass.Documents.Open(ref inputfileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
    if (document != null)
    {
    document.ExportAsFixedFormat(outputFileName, exportFormat, openAfterExport, wdExportOptimizeForPrint, wdExportAllDocument, from, to, wdExportDocumentContent, includeDocProps, keepIRM, wdExportCreateWordBookmarks, docStructureTags, bitmapMissingFonts, UseISO19005_1, ref missing);
    }
    result = true;
    }
    catch
    {
    result = false;
    }
    finally
    {
    if (document != null)
    {
    document.Close(ref missing, ref missing, ref missing);
    document = null;
    }
    if (applicationClass != null)
    {
    applicationClass.Quit(ref missing, ref missing, ref missing);
    applicationClass = null;
    }
    }
    return result;
    } public static bool ExcelToPdf(string sourcePath, string targetPath)
    {
    bool result = false;
    XlFixedFormatType xlTypePDF = XlFixedFormatType.xlTypePDF;//转换成pdf
    object missing = Type.Missing;
    Microsoft.Office.Interop.Excel.ApplicationClass applicationClass = null;
    Workbook workbook = null;
    try
    {
    applicationClass = new Microsoft.Office.Interop.Excel.ApplicationClass();
    string inputfileName = sourcePath;//需要转格式的文件路径
    string outputFileName = targetPath;//转换完成后PDF文件的路径和文件名名称
    XlFixedFormatType xlFixedFormatType = xlTypePDF;//导出文件所使用的格式
    XlFixedFormatQuality xlFixedFormatQuality = XlFixedFormatQuality.xlQualityStandard;//1.xlQualityStandard:质量标准,2.xlQualityMinimum;最低质量
    bool includeDocProperties = true;//如果设置为True,则忽略在发布时设置的任何打印区域。
    bool openAfterPublish = false;//发布后不打开
    workbook = applicationClass.Workbooks.Open(inputfileName, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
    if (workbook!=null)
    {
    workbook.ExportAsFixedFormat(xlFixedFormatType, outputFileName, xlFixedFormatQuality, includeDocProperties, openAfterPublish, missing, missing, missing, missing);
    }
    result = true;
    }
    catch
    {
    result = false;
    }
    finally
    {
    if (workbook != null)
    {
    workbook.Close(true, missing, missing);
    workbook = null;
    }
    if (applicationClass != null)
    {
    applicationClass.Quit();
    applicationClass = null;
    }
    }
    return result;
    } public static bool PPTConvertToPDF(string sourcePath, string targetPath)
    {
    bool result;
    PpSaveAsFileType ppSaveAsFileType = PpSaveAsFileType.ppSaveAsPDF;//转换成pdf
    object missing = Type.Missing;
    Microsoft.Office.Interop.PowerPoint.ApplicationClass application = null;
    Presentation persentation = null;
    try
    {
    application = new Microsoft.Office.Interop.PowerPoint.ApplicationClass();
    persentation = application.Presentations.Open(sourcePath, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse);
    if (persentation!=null)
    {
    persentation.SaveAs(targetPath, ppSaveAsFileType, MsoTriState.msoTrue);
    }
    result = true;
    }
    catch
    {
    result = false;
    }
    finally
    {
    if (persentation != null)
    {
    persentation.Close();
    persentation = null;
    }
    if (application != null)
    {
    application.Quit();
    application = null;
    }
    }
    return result;
    }

2.单独的Excel2PDF(Word2PDF同理,不过PPT2PDF就要采用上面的方法)


1.在后台添加using

using Microsoft.Office.Interop.Excel;
2.在项目中添加引用
3.添加如下代码
  1. public static bool ExcelToPdf(string sourcePath, string targetPath)
    {
    bool result = false;
    XlFixedFormatType xlTypePDF = XlFixedFormatType.xlTypePDF;//转换成pdf
    object missing = Type.Missing;
    Microsoft.Office.Interop.Excel.ApplicationClass applicationClass = null;
    Workbook workbook = null;
    try
    {
    applicationClass = new Microsoft.Office.Interop.Excel.ApplicationClass();
    string inputfileName = sourcePath;//需要转格式的文件路径
    string outputFileName = targetPath;//转换完成后PDF文件的路径和文件名名称
    XlFixedFormatType xlFixedFormatType = xlTypePDF;//导出文件所使用的格式
    XlFixedFormatQuality xlFixedFormatQuality = XlFixedFormatQuality.xlQualityStandard;//1.xlQualityStandard:质量标准,2.xlQualityMinimum;最低质量
    bool includeDocProperties = true;//如果设置为True,则忽略在发布时设置的任何打印区域。
    bool openAfterPublish = false;//发布后不打开
    workbook = applicationClass.Workbooks.Open(inputfileName, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
    if (workbook!=null)
    {
    workbook.ExportAsFixedFormat(xlFixedFormatType, outputFileName, xlFixedFormatQuality, includeDocProperties, openAfterPublish, missing, missing, missing, missing);
    }
    result = true;
    }
    catch
    {
    result = false;
    }
    finally
    {
    if (workbook != null)
    {
    workbook.Close(true, missing, missing);
    workbook = null;
    }
    if (applicationClass != null)
    {
    applicationClass.Quit();
    applicationClass = null;
    }
    }
    return result;
    }


4.如果遇到”无法嵌入互操作类型“……”,请改用适用的接口“
选中项目中引入的dll,鼠标右键,选择属性,把“嵌入互操作类型”设置为False。
 

PS


1.我在具体项目中使用时发现方法一种引入的DLL可以重复引用(感觉就像引用了没效果一样),而且每次关闭VS2013后重新打开编译工程都失败,都必须再重新引用一次。侥幸PPT转PDF功能用不上,最后选择了方法二。

ASP.NET VS2013 Office 转 PDF的更多相关文章

  1. Asp.Net调用Office组件操作时的DCOM配置 (转)

    Asp.Net调用Office组件操作时的DCOM配置 http://blog.csdn.net/gz775/article/details/6447758 在项目中将数据导出为Excel格式时出现“ ...

  2. java操作office和pdf文件java读取word,excel和pdf文档内容

    在平常应用程序中,对office和pdf文档进行读取数据是比较常见的功能,尤其在很多web应用程序中.所以今天我们就简单来看一下Java对word.excel.pdf文件的读取.本篇博客只是讲解简单应 ...

  3. 记录libreoffice实现office转pdf(适用于windows、linux)

    由于目前的工作跟office打交道比较多,所以才有了此篇blog,需求是实现word转换pdf方便页面展示.之前lz采用的是jacob(仅支持windows)进行转换的,但是现在服务器改成linux显 ...

  4. Confluence 6 Office 和 PDF 文件

    插入一个文件到页面中是能够让你将有用的文件,电子表格,幻灯片或者其他可用的文件在你小组中进行分享的好方法. 针对所有的文件类型,你可以选择以链接方式插入一个文件.缩略图将会对文档的内容进行预览同时可以 ...

  5. openoffice+pdf2swf+FlexPaper在线显示office和pdf

    前提:本人的系统为Ubuntu 13.10 64位系统.本篇是我在配置好环境后一段时间写的,所以操作上可能会有也错误,因此仅供参考. 搜索在线显示office和pdf,最常见的方法就是把都转为swf, ...

  6. Aspose实现Office转PDF (ASP.NET)

    0.添加Aspose的DLL 1.可以直接去官网下载,不过默认是带水印的,如需去除水印可以购买 2.当然也可以在国内的一些下载站下载 3.将Aspose.Cells.dll.Aspose.Words. ...

  7. Asp.net与office web apps的整合

    其实网上有关office web app的整合已经有相关的文章了,典型的是如何整合Office Web Apps至自己开发的系统(一) 和如何整合Office Web Apps至自己开发的系统(二), ...

  8. 【译】Asp.net mvc 使用ITextSharp PDF to HTML (解决img标签问题)

    前言:因项目需求,需要将HTML代码转成PDF.大致上已经实现了,可以是发现使用ITextSharp(我现在的版本是5.5.9)的时候,img标签中的src只能跟绝对路径. 在百度上找了一个上午,有一 ...

  9. ASP.NET调用Office Com组件权限设置

    ASP.NET在调用Office Com组件时,经常会出现权限限制的问题,而出现如下错误: 现通过以下几步设置,可解决上述问题:(1)64位系统中,请在IIS应用程序池集成模式中应启用调用32位应用程 ...

随机推荐

  1. flask入门与发送邮件与QQ邮箱

    前言: 快两个月没写博客了, 原因是懒了, 没有最初写博客那种看到阅读量上涨, 别人给自己文章点赞后的开心. 心态也发生了不少变化. 有机会再来写写. 前两个月我去厦门某公司实习, 本着去厦门玩一玩还 ...

  2. 小甲鱼OD学习第7讲

    这次我们任务是破解这个有日期限制的软件,效果如图所示 我们一边下断点一边调试哪里是NAG窗口的程序,最终我们发现在地址 00489912  处的代码是NAG窗口的代码 通过观察我们把004898E1处 ...

  3. 正负样本比率失衡SMOTE

    正负样本比率失衡SMOTE [TOC] 背景 这几天测试天池的优惠券预测数据在dnn上面会不会比集成树有较好的效果,但是正负样本差距太大,而处理这种情况的一般有欠抽样和过抽样,这里主要讲过抽样,过抽样 ...

  4. 高可用Redis服务架构分析与搭建

    基于内存的Redis应该是目前各种web开发业务中最为常用的key-value数据库了,我们经常在业务中用其存储用户登陆态(Session存储),加速一些热数据的查询(相比较mysql而言,速度有数量 ...

  5. CSS3动画中的animation-timing-function效果演示

    CSS3动画(animation)属性有如下几个: 属性 值 说明 animation-name name 指定元素要使用的keyframes名称 animation-duration time(ms ...

  6. Linux中的 awk查找日志中的相关记录

    假设要在 api.log.201707201830 文件中,(此文件的多个字段数据以不可见字符^A(键盘上按下Ctrl+V+A)分隔),要输出第70个字段: awk -F '^A' '{print $ ...

  7. 织梦搜索页使用arclist标签

    织梦默认不能在搜索页使用arclist标签,我们对代码做一些小改动即可 打开include/arc.searchview.class.php 一.查找代码: require_once(DEDEINC. ...

  8. MYSQL EXPLAIN执行计划命令详解(支持更新中)

    本文来自我的github pages博客http://galengao.github.io/ 即www.gaohuirong.cn 摘要: 本篇是根据官网中的每个一点来翻译.举例.验证的:英语不好,所 ...

  9. javascript同步分页

    目前网上分页的例子比较多,但是对其原理不是很了解,平时用的时候只是拿来调用,今天花了点时间,采用面向对象方式写了一个demo.对其方法做了封装,对外只提供一个调用接口. window.loadPage ...

  10. Spring-mvc 静态资源不拦截

    在Spring-mvc.xml文件中加入这个就可以了 <!-- 用于对静态文件进行解析 --> <mvc:annotation-driven /> <mvc:resour ...