首先说一下PDF文档的结构:

分为四层,第一层和第四层由低级操作来进行操作,第二层、第三层由高级对象操作

第一层操作只能使用PdfWriter.DirectContent操作,第四层使用DirectContentUnder操作。

第二层和第三层的PdfContentByte是由IText内部操作,没有提供api接口。

图形和文本状态解释

图形状态,就是作图时对图形一些环境设置,

使用低级操作输出文本或图形前,应该设置操作环境,并且操作完成后,应该恢复操作前的环境。

使用低级操作是非常复杂的一般情况不需要使用这种方式,下面对常用用个法进行说明:

1、PdfContentByte.ShowTextAligned()

方法签名如下:

   1: public void ShowTextAligned(int alignment, String text, float x, 
   2: float y, float rotation)
   3: public void ShowTextAlignedKerned(int alignment, String text,
   4:  float x, float y, float rotation) 

参数说明:

alignment:左、右、居中(ALIGN_CENTER, ALIGN_RIGHT or ALIGN_LEFT)

text:要输出的文本

x:文本输入的X坐标

y:文本输入的Y坐标

rotation:文本的旋转角度

实例:

代码:

   1: public class TextRotaiDemo : TestBase
   2:     {
   3:         protected override void WriteDocument(Document document, PdfWriter writer)
   4:         {
   5:             var direct = writer.DirectContent;
   6:             direct.SaveState();//保存当前的输出状态
   7:             String foobar = "ShowTextAligned演示";
   8:  
   9:             direct.BeginText();
  10:             direct.SetFontAndSize(Normal.BaseFont, 12);
  11:             direct.ShowTextAligned(Element.ALIGN_LEFT, foobar, 400, 788, 0);
  12:             direct.ShowTextAligned(Element.ALIGN_RIGHT, foobar, 400, 752, 0);
  13:             direct.ShowTextAligned(Element.ALIGN_CENTER, foobar, 400, 716, 0);
  14:             direct.ShowTextAligned(Element.ALIGN_CENTER, foobar, 400, 680, 30);
  15:             direct.ShowTextAligned(Element.ALIGN_LEFT, foobar, 400, 644, 0);
  16:             direct.EndText();
  17:             direct.RestoreState();//恢复输出状态
  18:         }
  19:     }

第一行文字: x = 400;y = 788. 
第二行文字: x = 400; y = 752. 
第三行文字: x = 400, y = 716. 
第四行文字: x = 400, y = 680, 并旋转30度. 
第五行文字: 使用showTextAlignedKerned() 方法显示相同的字符串, 但它使用了字形字距调整.

可以通过对位置的控制,在不同的位置显示出不同文字,并且也可以调用图形函数进行作图,如果创建矩形、圆、线条,表格等不规则图形。

2、对于文本的输出“ColumnText”对象的方法“ShowTextAligned”也是可用,并且使用起来比前面那个更方便,方法签名如下:

   1: public static void ShowTextAligned(PdfContentByte canvas, int alignment, 
   2: Phrase phrase, float x, float y, float rotation)
   3: public static void ShowTextAligned(PdfContentByte canvas, int alignment, 
   4: Phrase phrase, float x, float y, float rotation, int runDirection, int arabicOptions)

canvas:层对象

alignment:对齐方式

phrase:待写入对象

x:坐标x

y:坐标y

rotation:旋转角度

runDirection:这个是运行方向,从源代码中,说这个没有起什么作用

arabicOptions:这个参数具体功能不太清

在使用上只使用第一个版本就可以了

演示实例如下:

使用第一个重载                                使用第二个重载

从效果来看,两个生成的结果基本一致。

代码如下:

   1: public class ColumnTextShowTextAlignedDemo : TestBase
   2:     {
   3:         protected override void WriteDocument(Document document, PdfWriter writer)
   4:         {
   5:             var foobar = new Phrase("Show Text Aligned 演示",Normal);
   6:  
   7:             var direct = writer.DirectContent;
   8:             ColumnText.ShowTextAligned(direct,Element.ALIGN_LEFT, foobar, 400, 788, 0);
   9:             ColumnText.ShowTextAligned(direct, Element.ALIGN_RIGHT, foobar, 400, 752, 0);
  10:             ColumnText.ShowTextAligned(direct, Element.ALIGN_CENTER, foobar, 400, 716, 0);
  11:             ColumnText.ShowTextAligned(direct, Element.ALIGN_CENTER, foobar, 400, 680, 30);
  12:             ColumnText.ShowTextAligned(direct, Element.ALIGN_LEFT, foobar, 400, 644, 0);
  13:  
  14:  
  15:             ColumnText.ShowTextAligned(direct, Element.ALIGN_LEFT, foobar, 400, 488, 0, PdfWriter.RUN_DIRECTION_NO_BIDI, 0);
  16:             ColumnText.ShowTextAligned(direct, Element.ALIGN_RIGHT, foobar, 400, 452, 0, PdfWriter.RUN_DIRECTION_NO_BIDI, 0);
  17:             ColumnText.ShowTextAligned(direct, Element.ALIGN_CENTER, foobar, 400, 416, 0, PdfWriter.RUN_DIRECTION_LTR, 0);
  18:             ColumnText.ShowTextAligned(direct, Element.ALIGN_CENTER, foobar, 400, 380, 30, PdfWriter.RUN_DIRECTION_DEFAULT, 0);
  19:             ColumnText.ShowTextAligned(direct, Element.ALIGN_LEFT, foobar, 400, 344, 0, PdfWriter.RUN_DIRECTION_NO_BIDI, 1);
  20:         }
  21:     }

