1 比較老的一種方法了 Aspose.dll+SwfTool+flexpaper
可以實現excel表格 world文檔 pdf文件的在線預覽。
首先利用Aspose將文件轉換為pdf格式。Aspose.Words是一款先進的類庫,通過它可以直接在各個應用程序中執行各種文檔處理任務。它里面包含了幾個主要的可以操作不同格式文件的dll組件,我們這里主要使用了Aspose.worlds和Aspose.cells。其實它們能實現更為復雜的文件生成和操作,但是我這里只使用了它們的文件格式轉換功能。我下載的是aspose 的破解版本。把這兩個dll引到自己的項目里。寫一個方法用來轉換格式。
public class AsPoseHelper
{
//將word文檔轉換成pdf
public static bool GetPdfFromWord(string soursefilepath, string outpdfpath)
{
//讀取word文檔
try
{
using (System.IO.Stream stream = new System.IO.FileStream(soursefilepath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
Aspose.Words.Document doc = new Aspose.Words.Document(soursefilepath);
doc.Save(outpdfpath, Aspose.Words.SaveFormat.Pdf);
return true;
}
}
catch (Exception ex)
{
return false;
}
}
//excel文檔轉換問題
public static bool GetPdfFromExcel(string soursefilepath, string outpdfpath)
{
try
{
using (System.IO.Stream stream = new System.IO.FileStream(soursefilepath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
Aspose.Cells.Workbook workbook = new Workbook(stream);
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
pdfSaveOptions.Compliance = PdfCompliance.PdfA1b;
workbook.Save(outpdfpath, Aspose.Cells.SaveFormat.Pdf);
return true;
}
}
catch (Exception ex)
{
return false;
}
}
}
然后,將轉換成pdf格式的文件轉成swf格式,這里我使用的是swftool。里面的pdf2swf.exe,這個也是有別的相互轉換的功能,但是我這里只用了這個exe。
public static bool GetSwfFromPdf(string pdfpath, string swfpath)
{
try
{
string exepath = HttpContext.Current.Server.MapPath("~/exe/pdf2swf.exe");
StringBuilder sb = new StringBuilder();
sb.Append("\"" + pdfpath + "\"");//輸入位置
sb.Append(" -T 9");//flash版本
sb.Append(" -o " + "\"" + swfpath + "\"");//輸出位置
sb.Append(" -j 100");//生成flash的質量
if (!File.Exists(exepath))
{
//exe程序 不存在
return false;
}
else
{
//拼接cmd命令行
string command = "cmd.exe /C " + Path.GetFileName(exepath) + " " + sb.ToString();
using (Process process = new Process())
{
ProcessStartInfo startinfo = new ProcessStartInfo();
startinfo.FileName = "cmd.exe";
startinfo.Arguments = command;
startinfo.WorkingDirectory = Path.GetDirectoryName(exepath);
process.StartInfo = startinfo;
process.Start();
process.WaitForExit();
if (File.Exists(HttpContext.Current.Server.MapPath(swfpath)))//表明寫入成功
{
return true;
}
else
{
return false;
}
}
}
}
catch (Exception ex)
{
return false;
}
}
這里是先拼接好轉換命令再使用cmd調用pdf2swf.exe進行轉換。
最后,使用flexpaper把生成的swf文件在頁面上顯示。到flexpaper的官網上下載js文件。在你的頁面里面把<script src="~/Scripts/js/flexpaper.js"></script><link href="~/Scripts/css/flexpaper.css" rel="stylesheet" />這兩個引進來。swffile的地址是上面生成的swf文件的地址。
<div id="documentViewer" class="flexpaper_viewer" style="position:relative;width:80%;height:98%"></div>
<script type="text/javascript">
$("#documentViewer").FlexPaperViewer(
{
config: {
SWFFile: @ViewBag.URL,
Scale: 0.6,
ZoomTransition: 'easeOut',
ZoomTime: 0.5,
ZoomInterval: 0.2,
FitPageOnload: true,
FitWidthOnLoad: false,
FullScreenAsMaxWindow: false,
ProgressiveLoading: false,
MinZoomSize: 0.2,
MaxZoomSize: 5,
SearchMatchAll: false,
// Toolbar: toolbarData,
//BottomToolbar: 'UI_flexpaper_annotations.html',
InitViewMode: 'Portrait',
RenderingOrder: 'flash',
StartAtPage: '',
ViewModeToolsVisible: true,
ZoomToolsVisible: true,
NavToolsVisible: true,
CursorToolsVisible: true,
SearchToolsVisible: true,
WMode: 'window',
localeChain: 'en_US'
}
}
);
</script>
2 不把pdf文件轉換成swf,aspose+pdfobject.js
pdfobject.js是在網頁中顯示pdf文件的插件,非常簡單好用。
首先在官網上下載文件 https://pdfobject.com/ 將js文件引用到你的頁面中。
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<script src="~/pdfobject/pdfobject.js"></script>
<style type="text/css">
.pdfobject-container {
height: 900px;
}
.pdfobject {
border: 1px solid #666;
}
</style>
<div id="exm">
</div>
<script type="text/javascript">
var url = "./Content/test.pdf";
PDFObject.embed(url, "#exm");
</script>
然后就可以了。
3 aspose+pdf.js
