Asp.Net(C#)生成PDF详解(支持中文、水印、页眉、页脚、表格等)

2010年8月19日
 

第二篇Hello PDF! 你好,PDF!


有了 上一篇 的准备工作,我们现在开始真正的创建PDF的旅程了。

 

作为编程界的惯例,第一篇,当然是非 Hello World 莫属啦 ,不过,我这里叫 Hello PDF!

Let’s Go!

 

当然是添加引用了

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

简单代码

//定义一个Document,并设置页面大小为A4,竖向
iTextSharp.text.Document doc = new Document(PageSize.A4);
try
{
    //写实例
    PdfWriter.GetInstance(doc, new FileStream("chap01.pdf", FileMode.Create));
    //打开document
    doc.Open();
    //写入一个段落, Paragraph
    doc.Add(new Paragraph("Hello PDF !"));
    //关闭document
    doc.Close();
    //打开PDF,看效果
    Process.Start("chap01.pdf");
}
catch (DocumentException de)
{
    Console.WriteLine(de.Message);
    Console.ReadKey();
}
catch (IOException io)
{
    Console.WriteLine(io.Message);
    Console.ReadKey();
}

asp.net 生成 PDF

哈哈,是不是非常EASY啊,虽然效果很简陋,但终究是生成了文件。

下面,我们将上面的代码,稍作修改

将  doc.Add(new Paragraph(“Hello PDF !”));

改为

doc.Add(new Paragraph(“你好, PDF !”));

运行,看效果:

PDF

?? 你好,这几个字呢?消失啦。这是因为iTextSharp 默认没有中文字库,解决的方法,有2种。

  1. 使用iTextSharp自带的亚洲字库:iTextAsian.dlliTextAsianCmaps.dll
  2. 使用自己的字体库,动态载入字体

下面分别看一下2种方法的实现

//定义一个Document,并设置页面大小为A4,竖向
iTextSharp.text.Document doc = new Document(PageSize.A4);
try
{
    //写实例
    PdfWriter.GetInstance(doc, new FileStream("chap01.pdf", FileMode.Create));
    //打开document
    doc.Open();

    //载入字体
    BaseFont.AddToResourceSearch("iTextAsian.dll");
    BaseFont.AddToResourceSearch("iTextAsianCmaps.dll");

    //"UniGB-UCS2-H" "UniGB-UCS2-V"是简体中文,分别表示横向字 和 纵向字
    //"STSong-Light"是字体名称
    BaseFont baseFT = BaseFont.CreateFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
    iTextSharp.text.Font font = new iTextSharp.text.Font(baseFT);

    //写入一个段落, Paragraph
    doc.Add(new Paragraph("你好, PDF !", font));
    //关闭document
    doc.Close();
    //打开PDF,看效果
    Process.Start("chap01.pdf");
}
catch (DocumentException de)
{
    Console.WriteLine(de.Message);
    Console.ReadKey();
}
catch (IOException io)
{
    Console.WriteLine(io.Message);
    Console.ReadKey();
}

p_w_picpath

//定义一个Document,并设置页面大小为A4,竖向
iTextSharp.text.Document doc = new Document(PageSize.A4);
try
{
    //写实例
    PdfWriter.GetInstance(doc, new FileStream("chap01.pdf", FileMode.Create));
    //打开document
    doc.Open();

    //载入字体
    BaseFont baseFont = BaseFont.CreateFont(
        "C:\\WINDOWS\\FONTS\\SIMHEI.TTF", //黑体
        BaseFont.IDENTITY_H, //横向字体
        BaseFont.NOT_EMBEDDED);
    iTextSharp.text.Font font = new iTextSharp.text.Font(baseFont, 9); 

    //写入一个段落, Paragraph
    doc.Add(new Paragraph("你好, PDF !", font));
    //关闭document
    doc.Close();
    //打开PDF,看效果
    Process.Start("chap01.pdf");
}
catch (DocumentException de)
{
    Console.WriteLine(de.Message);
    Console.ReadKey();
}
catch (IOException io)
{
    Console.WriteLine(io.Message);
    Console.ReadKey();
}

p_w_picpath

好了,这篇就到此OK了。

本文作者: 三角猫 DeltaCat
文章出处: 真有意思网(http://www.zu14.cn)
引用地址:点击复制本文的 Trackback Url
文章链接: http://www.zu14.cn/2010/08/19/dotnet-aspnet-itextsharp-create-pdf-watermask-header-footer-table-pdfptable-2/ [ 复制] (转载请注明出处及链接)