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

初学PDF类库 itext

阅读更多

使用PDF类库itext2.1.4
javaDoc下载地址:http://nchc.dl.sourceforge.net/sourceforge/itext/iText-docs-2.1.4.tar.gz
iText-2.1.4.jar URL:http://jaist.dl.sourceforge.net/sourceforge/itext/iText-2.1.4.jar
iText-2.1.4-sources.jar URL:http://jaist.dl.sourceforge.net/sourceforge/itext/iText-2.1.4-sources.jar

1.生成简单hello world级别PDF文件
 * 引入itext.2.1.4.jar
  import com.lowagie.text.*;
 
 * 生成文档对象: Document document = new Document();
 * 生成PdfWriter对象: PdfWriter.getInstance(document, new FileOutputStream("d:\\helloworldB.pdf"));
 * 打开文档对象: document.open();
 * 给文档加入内容对象: document.add(new Chunk("Hello World!"));
  Chunk是可加入的最小单元,其它可加入对象还有Phrase Paragraph等
 * 关闭文档对象: document.close();
 
2.更改默认分隔符,当文本内容太长需要换行时,itext会根据分隔符截断文本进行换行
 * 使用setSplitCharacter(SplitCharacter splitCharacter)方法
 
 * SplitCharacter是一个接口,需要我们传递一个类实现它的isSplitCharacter方法
  参见splitCharacter()方法或itext文档
  
 * 无意间发现: 在对一个对象改变内容后,有可能会影响到前面已经使用过这个对象的地方
  Chunk chunk = new Chunk("a");
  Paragraph paragraph = new Paragraph(chunk);
  //此时paragraph 的内容应该是"a";
  chunk.append("bc");
  //此时paragraph 的内容就改变成是"abc";
  参见testChange()方法

import java.awt.Color;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.SplitCharacter;
import com.lowagie.text.pdf.PdfChunk;
import com.lowagie.text.pdf.PdfWriter;

/**
 * @author wfy
 *
 */
public class HelloWorld {
	public HelloWorld(){		
	}
	
