ASP.NET VS2013 Office 转 PDF

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

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 = 0;//起始页码
            int to = 0;//结束页码
            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。

来自为知笔记(Wiz)

时间: 2024-08-24 10:33:07

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

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

在平常应用程序中,对office和pdf文档进行读取数据是比较常见的功能,尤其在很多web应用程序中.所以今天我们就简单来看一下Java对word.excel.pdf文件的读取.本篇博客只是讲解简单应用.如果想深入了解原理.请读者自行研究一些相关源码. 首先我们来认识一下读取相关文档的jar包: 1. 引用POI包读取word文档内容 poi.jar 下载地址 http://apache.freelamp.com/poi/release/bin/poi-bin-3.6-20091214.zip 

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

前提:本人的系统为Ubuntu 13.10 64位系统.本篇是我在配置好环境后一段时间写的,所以操作上可能会有也错误,因此仅供参考. 搜索在线显示office和pdf,最常见的方法就是把都转为swf,然后通过FlexPaper显示.这个方法有缺点,FlexPaper不支持所有浏览器(我只能在chrome中使用,firefox要进行设置) 我用的系统是Ubuntu 13.10 64位 一下所提到的软件,我都会在附件中分享 一.openoffice安装 下载附件中的Apache_OpenOffice

C#打开office文件,pdf文件和视频文件

打开office文件 1 需要从网站下载dsoframer.ocx文件 2 把dsoframer.ocx文件复制到c:\windows\system32目录下 开始->运行->regsvr32 dsoframer.ocx , 系统会提示DSOFramer.ocx中的DllRegisterServer成功 3 在VS中新建项目,添加引用 4 将该控件加载到工具箱(添加新选项) 5 将控件拖到窗体中 打开文件:this.axFramerControl1.Open(filepath); (备注:也有

aspose实现Office转Pdf

aspose实现Office转Pdf关键代码: jar包: aspose-words-14.6.0.jar aspose-cells-10.8.jar aspose.slides-14.4.0.jar aspose-diagram-2.1.0.jar protected void realTransform(InputStream in, OutputStream out) throws IOException     {       String lowerFileName = this.fi

office与pdf之间的相互转换

office大家可能都已运用自如了,可是在当前使用比较流行的pdf格式上,还存在些问题.如:pdf不能编辑怎么办.pdf怎么转换成word,excel,ppt...,今天小编在这里与大家分享一个技巧,即:office与pdf之间的相互转换 方法  1.由于office本身并不具备转换功能,故需借助第三方工具进行office与pdf之间的相互转换了.  PS:第三方工具一般都是收费的,这里小编以在线转换为例,因为其免费. 步骤  1.用百度搜索引擎搜索“pdf转换”,并点击页面进入在线转换网站.

office 转 pdf文件

找过很多地方都找不到用php实现“office 转 pdf文件”的,最后只能用Linux命令行来做了.如下: 主要就是centos+libreoffice+unoconv+Xvfb 1. 安装libreoffice 1 yum install libreoffice 2.安装unoconv 1 yum install http://pkgs.repoforge.org/unoconv/unoconv-0.5-1.el6.rf.noarch.rpm 3.安装Xvfb 1 yum install x

office转pdf windows-linux-java工具类

概述 该文档详细描述了在windows和Linux环境下安装openoffice的全过程以及用java代码实现office转pdf文件的操作,文档中以Apache_OpenOffice_4.1.5_Win_x86_install_zh-CN.exe和Apache_OpenOffice_4.1.3_Linux_x86-64_install-rpm_zh-CN.tar.gz为例. Windows: 1.安装Apache_OpenOffice_4.1.5_Win_x86_install_zh-CN.e

Java程序员从笨鸟到菜鸟之(一百零三)java操作office和pdf文件(一)java读取word,excel和pd

在平常应用程序中,对office和pdf文档进行读取数据是比较常见的功能,尤其在很多web应用程序中.所以今天我们就简单来看一下java对word.excel.pdf文件的读取.本篇博客只是讲解简单应用.如果想深入了解原理.请读者自行研究一些相关源码. 首先我们来认识一下读取相关文档的jar包: 1. 引用POI包读取word文档内容 poi.jar 下载地址 http://apache.freelamp.com/poi/release/bin/poi-bin-3.6-20091214.ziph

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

由于目前的工作跟office打交道比较多,所以才有了此篇blog,需求是实现word转换pdf方便页面展示.之前lz采用的是jacob(仅支持windows)进行转换的,但是现在服务器改成linux显然不能用了,于是网上搜罗一圈,最终决定采用LibreOffice.(前提:需要安装jdk环境) LibreOffice中文官网:https://zh-cn.libreoffice.org/   下载合适的版本,本文下载的是6.1.6 已上传百度网盘(链接: https://pan.baidu.com