ItextPdf 学习总结

本文详细介绍了如何使用ItextPdf库进行PDF开发,包括环境搭建、PDF文件生成、页面设置、水印背景创建、内容添加如段落、表格、图片等,以及页面操作如删除、插入和排序,同时还涵盖了目录、header和footer的设置。
摘要由CSDN通过智能技术生成

一、开发环境搭建

创建一个项目,添加jar包:

  • 多需要的jar包
    itextpdf-5.2.1.jar
  • pom依赖包添加语句
	<dependency>
	    <groupId>com.itextpdf</groupId>
	    <artifactId>itextpdf</artifactId>
	    <version>5.2.1</version>
	</dependency>

二、代码实例

1. 生成pdf文件
public static void createPdf1() throws FileNotFoundException, DocumentException{
		// step 1:Create a document
		Document document = new Document();
		
		// step 2:Get a pdfWriter instance 
		PdfWriter.getInstance(document, new FileOutputStream("E://createSamplePdf1.pdf"));
		
		// step 3:Open the document
		document.open();
		
		// step 4:Add content
		document.add(new Paragraph("hello world!"));
		
		// step 5:Close the document
		document.close();
	}
2. 设置pdf文件的大小、背景色、版本、属性、边距
public static void createPdf2() throws FileNotFoundException, DocumentException{
		// step 1:Create page size
		Rectangle rect = new Rectangle(PageSize.B5.rotate());
		
		// step 2:Set background color
		rect.setBackgroundColor(BaseColor.ORANGE);  	
		Document document = new Document(rect);
		
		PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("E://createSamplePdf2.pdf"));
		
		// Set PDF version  
		writer.setPdfVersion(PdfWriter.PDF_VERSION_1_2);  
		// Set pdf passWord
//		writer.setEncryption("Hello".getBytes(), "World".getBytes(),PdfWriter.ALLOW_SCREENREADERS, PdfWriter.STANDARD_ENCRYPTION_128);   
		
		// Set document properties
//		document.addTitle("Title@sample");  
//		document.addAuthor("Author@rensanning");  
//		document.addSubject("Subject@iText sample");  
//		document.addKeywords("Keywords@iText");  
//		document.addCreator("Creator@iText");  
		
		// Set pdf margin
		document.setMargins(10, 20, 30, 40);
		
		// open pdf
		document.open();
		document.add(new Paragraph("hello world!"));
		document.close();
	}
3.生成新的一页
public static void createPdf3() throws DocumentException, FileNotFoundException{
		
		Document document = new Document();
		
		PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("E://createSamplePdf3.pdf"));		
		
		document.open();
		document.add(new Paragraph("first page"));
		document.add(new Paragraph(Document.getVersion()));
		
		document.newPage();
		writer.setPageEmpty(false);
		
		document.add(new Paragraph("new page"));
		
		document.close();
	}
4.生成水印和背景图片
public static void createPdf4() throws IOException, DocumentException{
		PdfReader reader = new PdfReader("E://createSamplePdf3.pdf");
		PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("E://createSamplePdf4.pdf"));
		
		// Set waterMark image
		Image img = Image.getInstance("E://waterMark.jpg");
		img.setAbsolutePosition(200, 400);
		
		PdfContentByte under = stamper.getUnderContent(1);
		
		under.addImage(img);
		// Set waterMark image
		PdfContentByte over = stamper.getOverContent(2);
		
		over.beginText();
		
		BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED);
		
		over.setFontAndSize(bf, 18);  
		over.setTextMatrix(30, 30);  
		over.showTextAligned(Element.ALIGN_LEFT, "DUPLICATE", 230, 430, 45);  
		over.endText(); 
		
		// Set background image
		Image img2 = Image.getInstance("E://test.jpg");  
		img2.setAbsolutePosition(0, 0);  
		PdfContentByte under2 = stamper.getUnderContent(3);  
		under2.addImage(img2);  
		
		stamper.close();
		reader.close();
	}
5.使用chunk、phrase、paragragh、list创建pdf
public static void createPdf5() throws DocumentException, FileNotFoundException{
		Document document = new Document();
		PdfWriter.getInstance(document, new FileOutputStream("E://createSamplePdf5.pdf"));
		
		document.open();
		// Add chunk
		document.add(new Chunk("China"));
		document.add(new Chu
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值