Java开源工具iText生成PDF简单实例
作者:junjie 时间:2022-09-12 15:06:54
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();
}
}
}
标签:Java,开源工具,iText,生成,PDF
0
投稿
猜你喜欢
Android输入框控件ClearEditText实现清除功能
2022-12-16 06:59:48
Spring Security实现两周内自动登录"记住我"功能
2021-12-15 17:59:22
springboot实现公众号接收回复消息和超过5秒被动回复消息
2022-10-16 04:40:48
Android实现波浪球效果
2021-09-02 16:07:53
Spring Boot项目添加外部Jar包以及配置多数据源的完整步骤
2023-10-12 13:20:39
深入理解Java垃圾回收机制以及内存泄漏
2023-09-24 22:45:13
C# 通过NI-VISA操作Tektronix TBS 2000B系列示波器的实现步骤
2023-11-02 01:44:38
Java用栈实现综合计算器
2021-08-16 02:01:36
SpringCloud实现Eureka服务注册与发现
2021-08-16 04:00:44
Android使用友盟集成QQ、微信、微博等第三方分享与登录方法详解
2022-03-07 01:46:30
C#中WPF ListView绑定数据的实例详解
2023-03-11 12:08:41
idea快速搭建spring cloud注册中心与注册的方法
2023-03-09 17:40:35
Android使用Gridview单行横向滚动显示
2021-07-28 17:40:43
Android编程实现下载时主界面与详细界面一致更新的方法
2023-01-02 10:07:35
java各种类型对象占用内存情况分析
2023-08-22 10:32:05
SpringBoot遇到的坑@Qualifier报红的解决
2022-06-03 07:49:55
java实现选课系统
2021-08-15 07:43:44
Java中lambda表达式的基本运用
2023-09-09 08:02:36
使用android隐藏api实现亮度调节的方法
2022-10-16 23:37:36
JDK8中的HashMap初始化和扩容机制详解
2023-11-25 10:06:44