最近在研究企业文档管理,这个是基本上所有企业都需要的软件,当然也是有很多种解决方案。对于企业文档来说,最基本的需求就是独立存储,共享。这种需求只需要建立一个Windows共享文件夹或者架一个Samba服务器即可实现,无法做复杂的权限管理,统计等。另一种方案就是架一个Web应用,比如SharePoint,就可以实现。

既然是WEB应用,进一步的需求是能够在线查看文档,根据用户需求可能不允许下载,不允许打印文档。这一点微软的高级解决方案是使用RMS,能够设置每个用户的打开权限,是否打印等,要求必须是域内,而且只管理Office文件的权限,对txt,pdf就没办法了。另外一个解决方案是在线文档预览,用户在网页中查看文档内容,用户无需拿到原始文档,如果有权限的话,可以允许用户下载文档。这就就是百度文库,豆丁之类的网站的功能。下面来说说怎么实现。

1.文档统一转换为pdf

这里的文档我们要看是什么格式,不同的格式有不同的转换方法。

1.1 Office文档转换pdf

对于Office文档(Word,Excel,PowerPoint),那么可以调用Office提供的COM接口,把文档另存为PDF。这个要求服务器上必须安装Office,同时要注意权限,不然很容易导致在本地调试时可以转换为PDF,但是一旦部署到服务器上去就不行。另外还需要注意的是,如果Office转换pdf时发生异常,可能导致Office的进程驻留在服务器,不断驻留Office进程会导致服务器资源耗尽。

这是Office文档转换为pdf的代码:

//将word文档转换成PDF格式public static bool ConvertWord2Pdf(string sourcePath, string targetPath)

