itextPDF生成PDF

本文档将介绍如何使用iText库在PDF中正确显示中文字符,包括设置字体和编码,确保生成的PDF文件能够完整呈现中文内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

创建一个普通的PDF:
import java.io.File;
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 CreatePDF {

	public static final String DEST = "results/objects/createPDF.pdf";
	
	public static void main(String args[]){
		File file = new File(DEST);
		file.getParentFile().mkdirs();
		
		try {
			Document document = new Document();
			PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
			document.open();
			document.add(new Paragraph("hello world!"));
			document.close();
			writer.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
}


生成含中文的PDF

public class CreatePDFChinese {

	public static final String DEST = "results/fonts/Chinese/f01_unembedded.pdf";
	public static final String FONT = "resources/fonts/NotoSansCJKsc-Regular.otf";
    
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new CreatePDFChinese().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        //设置字体
        Font font = FontFactory.getFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        document.open();
        document.add(new Paragraph("测试中文字符",font));
        document.close();
    }
}

生成加密PDF

//为生成的PDF加密,用户通过密码才能查看文件内容
public class createPDFSecurity {

	private static String USER_PASSWORD = "hello";
	private static String OWNER_PASSWORD = "world";
	private static String DEST = "results/security/createPDFSecurity.pdf";
	
	public static void main(String args[]) throws FileNotFoundException, DocumentException{
		File file = new File(DEST);
		file.getParentFile().mkdirs();
		
		Document document = new Document();
		PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(DEST));
		//设置密码和权限
		writer.setEncryption(USER_PASSWORD.getBytes(), 
				OWNER_PASSWORD.getBytes(), PdfWriter.ALLOW_PRINTING, 
				PdfWriter.ENCRYPTION_AES_128);
		
		document.open();
		document.add(new Phrase("set security for pdf!"));
		document.close();
		writer.close();
	}
}

生成含表格的PDF

public class CreatePDFTable {

	public static final String DEST = "results/tables/createPDFTable.pdf";
	
	public static void main(String args[]) throws FileNotFoundException, DocumentException{
		File file = new File(DEST);
		file.getParentFile().mkdirs();
		createTable(DEST);
	}
	
	//PDF中生成table,并设置table的行数、列数
	public static void createTable(String dest) throws FileNotFoundException, DocumentException{
		Document document = new Document();
		PdfWriter.getInstance(document, new FileOutputStream(dest));
		
		PdfPTable table = new PdfPTable(4);
		table.setWidthPercentage(100);//设置表格宽度
		
		PdfPCell cell = new PdfPCell(new Phrase("1,1"));
		table.addCell(cell);
		cell = new PdfPCell(new Phrase("1,2"));
		table.addCell(cell);
		
		PdfPCell cell23 = new PdfPCell(new Phrase("1,3and 1,4"));
		cell23.setColspan(2);//占两列
		cell23.setRowspan(2);//占2行
		cell23.setBackgroundColor(GrayColor.PINK);//设置背景颜色
		cell23.setHorizontalAlignment(Element.ALIGN_CENTER);//居中对齐
		table.addCell(cell23);
		
		cell = new PdfPCell(new Phrase("2,1"));
		table.addCell(cell);
		cell = new PdfPCell(new Phrase("2,2"));
		table.addCell(cell);
		
		document.open();
		document.add(table);
		document.close();
	}
}

github库:https://github.com/yangnn/itextPDF
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值