C# 仿百度文库实现方法(已测试可行)

C# 仿百度文库实现方法(已测试可行)
C# 仿百度文库实现方法(已测试可行)

C# 仿百度文库在线查看功能

资料发布到查看分为三个步骤:

1、上传文件(doc、xls、ppt)转换成Pdf文件

2、 Pdf文件转换成swf文件

3、把swf文件显示出来

第一步:把上传的文件(doc、xls、ppt)转换成Pdf文件。直接在后台用代码转换,新建一个类,这一步没有什么特殊的要求,转换代码如下:

using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using Word = Microsoft.Office.Interop.Word;

using Excel = Microsoft.Office.Interop.Excel;

using PowerPoint = Microsoft.Office.Interop.PowerPoint;

using Microsoft.Office.Core;

using System.Collections.Generic;

using System.Linq;

//注:

//using Word = Microsoft.Office.Interop.Word;

//using Excel = Microsoft.Office.Interop.Excel;

//using PowerPoint = Microsoft.Office.Interop.PowerPoint;

//上面三个引用在添加引用的.NET标签中

// using Microsoft.Office.Core;

//上面这个引用在添加引用COM中的Microsoft Office 12.0 Object Library

///

///Office2Pdf 的摘要说明

///

public class Office2Pdf

{

///

/// Word转换成pdf

///

/// 源文件路径

/// 目标文件路径

/// true=转换成功

public bool DOCConvertToPDF(string sourcePath, string targetPath)

{

bool result = false;

Word.WdExportFormat exportFormat =

Word.WdExportFormat.wdExportFormatPDF;

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;

}

catch

{

result = false;

}

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格式文件

///

/// 源文件路径

/// 目标文件路径

/// true=转换成功

public bool XLSConvertToPDF(string sourcePath, string targetPath)

{

bool result = false;

Excel.XlFixedFormatType targetType = Excel.XlFixedFormatType.xlTypePDF;

object missing = Type.Missing;

Excel.ApplicationClass application = null;

Excel.Workbook workBook = null;

try

{

application = new Excel.ApplicationClass();

object target = targetPath;

object type = targetType;

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;

}

///

/// 把PowerPoint文件转换成PDF格式文件

///

///源文件路径

///目标文件路径

///true=转换成功

public bool PPTConvertToPDF(string sourcePath, string targetPath)

{

bool result;

PowerPoint.PpSaveAsFileType targetFileType =

PowerPoint.PpSaveAsFileType.ppSaveAsPDF;

object missing = Type.Missing;

PowerPoint.ApplicationClass application = null;

PowerPoint.Presentation persentation = null;

try

{

application = new PowerPoint.ApplicationClass();

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

Microsoft.Office.Core.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;

}

}

第二步:Pdf文件转换成swf文件,这里需要借用到swftools这个软件下面的pdf2swf.exe,上网找到swftools-2013-04-09-1007,下载下来安装一下,在安装好的目录如下:

,接下来转换过程中还要用到一个字库来保证转换成功。

准备资料:

1.xpdfbin-win-3.03.zip、xpdf-chinese-simplified.tar.gz

下载地址:https://www.360docs.net/doc/609005688.html,/xpdf/download.html

2.两个中文字体文件:gkai00mp.ttf、Gbsn00lp.ttf

下载地址:

https://www.360docs.net/doc/609005688.html,/p/atyu30/downloads/detail?name=gbsn00lp.ttf.tar.gz&can= 2&q=

https://www.360docs.net/doc/609005688.html,/detail/blackjack2007u/1841186

3.加上PDF2SWF工具https://www.360docs.net/doc/609005688.html,/download.html

下面集中精力解决中文字符的问题。

1.解压缩xpdfbin-win-3.03.zip到指定目录(C:\xpdf)

2.解压缩xpdf-chinese-simplified.tar.gz 到上面的目录下

(C:\xpdf\xpdf-chinese-simplified)

3.拷贝两个字体文件gkai00mp.ttf、Gbsn00lp.ttf到CMap目录下

(C:\xpdf\xpdf-chinese-simplified\CMap)

4.修改C:\xpdf\xpdf-chinese-simplified下的add-to-xpdfrc文件<注意相关路径配置>,如下图:

接下来就是正式的转换了,新建个类,这下面的代码和上面转Pdf的代码所传递的文件路径都为绝对地址,如:D:/aa/aa.doc 等,转换后的路径也一样,把文件名一起写上去。转换代码如下:

using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.Diagnostics;

using System.IO;

using System.Text;

///

///Pdf2Swf 的摘要说明

///

public class Pdf2Swf

{

public void PDFConvertToSWF(string pdfPath, string swfPath)

{

string exe = "D:/swftools/pdf2swf.exe"; //pdf2swf.exe的安装地址

StringBuilder sb = new StringBuilder();

sb.Append(" \"" + pdfPath + "\"");//input

sb.Append(" -o \"" + swfPath + "\"");//output

sb.Append(" -s flashversion=9");//flash version

sb.Append(" -j 100");//SWF中的图片质量

string Command = sb.ToString();

System.Diagnostics.Process p = new System.Diagnostics.Process();

p.StartInfo.FileName = exe;

p.StartInfo.Arguments = Command;

p.StartInfo.WorkingDirectory =

第三步:把swf文件显示出来,这里面要用到FlexPaper,FlexPaper在google code 上的下载地址为https://www.360docs.net/doc/609005688.html,/p/flexpaper/。目前使用的版本为,这个测试过比较稳定FlexPaper_1.5.1_flash.zip。

下载、解压后,可以看到如下的目录:

接下来把这文件复制到你的项目中,把要显示的那个页面新建在这个文件下,不然是不可以显示的,如下图

在head中引入下面那个JS

接着把显示的主体,代码如下

相关主题
相关文档
最新文档