Spire.Doc系列教程(12):使用 Spire.PDFViewer for ASP.NET 在 Web From 上查看 PDF 文件
Spire.PDFViewer for ASP.NET 用于在ASP.NET Web Form上加載查看PDF文件,并能指定跳轉(zhuǎn)的頁面,在適應(yīng)頁面寬度或高度下查看文件,縮放頁面等等。下面介紹使用的詳細(xì)步驟:
第一步,創(chuàng)建一個(gè)web程序,把Spire dlls引用到項(xiàng)目中。

第二步,把控件添加到工具箱上,點(diǎn)擊工具箱,右鍵選擇添加Tab,給控件取名為Spire.PDFViewer

點(diǎn)擊Choose Items

選擇瀏覽,找到Spire.PdfViewer.Asp.dll,選中它然后點(diǎn)擊OK。

完成添加,控件出現(xiàn)在工具箱上。

點(diǎn)擊WebForm.aspx,選擇視圖設(shè)計(jì)器,將PdfViewer控件拖動(dòng)到Form上,并調(diào)整寬度和高度,將查看的文件放入文件夾Files。

第三步,在WebForm.aspx.cs文件的相應(yīng)位置添加代碼。注意在加載PDF文件前必須添加判斷語句if (!IsPostBack)。
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { //設(shè)置清空緩存的時(shí)間 this.PdfViewer1.CacheInterval = 1200; //設(shè)置緩存時(shí)間 this.PdfViewer1.CacheTime = 1000; //設(shè)置緩存圖片的數(shù)量 this.PdfViewer1.CacheNumberImage = 1000; //設(shè)置停止?jié)L動(dòng)響應(yīng)事件的時(shí)間 this.PdfViewer1.ScrollInterval = 300; //設(shè)置縮放比例 this.PdfViewer1.ZoomFactor = 1; //加載本地文件夾Files里的文件 this.PdfViewer1.LoadFromFile("Files/Sample1.pdf"); } }
運(yùn)行項(xiàng)目,結(jié)果截圖:

下面介紹如何查看files文件夾中的其他文檔。代碼與查看一個(gè)文檔有些差別。
if (!IsPostBack) { if (Request.QueryString["file"] != null) { this.PdfViewer1.CacheInterval = ushort.Parse(Request.QueryString["CacheInterval"].ToString()); this.PdfViewer1.CacheTime = ushort.Parse(Request.QueryString["CacheTime"].ToString()); this.PdfViewer1.CacheNumberImage = ushort.Parse(Request.QueryString["CacheNumberImage"].ToString()); this.PdfViewer1.ScrollInterval = ushort.Parse(Request.QueryString["ScrollInterval"].ToString()); this.PdfViewer1.LoadFromFile(@"Files\" + Request.QueryString["file"].ToString()); } else { //設(shè)置相關(guān)緩存參數(shù) this.PdfViewer1.CacheInterval = 1000; this.PdfViewer1.CacheTime = 1200; this.PdfViewer1.CacheNumberImage = 1000; this.PdfViewer1.ScrollInterval = 300; this.PdfViewer1.LoadFromFile(@"Files\Sample1.pdf"); } }
運(yùn)行程序,先顯示出第一個(gè)文件,當(dāng)切換文件時(shí),在瀏覽器上直接輸入U(xiǎn)RL,比如 //localhost:6411/WebForm1.aspx?CacheInterval=1000&CacheTime=1200&CacheNumberImage=1000&ScrollInterval=300&file=Sample3.pdf 切換后的結(jié)果。
