.net 实现 office文件转成pdf

jss122414092 2014-02-18 10:02:02
因为公司需求,要求网站内页能显示office文件(doc,xls,ppt)还能显示pdf,最后决定,把office文件都转成pdf统一都用pdf显示,从网上找了好多,但貌似都存在缺陷,如office2007插件,officepage插件,openoffice插件,希望有人做过这个的给点帮助,,最好给个不错的demo
...全文
218 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
天下在我心 2014-02-18
  • 打赏
  • 举报
回复
实现代码: WordToPDF.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Word = Microsoft.Office.Interop.Word;
using Microsoft.Office.Core;

namespace WordToPDFTest
{
    public class WordToPDF
    {
        public WordToPDF()
        { }

        public bool DoWordToPDF(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 = sourcePath;
                string paramExportFilePath = 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;
        }
    }
}
然后是本地文件上传后在服务器上转换成pdf文件

            //首先文件上传至服务器
            FileStream inStream = null;
            FileStream outStream = null;
            //保存到网络服务器的文件路径名
            string file;
            try
            {
                string fileName = localPathFile.Substring(localPathFile.LastIndexOf("\\") + 1);
                //读取本地文件流
                inStream = File.OpenRead(localPathFile);
                //保存到网络服务器的文件路径名
                file = Server.MapPath("/upload/") + fileName;
                outStream = new FileStream(file, FileMode.OpenOrCreate, FileAccess.Write);
                byte[] bytes = new byte[4096];
                int start = 0;
                int length;
                while ((length = inStream.Read(bytes, 0, 4096)) > 0)
                {
                    outStream.Write(bytes, 0, length);
                    start += length;
                }
            }
            catch (Exception e)
            {
                throw new Exception("错误出现在:" + e.Message);
            }
            finally
            {
                inStream.Close();
                outStream.Close();
            }
            //然后把word文件转化成pdf          

            Word.WdExportFormat wd = Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF;
            WordToPDF wtp = new WordToPDF();
            //在本地转换成pdf文件
            string targetFilename = Server.MapPath("/download/") + Guid.NewGuid().ToString() + ".pdf";
            wtp.DoWordToPDF(file, targetFilename, wd);
targetFilename 就是服务器上转换后的pdf文件完整路径名,下载方法略 localPathFile 本地word文件完成路径名
jss122414092 2014-02-18
  • 打赏
  • 举报
回复
求办法帖子别死啊
天下在我心 2014-02-18
  • 打赏
  • 举报
回复
引用 3 楼 jss122414092 的回复:
[quote=引用 2 楼 zx75991 的回复:] 可以用web的方式实现,我去年做过这样的一个例子。 不过需要转换的服务器上要安装office2007或以上版本并下载微软的一个插件,需要注册com+组件服务
这个貌似word样式加载不过来。[/quote] 可以实现,是吧本地文件上传到服务器,在服务器上转换成同名的pdf文件,然后再下载回本地。这个是实现思路。
哇哦好崇拜你 2014-02-18
  • 打赏
  • 举报
回复
很多插件是可以转,都有缺陷,可以用的插件都要钱,坑
jss122414092 2014-02-18
  • 打赏
  • 举报
回复
引用 1 楼 liuchaolin 的回复:
一般都是直接转成flash来显示,然后就是手工转和自动转的问题了,手工转比较好实现,自动转配置比较繁琐
你说的好像是flashpage插件,有弊端,统一时间只能转换一个文件,如果多个人通过后台转换会报错,
jss122414092 2014-02-18
  • 打赏
  • 举报
回复
引用 2 楼 zx75991 的回复:
可以用web的方式实现,我去年做过这样的一个例子。 不过需要转换的服务器上要安装office2007或以上版本并下载微软的一个插件,需要注册com+组件服务
这个貌似word样式加载不过来。
天下在我心 2014-02-18
  • 打赏
  • 举报
回复
可以用web的方式实现,我去年做过这样的一个例子。 不过需要转换的服务器上要安装office2007或以上版本并下载微软的一个插件,需要注册com+组件服务
md5e 2014-02-18
  • 打赏
  • 举报
回复
一般都是直接转成flash来显示,然后就是手工转和自动转的问题了,手工转比较好实现,自动转配置比较繁琐

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