1. 程式人生 > >java PDF 生成

java PDF 生成

專案中有時會要求根據內容生成PDF,動態生成PDF,html轉PDF,word轉pdf,熟悉瞭解PDF相關的轉換生成,可以提高程式設計速度;

第一節:PDF生成

PDF生成準備工作:

jar:連結:https://pan.baidu.com/s/1a44QfOX7Sdv8pBhCyhUdsg 提取碼: w3ec 

iText-2.1.7.jar

itext-rtf-2.1.7.jar

iTextAsian.jar

程式碼:

字型設定:這裡設定字型,否則中文不顯示

Font fontChinese;

BaseFont bf = BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);

fontChinese = new Font(bf,12,Font.NORMAL);

//建立document文件物件

Document d = new Document();

//建立PdfWriter 輸出流物件

PdfWriter p = PdfWriter.getInstance(d,new FileOutPutStream(輸出檔名字路徑));

//開啟文件物件

d.open();

//新增文件內容

d.add(new Paragraph("hello,今天好開心",fontChinese));

//關閉文件物件

d.close();