本来打算先写一个项目大家一起练习的,但是后来发现不懂一些基本的语法,几乎做了之后也有些不明白,下面我们一起简简单单的看一下哈~~
语法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();
}
}
}
}
}
效果:
附加源码:下载