3、chunk的缩放和倾斜、填充模式:

水平缩放:

public Chunk SetHorizontalScaling(float scale)

设置一个浮点数,1表示正常,小于1缩小

倾     斜:

public Chunk SetSkew(float alpha, float beta)

这个是模拟italic效果的一种方式,带有两个参数,表示两个角度

填充模式:

SetTextRenderMode:

■PdfContentByte.TEXT_RENDER_MODE_FILL—这是一种默认模式,填充整个对象,而不是笔画填充(the glyph shapes are filled, not stroked.

■ PdfContentByte.TEXT_RENDER_MODE_STROKE—这种模式使用笔画填充

■ PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE—

■ PdfContentByte.TEXT_RENDER_MODE_INVISIBLE—文本不可见

效果如下:

代码如下:

   1: public class ChunkScalSekewDemo : TestBase
   2:     {
   3:         protected override void WriteDocument(Document document, PdfWriter writer)
   4:         {
   5:             var foobar = "Show Text Aligned 演示";
   6:             var direct = writer.DirectContent;
   7:             var c = new Chunk(foobar, Normal);
   8:             c.SetHorizontalScaling(0.5f);
   9:             var phrase = new Phrase(c);
  10:             ColumnText.ShowTextAligned(direct,Element.ALIGN_LEFT, phrase, 400, 572, 0);
  11:             c = new Chunk(foobar, Normal);
  12:             c.SetSkew(15, 15);
  13:             phrase = new Phrase(c);
  14:             ColumnText.ShowTextAligned(direct,Element.ALIGN_LEFT, phrase, 400, 536, 0);
  15:             c = new Chunk(foobar, Normal);
  16:             c.SetSkew(0, 25);
  17:             phrase = new Phrase(c);
  18:             ColumnText.ShowTextAligned(direct,Element.ALIGN_LEFT, phrase, 400, 500, 0);
  19:             c = new Chunk(foobar, Normal);
  20:             c.SetTextRenderMode(
  21:               PdfContentByte.TEXT_RENDER_MODE_STROKE,0.1f, BaseColor.RED);
  22:             phrase = new Phrase(c);
  23:             ColumnText.ShowTextAligned(direct,Element.ALIGN_LEFT, phrase, 400, 464, 0);
  24:             c = new Chunk(foobar, Normal);
  25:             c.SetTextRenderMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE, 1, null);
  26:             phrase = new Phrase(c);
  27:             ColumnText.ShowTextAligned(direct,
  28:               Element.ALIGN_LEFT, phrase, 400, 428, -0);
  29:         }
  30:     }

4、ColumnText对象使用

分栏布局也是一种比较常见的方式,如果一段文字的段落比较多,但每段的文字比较少,如果采用分栏的方式可以节约很空间,同样,在显示表格时,如果表格的列比较少,也会节约很多空间。在iText中也是支持分栏布局的,使用的ColumnText对象。

首先说一下,ColumnText对象常用方法:

1、SetSimpleColumn

   1: public void SetSimpleColumn(float llx, float lly, float urx, float ury)
   2: public void SetSimpleColumn(float llx, float lly, float urx, float ury, 
   3: float leading, int alignment)
   4: public void SetSimpleColumn(Phrase phrase, float llx, float lly, 
   5: float urx, float ury, float leading, int alignment)

这个函数的功能就是设置列文本的矩形区域,第二个重载还可以进行设置对齐方式和间距,第三个调用AddText方法加入对象后,再直接调用第二个重载。

2、AddText

   1: public void AddText(Chunk chunk)
   2: public void AddText(Phrase phrase)

这两个方法在文本模式下有效,功能就是加入显示的文字内容,如果在复合模式下,调用这两个函数不起任何作用。

3、AddElement

   1: public void AddElement(IElement element)

复合模式下,使用此方法进行内容添加

4、HasMoreText

检查当前一列显示完成后,是否还有内容存在,如果有就要换下一列,或换页。

5、Go

   1: public int Go()
   2: public int Go(bool simulate)

第一个相当于传入false调用第二重载

simulate表示是否模拟写入

返回值表示,还有没有没有写入的内容

当为false时,就是直接写到文档对象中

重要属性:

Alignment:对齐方式

ExtraParagraphSpace

Leading:间距

Indent:缩进

RightIndent:右缩进

