Itext的简单应用和学习

本文介绍了如何使用iText库创建PDF文档,并演示了使用itextasian.jar包和本地计算机字体(如黑体字)的方法。通过示例代码,展示了如何设置作者、创建表格、添加文本等基本操作。

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

1.Itext使用需要使用itext.jar包。

2.Itext使用中文,主要有两种方式,下载itextasian.jar包,或者使用本地计算机字体。

   本地计算机字体:

<span style="font-size:12px;">  BaseFont bfHei = BaseFont.createFont("c:/Windows/fonts/SIMHEI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
  Font font = new Font(bfHei, 32);
  String text = "这是黑体字测试!";
  document.add(new Paragraph(text, font));</span>
   使用iTextAsian.jar中的字体:

<span style="font-size:12px;">  BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);</span>
3.Itext简单实例使用。
  Document document = new Document(PageSize.A3);
  document.addAuthor("Ryan");
  document.addCreationDate();
  try {
	PdfWriter.getInstance(document, new FileOutputStream("test.pdf"));
        document.open();
	//Chunk chunk = new Chunk("Holle  乐", new Font(BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED), 32));
	document.add(new Paragraph("Holle world!"));
<span style="white-space:pre">	</span>Table table = new Table(3);
	table.setBorderWidth(1);
	table.setBorderColor(new Color(0, 0, 255));
	table.setPadding(5);
	Cell cell = new Cell("header");
	cell.setHeader(true);
	cell.setColspan(3);
	table.addCell(cell);
	cell = new Cell("example cell with colspan 1 and rowspan 2");
	cell.setRowspan(2);
	cell.setBorderColor(new Color(255, 0, 0));
	table.addCell(cell);
	table.addCell("1.1");
	table.addCell("2.1");
	table.addCell("1.2");
	table.addCell("2.2");
	table.addCell("cell test1");
	cell = new Cell("big cell");
	cell.setRowspan(2);
	cell.setColspan(2);
	cell.setBackgroundColor(new Color(0xC0, 0xC0, 0xC0));
	table.addCell(cell);
	table.addCell("cell test2");
	document.add(table);
	//document.add(chunk);
	BaseFont bfHei = BaseFont.createFont("c:/Windows/fonts/SIMHEI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
	Font font = new Font(bfHei, 32);
	String text = "这是黑体字测试!";
	document.add(new Paragraph(text, font));
	document.close();
     } catch (FileNotFoundException e) {
	e.printStackTrace();
     } catch (DocumentException e) {
	e.printStackTrace();
     } catch (IOException e) {
	e.printStackTrace();
     }




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值