您的位置:首页 > 其它

PDF在线编辑器的实现

2012-02-02 15:01 127 查看
1、新建类库PdfViewer,在类库中建立一个ShowPdf的类,代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PdfViewer
{
[DefaultProperty("FilePath")]
[ToolboxData("<{0}:ShowPdf runat=server></{0}:ShowPdf>")]
public class ShowPdf : WebControl
{

#region "Declarations"

private string mFilePath;

#endregion

#region "Properties"

[Category("Source File")]
[Browsable(true)]
[Description("Set path to source file.")]
[Editor(typeof(System.Web.UI.Design.UrlEditor), typeof(System.Drawing.Design.UITypeEditor))]
public string FilePath
{
get
{
return mFilePath;
}
set
{
if (value == string.Empty)
{
mFilePath = string.Empty;
}
else
{
int tilde = -1;
tilde = value.IndexOf('~');
if (tilde != -1)
{
mFilePath = value.Substring((tilde + 2)).Trim();
}
else
{
mFilePath = value;
}
}
}
}   // end FilePath property

#endregion

#region "Rendering"

protected override void RenderContents(HtmlTextWriter writer)
{
try
{
StringBuilder sb = new StringBuilder();
sb.Append("<iframe src=" + FilePath.ToString() + " ");
sb.Append("width=" + Width.ToString() + " height=" + Height.ToString() + " ");
sb.Append("<View PDF: <a href=" + FilePath.ToString() + "</a></p> ");
sb.Append("</iframe>");

writer.RenderBeginTag(HtmlTextWriterTag.Div);
writer.Write(sb.ToString());
writer.RenderEndTag();
}
catch
{
// with no properties set, this will render "Display PDF Control" in a
// a box on the page
writer.RenderBeginTag(HtmlTextWriterTag.Div);
writer.Write("Display PDF Control");
writer.RenderEndTag();
}  // end try-catch
}   // end RenderContents

#endregion

}   // end class
}       // end namespace


2、在WEB项目中添加引用类库PdfViewer,以下是页面代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="PdfTestSite._Default" %>

<%@ Register Assembly="PdfViewer" Namespace="PdfViewer" TagPrefix="cc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>PDF文档在线编辑测试</title>
</head>
<body bottommargin="0" leftmargin="0" rightmargin="0" topmargin="0" style="font-family: Calibri" bgcolor="#cccccc">
<form id="form1" runat="server">
<div>
<asp:Panel ID="Panel1" runat="server" BackColor="LightSlateGray" BorderStyle="Outset" BorderWidth="2px"
Font-Bold="True" Font-Names="Calibri" Font-Size="X-Large" ForeColor="White" Height="80px"
Style="z-index: 100; left: 0px; position: absolute; top: 0px" Width="100%">
<br />
PDF在线编辑<br />

</asp:Panel>

<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/ABC.pdf" Style="z-index: 101;
left: 24px; position: absolute; top: 96px">在新窗口打开PDF文档</asp:HyperLink>
<cc1:ShowPdf ID="ShowPdf1" runat="server" BorderStyle="Inset" BorderWidth="2px" FilePath="ABC.pdf"
Height="352px" Style="z-index: 103; left: 24px; position: absolute; top: 128px"
Width="856px" />

</div>
</form>

</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: