PDF在线编辑器的实现

来源:未知 责任编辑:智问网络 发表时间:2013-11-12 17:20 点击:
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 
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> 
<%@ 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>
 摘自 邱征响的专栏
    发表评论
    请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
    用户名: 验证码:点击我更换图片
    最新评论 更多>>

    推荐热点

    • 浅析.NET下XML数据访问新机制
    • asp.net 面试+笔试题目第1/2页
    • C# 邮件地址是否合法的验证
    • asp.net 设置GridView的选中行的实现代码
    • C#高级编程:数据库连接[1]
    • 经典C++程序1
    • IIS 自动回收导致后台定时器失效的问题解决
    • ASP.NET&#160;GridView列表代码示例
    • Asp.net MVC源码分析--Action Filter的链式调用
    网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
    Copyright © 2008-2015 计算机技术学习交流网. 版权所有

    豫ICP备11007008号-1