itextPDF生成PDF

来源:互联网 发布:linux 定时器状态 编辑:程序博客网 时间:2024/05/22 18:47
创建一个普通的PDF:
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 blocke.printStackTrace();} catch (DocumentException e) {// TODO Auto-generated catch blocke.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
0 0