	public static void main(String[] args){
		try {
			HelloWorld hw = new HelloWorld();
			
			//生成一个简单PDF文件
			//hw.createHelloWorldPdf();
			
			//测试分隔符
			//hw.splitCharacter();
			
			hw.testChange();
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public void createHelloWorldPdf() throws Exception{
		Document document = new Document();
		//PdfWriter pdfWriterA = PdfWriter.getInstance(document, new FileOutputStream("d:\\helloworldA.pdf"));
		PdfWriter pdfWriterB = PdfWriter.getInstance(document, new FileOutputStream("d:\\helloworldB.pdf"));
		document.open();
		//pdfWriterA.pause();
		document.add(new Paragraph("Writer to :"));
		//pdfWriterA.resume();
		Chunk chunk = new Chunk("Hello World", FontFactory.getFont(FontFactory.COURIER, 20, Font.ITALIC, new Color(255, 0, 0)));
		document.add(chunk);
		chunk.setTextRise((float) 2.0);
		Paragraph paragraph1 = new Paragraph("this is a Paragraph");
		paragraph1.add(new Chunk("add a chunk", FontFactory.getFont(FontFactory.COURIER_BOLD, 20, Font.SYMBOL, new Color(10, 0 , 250))));
		paragraph1.add(new Paragraph( "insert"));
		paragraph1.add("String");
		paragraph1.add(new Paragraph(" Paragraph2"));
		Phrase phrase1 = new Phrase("string1 ", new Font(Font.TIMES_ROMAN));
		phrase1.add("string2 ");
		phrase1.add(new Phrase("string3", new Font(Font.HELVETICA)));
		document.add(phrase1);
		document.add(paragraph1);
		document.close();
	}
	public void splitCharacter() throws Exception{
		Document document = new Document();
		PdfWriter.getInstance(document, new FileOutputStream("d:\\splitchar.pdf"));
		document.open();
		Chunk chunk = new Chunk("app-*.xml/i text ");
		
		//更改默认分隔符" "和"-"为"/"
		chunk.setSplitCharacter(new SplitCharacter(){

			public boolean isSplitCharacter(int start, int current, int end,
					char[] cc, PdfChunk[] ck) {
				char c;
			    if (ck == null)
			        c = cc[current];
			    else
			        c = (char) ck[Math.min(current, ck.length - 1)].getUnicodeEquivalent(cc[current]);
			    
			    //用此句替换下面被注释的默认设置
			    if(c == '/')
			    	return true;
			    /*if (c <= ' ' || c == '-') {
			        return true;
			    }*/
			    
			    if (c < 0x2e80)
			        return false;
			    return ((c >= 0x2e80 && c < 0xd7a0)
			    || (c >= 0xf900 && c < 0xfb00)
			    || (c >= 0xfe30 && c < 0xfe50)
			    || (c >= 0xff61 && c < 0xffa0));

			}
			
		});
		Paragraph paragraph1 = new Paragraph("01" + chunk);
		paragraph1.add(chunk);
		paragraph1.add(chunk);
		paragraph1.add(chunk);
		paragraph1.add(chunk);
		paragraph1.add(chunk);
		paragraph1.add(chunk);
		
		//此语句影响到前面使用过chunk的地方
		paragraph1.add(chunk.append(" wfy ").toString());
		
		document.add(paragraph1);
		//document.add(new Phrase("WFY"));
		paragraph1.clear();
		
		paragraph1.add("02" + chunk+ "wfy789 ");
		paragraph1.add(chunk);
		paragraph1.add(chunk);
		paragraph1.add(chunk);
		paragraph1.add(chunk);
		paragraph1.add(chunk);
		document.add(paragraph1);
		document.close();
	}
	
	public void testChange() throws Exception{
		Document doc = new Document();
		PdfWriter.getInstance(doc, new FileOutputStream("d:\\testChange.pdf"));
		doc.open();
		Chunk chunk = new Chunk("a");
		doc.add(new Paragraph("before change: " + chunk));
		
		//在chunk对象更改之前已经定义好了paragraph对象
		Paragraph p = new Paragraph(chunk);
		
		chunk.append("bc");
		doc.add(new Chunk("after change: " + chunk));
		doc.add(p);
		doc.close();
	}
}

 

分享到:
评论

相关推荐

    java源码包---java 源码 大量 实例

     基于JAVA的UDP服务器模型源代码,内含UDP服务器端模型和UDP客户端模型两个小程序,向JAVA初学者演示UDP C/S结构的原理。 简单聊天软件CS模式 2个目标文件 一个简单的CS模式的聊天软件,用socket实现,比较简单。 ...

    JAVA上百实例源码以及开源项目源代码

     基于JAVA的UDP服务器模型源代码,内含UDP服务器端模型和UDP客户端模型两个小程序,向JAVA初学者演示UDP C/S结构的原理。 简单聊天软件CS模式 2个目标文件 一个简单的CS模式的聊天软件,用socket实现,比较简单。 ...

    JAVA上百实例源码以及开源项目

     基于JAVA的UDP服务器模型源代码,内含UDP服务器端模型和UDP客户端模型两个小程序,向JAVA初学者演示UDP C/S结构的原理。 简单聊天软件CS模式 2个目标文件 一个简单的CS模式的聊天软件,用socket实现,比较简单。 ...

    java源码包2

     基于JAVA的UDP服务器模型源代码,内含UDP服务器端模型和UDP客户端模型两个小程序,向JAVA初学者演示UDP C/S结构的原理。 简单聊天软件CS模式 2个目标文件 一个简单的CS模式的聊天软件,用socket实现,比较简单。 ...

    java源码包3

     基于JAVA的UDP服务器模型源代码,内含UDP服务器端模型和UDP客户端模型两个小程序,向JAVA初学者演示UDP C/S结构的原理。 简单聊天软件CS模式 2个目标文件 一个简单的CS模式的聊天软件,用socket实现,比较简单。 ...

    java源码包4

     基于JAVA的UDP服务器模型源代码,内含UDP服务器端模型和UDP客户端模型两个小程序,向JAVA初学者演示UDP C/S结构的原理。 简单聊天软件CS模式 2个目标文件 一个简单的CS模式的聊天软件,用socket实现,比较简单。 ...

    成百上千个Java 源码DEMO 4(1-4是独立压缩包)

    Message-Driven Bean EJB实例源代码 2个目标文件 摘要:Java源码,初学实例,EJB实例 Message-Driven Bean EJB实例源代码,演示一个接收购物订单的消息驱动Bean,处理这个订单同时通过e-mail的形式 //给客户发一个感谢...

    成百上千个Java 源码DEMO 3(1-4是独立压缩包)

    Message-Driven Bean EJB实例源代码 2个目标文件 摘要:Java源码,初学实例,EJB实例 Message-Driven Bean EJB实例源代码,演示一个接收购物订单的消息驱动Bean,处理这个订单同时通过e-mail的形式 //给客户发一个感谢...

    Java开发实战1200例(第1卷).(清华出版.李钟尉.陈丹丹).part3

    PDF格式扫描版,全书分为24章,共817页。2011年1月出版。 全书压缩打包成4部分,这是第3部分 注:本系列图书的第I、II卷再版时均相应改名为《xxx开发实例大全》(基础卷)及(提高卷),但内容基本无变化,需要的童鞋可...

Global site tag (gtag.js) - Google Analytics