创建一个普通的PDF:
生成加密PDF
生成含表格的PDF
github库:https://github.com/yangnn/itextPDF
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
public class CreatePDF {
public static final String DEST = "results/objects/createPDF.pdf";
public static void main(String args[]){
File file = new File(DEST);
file.getParentFile().mkdirs();
try {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
document.open();
document.add(new Paragraph("hello world!"));
document.close();
writer.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
生成含中文的PDF
public class CreatePDFChinese {
public static final String DEST = "results/fonts/Chinese/f01_unembedded.pdf";
public static final String FONT = "resources/fonts/NotoSansCJKsc-Regular.otf";
public static void main(String[] args) throws IOException, DocumentException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new CreatePDFChinese().createPdf(DEST);
}
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(dest));
//设置字体
Font font = FontFactory.getFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
document.open();
document.add(new Paragraph("测试中文字符",font));
document.close();
}
}
生成加密PDF
//为生成的PDF加密,用户通过密码才能查看文件内容
public class createPDFSecurity {
private static String USER_PASSWORD = "hello";
private static String OWNER_PASSWORD = "world";
private static String DEST = "results/security/createPDFSecurity.pdf";
public static void main(String args[]) throws FileNotFoundException, DocumentException{
File file = new File(DEST);
file.getParentFile().mkdirs();
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(DEST));
//设置密码和权限
writer.setEncryption(USER_PASSWORD.getBytes(),
OWNER_PASSWORD.getBytes(), PdfWriter.ALLOW_PRINTING,
PdfWriter.ENCRYPTION_AES_128);
document.open();
document.add(new Phrase("set security for pdf!"));
document.close();
writer.close();
}
}
生成含表格的PDF
public class CreatePDFTable {
public static final String DEST = "results/tables/createPDFTable.pdf";
public static void main(String args[]) throws FileNotFoundException, DocumentException{
File file = new File(DEST);
file.getParentFile().mkdirs();
createTable(DEST);
}
//PDF中生成table,并设置table的行数、列数
public static void createTable(String dest) throws FileNotFoundException, DocumentException{
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(dest));
PdfPTable table = new PdfPTable(4);
table.setWidthPercentage(100);//设置表格宽度
PdfPCell cell = new PdfPCell(new Phrase("1,1"));
table.addCell(cell);
cell = new PdfPCell(new Phrase("1,2"));
table.addCell(cell);
PdfPCell cell23 = new PdfPCell(new Phrase("1,3and 1,4"));
cell23.setColspan(2);//占两列
cell23.setRowspan(2);//占2行
cell23.setBackgroundColor(GrayColor.PINK);//设置背景颜色
cell23.setHorizontalAlignment(Element.ALIGN_CENTER);//居中对齐
table.addCell(cell23);
cell = new PdfPCell(new Phrase("2,1"));
table.addCell(cell);
cell = new PdfPCell(new Phrase("2,2"));
table.addCell(cell);
document.open();
document.add(table);
document.close();
}
}
github库:https://github.com/yangnn/itextPDF