Java开源工具iText生成PDF简单实例
作者:junjie 发布时间:2022-09-12 15:06:54
标签:Java,开源工具,iText,生成,PDF
iText下载页面: http://sourceforge.net/projects/itext/files/
1.创建简单的PDF文件
package console.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.pdf.PdfWriter;
/**
* 使用iText生成PDF文件
*/
public class CreatePDF {
public static void main(String[] args) {
CreatePDF p001 = new CreatePDF();
String filename = "P001.pdf";
p001.createPDF(filename);
}
public void createPDF(String filename) {
// step 1
Document document = new Document(PageSize.A4);
// step 2
try {
PdfWriter.getInstance(document, new FileOutputStream(filename));
document.addTitle("ID.NET");
document.addAuthor("dotuian");
document.addSubject("This is the subject of the PDF file.");
document.addKeywords("This is the keyword of the PDF file.");
// step 3
document.open();
// step 4
document.add(new Paragraph("Hello World!"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
} finally {
// step 5
document.close();
}
}
}
2.在PDF文件中添加Table
package console.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.PageSize;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
/**
* 使用iText生成PDF文件
* 在PDF文件中创建表格
*/
public class TableOfPDF {
public static void main(String[] args) {
TableOfPDF p001 = new TableOfPDF();
String filename = "P002.pdf";
p001.createPDF(filename);
}
public void createPDF(String filename) {
// step 1
Document document = new Document(PageSize.A4);
// step 2
try {
PdfWriter.getInstance(document, new FileOutputStream(filename));
document.addTitle("ID.NET");
document.addAuthor("dotuian");
document.addSubject("This is the subject of the PDF file.");
document.addKeywords("This is the keyword of the PDF file.");
// step 3
document.open();
// step 4
PdfPTable table = createTable1();
document.add(table);
table = createTable2();
table.setSpacingBefore(5);
table.setSpacingAfter(5);
document.add(table);
table = createTable3();
document.add(table);
table = createTable4();
table.setSpacingBefore(5);
table.setSpacingAfter(5);
document.add(table);
table = createTable5();
document.add(table);
table = createTable6();
document.add(table);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
} finally {
// step 5
document.close();
}
}
/**
* Creates a table; widths are set with setWidths().
*
* @return a PdfPTable
* @throws DocumentException
*/
public static PdfPTable createTable1() throws DocumentException {
PdfPTable table = new PdfPTable(3);
table.setWidthPercentage(288 / 5.23f);
table.setWidths(new int[] { 2, 1, 1 });
PdfPCell cell;
cell = new PdfPCell(new Phrase("Table 1"));
cell.setColspan(3);
table.addCell(cell);
cell = new PdfPCell(new Phrase("Cell with rowspan 2"));
cell.setRowspan(2);
table.addCell(cell);
table.addCell("row 1; cell 1");
table.addCell("row 1; cell 2");
table.addCell("row 2; cell 1");
table.addCell("row 2; cell 2");
return table;
}
/**
* Creates a table; widths are set with setWidths().
*
* @return a PdfPTable
* @throws DocumentException
*/
public static PdfPTable createTable2() throws DocumentException {
PdfPTable table = new PdfPTable(3);
table.setTotalWidth(288);
table.setLockedWidth(true);
table.setWidths(new float[] { 2, 1, 1 });
PdfPCell cell;
cell = new PdfPCell(new Phrase("Table 2"));
cell.setColspan(3);
table.addCell(cell);
cell = new PdfPCell(new Phrase("Cell with rowspan 2"));
cell.setRowspan(2);
table.addCell(cell);
table.addCell("row 1; cell 1");
table.addCell("row 1; cell 2");
table.addCell("row 2; cell 1");
table.addCell("row 2; cell 2");
return table;
}
/**
* Creates a table; widths are set in the constructor.
*
* @return a PdfPTable
* @throws DocumentException
*/
public static PdfPTable createTable3() throws DocumentException {
PdfPTable table = new PdfPTable(new float[] { 2, 1, 1 });
table.setWidthPercentage(55.067f);
PdfPCell cell;
cell = new PdfPCell(new Phrase("Table 3"));
cell.setColspan(3);
table.addCell(cell);
cell = new PdfPCell(new Phrase("Cell with rowspan 2"));
cell.setRowspan(2);
table.addCell(cell);
table.addCell("row 1; cell 1");
table.addCell("row 1; cell 2");
table.addCell("row 2; cell 1");
table.addCell("row 2; cell 2");
return table;
}
/**
* Creates a table; widths are set with special setWidthPercentage() method.
*
* @return a PdfPTable
* @throws DocumentException
*/
public static PdfPTable createTable4() throws DocumentException {
PdfPTable table = new PdfPTable(3);
Rectangle rect = new Rectangle(523, 770);
table.setWidthPercentage(new float[] { 144, 72, 72 }, rect);
PdfPCell cell;
cell = new PdfPCell(new Phrase("Table 4"));
cell.setColspan(3);
table.addCell(cell);
cell = new PdfPCell(new Phrase("Cell with rowspan 2"));
cell.setRowspan(2);
table.addCell(cell);
table.addCell("row 1; cell 1");
table.addCell("row 1; cell 2");
table.addCell("row 2; cell 1");
table.addCell("row 2; cell 2");
return table;
}
/**
* Creates a table; widths are set with setTotalWidth().
*
* @return a PdfPTable
* @throws DocumentException
*/
public static PdfPTable createTable5() throws DocumentException {
PdfPTable table = new PdfPTable(3);
table.setTotalWidth(new float[] { 144, 72, 72 });
table.setLockedWidth(true);
PdfPCell cell;
cell = new PdfPCell(new Phrase("Table 5"));
cell.setColspan(3);
table.addCell(cell);
cell = new PdfPCell(new Phrase("Cell with rowspan 2"));
cell.setRowspan(2);
table.addCell(cell);
table.addCell("row 1; cell 1");
table.addCell("row 1; cell 2");
table.addCell("row 2; cell 1");
table.addCell("row 2; cell 2");
return table;
}
public static PdfPTable createTable6() throws DocumentException{
PdfPTable table = new PdfPTable(10);
table.setTotalWidth(595);
//table.setLockedWidth(true);
PdfPCell cell;
cell = new PdfPCell(new Phrase("Table 6"));
cell.setColspan(10);
table.addCell(cell);
for (int i = 1; i < 100; i++) {
cell = new PdfPCell(new Phrase(String.valueOf(i)));
cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
table.addCell(cell);
}
// cell = new PdfPCell(new Phrase("Cell with rowspan 2"));
// cell.setRowspan(2);
// table.addCell(cell);
// table.addCell("row 1; cell 1");
// table.addCell("row 1; cell 2");
// table.addCell("row 2; cell 1");
// table.addCell("row 2; cell 2");
return table;
}
}
3.在PDF文件中添加图片,并且指定文本位置
package console.pdf;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfWriter;
/**
* 使用iText生成PDF文件
* 在PDF文件中添加背景图片,并指定文本在PDF文件中的位置
*/
public class BackgroundImageOfPDF {
public static void main(String[] args) {
BackgroundImageOfPDF p001 = new BackgroundImageOfPDF();
String filename = "P003.pdf";
p001.createPDF(filename);
}
public void createPDF(String filename) {
// step 1
Document document = new Document(PageSize.A4.rotate(),0,0,0,0);
// step 2
try {
PdfWriter pdfwriter = PdfWriter.getInstance(document, new FileOutputStream(filename));
document.addTitle("ID.NET");
document.addAuthor("dotuian");
document.addSubject("This is the subject of the PDF file.");
document.addKeywords("This is the keyword of the PDF file.");
// step 3
document.open();
// step 4
Image image = Image.getInstance("bg.jpg");
document.add(image);
PdfContentByte pdfContentByte = pdfwriter.getDirectContent();
pdfContentByte.beginText();
BaseFont bf = BaseFont.createFont(BaseFont.TIMES_ROMAN,BaseFont.WINANSI,BaseFont.EMBEDDED);
pdfContentByte.setFontAndSize(bf, 12);
for (int i = 0; i <= 842; i = i + 50) {
for (int j = 0; j <= 595; j = j + 20) {
pdfContentByte.setTextMatrix(i, j);
pdfContentByte.showText("(" + i + ":" + j + ")");
}
}
pdfContentByte.endText();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
// step 5
document.close();
}
}
}


猜你喜欢
- 开发背景开发工具:VS2017语言:C#DotNet版本:.Net FrameWork 4.0及以上系统:Win10 X64一、首先建立一个
- package com.wa.xwolf.sblog.util;import java.io.BufferedInputStre
- 目录前言一、Android应用DAC沙盒二、Android应用权限三、应用信息的存储四、应用权限的映射五、应用的SELinux标签六、And
- 本文实例讲述了C#简单读取、改变文件的创建、修改及访问时间的方法。分享给大家供大家参考。具体如下:FileInfo fi = new Fil
- 建造者模式(Builder Pattern)主要用于“分步骤构建一个复杂的对象”,在这其中“分步骤”是一个稳定的算法,而复杂对象的各个部分则
- 1、Nacos config springboot starter包我们在springboot应用中集成nacos配置中心时,添加了以下依赖
- 问题在项目过程中使用MyBatis-Puls的saveBatch一次性添加大量数据时很慢原因MyBatis-Puls的saveBatch默认
- 1.概述1、Spring 是轻量级的开源的 JavaEE 框架2、 Spring 可以解决企业应用开发的复杂性3、Spring 有两个核心部
- 公司最近也开始基于android4.0 ICS修改框架了,公司的手机暂时不适合拿回家测试,也没有kernel的权限。从个人的角度看,我手上现
- 一、下载RabbitMQhttp://www.rabbitmq.com/install-windows.html二、下载OTPhttp://
- 前言与消息发送紧密相关的几行代码:1. DefaultMQProducer producer = new DefaultMQProducer
- 目录一、 全局JDK设置(默认配置)二、主题设置三、字体大小设置四、字符集和配置文件编码格式设置五、自动导入设置六、自动忽视大小写设置七、关
- 前言在工作中遇到这样一个问题:开发过程中将数据库的账号、密码等信息配置在了一个单独的properties配置文件中(使用明文)。但运维人员要
- 在java中调用xls格式化xml 使用javax.xml.transform.Transformer,将xml用xls格式化为另一种xml
- 两种解决方案前端查询字典数据然后前端转码后端查询字典值,然后再转码返回给前段。本文及时针对方案2 进行的改进目标:在需要返回给前段的字段上添
- 本文实例为大家分享了C#获取计算机信息的具体代码,供大家参考,具体内容如下using System;using System.Configu
- 通用配置#下面介绍的整合JDBC和整合MyBatis都需要添加的实体类和配置数据库表#CREATE TABLE `user` ( `id`
- 目录安装Nginx准备SpringBoot应用添加网关现如今的项目开发基本都是微服务方式,导致一个系统中会有很多的服务,每个模块都对应着不同
- 一.创建Spring boot项目,添加如下依赖<dependency> <gro
- 最近看了一些淘宝购物车的demo,于是也写了一个。效果图如下:主要代码如下: actvity中的代码:public class Shoppi