iText制作PDF文件 学习笔记 (一)

iText制作PDF文件

简介:iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库。通过iText不仅可以生成PDF或rtf的文档,而且可以将XML、Html文件转化为PDF文件。 iText的安装非常方便,下载iText.jar文件后,只需要在系统的CLASSPATH中加入iText.jar的路径,在程序中就可以使用iText类库了。 [摘自百度百科]

jar文件:所用到的jar包如图所示:

(一)学习如何制作一个简单的PDF,并且可以自定义它的大小,背景,页数。

Hello World: 
package com.java.pdf;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

public class Pdf_HelloWord {
	public static void main(String[] args) {		
		System.out.println("PDF Hello World!!");
		//创建一个Document对象
		Document document = new Document();
		try {
			//为该Document创建一个Writer实例
			PdfWriter.getInstance(document, new FileOutputStream("HelloWorld.pdf"));
			//打开document
			document.open();
			//添加内容
			document.add(new Paragraph("This is my PDF."));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (DocumentException e) {
			e.printStackTrace();
		}finally{
			document.close();
		}
	}
}


自定义设置PDF大小,页数:
package com.java.pdf;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfWriter;
/**
 * 创建日期:2017-9-28下午1:55:04
 * 修改日期:
 * 作者:ttan
 * 描述:自定义PDF大小
 */
public class Pdf_pageSize {
	public static void main(String[] args) {
		//创建Document对象
		Document document = new Document();
		try {
			//为该Document创建一个Writer实例
			PdfWriter.getInstance(document, new FileOutputStream("defaultPageSize.pdf"));
			document.open();
			document.add(new Paragraph("The default PageSize A4"));
			//新增一页PDF A3
			document.setPageSize(PageSize.A3);
			document.newPage();
			document.add(new Paragraph("The default PageSize A3"));
			document.add(new Paragraph("The default PageSize A3-1"));
			//新增一页PDF A2
			document.setPageSize(PageSize.A2);
			document.newPage();
			document.add(new Paragraph("The default PageSize A2"));
			
			//新增一页PDF 自定义
			document.setPageSize(new Rectangle(500,500));
			document.newPage();
			document.add(new Paragraph("The default PageSize 500x500"));
			
			//若想使用横向页面 
			//Document document = new Document(PageSize.A4.rotate());
			//可这样定义Document
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			document.close();
		}	
	}
}

添加标题、作者、关键字等信息:
package com.java.pdf;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfWriter;

/**
 * 创建日期:2017-9-28下午2:35:53
 * 修改日期:
 * 作者:ttan
 * 描述:添加文档属性信息
 */
public class Pdf_addProperties {
	public static void main(String[] args) {
		//自定义大小 并设置背景色
		Rectangle pageSize = new Rectangle(300,300);
		pageSize.setBackgroundColor(new BaseColor(0xFF,0xFF,0xDE));
		Document document = new Document(pageSize);
		try {
			PdfWriter.getInstance(document, new FileOutputStream("HelloWorldMeta.pdf"));
			document.addTitle("Hello title");//添加标题
			document.addAuthor("ttan");//作者
			document.addKeywords("PDF");//关键字
			document.open();
			document.add(new Paragraph("This is my PDF!!"));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (DocumentException e) {
			e.printStackTrace();
		}finally{
			document.close();
		}
	}
}

个人总结:iText类库十分好用,将相应的参数填好,就能制作出一个PDF。但是现在内容无法是中文,因为没有字体,在以后的会学习到。


 


猜你喜欢

转载自blog.csdn.net/qq_37421862/article/details/78123609
今日推荐