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
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Springboot通过run启动web应用的方法
2021-08-30 01:57:37
![](https://img.aspxhome.com/file/2023/7/64527_0s.png)
分享15款Java程序员必备的开发工具
2021-12-07 19:09:07
![](https://img.aspxhome.com/file/2023/3/69343_0s.jpg)
Java针对ArrayList自定义排序的2种实现方法
2022-01-30 03:47:34
java使用TimerTask定时器获取指定网络数据
2022-08-14 10:52:20
SpringBoot之自定义Filter获取请求参数与响应结果案例详解
2023-07-16 20:22:21
Java时间转换成unix时间戳的方法
2022-06-09 14:39:36
IntelliJ IDEA 2020.2正式发布,两点多多总能助你提效
2023-08-30 18:15:18
![](https://img.aspxhome.com/file/2023/2/58082_0s.jpg)
java开发工作中对InheritableThreadLocal使用思考
2023-11-24 21:46:44
![](https://img.aspxhome.com/file/2023/6/59186_0s.jpg)
Java使用Thread和Runnable的线程实现方法比较
2021-11-17 07:52:54
IDEA(jetbrain通用)使用教程图解
2023-04-15 04:05:49
![](https://img.aspxhome.com/file/2023/9/61389_0s.gif)
Java实现上传文件图片到指定服务器目录
2023-06-28 00:23:32
java操作Apache druid的实例代码
2023-12-24 02:10:58
彻底掌握C语言strcpy函数的用法
2023-07-03 07:21:18
![](https://img.aspxhome.com/file/2023/4/103104_0s.png)
Flutter中http请求抓包的完美解决方案
2023-08-22 18:47:47
![](https://img.aspxhome.com/file/2023/9/110729_0s.jpg)
如何利用Java输出链表中倒数第k个结点
2022-11-29 10:13:28
![](https://img.aspxhome.com/file/2023/4/61524_0s.png)
@Autowired注解在抽象类中失效的原因及解决
2021-07-15 06:12:04
MyBatis-Plus实现分页的方法使用详解
2023-02-14 18:22:16
![](https://img.aspxhome.com/file/2023/6/60026_0s.png)
浅谈springboot之JoinPoint的getSignature方法
2022-12-25 11:23:20
C#使用LINQ查询操作符实例代码(一)
2021-11-27 01:41:57
![](https://img.aspxhome.com/file/2023/8/77258_0s.png)
Java实现聊天机器人完善版
2022-10-07 09:31:11
![](https://img.aspxhome.com/file/2023/2/72902_0s.png)