word,excel,ppt转Pdf,Pdf转Swf,通过flexpaper+swftools实现在线预览
- 其实这是我好几年前的项目,现在再用这种方式我也不建议了,毕竟未来flash慢慢会淘汰,此方式也是因为目测大部分人都装了flash,才这么做的,但是页面展示效果也不好。其实还是考虑收费的控件,毕竟收费的还是做的不错的!
而且此方式服务器要安装office组件,项目实施也不好!
以上为最新更新建议!
- 自己上网查了好多种方法,最后还是选了这种不收费,还挺好用的方法
为了用户有一个好的体验我将word、excel、ppt转Pdf,Pdf转Swf写在服务上,因为我当时做的时候Pdf转Swf会执行pdf2swf.exe弹出黑框,对用户体验不好,还有就是ppt转swf时会有一个弹出框,提示正在发布,我没搞明白,就把他们都写在服务上了。
- Office2Pdf
/// <summary>
/// Office2Pdf 将Office文档转化为pdf
/// </summary>
public class Office2PDFHelper
{
public Office2PDFHelper()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
#region 1.0 Word转换成pdf + bool DOCConvertToPDF(string sourcePath, string targetPath)
/// <summary>
/// Word转换成pdf
/// </summary>
/// <param name="sourcePath">源文件路径(物理路径)</param>
/// <param name="targetPath">目标文件路径(物理路径)</param>
/// <returns>true=转换成功</returns>
public static bool DOCConvertToPDF(string sourcePath, string targetPath)
{
// string targetPath = "G:\\工作\\ceshi\\ceshi.pdf";
// sourcePath = "G:\\工作\\OfficePreview\\Preview\\SourceFile\\测试.doc";
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 = ;
int paramEndPage = ;
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;
}
#endregion #region 2.0 把Excel文件转换成PDF格式文件 + 把Excel文件转换成PDF格式文件
/// <summary>
/// 把Excel文件转换成PDF格式文件
/// </summary>
/// <param name="sourcePath">源文件路径(物理路径)</param>
/// <param name="targetPath">目标文件路径(物理路径)</param>
/// <returns>true=转换成功</returns>
public static 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;
}
#endregion #region 3.0 把PowerPoint文件转换成PDF格式文件 + bool PPTConvertToPDF(string sourcePath, string targetPath)
///<summary>
/// 把PowerPoint文件转换成PDF格式文件
///</summary>
///<param name="sourcePath">源文件路径(物理路径)</param>
///<param name="targetPath">目标文件路径(物理路径)</param>
///<returns>true=转换成功</returns>
public static 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);
if (persentation != null)
{
//写入
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;
}
#endregion }
- PDF2SwfHelper
#region 1.0 转换所有的页,图片质量100% + bool PDF2SWF(string pdfPath, string swfPath)
/// <summary>
/// 转换所有的页,图片质量100%
/// </summary>
/// <param name="pdfPath">PDF文件地址</param>
/// <param name="swfPath">生成后的SWF文件地址</param>
public static bool PDF2SWF(string pdfPath, string swfPath)
{
return PDF2SWF(pdfPath, swfPath, , GetPageCount(pdfPath), );
}
#endregion #region 2.0 转换前N页,图片质量100% + bool PDF2SWF(string pdfPath, string swfPath, int page)
/// <summary>
/// 转换前N页,图片质量100%
/// </summary>
/// <param name="pdfPath">PDF文件地址</param>
/// <param name="swfPath">生成后的SWF文件地址</param>
/// <param name="page">页数</param>
public static bool PDF2SWF(string pdfPath, string swfPath, int page)
{
return PDF2SWF(pdfPath, swfPath, , page, );
}
#endregion #region 3.0 PDF格式转为SWF + bool PDF2SWF(string pdfPath, string swfPath, int beginpage, int endpage, int photoQuality)
/// <summary>
/// PDF格式转为SWF
/// </summary>
/// <param name="pdfPath">PDF文件地址</param>
/// <param name="swfPath">生成后的SWF文件地址</param>
/// <param name="beginpage">转换开始页</param>
/// <param name="endpage">转换结束页</param>
private static bool PDF2SWF(string pdfPath, string swfPath, int beginpage, int endpage, int photoQuality)
{ //swftool,首先先安装,然后将安装目录下的东西拷贝到相应目录下
string exe = System.IO.Path.GetFullPath("pdf2swf.exe");
//string exe = System.Web.Hosting.HostingEnvironment.MapPath("~");
//pdfPath = HttpContext.Current.Server.MapPath(pdfPath);
//swfPath = HttpContext.Current.Server.MapPath(swfPath);
if (!System.IO.File.Exists(exe) || !System.IO.File.Exists(pdfPath))
{
return false;
}
StringBuilder sb = new StringBuilder();
sb.Append(" \"" + pdfPath + "\"");
sb.Append(" -o \"" + swfPath + "\"");
sb.Append(" -s flashversion=9");
if (endpage > GetPageCount(pdfPath)) endpage = GetPageCount(pdfPath);
sb.Append(" -p " + "\"" + beginpage + "" + "-" + endpage + "\"");
sb.Append(" -j " + photoQuality);
string Command = sb.ToString();
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = exe;
p.StartInfo.Arguments = Command;
p.StartInfo.WorkingDirectory = System.IO.Path.GetFullPath("../Release/");
string ss = p.StartInfo.WorkingDirectory;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = false;
p.Start();
p.BeginErrorReadLine();
p.WaitForExit();
p.Close();
p.Dispose();
return true;
} #endregion #region 4.0 返回页数 + int GetPageCount(string pdfPath)
/// <summary>
/// 返回页数
/// </summary>
/// <param name="pdfPath">PDF文件地址</param>
private static int GetPageCount(string pdfPath)
{
byte[] buffer = System.IO.File.ReadAllBytes(pdfPath);
int length = buffer.Length;
if (buffer == null)
return -;
if (buffer.Length <= )
return -;
string pdfText = Encoding.Default.GetString(buffer);
System.Text.RegularExpressions.Regex rx1 = new System.Text.RegularExpressions.Regex(@"/Type\s*/Page[^s]");
System.Text.RegularExpressions.MatchCollection matches = rx1.Matches(pdfText);
return matches.Count;
}
#endregion
- flexpaper+swftools
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="OfficePreview.aspx.cs" Inherits="Preview.OfficePreview" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!-- saved from url=()about:internet -->
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
<script src="js/swfPreview/jquery.js" type="text/javascript"></script>
<script src="js/swfPreview/flexpaper_flash_debug.js" type="text/javascript"></script>
<script src="js/swfPreview/flexpaper_flash.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
var winWidth = ;
var winHeight = ;
function findDimensions() //函数:获取尺寸
{
////获取窗口宽度
//if (window.innerWidth)
// winWidth = window.innerWidth;
//else if ((document.body) && (document.body.clientWidth))
// winWidth = document.body.clientWidth; //获取窗口高度
if (window.innerHeight)
winHeight = window.innerHeight;
else if ((document.body) && (document.body.clientHeight))
winHeight = document.body.clientHeight;
//通过深入Document内部对body进行检测,获取窗口大小 && document.documentElement.clientWidth
if (document.documentElement && document.documentElement.clientHeight) {
winHeight = document.documentElement.clientHeight;
//winWidth = document.documentElement.clientWidth;
}
//高度
$("#viewerPlaceHolder").height(winHeight);
}
findDimensions();
//调用函数,获取数值
window.onresize = findDimensions;
})
</script>
<style type="text/css">
body
{
margin-left: 0px;
margin-top: 0px;
margin-bottom: 0px;
margin-right: 0px;
height: %;
} #div1
{
margin: 0px auto;
/*border: 0px solid #0094ff;*/
width: %;
height: %;
} #viewerPlaceHolder
{
/*height: 598px;*/
}
</style>
</head>
<body style="overflow-y: hidden">
<div id="center">
<a id="viewerPlaceHolder" style="display: block;"></a>
<script type="text/javascript">
$(function () {
$("#pageloading").show();
var fp = new FlexPaperViewer(
'js/swfPreview/FlexPaperViewer',
'viewerPlaceHolder',
{
config: {
SwfFile: escape('TargetFile/Swf/swf.swf'),//这里是要显示的swf的位置,相对根目录
Scale: ,//缩放比例
ZoomTransition: 'easeOut',//Flexpaper中缩放样式,它使用和Tweener一样的样式,默认参数值为easeOut.其他可选值包括: easenone, easeout, linear, easeoutquad
ZoomTime: 0.5,//从一个缩放比例变为另外一个缩放比例需要花费的时间,该参数值应该为0或更大。
ZoomInterval: 0.2,//缩放比例之间间隔,默认值为0.1,该值为正数。
FitPageOnLoad: false,//初始化的时候自适应页面,与使用工具栏上的适应页面按钮同样的效果。
FitWidthOnLoad: false,//初始化的时候自适应页面宽度,与工具栏上的适应宽度按钮同样的效果。
PrintEnabled: false,//是否支持打印
FullScreenAsMaxWindow: false,//是否支持打印
ProgressiveLoading: false,//当设置为true的时候,展示文档时不会加载完整个文档,而是逐步加载,但是需要将文档转化为9以上的flash版本(使用pdf2swf的时候使用-T 9 标签)。
MinZoomSize: 0.2,//最小的缩放比例。
MaxZoomSize: ,//设置最大的缩放比例。
SearchMatchAll: false,//设置为true的时候,单击搜索所有符合条件的地方高亮显示。
InitViewMode: 'TwoPage',//启动模式,如”Portrait” or “TwoPage”.
ViewModeToolsVisible: false,//工具栏上是否显示样式选择框(就是显示缩略图或分页显示的工具)
ZoomToolsVisible: true,//工具栏上是否显示缩放工具
NavToolsVisible: false,//工具栏上是否显示导航工具(也就是页码工具)
CursorToolsVisible: true,//工具栏上是否显示光标工具
SearchToolsVisible: true,//工具栏上是否显示搜索
localeChain: 'zh_CN'//语言
}
}
);
$("#pageloading").fadeOut();
});
</script>
</div>
<div id="pageloading" style="background-color: White; width: 100%; height: 100%; position: fixed; top: 0; left: 0; display: none; text-align: center; margin: 0px auto; vertical-align: middle; z-index: 999">
<img src="/images/loading.gif" style="width: 48px; height: 48px; position: fixed; top: 45%; left: 45%; right: 50%; bottom: 50%;" />
</div>
<div id="txtPreview" style="width: 100%; height: 100%; top: 0; left: 0;">
<pre id="pre"></pre>
</div>
</body>
</html>
word,excel,ppt转Pdf,Pdf转Swf,通过flexpaper+swftools实现在线预览的更多相关文章
- java 如何将 word,excel,ppt如何转pdf --openoffice (1)
承上启下,可折叠 上一篇说的是:服务器是windows server时,用jacob将msoffice(指的是word,excel,ppt)转换成pdf. 若被部署项目的服务器是centOS等linu ...
- .net mvc使用FlexPaper插件实现在线预览PDF,EXCEL,WORD的方法
FlexPaper插件可以实现在浏览器中在线预览pdf,word,excel等. 在网上看到很多关于这个插件实现预览的技术,但是很难做到word和excel在线预览. pdf很好实现. 首先下载相关的 ...
- word,excel,ppt,txt转换为 PDF
/// <summary> /// 将word文档转换成PDF格式 /// </summary> /// <param name="sourcePath&quo ...
- windows环境下 php 将office文件(word/excel/ppt)转化为pdf(转)
将office文件转化为pdf的方法有 1.利用openoffice提供的服务 (比较简单,但是转化的效果不太好) 2.使用office提供的服务 (注:这在windows服务器上,并且服务器上面安装 ...
- php 将office文件(word/excel/ppt)转化为pdf(windows和linux只要安装对应组件应该就行)
一.配置环境 (1)配置php.ini 添加:extension=php_com_dotnet.dll com.allow_dcom = true // 去掉号,改为true 重启环境 (2) 安装 ...
- PDF/WORD/EXCEL/PPT 文档在线阅读
查资料看了2种解决方法: 1.通过办公软件dll转换,用flans去看 2.通过Aspose转换成pdf格式,在用js前台读pdf(我用的pdf.js) 今天我解决的就是WORD/EXCEL/PPT ...
- Java通过openOffice实现word,excel,ppt转成pdf实现在线预览
Java通过openOffice实现word,excel,ppt转成pdf实现在线预览 一.OpenOffice 1.1 下载地址 1.2 JodConverter 1.3 新建实体类PDFDemo ...
- java实现在线预览--poi实现word、excel、ppt转html
java实现在线预览 - -之poi实现word.excel.ppt转html 简介 java实现在线预览功能是一个大家在工作中也许会遇到的需求,如果公司有钱,直接使用付费的第三方软件或者云在线预览服 ...
- java 如何将 word,excel,ppt如何转pdf--jacob
问题:java 如果将 word,excel,ppt如何转pdf 我个人的观点:windows server下用 jacob; linux server下 用openoffice. PS:1.本文 ...
随机推荐
- vue 实现 换一换 功能
点击按钮列表页随机获取三个商品并渲染 后台返回的数据为 d为一个数组 数组 arr=[0,1,2]初始值 data:{ list:d, arr:[0,1,2] } 生产随机数 replace:func ...
- HashMap实现原理
学习笔记之HashMap篇,简单学习了解HashMap的实现原理和扩容. 大家都知道HashMap处理数据很快,时间复杂度O(1),那么是怎么做到的呢?那就先了解一下常见数据结构. 一般来说,我们把存 ...
- codesmith连接Mysql提示“找不到请求的 .Net Framework Data Provider。可能没有安装。"
1,首先需要将MySql.Data.dll复制到codesmith安装目录下bin文件夹下,注意dll的版本 2,其次因为codesmith7采用的是.net4.0的配置文件,(64位系统)找到C:\ ...
- WPF DataGrid绑定一个组合列
WPF DataGrid绑定一个组合列 前台: <Page.Resources> <local:InfoConverter x:Key="converter& ...
- 多线程(RunLoop)
1.RunLoop的概念及作用 2.RunLoop的使用 3.RunLoop的相关类 4.RunLoop的工作原理 5.小结 6.思考 什么是RunLoop? 从字面意思上是一直循环跑,事实上就是一个 ...
- JS 巧用 && 与 ||
在对于流程控制语句当中,我们最熟悉不过的就是 if (条件){ //代码块 }else{ //代码块 } 对于一个执行不同的代码来说,如果执行的代码很多,可能就有必要使用上面这种方式 但往往我们开发当 ...
- 用Eclipse 搭建一个Maven Spring SpringMVC 项目
1: 先创建一个maven web 项目: 可以参照之前的文章: 用Maven 创建一个 简单的 JavaWeb 项目 创建好之后的目录是这样的; 2: 先配置maven 修改pom.xml & ...
- ViewPager使用记录2——展示动态数据
ViewPager是v4支持库中的一个控件,相信几乎所有接触Android开发的人都对它不陌生.之所以还要在这里翻旧账,是因为我在最近的项目中有多个需求用到了它,觉得自己对它的认识不够深刻.我计划从最 ...
- yum fastermirror插件屏蔽一些国内源
最近被yum上hust源的问题恶心的受不了了,真不明白这种源还活着有什么意义,干脆关了得了,省得恶心人,经常Errno 14,404not found,去网页一看,好家伙,提示404的xml.gz路径 ...
- java 上传文件-生成文件首页缩略图 生成pdf 抓取图片
方法:1.文件转换成pdf(采用openoffice或者jacob) 2.抓取pdf首页图 第一步:采用jacob: a.下载jacob 注意区分32位,64位,否则不能用 将dll文件放在ja ...