`
8366
  • 浏览: 801171 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类
最新评论

Itext操做PDF文档

 
阅读更多

 

 

iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库。通过iText不仅可以生成PDF或rtf的文档,而且可以将XML、Html文件转化为PDF文件。 iText的安装非常方便,在http://www.lowagie.com/iText/download.html - download 网站上下载iText.jar文件后,只需要在系统的CLASSPATH中加入iText.jar的路径,在程序中就可以使用iText类库了。

 

网站:

官方网站:http://www.lowagie.com/iText/

api:http://www.1t3xt.info/api/

 

 

步骤:首先下载需要的jar,主要有三个,将这3个包导入到我们的web项目的lib目录下,就可以使用itext生成 标准的pdf文档了

 

iText.jar : 必须的jar

 

iTextAsian.jar: 支持中文的jar

bcprov-jdk15-140.jar: pdf文档加密所要使用的一个包

 

例子:生成一个HelloWord!:

 

用iText生成PDF文档需要5个步骤:

 

①建立com.lowagie.text.Document对象的实例。 Document document = new Document(); 

②建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中。

PDFWriter.getInstance(document, new FileOutputStream("Helloworld.PDF")); 

③打开文档。 document.open(); 

④向文档中添加内容。 document.add(new Paragraph("Hello World")); 

⑤关闭文档。 document.close(); 

通过上面的5个步骤,就能产生一个Helloworld.PDF的文件,文件内容为"Hello World"。

 

 

如何支持中文:

在这里我们的的解决思路是将字体构造成中文的,然后在需要中文的时候,使用该字体完成对中文的支持

 

BaseFont bf=null;

//定义标题中文标题字体

Font fontTopic = null;

//定义黑色字体

Font fontBlack=null;

 

bf = BaseFont.createFont("STSong-Light",

"UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

fontTopic = new Font(bf, 14, Font.NORMAL,Color.BLUE);

fontBlack = new Font(bf, 6, Font.NORMAL,Color.BLACK);

 

首先要定义一个基础字体BaseFont bf,用他构建一个可以书写中文的字体,其他字体如fontTopic ,fontBlack 可以在这个字体上构建,给他加入一些新的特性,比如字体大小,颜色,在构建好以后,就可以使用这些字体了,用他们来书写中文

 

设置文档页面大小,页面布局

 

//new文档的时候,可以使用下面的构造方法,传递纸的大小,左边距,右边距,上边距,下边距

 

Document document = new Document(PageSize.A4,60,60,50,50);

 

添加段落:

//使用字体fontTopic

1.Paragraph p= new Paragraph("套餐论证指标",fontTopic);

//设置段落对其方式为居中,1-居中,2-具右,默认具左

2.p.setAlignment(1);

3.document.add(p);

如果我们需要页面布局,比如在标题下面和表格上面有一些空白,我门可以用几个空段落来实现这个目标,也是3

Paragraph p1= new Paragraph(" ",fontTopic);

p1.setAlignment(1);

document.add(p1);

 

 

 

 

添加表格:

 

  1. Table table = new Table(8); //构造一个有8列的表格

 

//设置列宽

  1. int headerwidths[] = {20, 22, 22, 10, 11, 11, 11, 10};
  2. table.setWidths(headerwidths);
  3. table.setWidth(110);
  4. table.setPadding(3);//设置表格中cell相对表格的大小,值越大生成表格的cell越大
  5. Cell cell=new Cell(new Paragraph("商品名称", fontRed));//给表格里添加一个cell

table.addCell(cell);

  1. //如果要设置下一个cell 3 则可以
  2. cell= new Cell(new Paragraph(""));

cell.setColspan(2);

table.addCell(cell);

  1. //最后记得
  2. document.add(table);
  3. document.close();

 

在我们构造我们需要的表格的样子时候,我们最好一行一行构造,在有行跨多列的时候,我们可以使用setColspan(?),这样我们就可以清晰的构造出我们想要的表格的样式了,不要一会构造行,一会构造列,到最后就乱了

 

设置图片:

 

  1. Image jpgBack = Image.getInstance("logo.gif");
  2. jpgBack.setAbsolutePosition(3, 768);//设置图片的左上角的坐标为(3, 768),注意屏幕左下脚的坐标为(0,0)
  3. pgBack.setAlignment(Image.UNDERLYING);
  4. document.add(jpgBack);

 

设置水印图片和水印文字

 

水印图片和水印文字主要是给已经存在的pdf文档中添加的,假设我们的目录下有Chap0101.pdf 文档,现在我们要给这个文档添加水印图片和水印文字

 

PdfReader reader = new PdfReader("Chap0101.pdf");

PdfContentByte under;//文档起始页

PdfContentByte over;//文档结束页

Image img = Image.getInstance("watermark.jpg");

//设置图片显示的位置

img.setAbsolutePosition(200, 500);

//水印效果一定是给一个已经存在的文档加的,加好以后要另起一个名字

PdfStamper stamp = new PdfStamper(reader, new FileOutputStream("Chap0102.pdf"));

under = stamp.getUnderContent(1);

under.addImage(img);

over = stamp.getOverContent(1);

over.beginText();

over.setFontAndSize(bf, 30);

over.showTextAligned(Element.ALIGN_LEFT, " 

析", 220, 500, 45);

 

over.endText();

stamp.close();

//删除以前没有水印文件

File file =new File("Chap0101.pdf");

 

if(file.exists())

{

file.delete();

}

 

设置密码:

 

设置密码也是在写之前就要先设置密码,主要是在得到PdfWriter  对象后,就开始对文档加密

 

 

PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream("Chap0102.pdf"));

writer.setEncryption(PdfWriter.STRENGTH128BITS, "user", "8366", PdfWriter.AllowCopy | PdfWriter.AllowPrinting);

 

然后进行其他操作

 

完整的在套餐分析中使用itext的代码以及显示效果:

 

 

public class NewPDF

 

{

 

public static void main(String[] args)

{

 

BaseFont bf=null;

//定义标题中文标题字体

Font fontTopic = null;

//定义黑色字体

Font fontBlack=null;

//定义红色字体

Font fontRed=null;

try

{

 

bf = BaseFont.createFont("STSong-Light",

"UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

fontTopic = new Font(bf, 14, Font.NORMAL,Color.BLUE);

fontBlack = new Font(bf, 6, Font.NORMAL,Color.BLACK);

fontRed = new Font(bf, 6, Font.NORMAL,Color.RED);

 

Document document = new Document(PageSize.A4,60,60,60,50);

 

 

PdfWriter.getInstance(document, new FileOutputStream ("Chap0101.pdf"));

 

document.open();

//使用自定义的中文段落字体

Paragraph p= new Paragraph("套餐论证指标",fontTopic);

 

//添加两行空行

Paragraph p1= new Paragraph(" ",fontTopic);

Paragraph p2= new Paragraph(" ",fontTopic);

 

 

//设置段落对其方式为居中,1-居中,2-具右,默认具左

p.setAlignment(1);

p1.setAlignment(1);

p2.setAlignment(1);

document.add(p);

document.add(p1);

document.add(p2);

 

//设置图片

Image jpgBack = Image.getInstance("logo.gif");

jpgBack.setAbsolutePosition(3, 768);

 

jpgBack.setAlignment(Image.UNDERLYING);

 

document.add(jpgBack);

//设置表格为8行

 

Table table = new Table(8);

 

//设置列宽

int headerwidths[] = {20, 22, 22, 10, 11, 11, 11, 10};

table.setWidths(headerwidths);

table.setWidth(110);

//设置表格中cell相对表格的大小,值越大生成表格的cell越大

table.setPadding(2);

Cell cell=new Cell(new Paragraph("商品名称", fontRed));

table.addCell(cell);

 

cell= new Cell(new Paragraph("商称的值", fontBlack));

table.addCell(cell);

 

cell= new Cell(new Paragraph(""));

cell.setColspan(2);

table.addCell(cell);

 

 

cell= new Cell(new Paragraph("宽表", fontRed));

table.addCell(cell);

 

cell= new Cell(new Paragraph("时间", fontBlack));

table.addCell(cell);

 

cell= new Cell(new Paragraph("", fontBlack));

cell.setColspan(2);

table.addCell(cell);

 

cell= new Cell(new Paragraph("新增客户数", fontRed));

table.addCell(cell);

 

 

cell= new Cell(new Paragraph("新客数值", fontBlack));

table.addCell(cell);

 

cell= new Cell(new Paragraph("", fontBlack));

cell.setColspan(6);

table.addCell(cell);

 

cell= new Cell(new Paragraph("纯理性条件下本网预计目标客户数", fontRed));

table.addCell(cell);

 

cell= new Cell(new Paragraph("纯数值", fontBlack));

table.addCell(cell);

cell= new Cell(new Paragraph("", fontBlack));

cell.setColspan(6);

table.addCell(cell);

 

cell= new Cell(new Paragraph("合理性条件下本网预计目标客户数", fontRed));

table.addCell(cell);

 

cell= new Cell(new Paragraph("合数值", fontBlack));

table.addCell(cell);

cell= new Cell(new Paragraph("", fontBlack));

cell.setColspan(6);

table.addCell(cell);

 

cell= new Cell(new Paragraph("最大可能损失指标", fontRed));

table.addCell(cell);

 

cell= new Cell(new Paragraph("最标值", fontBlack));

table.addCell(cell);

cell= new Cell(new Paragraph("", fontBlack));

cell.setColspan(6);

table.addCell(cell);

 

 

cell= new Cell(new Paragraph("本网客户收益", fontRed));

table.addCell(cell);

 

cell= new Cell(new Paragraph("收益值", fontBlack));

table.addCell(cell);

cell= new Cell(new Paragraph("新增客户收益", fontRed));

table.addCell(cell);

 

cell= new Cell(new Paragraph("信益值", fontBlack));

table.addCell(cell);

cell= new Cell(new Paragraph("挽留客户数", fontRed));

table.addCell(cell);

 

cell= new Cell(new Paragraph("挽值", fontBlack));

table.addCell(cell);

 

cell= new Cell(new Paragraph("", fontBlack));

cell.setColspan(2);

table.addCell(cell);

 

cell= new Cell(new Paragraph("发展预计", fontRed));

cell.setColspan(8);

table.addCell(cell);

 

 

cell= new Cell(new Paragraph("", fontRed));

table.addCell(cell);

 

cell= new Cell(new Paragraph("纯理性条件下本网预计目标客户数(个)", fontRed));

 

table.addCell(cell);

 

cell= new Cell(new Paragraph("合理性条件下本网预计目标客户数(个)", fontRed));

 

table.addCell(cell);

 

cell= new Cell(new Paragraph("新增客户数(个)", fontRed));

 

table.addCell(cell);

cell= new Cell(new Paragraph("最大可能损失(元)", fontRed));

 

table.addCell(cell);

cell= new Cell(new Paragraph("本网客户收益(元)", fontRed));

 

table.addCell(cell);

cell= new Cell(new Paragraph("新增客户收益(元)", fontRed));

 

table.addCell(cell);

cell= new Cell(new Paragraph("挽留客户数(个)", fontRed));

 

table.addCell(cell);

 

cell= new Cell(new Paragraph("第一月", fontRed));

 

table.addCell(cell);

cell= new Cell(new Paragraph("值1", fontBlack));

 

table.addCell(cell);

cell= new Cell(new Paragraph("值2", fontBlack));

 

table.addCell(cell);

cell= new Cell(new Paragraph("值3", fontBlack));

 

table.addCell(cell);

cell= new Cell(new Paragraph("值4", fontBlack));

 

table.addCell(cell);

cell= new Cell(new Paragraph("值5", fontBlack));

 

table.addCell(cell);

cell= new Cell(new Paragraph("值7", fontBlack));

 

table.addCell(cell);

cell= new Cell(new Paragraph("值7", fontBlack));

 

table.addCell(cell);

cell= new Cell(new Paragraph("第二月", fontRed));

 

table.addCell(cell);

cell= new Cell(new Paragraph("值1", fontBlack));

 

table.addCell(cell);

cell= new Cell(new Paragraph("值2", fontBlack));

 

table.addCell(cell);

cell= new Cell(new Paragraph("值3", fontBlack));

 

table.addCell(cell);

cell= new Cell(new Paragraph("值4", fontBlack));

 

table.addCell(cell);

cell= new Cell(new Paragraph("值5", fontBlack));

 

table.addCell(cell);

cell= new Cell(new Paragraph("值7", fontBlack));

 

table.addCell(cell);

cell= new Cell(new Paragraph("值7", fontBlack));

 

table.addCell(cell);

cell= new Cell(new Paragraph("第三月", fontRed));

 

table.addCell(cell);

cell= new Cell(new Paragraph("值1", fontBlack));

 

table.addCell(cell);

cell= new Cell(new Paragraph("值2", fontBlack));

 

table.addCell(cell);

cell= new Cell(new Paragraph("值3", fontBlack));

 

table.addCell(cell);

cell= new Cell(new Paragraph("值4", fontBlack));

 

table.addCell(cell);

cell= new Cell(new Paragraph("值5", fontBlack));

 

table.addCell(cell);

cell= new Cell(new Paragraph("值7", fontBlack));

 

table.addCell(cell);

cell= new Cell(new Paragraph("值7", fontBlack));

 

table.addCell(cell);

document.add(table);

document.close();

PdfReader reader = new PdfReader("Chap0101.pdf");

PdfContentByte under;

PdfContentByte over;

Image img = Image.getInstance("watermark.jpg");

img.setAbsolutePosition(200, 500);

//水印效果一定是给一个已经存在的文档加的,加好以后要另起一个名字

PdfStamper stamp = new PdfStamper(reader, new FileOutputStream("Chap0102.pdf"));

 

//设置密码,给文档写之前就要先设定密码,并且设置为可拷贝,可打印

stamp.setEncryption(PdfWriter.STRENGTH128BITS, "userpass", "8366", PdfWriter.AllowCopy | PdfWriter.AllowPrinting);

under = stamp.getUnderContent(1);

 

under.addImage(img);

 

over = stamp.getOverContent(1);

over.beginText();

over.setFontAndSize(bf, 30);

over.showTextAligned(Element.ALIGN_LEFT, " 

析", 220, 500, 45);

 

over.endText();

stamp.close();

//删除以前没有水印文件

File file =new File("Chap0101.pdf");

 

if(file.exists())

{

file.delete();

}

 

}

catch(Exception e)

{

e.printStackTrace();

}

 

 

 

}

 

}

 

 

效果:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics