公司里面用的iTextSharp(教程)---生成一个简答的PDF的语法

本来打算先写一个项目大家一起练习的,但是后来发现不懂一些基本的语法,几乎做了之后也有些不明白,下面我们一起简简单单的看一下哈~~

语法1:Document document = new Document(); //可以理解为显示中的一张白纸,需要使用打开(Open())和关闭(Close())来处理

语法2:PdfWriter.getInstance(document, new FileStream("XX.pdf",FileMode.Create)); //创建一个Writer 实例

语法3:document.Add(new Paragraph("Today is my first time to create a PDF!")); //为当前的文档添加内容

 

下面我们来看一个小小的实例:

using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
 
namespace Test1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
 
        }
     
        protected void Button1_Click(object sender, EventArgs e)
        {
 
            Document doc = new Document();
            try
            {
                PdfWriter.GetInstance(doc, new FileStream(Server.MapPath("Elaine.pdf"), FileMode.Create));
                doc.Open();
 
                doc.Add(new Paragraph("Today is my first time to create a PDF!")); //为当前的文档添加内容
 
            }
            catch (Exception DOCEx)
            {
 
                Response.Write(DOCEx.Message);
            }
            finally
            {
                if (doc.IsOpen())
                {
                    doc.Close();
                }
            }
 
        }
    }
}

效果:

image

附加源码:下载

转载于:https://www.cnblogs.com/ITGirl00/p/3473333.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值