SpaceCharRatio:在使用“justified”时使用

一步一步ITextSharp 低级操作函数使用的更多相关文章

  1. javascript 函数 add(1)(2)(3)(4)实现无限极累加 —— 一步一步原理解析

    问题:我们有一个需求,用js 实现一个无限极累加的函数, 形如 add(1) //=> 1; add(1)(2)  //=> 2; add(1)(2)(3) //=>  6; add ...

  2. ElasticSearch第五步-.net平台下c#操作ElasticSearch详解

    前面我们讲解了关于ElasticSearch的安装配置,以及CRUD 本章我将讲解怎么使用c#操作ElasticSearch. 首先你需要一定的技术储备,比如:asp.net webapi,mvc,j ...

  3. python 装饰器 第六步:带有收集参数的函数的装饰器

    #第六步:带有收集参数的函数的装饰器 #装饰器函数 def kuozhan(func): #内部函数(扩展之后的eat函数) def neweat(*w,**n): #以下三步就是扩展之后的功能,于是 ...

  4. 一步一步的理解C++STL迭代器

    一步一步的理解C++STL迭代器 "指针"对全部C/C++的程序猿来说,一点都不陌生. 在接触到C语言中的malloc函数和C++中的new函数后.我们也知道这两个函数返回的都是一 ...

  5. 一步一步学ROP之linux_x64篇

    一步一步学ROP之linux_x64篇 一.序 **ROP的全称为Return-oriented programming(返回导向编程),这是一种高级的内存攻击技术可以用来绕过现代操作系统的各种通用防 ...

  6. 大流量网站性能优化:一步一步打造一个适合自己的BigRender插件

    BigRender 当一个网站越来越庞大,加载速度越来越慢的时候,开发者们不得不对其进行优化,谁愿意访问一个需要等待 10 秒,20 秒才能出现的网页呢? 常见的也是相对简单易行的一个优化方案是 图片 ...

  7. 使用Python一步一步地来进行数据分析总结

    原文链接:Step by step approach to perform data analysis using Python译文链接:使用Python一步一步地来进行数据分析--By Michae ...

  8. 一步一步写平衡二叉树(AVL树)

    平衡二叉树(Balanced Binary Tree)是二叉查找树的一个进化体,也是第一个引入平衡概念的二叉树.1962年,G.M. Adelson-Velsky 和 E.M. Landis发明了这棵 ...

  9. php文件夹与文件目录操作函数

    在php中一些常用的文件夹/文件目录操作函数总结. php文件夹操作函数 string basename ( string path [, string suffix] ) 给出一个包含有指向一个文件 ...

随机推荐

  1. jstl删除session,choose,动态获取request当前工程路径

    1.jstl标签c:remove删除session request.getSession().setAttribute("ssmsg", "修改成功"); &l ...

  2. 【设计模式六大原则6】开闭原则(Open Close Principle)

      定义:一个软件实体如类.模块和函数应该对扩展开放,对修改关闭. 问题由来:在软件的生命周期内,因为变化.升级和维护等原因需要对软件原有代码进行修改时,可能会给旧代码中引入错误,也可能会使我们不得不 ...

  3. POJ 3278Catch That Cow

    http://poj.org/problem?id=3278 大意是说牛在原地不动,他在某点去抓牛,他有两种方式可以走,第一种走一步,往前往后都可,第二种是走现在所在点的两倍的数目.只要能够刚好到达牛 ...

  4. Python新式类和旧式类的区别

    新式类是为了统一**而在2.2中开始引入的. 代码讲解 上面的例子比较明白的说明了问题. B是定义的新式类.那么输入b的时候,不论是type(b),还是b.__class__都是输出的<clas ...

  5. no such partition grub rescue>

    事出有因: 电脑系统是win7+ubuntu,然后在win7下把ubuntu的分区给删除了,重启,出现 no such partition grub rescue> 错误. 原因是双系统之前是由 ...

  6. 能量项链//区间DP

    P1056 能量项链 时间: 1000ms / 空间: 131072KiB / Java类名: Main 背景 NOIP2006 提高组 第一道 描述     在Mars星球上,每个Mars人都随身佩 ...

  7. 李洪强漫谈iOS开发[C语言-038]-if else if语句

    李洪强漫谈iOS开发[C语言-038]-if else if语句

  8. Spring两种实现AOP的方式

    有两种实现AOP的方式:xml配置文件的方式和注解的形式 我们知道通知Advice是指对拦截到的方法做什么事,可以细分为 前置通知:方法执行之前执行的行为. 后置通知:方法执行之后执行的行为. 异常通 ...

  9. Tomcat下的一些配置

    1. JAVA虚拟机性能优化,修改bin下的  catalina.sh/bat rem ----- Execute The Requested Command -------------------- ...

  10. OSSEC 安装执行./install.sh详细信息

    下载好ossec安装文件后解压得到如下目录 [root@localhost ~]# cd ossec-hids-/ [root@localhost ossec-hids-]# ll total drw ...