示例源码

//Document:(文档)生成pdf必备的一个对象,生成一个Document示例

Document document = new Document(PageSize.A4, 30, 30, 5, 5);

//为该Document创建一个Writer实例:

PdfWriter.GetInstance(document, new FileStream(Server.MapPath("/upload/"+"Chap0101.pdf"), FileMode.Create));

//打开当前Document

document.Open();

//为当前Document添加内容:

document.Add(new Paragraph("Hello World"));

//另起一行。有几种办法建立一个段落,如:

Paragraph p1 = new Paragraph(new Chunk("This is my first paragraph.\n", FontFactory.GetFont(FontFactory.HELVETICA, 12)));

Paragraph p2 = new Paragraph(new Phrase("This is my second paragraph.", FontFactory.GetFont(FontFactory.HELVETICA, 12)));

Paragraph p3 = new Paragraph("This is my third paragraph.", FontFactory.GetFont(FontFactory.HELVETICA, 12));

//所有有些对象将被添加到段落中:

p1.Add("you can add string here\n\t");

p1.Add(new Chunk("you can add chunks \n")); p1.Add(new Phrase("or you can add phrases.\n"));

document.Add(p1); document.Add(p2); document.Add(p3);

//创建了一个内容为“hello World”、红色、斜体、COURIER字体、尺寸20的一个块:

Chunk chunk = new Chunk("Hello world", FontFactory.GetFont(FontFactory.COURIER, 20, iTextSharp.text.Font.COURIER, new iTextSharp.text.Color(255, 0, 0)));

document.Add(chunk);

//如果你希望一些块有下划线或删除线,你可以通过改变字体风格简单做到:

Chunk chunk1 = new Chunk("This text is underlined", FontFactory.GetFont(FontFactory.HELVETICA, 12, iTextSharp.text.Font.UNDEFINED));

Chunk chunk2 = new Chunk("This font is of type ITALIC | STRIKETHRU", FontFactory.GetFont(FontFactory.HELVETICA, 12, iTextSharp.text.Font.ITALIC | iTextSharp.text.Font.STRIKETHRU));

//改变块的背景

chunk2.SetBackground(new iTextSharp.text.Color(0xFF, 0xFF, 0x00));

//上标/下标

chunk1.SetTextRise(5);

document.Add(chunk1);

document.Add(chunk2);

//外部链接示例:

Anchor anchor = new Anchor("website", FontFactory.GetFont(FontFactory.HELVETICA, 12, iTextSharp.text.Font.UNDEFINED, new iTextSharp.text.Color(0, 0, 255)));

anchor.Reference = "http://itextsharp.sourceforge.net/";

anchor.Name = "website";

//内部链接示例:

Anchor anchor1 = new Anchor("This is an internal link\n\n");

anchor1.Name = "link1";

Anchor anchor2 = new Anchor("Click here to jump to the internal link\n\f");

anchor2.Reference = "#link1";

document.Add(anchor); document.Add(anchor1); document.Add(anchor2);

//排序列表示例:

List list = new List(true, 20);

list.Add(new iTextSharp.text.ListItem("First line"));

list.Add(new iTextSharp.text.ListItem("The second line is longer to see what happens once the end of the line is reached. Will it start on a new line?"));

list.Add(new iTextSharp.text.ListItem("Third line"));

document.Add(list);

//文本注释:

Annotation a = new Annotation("authors", "Maybe its because I wanted to be an author myself that I wrote iText.");

document.Add(a);

//包含页码没有任何边框的页脚。

HeaderFooter footer = new HeaderFooter(new Phrase("This is page: "), true);

footer.Border = iTextSharp.text.Rectangle.NO_BORDER;

document.Footer = footer;

//Chapter对象和Section对象自动构建一个树:

iTextSharp.text.Font f1 = new iTextSharp.text.Font();

f1.SetStyle(iTextSharp.text.Font.BOLD);

Paragraph cTitle = new Paragraph("This is chapter 1", f1);

Chapter chapter = new Chapter(cTitle, 1);

Paragraph sTitle = new Paragraph("This is section 1 in chapter 1", f1);

Section section = chapter.AddSection(sTitle, 1);

document.Add(chapter);

//构建了一个简单的表:

iTextSharp.text.Table aTable = new iTextSharp.text.Table(4, 4);

aTable.AutoFillEmptyCells = true;

aTable.AddCell("2.2", new Point(2, 2));

aTable.AddCell("3.3", new Point(3, 3));

aTable.AddCell("2.1", new Point(2, 1));

aTable.AddCell("1.3", new Point(1, 3));

document.Add(aTable);

//构建了一个不简单的表:

iTextSharp.text.Table table = new iTextSharp.text.Table(3);

table.BorderWidth = 1;

table.BorderColor = new iTextSharp.text.Color(0, 0, 255);

table.Cellpadding = 5;

table.Cellspacing = 5;

Cell cell = new Cell("header");

cell.Header = true;

cell.Colspan = 3;

table.AddCell(cell);

cell = new Cell("example cell with colspan 1 and rowspan 2");

cell.Rowspan = 2;

cell.BorderColor = new iTextSharp.text.Color(255, 0, 0);

table.AddCell(cell);

table.AddCell("1.1");

table.AddCell("2.1");

table.AddCell("1.2");

table.AddCell("2.2");

table.AddCell("cell test1");

cell = new Cell("big cell");

cell.Rowspan = 2;

cell.Colspan = 2;

cell.BackgroundColor = new iTextSharp.text.Color(0xC0, 0xC0, 0xC0);

table.AddCell(cell);

table.AddCell("cell test2");

// 改变了单元格“big cell”的对齐方式:

