自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Java操作PDF文件,竟然如此簡單

開發(fā) 后端
本文從 iText 最基本的用法,分別介紹從表格,圖片,中文來介紹 iText,希望能夠幫助到你。

[[439487]]

 

maven 依賴

使用 iText 的時(shí)候需要用 maven 依賴如下:

 

  1. <dependency> 
  2.     <groupId>com.itextpdf</groupId> 
  3.     <artifactId>itext7-core</artifactId> 
  4.     <version>7.2.0</version> 
  5.     <type>pom</type> 
  6. </dependency> 

簡單實(shí)例

建立 PDF 需要 4 個(gè)步驟:

  • 創(chuàng)建 一個(gè) PdfWriter,用于定義 pdf 的路徑地址
  • 創(chuàng)建一個(gè) pdf 文檔,Document,與 PdfWriter 關(guān)聯(lián)
  • 向文檔中添加內(nèi)容
  • 關(guān)閉文檔

下面從 hello world 開始。

 

  1. import com.itextpdf.kernel.pdf.PdfDocument; 
  2. import com.itextpdf.kernel.pdf.PdfWriter; 
  3. import com.itextpdf.layout.Document; 
  4. import com.itextpdf.layout.element.Paragraph; 
  5.  
  6. public class PDFDemo { 
  7.  
  8.     public static void main(String[] args) { 
  9.         try{ 
  10.             PdfWriter pdfWriter = new PdfWriter("./demo.pdf"); 
  11.             PdfDocument pdfDocument = new PdfDocument(pdfWriter); 
  12.             Document document = new Document(pdfDocument); 
  13.             document.add(new Paragraph("Hello World")); 
  14.             document.close(); 
  15.         }catch(Exception e){ 
  16.             e.printStackTrace(); 
  17.         } 
  18.     } 

示例結(jié)果:

中文

iText 不支持默認(rèn)是不支持中文字體的,所以需要定義 PdfFont 中文字體樣式。使用 PdfFontFactory 的 createFont 創(chuàng)建一個(gè)中文字體,并將中文字體設(shè)置到 document 中。

 

  1. public static void main(String[] args) { 
  2.     try{ 
  3.         PdfWriter pdfWriter = new PdfWriter("./demo1.pdf"); 
  4.         PdfDocument pdfDocument = new PdfDocument(pdfWriter); 
  5.         //字體文件可以使用 windows 系統(tǒng)中的 
  6.         PdfFont font = PdfFontFactory.createFont("E:\\pdfProject\\src\\main\\java\\simhei.ttf"); 
  7.         Document document = new Document(pdfDocument).setFont(font); 
  8.         document.add(new Paragraph("Hello World!你好,itext")); 
  9.         document.close(); 
  10.     }catch(Exception e){ 
  11.         e.printStackTrace(); 
  12.     } 

示例結(jié)果:

表格

在項(xiàng)目中經(jīng)常需要 PDF 文件中打印表格,這時(shí)就可以使用 Table 對象,創(chuàng)建一個(gè)個(gè)單元格并將表格內(nèi)容寫入單元格中。

 

  1. public static void main(String[] args) { 
  2.     try{ 
  3.         PdfWriter pdfWriter = new PdfWriter("./demo2.pdf"); 
  4.         PdfDocument pdfDocument = new PdfDocument(pdfWriter); 
  5.         PdfFont font = PdfFontFactory.createFont("E:\\pdfProject\\src\\main\\java\\simhei.ttf"); 
  6.         Document document = new Document(pdfDocument).setFont(font); 
  7.         Table table = new Table(4); 
  8.         table.setWidth(500); 
  9.         table.addHeaderCell("header 1").addHeaderCell("header 2").addHeaderCell("header 3").addHeaderCell("header 4"); 
  10.         for(int i = 0; i < 16; i++) { 
  11.             table.addCell("cell " + i); 
  12.         } 
  13.         document.add(table); 
  14.  
  15.         document.close(); 
  16.     }catch(Exception e){ 
  17.         e.printStackTrace(); 
  18.     } 

示例結(jié)果:

 

圖片

圖片的設(shè)置也很簡單,創(chuàng)建 Image 對象填充到 document 中就可以了。

 

  1. public static void main(String[] args) { 
  2.     try{ 
  3.         PdfWriter pdfWriter = new PdfWriter("./demo3.pdf"); 
  4.         PdfDocument pdfDocument = new PdfDocument(pdfWriter); 
  5.         PdfFont font = PdfFontFactory.createFont("E:\\pdfProject\\src\\main\\java\\simhei.ttf"); 
  6.         Document document = new Document(pdfDocument).setFont(font); 
  7.         Image img = new Image(ImageDataFactory.create("E:\\pdfProject\\src\\main\\java\\img.png")); 
  8.         document.add(img.setAutoScale(true)); 
  9.  
  10.         document.close(); 
  11.     }catch(Exception e){ 
  12.         e.printStackTrace(); 
  13.     } 

示例結(jié)果:

總結(jié)

本文從 iText 最基本的用法,分別介紹從表格,圖片,中文來介紹 iText,希望能夠幫助到你。

責(zé)任編輯:未麗燕 來源: 今日頭條
相關(guān)推薦

2021-12-08 10:36:46

JavaPDF文件

2020-12-28 07:47:35

動(dòng)態(tài)代理AOP

2024-12-03 08:43:49

2020-06-19 17:49:23

建網(wǎng)

2018-08-27 08:31:25

InnoDBMySQL

2022-07-08 14:35:05

Java組件LiteFlow

2020-02-20 16:07:45

IT需求

2011-04-20 17:27:27

ESFramework

2022-01-09 23:38:42

通信協(xié)議網(wǎng)絡(luò)

2022-02-23 20:42:40

HTMLmarkdownturndown

2010-08-25 21:50:36

配置DHCP

2022-08-12 12:19:13

Cluster檢索集群

2010-03-05 09:49:34

Python文件操作

2011-10-11 10:53:29

Ubuntu 11.1Gnome 3.2

2009-04-29 01:39:57

破解美萍萬象

2011-09-15 10:35:12

Android應(yīng)用IOS應(yīng)用著裝搭配

2024-04-07 00:00:00

億級數(shù)據(jù)ES

2009-07-24 17:54:20

iBatis配置

2009-08-28 15:49:45

C#對INI文件操作
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號