{

bool result;

Word.WdExportFormat exportFormat= Word.WdExportFormat.wdExportFormatPDF;

object paramMissing = Type.Missing;

Word.Application wordApplication = new Word.Application();

Word.Document wordDocument = null;

try

{

object paramSourceDocPath = sourcePath;

string paramExportFilePath = targetPath;

Word.WdExportFormat paramExportFormat = exportFormat;

Word.WdExportOptimizeFor paramExportOptimizeFor =

Word.WdExportOptimizeFor.wdExportOptimizeForPrint;

Word.WdExportRange paramExportRange = Word.WdExportRange.wdExportAllDocument;

int paramStartPage = 0;

int paramEndPage = 0;

Word.WdExportItem paramExportItem = Word.WdExportItem.wdExportDocumentContent;

Word.WdExportCreateBookmarks paramCreateBookmarks =

Word.WdExportCreateBookmarks.wdExportCreateWordBookmarks;

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, false,

paramExportOptimizeFor, paramExportRange, paramStartPage,

paramEndPage, paramExportItem, true,

true, paramCreateBookmarks, true,

true, false,

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;

}

//将excel文档转换成PDF格式public static bool ConvertExcel2Pdf(string sourcePath, string targetPath)

{

bool result;

object missing = Type.Missing;

Excel.XlFixedFormatType targetType= Excel.XlFixedFormatType.xlTypePDF;

Excel.Application application = null;

Excel.Workbook workBook = null;

try

{

application = new Excel.Application();

object target = targetPath;

workBook = application.Workbooks.Open(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;

}

//将ppt文档转换成PDF格式public static bool ConvertPowerPoint2Pdf(string sourcePath, string targetPath)

{

bool result;

PowerPoint.PpSaveAsFileType targetFileType= PowerPoint.PpSaveAsFileType.ppSaveAsPDF;

PowerPoint.Application application = null;

PowerPoint.Presentation persentation = null;

try

{

application = new PowerPoint.Application();

persentation = application.Presentations.Open(sourcePath, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse);

persentation.SaveAs(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;

}

1.2 纯文本转换pdf

如果是文本需要转换为PDF,我们可以使用iTextSharp这个组件,对于纯文本,注意的是源文件中没有设置字体之类的,需要在转换成PDF时指定字体,否则对于中文可能由于没有设置字体而转换不出来。

//将Txt转换为PDF       public static bool ConvertText2Pdf(string sourcePath, string targetPath)

{

var text = FileHelper.ReadTextFile(sourcePath);

Document document = new Document(PageSize.A4);

try

{

//step 2:创建一个writer用于监听Document以及通过PDF-stream指向一个文件               PdfWriter.GetInstance(document, new FileStream(targetPath, FileMode.Create));

//step 3: 打开document               document.Open();

var f = GetFont();

//step 4: 添加一段话到document中               document.Add(new Paragraph(text, f));

}

catch (Exception ex)

{

return false;

}

finally

{

if (document.IsOpen())

//step 5: 关闭document                   document.Close();

}

return true;

}

private static Font GetFont()

{

var fontPath = (string) ConfigurationManager.AppSettings["FontPath"];

if (string.IsNullOrEmpty(fontPath))//没有指定字体就用楷体           {

var fontName = "楷体";

if (!FontFactory.IsRegistered(fontName))

{

fontPath = Environment.GetFolderPath(Environment.SpecialFolder.Windows) + @"\Fonts\simkai.ttf";

FontFactory.Register(fontPath);

}

return FontFactory.GetFont(fontName, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

}

BaseFont bfChinese = BaseFont.CreateFont(fontPath,BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);

Font fontChinese = new Font(bfChinese, 16f, Font.NORMAL);

return fontChinese;

}

1.3 HTML转换pdf

HTML中包含的元素较多,比较复杂,主要有两种方法,一种是调用浏览器的接口,让浏览器把HTML打印为PDF,另外就是ITextSharp提供了专门的XML/HTML转换组件:XML Worker,这个已经独立出来,不包含在ITextSharp中,需要单独下载。

public static bool ConvertHtml2Pdf(string text, string pdfPath)

{

Document document = new Document(PageSize.A4);

try

{

PdfWriter.GetInstance(document, new FileStream(pdfPath, FileMode.Create));

document.Open();

var fontName = "楷体";

if (!FontFactory.IsRegistered(fontName))

{

var fontPath = Environment.GetFolderPath(Environment.SpecialFolder.Windows) + @"\Fonts\simkai.ttf";

FontFactory.Register(fontPath);

}

var elements = iTextSharp.tool.xml.XMLWorkerHelper.ParseToElementList(text, @"body {

font-size: 16px;

color: #F00;

font-family: 楷体;

}");

//iTextSharp.text.                foreach (var element in elements)

{

document.Add(element);

}

}

catch (DocumentException de)

{

Console.Error.WriteLine(de.Message);

}

catch (IOException ioe)

{

Console.Error.WriteLine(ioe.Message);

}

document.Close();

return true;

}

1.4添加水印

以上都是转换成pdf的功能,在转换后,我们可以进一步使用ITextSharp对pdf进行加工,比较常见的添加水印功能。其实就是做一个淡淡的背景透明的图片,然后打开pdf文件,在每一页中画上水印图片即可。

//添加水印//源PDF文件路径///加水印后的PDF路径///水印图片的路径//public static bool AddWatermark(string inputPath, string outputPath, string watermarkPath, ref string error)

{

try

{

PdfReader pdfReader = new PdfReader(inputPath);

int numberOfPages = pdfReader.NumberOfPages;

FileStream outputStream = new FileStream(outputPath, FileMode.Create);

PdfStamper pdfStamper = new PdfStamper(pdfReader, outputStream);

PdfContentByte waterMarkContent;

iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(watermarkPath);

image.SetAbsolutePosition(10, 10);

for (int i = 1; i <= numberOfPages; i++)

{

waterMarkContent = pdfStamper.GetUnderContent(i);

waterMarkContent.AddImage(image);

}

pdfStamper.Close();

pdfReader.Close();

outputStream.Close();

return true;

}

catch (Exception ex)

{

error = ex.StackTrace;

return false;

}

}

2.在线预览pdf文档

前面已经统一转换为pdf文档,接下来就是对pdf的在线预览。这个在以前是不现实的,现在有了HTML5,只要浏览器支持HTML5就可以使用pdf.js库,将服务器上的pdf文件转换成HTML5代码展示在浏览器上。另外还有一个解决方案是使用Flash,需要把pdf文件进一步转换为swf文件,然后由Flash播放器来播放这个文档。可惜Flash已经是一个过时即将淘汰的技术了,像iPad,iPhone就不支持Flash,所以使用HTML5才是更明智的选择。

pdf.js网站已经提供了库和示例,浏览页面是http://mozilla.github.io/pdf.js/web/viewer.html,我们要打开我们转换的文件,只需要在URL中添加参数即可: /web/viewer.html?file=yourpdf.pdf

我们可以进一步修改viewer.html中的代码,根据需求去掉下载,打印等按钮,禁止用户下载和打印文件。

wps在线预览接口_文档在线预览的实现相关推荐

  1. wps在线预览接口_WPS文档在线预览接入的一点心得

    花了大半天时间接入WPS文档在线预览功能,还算比较顺利. 原来的OA系统一直用到文档在线预览功能,之前是用微软+officeweb365来实现的.一直感觉不太理想.昨天无意间见到金山WPS开放了在线文 ...

  2. java腾讯滤镜接口_文档中心

    滤镜 简介 HI,您好,欢迎使用腾讯AI开放平台图片滤镜特效API接口服务. 本文档主要针对需要集成HTTP API的技术研发工程师,详细描述图片滤镜特效能力相关的技术内容. 如果您对文档内容有任何疑 ...

  3. 绘制四叶玫瑰线matlab,数学实验_word文档在线阅读与下载_文档网

    成都学院二零一三到二零一四学年第二学期半期考试 <数学实验>课程考试题 (120分钟) 考试形式:闭卷 考试日期: 年 月 日 所有答案一律写在答题纸上,写在试卷上无效. 一.单项选择题( ...

  4. linux 源码 在线浏览,Linux下实现文档在线浏览

    此次Linux下实现在线浏览功能已投入生产环境,生产环境是Centos6.4. 1.下载jodconverter压缩包,将相应jar包添加到工程目录下. 在openoffice下的RPMS目录下执行r ...

  5. 文档在线化管理系统Confluce使用

    一.目的 减少技术文档分散.碎片化.难沉淀问题.消除研发过程中各种word,excel文档多办版本混乱的局面. 二.工具 Atlassian Confluence在线文档系统. 三.confluce功 ...

  6. wps在线预览接口_金山文档在线编辑 - 快速接入 - 《WPS开放平台技术文档》 - 书栈网 · BookStack...

    快速接入 一.申请和上线流程如下: 1.申请Appid和SecretKey 需要前往https://open.wps.cn 注册服务商,并且申请开通金山文档在线编辑服务. 2.实现回调接口 根据本文档 ...

  7. project甘特图导出图片_云盒子预览升级,新增WPS、Visio、Project文档在线预览

    在线预览是企业云盘的一个重要功能.通过企业云盘提供的文件转换服务,将不同类型文档格式转换为可以直接预览的格式,不需要安装任何插件在线查看文件,为用户提供极大便捷,在线预览已经逐渐变成用于查阅文档的使用 ...

  8. Office文档在线预览接口服务器

    现在的Office文档在线预览基本都是收费的,但这个功能几乎在所有软件系统中都会有这个需求,微软有一个Office online是免费的,但是安装跟配置非常复杂,可以说用难度5颗星来形容,有没有一个更 ...

  9. 怎么在线预览.doc,.docx,.ofd,.pdf,.wps,.cad文件以及Office文档的在线解析方式。

    Office文档在线预览大全,OFD文件在线预览 JavaScript文件在线预览 Office文档在线预览大全,OFD文件在线预览 前言 一. 什么是office文档在线预览解析? 二.预览流程分析 ...

最新文章

  1. 极客新闻——09、如何打造核心骨干团队
  2. 一步步学习 Spring Data 系列之JPA(一)
  3. xlst 解析 html c,怎樣實現利用xslt把xml文件內容顯示到html文件中?急!
  4. 系统架构面临的三大挑战,看 Kubernetes 监控如何解决?
  5. java getstringarray_Java AnnotationAttributes.getStringArray方法代碼示例
  6. 【三维激光扫描】实验01:环境搭建CAD2014+StonexSiScan软件安装
  7. python try语句相关(try/except/else/finally)
  8. 802.1X基本配置
  9. mysql 配置自动截断_MySql超长自动截断实例详解
  10. linux nginx django,如何在Linux下使用Nginx部署Django项目
  11. Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.8.2:deploy
  12. 抓包工具_Charles使用
  13. 版本控制工具-Git
  14. 汉字笔画动图怎么做_汉字笔画动态图软件 汉字笔画大全
  15. 征集开始!2022年湖北省人工智能 大数据十大优秀应用案例征集申报时间要求及征集范围、注意事项
  16. arm应用程序之文件读写操作差异open与fopen
  17. X509数字证书格式
  18. 【NIO】解读 java.nio.channels.Channel
  19. 1-2 经济学发展史、实证/规范经济学
  20. [微信小程序]云服务器上传图片或视频

热门文章

  1. 信号与系统——初识到理解(第二章——信号与系统)
  2. CLO如何在其软件结构中集成V-RAY
  3. 多个域名对应一个ip的解决办法
  4. OI国家队集训论文集
  5. vscode在html看到图片的插件_利用花瓣插件 下载高清大图
  6. zigzag convert
  7. 99条为人处事经典法则剩下的一条由你自己来感悟
  8. IdentityServer4 获取Token及刷新Token
  9. 2.开关电源中常见的控制算法
  10. Android美化之全局透明背景