iText 生成PDF全攻略

来源:百度文库 编辑:神马文学网 时间:2024/05/02 04:54:56
首先还是看看文档。看完了,你把我写的例子跑一遍你就会了。
PDF
创建文档
//创建一个文档对象,并设置他的初始化大小
Rectangle pSize=new Rectangle(144,90);
//文档的背景色
pSize.setBackgroundColor(Color.blue);
//常用页面大小  PageSize.A4;
Document doc=new Document(pSize);
try {
//定义输出位置并把文档对象装入输出对象中
PdfWriter.getInstance(doc, new FileOutputStream("c:/hello.pdf"));
//打开文档对象
doc.open();
// 加入文字“Hello World”
doc.add(new Paragraph("HelloWorld"));
//  关闭文档对象,释放资源
doc.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}
设置字体
亚洲国家的字体你可以从http://itext.sourceforge.net/downloads/iTextAsian.jar下载这个包。然后把它直接放到你的ClassPath中就可以了
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);
表格
//定义一个表格
Table table = new Table(2);
//设置表格边框
table.setBorderWidth(1);
Cell cell = new Cell("Matrix III");
cell.setHeader(true);
//分列
cell.setColspan(2);
cell.setBackgroundColor(Color.blue);
//分行
Cell.setRowspan(2);
table.addCell(cell);
创建新页
// 创建第2的页面
document.newPage();
document.add(new Paragraph("Hello Earth"));
document.resetHeader();
// 创建第3的页面
document.newPage();
document.add(new Paragraph("Hello Sun"));
document.add(new Paragraph("Remark: the header has vanished!"));
document.resetPageCount();
设置布局
PdfWriter writerA = PdfWriter.getInstance(document,
new FileOutputStream("d:\\Chap0108a.pdf"));
// 设置布局
writerA.setViewerPreferences(PdfWriter.PageLayoutTwoColumnLeft);
PdfWriter writerB = PdfWriter.getInstance(document,
new FileOutputStream("d:\\Chap0108b.pdf"));
// 设置布局
writerB.setViewerPreferences(PdfWriter.HideMenubar
| PdfWriter.HideToolbar);
PdfWriter writerC = PdfWriter.getInstance(document,
new FileOutputStream("d:\\Chap0108c.pdf"));
// 设置布局
writerC.setViewerPreferences(PdfWriter.PageLayoutTwoColumnLeft
| PdfWriter.PageModeFullScreen
| PdfWriter.NonFullScreenPageModeUseThumbs);
加密
document = new Document(PageSize.A4, 50, 50, 50, 50);
PdfWriter writer = PdfWriter.getInstance(document,
new FileOutputStream("d:\\Chap0109.pdf"));
// setEncryption方法中可以设置如下内容(这样打开pdf时需要输入口令!!!!!!!!!!!)
// PdfWriter.STRENGTH128BITS, "userpass", "ownerpass",
// PdfWriter.AllowCopy | PdfWriter.AllowPrinting
writer.setEncryption(PdfWriter.STRENGTH40BITS, "", "",
PdfWriter.AllowCopy);
http://itextsharp.sourceforge.net/index.html)中看到了专门制作PDF文件的控件的介绍,暗喜之余,立马下载试验,果然非常轻松地制作出了想要的PDF文件,因为网站为英文,内容又多,读起来非常费力,在解决了自己的问题后,看到许多网友还在为PDF文件制作而郁闷,遂决定将该内容翻译为中文,由于本人英语水平一般,许多地方又晦涩难懂,故翻译质量不是很满意,敬请斧正,但大部分能看懂。本文的目的一是解决部分网友的燃眉之急,二是抛砖引玉,如果哪位仁兄愿意将该网站中的内容准确翻译出来,则是天下之大幸。
要用本文的方法生成PDF文件,需要两个控件:itextsharp.dll和ICSharpCode.SharpZipLib.dll,由于示例代码实在太多,我将代码全部整理出来,放在另外一个文件“示例代码.doc”中,所有这些资源,我均放在了本人的ftp站点(ftp://202.107.251.26)上的“Pdf文件制作全攻略”文件夹中(文件夹中另外两个rar压缩文件为两个控件的源代码,供大家学习研究使用),你可以到这里下载相应的资源,或者直接到原网站下载。
为便于调试和叙述,所有例子均为DOS控制台程序,windows程序使用方法完全一样,按照下面的步骤创建一个可调试的项目:
1、  打开VS2003;
2、  单击菜单“文件”→“新建”→“项目”,在项目类型中选择“Visual C#项目”,在模板中选择“控制台应用程序”,输入文件名称如“MakePdf”,指定好存放路径,然后点确定按钮;
3、  在“解决方案资源管理器”中右键单击“引用”,从弹出的菜单中选择“添加引用”,在“.NET”选项夹中选择“浏览”,添加前面提到的两个应用,如下图:
4、  在代码窗口顶部添加两个引用:
using iTextSharp.text;
using iTextSharp.text.pdf;
至此,准备工作完毕。
ViewerPreferences 或Encryption)。所以通过下面的办法得到实例已经足够了: PdfWriter.getInstance(document, new FileStream("Chap01xx.pdf"));
在第一步中创建一个文档时,第一个参数意义不大,第二个参数可以是任何一种流,到目前为止我们一直使用System.IO.FileStream将Document写入文件中,示例代码0105用到了System.IO.MemoryStream(这不是一个独立的例子,你必须在Servlet Engine中测试这些代码。
Chap0111a.pdf和Chap0111b.pdf的区别
http://www.lowagie.com/iText/faq.html#images)。
u       通过URL得到图片实例
这是添加一个图片最简单的办法,见示例代码0601,我们添加了一个WMF、一个Gif、一个Jpeg和一个PNG图片到文档中,使用4个URL得到:
Image wmf = Image.getInstance(new URL("../examples/harbour.wmf"));
Image gif = Image.getInstance(new URL("../examples/vonnegut.gif"));
Image jpeg = Image.getInstance(new URL("../examples/myKids.jpg"));
Image png = Image.getInstance(new URL("../examples/hitchcock.png"));
备注:许多PDF库在插入一个图片前都将其解压缩并转换成位图格式,下面是几个我为什么不这样做的原因:
这将导致PDF文件增大,这样产生的PDF文件尺寸是不同图片文件尺寸总和的数十倍。 面临一个法律问题:LZW算法受专利保护,所以不允许使用这种算法来解压缩GIF等文件。
u       通过文件名得到图片实例
通过简单地改变图片引用路径将示例代码0601改成示例代码0602:
Image gif = Image.getInstance("vonnegut.gif");
Image jpeg = Image.getInstance("myKids.jpg");
Image png = Image.getInstance("hitchcock.png");
同示例代码0601的区别只是该图象从本地获取而已,另外一个例子见示例代码0603。
http://itextsharp.sourceforge.net/examples/h.gif下载得到。
http://itextsharp.sourceforge.net/examples/pngnow.png下载。
u       图片在表格中
你可以将图片添加到单元格中,但有两个副作用:
l       表格的宽度是确定,当图片超出单元格的宽度时,将自动缩小。
l       你不能进行文字绕排和为图片添加下划线。
参见示例代码0615。
u       图片链接注释
如果你希望得到一个可点击的图片,或者想添加链接注释到图片上,你需要创建一个Annotation对象,并添加到图片上,你不需要指定位置(你可以使用0,0,0,0),该位置会内部更新以适合该图片。
gif.Annotation = new Annotation(0, 0, 0, 0, "Chap1102b.pdf", 3);
jpeg.Annotation = new Annotation("picture", "These are my children", 0, 0, 0, 0);
参加示例代码0616。
caesar_coin.jpg的图片:

在第5章中,我们简要地讲述了PdfPTable对象,现在我们将讨论该对象更多的的特性。
你可以用3种不同的方法创建PdfTable:
PdfPTable(float[] relativeWidths);
PdfPTable(int numColumns);
PdfPTable(PdfPTable table);
你可以给该表设置更多的参数,如表宽度、列宽度、水平对齐方式等,你可以通过下面的办法添加单元格:
public void addCell(PdfPCell cell);
public void addCell(PdfPTable table);
public void addCell(Phrase phrase);
public void addCell(String text);
除了单元格填距和和间距,这些方法同Table对象非常类似。这些参数对每个单元格个体进行了设置,当然,你可以设置单元格的默认值,为改变单元格的默认值,使用getDefaultCell()和调用一个或更多的类PdfPCell的方法(你可以设置对齐方式、间距、边框、颜色甚至最低高度)。
注:通过PdfPTable,你能改变一个单元格的列跨度,但不能改变行跨度!在PdfPTable内部是一些独立的行,要让它支持行跨度更改需要对PdfPTable对象进行很大的调整,不要期望在近期内实现,你可以用嵌套表来解决这些问题。
你可以象第5章一样将一个PdfPTable添加到当前文档中,但你也可以添加一个表在当前页中的绝对位置:
public float writeSelectedRows(int rowStart, int rowEnd, float xPos, float yPos, PdfContentByte canvas);
参数rowStart是你想开始的行的数目,参数rowEnd是你想显示的最后的行(如果你想显示所有的行,用-1),xPos和yPos是表格的坐标,canvas是一个PdfContentByte对象。在示例代码1009中,我们添加了一个表在(100,600)处:
table.writeSelectedRows(0, -1, 100, 600, writer.DirectContent);
使用PdfPTable,你不能设置行跨度和(或)来跨度(怎么和上面的有点矛盾?)你可以使用嵌套表来解决,见示例代码1010。
最后,示例代码1011和示例代码1012展示了PdfTable可以和templates 和 columns一起使用,在示例代码1012中将用到cover.png图片如下:

SpotColors)和图案(Patterns)
颜色(spotcolors)的使用见示例代码1013,示例代码1014和示例代码1015演示了图案(patterns)的使用方法。
本地和异地转向、目标和概要
有时你需要一个允许读者从文档的一个地方跳转到另外一个地方的链接,你可以通过类Chunk的setLocalGoto 和setLocalDestination两个方法实现,例:
Chunk localgoto = new Chunk("this word", FontFactory.getFont(FontFactory.HELVETICA, 12, Font.NORMAL, new Color(0, 0, 255))).setLocalGoto("test");
Chunk destination = new Chunk("local destination", FontFactory.getFont(FontFactory.HELVETICA, 12, Font.NORMAL, new Color(0, 255, 0))).setLocalDestination("test");
见示例代码1101。
在第3章中,我们演示了一个锚点如何转向到其他URL,一个锚点通过不同的字体、风格和颜色,可以包含不同的Chunks,在iText的高级应用中,下面定义链接到URL的其他方法:
Chunk chunk = new Chunk("anchor", FontFactory.getFont(FontFactory.HELVETICA, 12)).setAnchor(new URL("http://www.lowagie.com/iText/"));
u       转到PDF文档中的指定位置
如果你在文档中指定了一个目的地,你可以从另外一个文档跳转到这里,为实现该功能,你可以使用方法:
setRemoteGoto: Chunk chunk = new Chunk("jump", FontFactory.getFont(FontFactory.HELVETICA, 12, Font.ITALIC)).setRemoteGoto("test.pdf", "test"));
test.pdf是另外一个pdf文件,”test”是该文件的一个目的地。
跳转到另一个PDF文件指定页
使用方法setRemoteGoto,用页码参数代替名称参数,可以非常容易地跳转定另外一个文档的指定页:
chunk = new Chunk("jump", FontFactory.getFont(FontFactory.HELVETICA, 12, Font.ITALIC)).setRemoteGoto("test.pdf", 3));
见示例代码1102
u       启动一个应用程序
可以使用下面的方法启动一个应用程序:
public PdfAction(String application, String parameters, String operation, String defaultDir)
如果application为“c:/winnt/notepad.exe”(其余参数可以为null),你可以通过PDF文件中的链接来启动记事本程序。
u       文件和URL
如果你想跳转到其他文档或URL,你需要通过下面的构造函数之一创建一个:
PdfAction(String filename, String name);
PdfAction(String filename, int page);
PdfAction(URL url);
PdfAction(String url);
前面两个构造函数允许你跳转到文件的指定位置或页码,后两个构造函数允许你跳转到其他URL上。
其余部分略。
页面和表格事件
略。
----------------------------------------------------------------------------------------------------------------------
package com.test;
import java.awt.Color;
import java.io.FileOutputStream;
import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.HeaderFooter;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.AcroFields;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfDictionary;
import com.lowagie.text.pdf.PdfImportedPage;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
import com.lowagie.text.pdf.PdfTemplate;
import com.lowagie.text.pdf.PdfWriter;
public class PDFMain {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
// TODO 自动生成方法存根
//test3();
test3();
}
public static void test3() throws Exception
{
//上、下、左、右页边距
Document document = new Document(PageSize.A4.rotate(),10,10,1,1);
PdfWriter writer=null;
try {
writer = PdfWriter.getInstance(document, new FileOutputStream("c://chapter5.pdf"));
}
catch(Exception e)
{
}
//  设置页眉和页脚
HeaderFooter   header   =   new   HeaderFooter(new   Phrase("页眉"),false);
HeaderFooter   footer   =   new   HeaderFooter(new   Phrase("页脚"),false);
document.setHeader(header);
document.setFooter(footer);
document.open();
chapter5(document,writer);
document.close();
}
private static  void chapter5(Document document,PdfWriter writer) throws Exception
{
BaseFont bfChinese=null;
try {
bfChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
} catch (Exception e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
Font fontChinesed = new Font(bfChinese, 20, Font.NORMAL);
Font fontChinesed1 = new Font(bfChinese, 15, Font.NORMAL);
Font fontChinesed2 = new Font(bfChinese, 15, Font.BOLD);
Paragraph par = new Paragraph("第五章投资资料表",fontChinesed);
par.setAlignment("center");
Paragraph par1 = new Paragraph("标段一",fontChinesed);
par1.setAlignment("center");
Paragraph p1 = new Paragraph("\t本资料表是根据本项目的具体情况对 “投标人须知”中有关条款所作的补充和修改。\n两者如有不一致,以本资料表为准。",fontChinesed1);
p1.setAlignment(Element.ALIGN_CENTER);
float [] widths={0.2f,0.8f};//20%,80%
Table tab1=new Table(2);
tab1.setWidths(widths);//设置表格2列所分配比例
tab1.setPadding(5);   //表格间高度
//tab1.setSpacing(5); //边框高度
//第一行
Paragraph par2 = new Paragraph("条款号",fontChinesed2);
par2.setAlignment("left");
Cell cel1=new Cell(par2);
tab1.addCell(cel1);
Paragraph par3 = new Paragraph("内容",fontChinesed2);
Cell cel2=new Cell(par3);
cel2.setVerticalAlignment(Element.ALIGN_CENTER); //水平方向居中
cel2.setHorizontalAlignment(Element.ALIGN_CENTER);//垂直方向居中
tab1.addCell(cel2);
//第二行,添加2列
Cell cel3=new Cell();
tab1.addCell(cel3);
Paragraph par5 = new Paragraph("说明",fontChinesed2);
Cell cel4=new Cell(par5);
cel4.setHorizontalAlignment(Element.ALIGN_LEFT);
tab1.addCell(cel4);
//第三行
Paragraph par6 = new Paragraph("1.1",fontChinesed2);
Cell cel6=new Cell(par6);
cel6.setVerticalAlignment(Element.ALIGN_CENTER);
cel6.setRowspan(2);
tab1.addCell(cel6);
//拆分
Paragraph par7 = new Paragraph("招标人名称:",fontChinesed2);
Cell tab2_cel1=new Cell(par7);
Paragraph par8 = new Paragraph("合同名称:",fontChinesed2);
Cell tab2_cel2=new Cell(par8);
tab1.addCell(tab2_cel1);
tab1.addCell(tab2_cel2);
//第四行
Cell cel7=new Cell();
cel7.setVerticalAlignment(Element.ALIGN_LEFT);
tab1.addCell(cel7);
Paragraph par9 = new Paragraph("投标报价",fontChinesed2);
Cell cel8=new Cell(par9);
tab1.addCell(cel8);
//第五行
Paragraph par10 = new Paragraph("11",fontChinesed2);
Cell cel9=new Cell(par10);
cel9.setVerticalAlignment(Element.ALIGN_CENTER);
cel9.setRowspan(2);
tab1.addCell(cel9);
//拆分
Paragraph par11 = new Paragraph("投标人应单独报出货物由投标人制造厂当地交到买方工厂现场所需的运输费和\n保险费,作为招标人评标时的评标因素。",fontChinesed2);
Cell tab3_cel1=new Cell(par11);
Paragraph par12 = new Paragraph("投标报价为:",fontChinesed2);
Paragraph par13 = new Paragraph("相关费用:包括货物总价、包装费和伴随服务费等\n"+
"(说明:\n"+
"\t\ta. 投标人在报价时,包括制造和组装货物使用的配套件和原材料费、\n"+
"\t\t相关服务费和已付的全部增值税或其它税(所有税费填写时均计\n"+
"\t\t入货价)。\n"+
"\t\tb. 列出要求的备件的费用和清单。\n"+
"\t\tc. 列出随供件、易损件和专用工具的费用和清单。\n"+
"\t\td. 每一货物的价格应是唯一的,招标机构和招标人不接受任何可选\n"+
"\t\t的报价。\n"+
"\t\te. 合同要求的设计费含技术资料费。\n"+
"\t\tf. 履行合同所需的所有项目,如没有另外说明价款,有关费用视作\n"+
"\t\t已包括在其他有价款的项目的单价和总价中。\n"+
"\t\tg. 所有项目应列出数量、单价和生产厂商等的明细清单,投标人如\n"+
"\t\t将数个项目以一个总额标价,招标机构有权视情况要求投标人将\n"+
"\t\t每项单价分列表示。)\n"+
"\t\th. 投标人承诺投标基本方案价已包含招标文件所规定的所有货物和\n"+
"\t\t服务范围。此报价若有漏项,由投标人自行承担。履行合同所需\n"+
"\t\t的所有项目,如没有另外单独价款说明,有关费用视作已包括在\n"+
"\t\t其他有价款的项目的单价和总价中。",fontChinesed2);
par12.add(par13);
Cell tab3_cel2=new Cell(par12);
tab1.addCell(tab3_cel1);
tab1.addCell(tab3_cel2);
//第六行
Cell cel10=new Cell();
cel10.setVerticalAlignment(Element.ALIGN_LEFT);
tab1.addCell(cel10);
Paragraph par14 = new Paragraph("证明投标人合格和资格的文件",fontChinesed2);
Cell cel11=new Cell(par14);
tab1.addCell(cel11);
//第七行
Paragraph par15 = new Paragraph("13",fontChinesed2);
Cell cel12=new Cell(par15);
cel12.setVerticalAlignment(Element.ALIGN_LEFT);
tab1.addCell(cel12);
Paragraph par16 = new Paragraph("资格条件和提供文件:",fontChinesed2);
Paragraph par17 = new Paragraph("\n任何对上述资格条件的偏离和没有实质性的响应均将导致废标。",fontChinesed2);
par16.add(par17);
Cell cel13=new Cell(par16);
tab1.addCell(cel13);
//第八行
par15 = new Paragraph("14.3",fontChinesed2);
cel12=new Cell(par15);
cel10.setVerticalAlignment(Element.ALIGN_LEFT);
tab1.addCell(cel12);
par16 = new Paragraph("提供货物保证期满后运行1 年所需的备件清单,包括货源和价格表。(不计入投标总价)",fontChinesed2);
cel13=new Cell(par16);
tab1.addCell(cel13);
//第九行
cel12=new Cell();
cel10.setVerticalAlignment(Element.ALIGN_LEFT);
tab1.addCell(cel12);
par16 = new Paragraph("投标文件的编制和递交",fontChinesed2);
cel13=new Cell(par16);
tab1.addCell(cel13);
//第十行
par15 = new Paragraph("15.1",fontChinesed2);
cel12=new Cell(par15);
cel10.setVerticalAlignment(Element.ALIGN_LEFT);
tab1.addCell(cel12);
par16 = new Paragraph("投标保证金:",fontChinesed2);
cel13=new Cell(par16);
tab1.addCell(cel13);
//第十一行
par15 = new Paragraph("15.3",fontChinesed2);
cel12=new Cell(par15);
cel10.setVerticalAlignment(Element.ALIGN_LEFT);
tab1.addCell(cel12);
par16 = new Paragraph("投标保证金采用形式:投标保证金应注明招标编号,应使用人民币,采用投标人"+
"企业网银、电汇、银行汇票(不包括承兑汇票)、本票、支票(仅限于上海本地"+
"支票)、贷记凭证、现金交款单等形式(现金交款单原件应交于宝华招标财务),"+
"不接收转帐支票、全国支票、人民币现钞、银行保函、已背书的票据和以个人名"+
"义汇入的汇款或解现等。",fontChinesed2);
cel13=new Cell(par16);
tab1.addCell(cel13);
//第十二行
par15 = new Paragraph("16.1",fontChinesed2);
cel12=new Cell(par15);
cel10.setVerticalAlignment(Element.ALIGN_LEFT);
tab1.addCell(cel12);
par16 = new Paragraph("投标有效期: 90 天",fontChinesed2);
cel13=new Cell(par16);
tab1.addCell(cel13);
//第十三行
par15 = new Paragraph("17.1",fontChinesed2);
cel12=new Cell(par15);
cel10.setVerticalAlignment(Element.ALIGN_LEFT);
tab1.addCell(cel12);
par16 = new Paragraph("同一招标项目下的所有投标文件应采用投标人企业同一电子CA 签名。",fontChinesed2);
cel13=new Cell(par16);
tab1.addCell(cel13);
//第十四行
par15 = new Paragraph("18.2",fontChinesed2);
cel12=new Cell(par15);
cel10.setVerticalAlignment(Element.ALIGN_LEFT);
tab1.addCell(cel12);
par16 = new Paragraph("第9.2 条2)、3)、4)、8)和9.3条1)内容须经投标人企业同一电子CA签名和加密。",fontChinesed2);
cel13=new Cell(par16);
tab1.addCell(cel13);
//第十五行
cel12=new Cell();
cel10.setVerticalAlignment(Element.ALIGN_LEFT);
tab1.addCell(cel12);
par16 = new Paragraph("开标",fontChinesed2);
cel13=new Cell(par16);
tab1.addCell(cel13);
//第十六行
par15 = new Paragraph("19.1",fontChinesed2);
cel12=new Cell(par15);
cel10.setVerticalAlignment(Element.ALIGN_LEFT);
tab1.addCell(cel12);
par16 = new Paragraph("投标截止期:(北京时间)\n",fontChinesed2);
par17 = new Paragraph("开标地点:",fontChinesed2);
par16.add(par17);
cel13=new Cell(par16);
tab1.addCell(cel13);
//第十七行
par15 = new Paragraph("22.1",fontChinesed2);
cel12=new Cell(par15);
cel10.setVerticalAlignment(Element.ALIGN_LEFT);
tab1.addCell(cel12);
par16 = new Paragraph("第一步开标时间:(北京时间)\n",fontChinesed2);
par17 = new Paragraph("第二步开标时间:(北京时间)",fontChinesed2);
par16.add(par17);
cel13=new Cell(par16);
tab1.addCell(cel13);
//第十八行
cel12=new Cell();
cel10.setVerticalAlignment(Element.ALIGN_LEFT);
tab1.addCell(cel12);
par16 = new Paragraph("评标",fontChinesed2);
cel13=new Cell(par16);
tab1.addCell(cel13);
//第十九行
par6 = new Paragraph("25.3",fontChinesed2);
cel6=new Cell(par6);
cel6.setVerticalAlignment(Element.ALIGN_CENTER);
cel6.setRowspan(2);
tab1.addCell(cel6);
//拆分
par7 = new Paragraph("交货时间:以下选择",fontChinesed2);
tab2_cel1=new Cell(par7);
par8 = new Paragraph("付款条件:按招标文件第六章合同资料表要求付款。",fontChinesed2);
tab2_cel2=new Cell(par8);
tab1.addCell(tab2_cel1);
tab1.addCell(tab2_cel2);
try {
document.add(par);
document.add(par1);
document.add(p1);
document.add(tab1);
} catch (Exception e) {
// TODO 自动生成 catch 块
e.printStackTrace();
throw new Exception(e);
}
}
public static void test1() throws Exception
{
Document document = new Document(PageSize.A4.rotate(),10,10,10,10);
FileOutputStream fops = new FileOutputStream("c://test.pdf");
PdfWriter.getInstance(document, fops);
document.open();
BaseFont bfChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font FontChinesed = new Font(bfChinese, 15, Font.NORMAL);
Paragraph par = new Paragraph("aaaaa",FontChinesed);
document.add(par);
Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);
par = new Paragraph("时间: 2007-2-1 -- 2007-4-8", FontChinese);
document.add(par);
par = new Paragraph(" ", FontChinese);
Table table = new Table(3);
table.setBorderWidth(5);
table.setBorderColor(new Color(0, 0, 255));
table.setPadding(5);
table.setSpacing(5);
Cell cell = new Cell("header");
cell.setHeader(true);
cell.setColspan(3);
table.addCell(cell);
table.endHeaders();
cell = new Cell("example cell with colspan 1 and rowspan 2");
cell.setRowspan(2);
cell.setBorderColor(new 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.setRowspan(2);
cell.setColspan(2);
table.addCell(cell);
table.addCell("cell test2");
document.add(table);
document.close();
}
public static void test2() throws Exception
{
/*打开已经定义好字段以后的pdf模板*/
PdfReader reader = new PdfReader("c://chapter5InvestTemplate.pdf");
/*将要生成的目标PDF文件名称*/
//  PdfStamper stamp = new PdfStamper(reader, new FileOutputStream("c://iText报表结果.pdf"));
//  PdfContentByte under = stamp.getUnderContent(1);
/*使用中文字体*/
BaseFont bf = BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
Font FontChinese = new Font(bf, 12, Font.NORMAL);
/*取出报表模板中的所有字段*/
//AcroFields form = stamp.getAcroFields();
/*为字段赋值,注意字段名称是区分大小写的*/
//  form.setField("Name", "裴贺先");
//  form.setField("Age","26");
//  form.setField("xueli","高二辍学");
//  form.setField("email","phx@x263.net");
//  form.setField("beizhu","用iText做报表简单吗?");
//  stamp.setFormFlattening(true);
//stamp.close();
/*必须要调用这个,否则文档不会生成的*/
Document document   =   new   Document(reader.getPageSize(1));
PdfWriter   writer   =   PdfWriter.getInstance(document,   new   FileOutputStream("c://iText多行报表结果.pdf"));
document.open();
PdfContentByte   cb   =   writer.getDirectContent();
int pageNumber=1;
PdfImportedPage   page1   =   writer.getImportedPage(reader,pageNumber);
//cb.addTemplate(page1,0,1f,1f,1f,0,0);
// cb.addTemplate(page1,   1f,   0,   0,   1f,   0,   0);
//cb.addTemplate(page1, 0, 1, -1, 0, 600, 500);
//cb.addTemplate(page1, 0, 1, 0, 1, 600, 500);
//float textBase = document.bottom() - 20;
//cb.addTemplate(page1,document.left(),textBase);
//cb.addTemplate(page1, 1, -1, 1, 5, 10, 700);
PdfTemplate template = cb.createTemplate(500, 200);
template.moveTo(0, 200);
template.lineTo(500, 0);
template.stroke();
template.beginText();
bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
template.setFontAndSize(bf, 12);
template.setTextMatrix(100, 100);
template.showText("Text at the position 100,100 (relative to the template!)");
template.endText();
//cb.addTemplate(template, 0, 400);
cb.addTemplate(page1,(float)Math.cos(180/180),(float)Math.sin(180/180),(float)Math.sin(180/180),(float)Math.cos(180/180), 0, 400);
//  cb.beginText();
//  cb.setFontAndSize(bf,   12);
//  cb.setTextMatrix(200,   20);   //这里就是用来定位的值
//  cb.showText("sdfsdfsdf");
//  cb.endText();
document.close();
}
}
提示:你把pdfWriter 改成 rtfWriter 就可以生成word文档了。没试过,理论是这样的 ^_^