---
title: ".Net开源PDF类库 itextsharp 测试"
author: "zu14"
date: "2010-01-09T17:38:00Z"
tags: []
slug: "net-pdf-itextsharp"
draft: false
lastmod: "2010-01-09T17:38:00Z"
id: 779855
url: "https://zu14.iteye.com/blog/779855"
---

Pdf，一个让人又爱又恨的文件格式，但是它的应用却真的是愈来愈广泛了。虽然，目前的Office 2007及后续的版本都已经支持Pdf格式了，但是，用微软的开发平台，目前来生成PDF文档的免费选择仍然十分少。

iTextSharp，是目前做的比较成熟的.net 平台下的开源项目，它的地址在 [http://sourceforge.net/projects/itextsharp/](http://sourceforge.net/projects/itextsharp/ "http://sourceforge.net/projects/itextsharp/")。

按照它的官方说明，是支持的非常广泛的，PDF、XML、图片等待都支持的。经过测试，发现它生成的pdf文件是 **PDF1.4** 的版本，就是 **Acrobat Reader 5.x** 的版本。

今天，为了生成PDF，找到这个类库，做了测试，结果令我大失所望。把测试结果发布出来，希望能和大家交流，也许是我自己没能正确使用它吧![](http://www.zu14.cn/coolemotion/emotions/zz_14.gif)

生成的PDF文件，截图如下：

![itextsharp pdf csharp c# asp.net](http://www.zu14.cn/wp-content/uploads/itextsharp/image.png "itextsharp pdf csharp c# asp.net")

对PDF文件的属性设置，支持的倒是比较完美：

![itextsharp pdf c# csharp asp.net](http://www.zu14.cn/wp-content/uploads/itextsharp/image_3.png "itextsharp pdf c# csharp asp.net")

在上面的图中，大家可能看到了那句 **not support chinese** 了，哈哈，就是我测试的凄惨结果：**文档内容不支持中文**！

下面是测试代码：

```
using iTextSharp.text;
using iTextSharp.text.pdf;

Document document = new Document();

try
{

    // step 2:
    // we create a writer that listens to the document
    // and directs a PDF-stream to a file

    PdfWriter.GetInstance(document, new FileStream("Chap0101.pdf", FileMode.OpenOrCreate));

    // step 3: we open the document
    document.Open();

    // step 4: we Add a paragraph to the document
    document.AddAuthor("三?角?猫?");
    document.AddCreationDate();
    document.AddCreator("DeltaCat");
    document.AddSubject("DotNet 使?用?itextsharp 类?库?创?建?PDF文?件?的?例?子?");
    document.AddTitle("此?PDF由?三?角?猫?创?建?，?嘿?嘿?");
    document.Add(new Paragraph("Hello World"));
    document.Add(new Paragraph("-------------------------------"));
    document.Add(new Paragraph(new Chunk("Not Support Chinese", new Font(5, 30, 2, new BaseColor(223)))));
}
catch(DocumentException de)
{
    Console.Error.WriteLine(de.Message);
}
catch(IOException ioe)
{
    Console.Error.WriteLine(ioe.Message);
}

// step 5: we close the document
document.Close();
```