iTEXT官方网站:
https://developers.itextpdf.com/examples
常用属性:
public static void main(String[] args) throws DocumentException, IOException {
//创建文件
Document document = new Document();
// 也可以自定义文件页面大小
// Rectangle pagesize = new Rectangle(216f, 720f);
// Document document = new Document(pagesize, 36f, 72f, 108f, 180f);
//建立一个书写器
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("D:/hello.pdf"));
//打开文件
document.open();
//中文字体,解决中文不能显示问题
BaseFont bfChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
Font blackFont = new Font(bfChinese);
blackFont.setColor(BaseColor.BLACK); // 设置字体颜色为黑色
Font blackFontB = new Font(bfChinese,8); // 设置字体大小为8
document.add(new Paragraph("你好",blackFont)); //添加内容
document.addTitle("标题"); //标题
document.addAuthor("作者"); //作者
document.addSubject("主题"); //主题
document.addKeywords("关键字"); //关键字
document.addCreationDate(); //创建时间
document.addCreator("应用程序"); //应用程序
// 添加图片
Image image1 = Image.getInstance("D:hello.JPG");
image1.setAbsolutePosition(100f, 550f); //设置图片位置的x轴和y轴
image1.scaleAbsolute(200, 200); //设置图片的宽度和高度
document.add(image1); //将图片1添加到pdf文件中
//添加有序列表
List orderList = new List(List.ORDERED);
orderList.add(new ListItem("Item one"));
orderList.add(new ListItem("Item two"));
orderList.add(new ListItem("Item three"));
document.add(orderList);
//创建章节
Paragraph chapterTitle = new Paragraph("段落标题xxxx", greenFont);
Chapter chapter1 = new Chapter(chapterTitle, 1);
chapter1.setNumberDepth(0);
Paragraph sectionTitle = new Paragraph("部分标题", greenFont);
Section section1 = chapter1.addSection(sectionTitle);
Paragraph sectionContent = new Paragraph("部分内容", blueFont);
section1.add(sectionContent);
document.add(chapter1);
// 设置段落
Paragraph paragraph = new Paragraph("段落", titlefont);
paragraph.setAlignment(1); //设置文字居中 0靠左 1,居中 2,靠右
paragraph.setIndentationLeft(12); //设置左缩进
paragraph.setIndentationRight(12); //设置右缩进
paragraph.setFirstLineIndent(24); //设置首行缩进
paragraph.setLeading(50f); //行间距
paragraph.setSpacingBefore(5f); //设置段落上空白
paragraph.setSpacingAfter(10f); //设置段落下空白
document.add(paragraph);
// 直线
Paragraph p1 = new Paragraph(); p1.add(new Chunk(new LineSeparator()));
document.add(p1);
// 点线
Paragraph p2 = new Paragraph(); p2.add(new Chunk(new DottedLineSeparator()));
document.add(p2);
// 超链接
Anchor anchor = new Anchor("baidu");
anchor.setReference("www.baidu.com");
document.add(anchor);
// 定位
Anchor gotoP = new Anchor("goto"); gotoP.setReference("#top");
document.add(gotoP);
// 设置密码(要先设置密码,再打开文件document.open();)
//用户密码
String userPassword = "123456";
//拥有者密码
String ownerPassword = "hd";
writer.setEncryption(userPassword.getBytes(), ownerPassword.getBytes(), PdfWriter.ALLOW_PRINTING,
PdfWriter.ENCRYPTION_AES_128);
// 打开文件
document.open();
//添加内容
document.add(new Paragraph("password !!!!"));
// 设置只读权限(也是先设置权限,再打开文件document.open();)
writer.setEncryption("".getBytes(), "".getBytes(), PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128);
//关闭文档
document.close();
//关闭书写器
writer.close();
}
表格
public static void main(String[] args) throws DocumentException, IOException {
//创建文件
Document document = new Document();
//建立一个书写器
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("D:/hello.pdf"));
//打开文件
document.open();
//中文字体,解决中文不能显示问题
BaseFont bfChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
Font blackFont = new Font(bfChinese);
blackFont.setColor(BaseColor.BLACK);
// 创建表格(1代表只有1列)
PdfPTable table = new PdfPTable(1);
table.setWidthPercentage(100); // 表格宽度比例为100%
table.setTotalWidth(500); // 设置表格的宽度
table.setTotalWidth(new float[] { 160, 70, 130, 100 }); // 设置每列宽度
table.setLockedWidth(true); // 锁住宽度
table.setSpacingBefore(10f); // 设置表格上面空白宽度
table.getDefaultCell().setBorder(0); // 设置表格默认为无边框
PdfPCell cell = new PdfPCell(new Phrase("单元格")); // 新建单元格
cell.setBorderColor(BaseColor.BLUE); // 设置边框颜色
cell.setBackgroundColor(BaseColor.ORANGE); // 设置背景颜色
cell.setRowspan(2); // 设置跨两行
cell.setColspan(2); // 设置占用列数2列
cell.setPaddingLeft(10); // 设置距左边的距离
cell.setFixedHeight(20); // 设置高度
cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 设置内容水平居中显示
cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 设置垂直居中
cell.disableBorderSide(1); //隐藏上边框
cell.disableBorderSide(2); //隐藏下边框
cell.disableBorderSide(3); //隐藏上、下边框
cell.disableBorderSide(4); //隐藏左边框
cell.disableBorderSide(5); //隐藏左、上边框
cell.disableBorderSide(6); //隐藏左、下边框
cell.disableBorderSide(7); //隐藏左、上、下边框
cell.disableBorderSide(8); //隐藏右边框
cell.disableBorderSide(9); //隐藏右、上边框
cell.disableBorderSide(10); //隐藏右、下边框
cell.disableBorderSide(11); //隐藏右、上、下边框
cell.disableBorderSide(12); //隐藏左、右边框
cell.disableBorderSide(13); //隐藏上、左、右边框
cell.disableBorderSide(14); //隐藏下、左、右边框
cell.disableBorderSide(15); //隐藏全部
table.addCell(cell); // 将单元格加入表格中
// 增加一个条形码到表格
Barcode128 code128 = new Barcode128();
code128.setCode("14785236987541");
code128.setCodeType(Barcode128.CODE128);
// 生成条形码图片
Image code128Image = code128.createImageWithBarcode(cb, null, null);
// 加入到表格
PdfPCell cellcode = new PdfPCell(code128Image, true);
cellcode.setHorizontalAlignment(Element.ALIGN_CENTER);
cellcode.setVerticalAlignment(Element.ALIGN_MIDDLE);
cellcode.setFixedHeight(30);
table.addCell(cellcode);
document.add(table);
//关闭文档
document.close();
//关闭书写器
writer.close();
}
其中表格是可以嵌套的,也就是说一个表格可以加入到另一个表格的单元格中。
压缩PDF文件
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
public class PDFZip {
public static void main(String[] args)throws DocumentException, IOException {
// 压缩多个PDF文件
ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(RESULT));
for (int i = 1; i <= 3; i++) {
ZipEntry entry = new ZipEntry("test" + i + ".pdf");
zip.putNextEntry(entry);
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, zip);
writer.setCloseStream(false);
document.open();
document.add(new Paragraph("testpdfzip " + i));
document.close();
zip.closeEntry();
}
zip.close();
}
}