cell.HorizontalAlignment = Element.ALIGN_CENTER;

cell.VerticalAlignment = Element.ALIGN_MIDDLE;

document.Add(table);

//关闭Document

document.Close();

PDF ITextSharp的更多相关文章

  1. 【译】在Asp.Net中操作PDF - iTextSharp - 利用列进行排版

    原文 [译]在Asp.Net中操作PDF - iTextSharp - 利用列进行排版 在使用iTextSharp通过ASP.Net生成PDF的系列文章中,前面的文章已经讲述了iTextSharp所涵 ...

  2. 【译】在Asp.Net中操作PDF - iTextSharp - 绘制矢量图

    原文 [译]在Asp.Net中操作PDF - iTextSharp - 绘制矢量图 在上一篇iTextSharp文章中讲述了如何将现有的图片插入PDF中并对其进行操作.但有时,你需要在PDF中绘制不依 ...

  3. 【译】在Asp.Net中操作PDF – iTextSharp - 操作图片

    原文 [译]在Asp.Net中操作PDF – iTextSharp - 操作图片 作为我的iTextSharp系列的文章的第七篇,开始探索使用iTextSharp在PDF中操作图片,理解本篇文章需要看 ...

  4. 【译】在Asp.Net中操作PDF – iTextSharp - 使用表格

    原文 [译]在Asp.Net中操作PDF – iTextSharp - 使用表格 使用Asp.Net生成PDF最常用的元素应该是表格,表格可以帮助比如订单或者发票类型的文档更加格式化和美观.本篇文章并 ...

  5. 【译】在Asp.Net中操作PDF – iTextSharp - 使用链接和书签

    原文 [译]在Asp.Net中操作PDF – iTextSharp - 使用链接和书签 用户和PDF文档的交互可以通过锚(链接)和书签进行,接着我前面iTextSharp的系列文章,本篇文章主要讲通过 ...

  6. 【译】在Asp.Net中操作PDF – iTextSharp -利用块,短语,段落添加文本

    原文 [译]在Asp.Net中操作PDF – iTextSharp -利用块,短语,段落添加文本 本篇文章是讲述使用iTextSharp这个开源组件的系列文章的第三篇,iTextSharp可以通过As ...

  7. 【译】在Asp.Net中操作PDF - iTextSharp - 使用字体

    原文 [译]在Asp.Net中操作PDF - iTextSharp - 使用字体 紧接着前面我对iTextSharp简介博文,iTextSharp是一个免费的允许Asp.Net对PDF进行操作的第三方 ...

  8. 【译】在Asp.Net中操作PDF - iTextSharp - 利用列进行排版(转)

    [译]在Asp.Net中操作PDF - iTextSharp - 利用列进行排版   在使用iTextSharp通过ASP.Net生成PDF的系列文章中,前面的文章已经讲述了iTextSharp所涵盖 ...

  9. 在Asp.Net中操作PDF – iTextSharp - 使用表格

    使用Asp.Net生成PDF最常用的元素应该是表格,表格可以帮助比如订单或者发票类型的文档更加格式化和美观.本篇文章并不会深入探讨表格,仅仅是提供一个使用iTextSharp生成表格的方法介绍 使用i ...

随机推荐

  1. CentOS(七)--Linux文件类型及目录配置

    这篇随笔将会对Linux系统的文件类型以及Linux的目录结构进行详细补充(linux中目录管理和权限非常重要,特别是在linux安装数据库类软件). 一.Linux更改文件权限的两种方式 在之前的一 ...

  2. [转]Install Windows Server 2012 in VMware Workstation

    本文转自:http://kb4you.wordpress.com/2012/06/28/install-windows-server-2012-in-vmware-workstation-2/ Thi ...

  3. [改善Java代码]推荐使用String直接量赋值

    建议52:推荐使用String直接量赋值 一.建议 String对象的生成方式有两种: 1.通过new关键字生成,String str3 = new String(“中国”); 2.直接声明,如:St ...

  4. DisableExplicitGC和Direct ByteBuffer

    直接堆外内存请参见:http://blog.csdn.net/lantian0802/article/details/39257087 JVM调优请参见:http://hllvm.group.itey ...

  5. Angular2 从0到1 (一)

    史上最简单Angular2教程,大叔都学会了 作者:王芃 wpcfan@gmail.com 第一节:Angular 2.0 从0到1 (一)第二节:Angular 2.0 从0到1 (二)第三节:An ...

  6. nmap命令-----基础用法

    系统漏洞扫描之王-nmap   NMap,也就是Network Mapper,是Linux下的网络扫描和嗅探工具包.   其基本功能有三个: (1)是扫描主机端口,嗅探所提供的网络服务 (2)是探测一 ...

  7. .NET 认识

  8. UpdatePanel中执行js

    在UpdatePanel中,直接使用Page.ClientScript.RegisterStartupScript的方式执行javascript,会导致无法执行.原因可能是因为RegisterStar ...

  9. 三道题(关于虚表指针位置/合成64位ID/利用栈实现四则运算)

    第一题 C++标准中,虚表指针在类的内存结构位置没有规定,不同编译器的实现可能是不一样的.请实现一段代码,判断当前编译器把虚表指针放在类的内存结构的最前面还是最后面.  第二题 在游戏中所有物品的实例 ...

  10. 企业SAAS的春天,将以手机应用的形式,即将到来

    派尔科技吴春福 *本文是派尔为什么要投身企业移动应用的内部分享文章: *我没有仔细核查资料,仅代表个人看法,思路也是在整理过程,逻辑未必很完整,看官将就着看. 企业SAAS,概念起源是N年前,先行者也 ...