itextPDF生成PDF
来源:互联网 发布:linux 定时器状态 编辑:程序博客网 时间:2024/05/22 18:47
创建一个普通的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 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
- itextPDF生成PDF
- 利用ItextPdf、core-renderer-R8 来生成PDF
- 用itextpdf生成水印来代替pdf的替换问题
- 利用ItextPdf、core-renderer-R8 来生成PDF
- java-pdf-itext7、itextpdf 生成pdf 文档,使用pdf模板生成(支持中文字体)
- itextpdf输出PDF笔记
- 使用itextpdf操作pdf
- java 使用itextpdf.jar和itext-asian.jar根据pdf模板填写数据后生成一个新的pdf文件
- pdf文档的生成利用itextpdf jar包 二维码生成利用goole zing包 jar包可以在网上收
- iTextPdf--the world's preferred PDF library
- ItextPdf 数字签名,HTML转PDF功能
- itextpdf解决PDF合并的问题
- java 用itextpdf读写pdf文件
- Java操作PDF之iTextPDF中文支持
- 使用itextpdf 将 前台 数据 转成pdf !
- itextpdf
- itextpdf通过for导出多个pdf出现com.itextpdf.kernel.PdfException: pdf.inderect.object.belong.to.other.pdf.docu
- IText实现Html转PDF itextpdf-5.5.5.jar
- mysql索引问题
- Redis开源代码读书笔记二(源代码及工程结构)
- WEB学习笔记
- JavaBean简介
- iOS涂鸦-自由画笔可以用到的代码
- itextPDF生成PDF
- 重写strcpy
- 成为JavaGC专家(1)
- Mac下valgrind的安装和使用
- IOS异步获取数据并刷新界面dispatch_async的使用方法
- Picasso加载图片
- 给gridview子项设置宽高考虑屏幕适配
- 蓝桥杯 概率计算(DP)
- Android屏幕适配大法