当前位置:编程学习 > asp >>

asp.net工具类-PDFOperation

using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;

/// <summary>
/// PDF文档操作类
/// </summary>
//------------------------------------调用--------------------------------------------
//PDFOperation pdf = new PDFOperation();
//pdf.Open(new FileStream(path, FileMode.Create));
//pdf.SetBaseFont(@"C:\Windows\Fonts\SIMHEI.TTF");
//pdf.AddParagraph("测试文档(生成时间:" + DateTime.Now + ")", 15, 1, 20, 0, 0);
//pdf.Close();
//--------------------------------------------------------------------------------
public class PDFOperation
{
    #region 构造函数
    /// <summary>
        /// 构造函数
        /// </summary>
        public PDFOperation()
        {
                rect = PageSize.A4;
                document = new Document(rect);
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="type">页面大小(如"A4")</param>
        public PDFOperation(string type)
        {
                SetPageSize(type);
                document = new Document(rect);
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="type">页面大小(如"A4")</param>
        /// <param name="marginLeft">内容距左边框距离</param>
        /// <param name="marginRight">内容距右边框距离</param>
        /// <param name="marginTop">内容距上边框距离</param>
        /// <param name="marginBottom">内容距下边框距离</param>
        public PDFOperation(string type,float marginLeft, float marginRight, float marginTop, float marginBottom)
        {
                SetPageSize(type);
                document = new Document(rect,marginLeft,marginRight,marginTop,marginBottom);
    }
    #endregion

    #region 私有字段
    private Font font;
    private Rectangle rect;   //文档大小
    private Document document;//文档对象
    private BaseFont basefont;//字体
    #endregion

    #region 设置字体
    /// <summary>
    /// 设置字体
    /// </summary>
    public void SetBaseFont(string path)
    {
        basefont = BaseFont.CreateFont(path, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
    }

    /// <summary>
    /// 设置字体
    /// </summary>
    /// <param name="size">字体大小</param>
    public void SetFont(float size)
    {
        font = new Font(basefont, size);
    }
    #endregion

    #region 设置页面大小
    /// <summary>
        /// 设置页面大小
        /// </summary>
        /// <param name="type">页面大小(如"A4")</param>
        public void SetPageSize(string type)
        {
                switch (type.Trim())
                {
                        case "A4":
                                rect = PageSize.A4;
                                break;
                        case "A8":
                                rect = PageSize.A8;
                                break;
                }
    }
    #endregion

    #region 实例化文档
    /// <summary>
        /// 实例化文档
        /// </summary>
        /// <param name="os">文档相关信息(如路径,打开方式等)</param>
        public void GetInstance(Stream os)
        {
                PdfWriter.GetInstance(document, os);
    }
    #endregion

    #region 打开文档对象
    /// <summary>
        /// 打开文档对象
        /// </summary>
        /// <param name="os">文档相关信息(如路径,打开方式等)</param>
        public void Open(Stream os)
        {
                GetInstance(os);
  &

补充:Web开发 , ASP.Net ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,