溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Java使用itext5實現(xiàn)PDF表格文檔導(dǎo)出的示例分析

發(fā)布時間:2021-04-17 12:43:35 來源:億速云 閱讀:276 作者:小新 欄目:編程語言

這篇文章給大家分享的是有關(guān)Java使用itext5實現(xiàn)PDF表格文檔導(dǎo)出的示例分析的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

最近拿到一個需求,需要導(dǎo)出PDF文檔,市面上可以實現(xiàn)的方法有很多,經(jīng)過測試和調(diào)研決定使用itext5來實現(xiàn),話不多說,說干就干。

1.依賴導(dǎo)入

<!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf -->
  <dependency>
   <groupId>com.itextpdf</groupId>
   <artifactId>itextpdf</artifactId>
   <version>5.5.13.1</version>
  </dependency>
  <!-- https://mvnrepository.com/artifact/com.itextpdf/itext-asian -->
  <dependency>
   <groupId>com.itextpdf</groupId>
   <artifactId>itext-asian</artifactId>
   <version>5.2.0</version>
  </dependency>

這里說明下:上面的依賴就是主要實現(xiàn)PDF生成的,下面的依賴是中文字體相關(guān)依賴;

2.PDF表格導(dǎo)出實現(xiàn)

1.導(dǎo)出PDF

//   1.打開文檔并設(shè)置基本屬性
   Document document = new Document();
//   2.設(shè)置請求頭,encode文件名
   response.setContentType("application/pdf;charset=UTF-8");
   response.setHeader("Content-Disposition", 
   "attachment; filename=" + java.net.URLEncoder.encode("" + 
   recordDto.getTitle() + ".pdf", "UTF-8"));
//   3.通過流將pdf實例寫出到瀏覽器
   PdfWriter writer = PdfWriter.getInstance(document, response.getOutputStream());

至此導(dǎo)出PDF已經(jīng)實現(xiàn)了,只是這個PDF中什么內(nèi)容都沒有,明白這一點,接下來做的就是給這個文檔“加料”咯(這里的response就是HttpServletResponse)。

2.頁面美化

//    這里的wirter就是上文的writer
   writer.setViewerPreferences(PdfWriter.PageModeUseThumbs);
   writer.setPageSize(PageSize.A4);

這里設(shè)置了文檔的顯示縮略圖以及文檔大小為A4;

3.中文字體設(shè)置

public static Font getPdfChineseFont() throws Exception {
  BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H",
    BaseFont.NOT_EMBEDDED);
  Font fontChinese = new Font(bfChinese, 12, Font.NORMAL);
  fontChinese.setColor(BaseColor.BLACK);
  fontChinese.setSize(11);
  return fontChinese;
 }

這個方法設(shè)置了中文字體樣式,感興趣的同學(xué)可以試試其他的樣式,例如:字體顏色,大小,字體都可以修改;

4.輸出表格內(nèi)容到文檔

// 首先打開文檔
document.open();
// 向文檔中添加表格數(shù)據(jù)
private static void printBasicInfo(ShopApplyRecordDto recordDto, Document document, Font font) throws DocumentException {
// 表格中的數(shù)據(jù)
  Object[][] basicDatas = {
    {"標(biāo)題","xxx申請", "審批編號","1234"},
    {"申請人","小明", "申請商鋪","xxx商場"},
    {"申請日期","2020/1/16", "審批結(jié)果","同意")}};
// 每個cell的寬度
  float[] widthss = {50, 200, 50, 200};
//   創(chuàng)建一個表格,每一行有四個cell
  PdfPTable basicTable = new PdfPTable(widthss);
// 外層循環(huán)表格的行
  for (int i = 0; i < basicDatas.length; i++) {
// 內(nèi)層循環(huán)每一行具體數(shù)據(jù)
   for (int j = 0; j < basicDatas[i].length; j++) {
//   新建一個cell
    PdfPCell cell = new PdfPCell();
// 這個方法是統(tǒng)一設(shè)置表格和cell的樣式,下面會寫 
    setTableStyle(basicTable, cell);
// cell中需要填充數(shù)據(jù)的格式
    Paragraph paragraph = 
    new Paragraph(StrUtil.toString(basicDatas[i][j]), font);
// 設(shè)置cell的值
    cell.setPhrase(paragraph);
// 將cell添加到表格中
    basicTable.addCell(cell);
   }
  }
// 將表格添加到文檔中
  document.add(basicTable);
 }
// 結(jié)束時要關(guān)閉文檔
document.close();

大功告成,現(xiàn)在導(dǎo)出的PDF中已經(jīng)有了類似這樣的表格了:

Java使用itext5實現(xiàn)PDF表格文檔導(dǎo)出的示例分析

當(dāng)然你的樣式會很丑,接下來我們來設(shè)置下樣式。

5.表格和cell樣式設(shè)置

public static void setTableStyle(PdfPTable table, PdfPCell cell) {
// 設(shè)置表格樣式
  table.setLockedWidth(true);
  table.setTotalWidth(500);
  table.setHorizontalAlignment(Element.ALIGN_LEFT);
// 設(shè)置單元格樣式
  cell.setMinimumHeight(35);
  cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
  cell.setBackgroundColor(BaseColor.WHITE);
  cell.setBorder(0);
  cell.setBorderWidthTop(0.1f);
  cell.setBorderWidthBottom(0.1f);
  cell.setBorderWidthLeft(0.1f);
  cell.setBorderWidthRight(0.1f);
  cell.setBorderColorBottom(BaseColor.BLACK);
  cell.setBorderColorLeft(BaseColor.BLACK);
  cell.setBorderColorRight(BaseColor.BLACK);
  cell.setBorderColorTop(BaseColor.BLACK);
  cell.setPadding(3);
 }

api方法還是比較易懂的,這里就不多贅述了,不明白的自己設(shè)置試試就可以做出自己喜歡的樣式咯。

6.頁眉和頁碼的設(shè)置

這里說明下,itext2和itext5的api有很大不同,2的版本有一個專門的HeaderFooter類來設(shè)置樣式,5的版本沒有這樣的類,取而代之的是PdfPageEventHelper這樣一個事件處理類,這里大家千萬別弄混了,這兩個版本的api互相不兼容;
這里首先寫一個PdfPageEventHelper的子類來實現(xiàn)頁眉頁碼的打?。?/p>

public class HeaderFooter extends PdfPageEventHelper {
// 這里是業(yè)務(wù)相關(guān)的屬性可以無視
 private ShopApplyRecordDto recordDto;
 private SysUserInfo userInfo;
// 大部分情況下頁眉的值是動態(tài)的,這里可以在初始化的時候進行參數(shù)傳遞
 public HeaderFooter(ShopApplyRecordDto recordDto, SysUserInfo userInfo) {
  this.recordDto = recordDto;
  this.userInfo = userInfo;
 }

 public HeaderFooter() {
 }

 public ShopApplyRecordDto getRecordDto() {
  return recordDto;
 }

 public void setRecordDto(ShopApplyRecordDto recordDto) {
  this.recordDto = recordDto;
 }

 public SysUserInfo getUserInfo() {
  return userInfo;
 }

 public void setUserInfo(SysUserInfo userInfo) {
  this.userInfo = userInfo;
 }
// 這個方法就是實現(xiàn)頁眉和頁碼的關(guān)鍵:它的含義是每當(dāng)頁面結(jié)束會執(zhí)行該方法
 @Override
 public void onEndPage(PdfWriter writer, Document document) {
  Font font = null;
  try {
   font = getPdfChineseFont();
  } catch (Exception e) {
   e.printStackTrace();
  }
  SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm");
// 設(shè)置頁眉:這里圖省事就用空格來實現(xiàn)左中右三個位置的頁眉,其實可以寫三個,通過Element.ALIGN_LEFT來控制頁眉的位置,document.left()/document.top()這兩個可以設(shè)置頁眉具體位置類似于html的上下調(diào)整,大家可以多試試
  ColumnText.showTextAligned(writer.getDirectContent(), 
  Element.ALIGN_LEFT,
  new Phrase("所屬項目:" + recordDto.getMallName() + "             打印時間:" + format.format(new Date()) + "         打印人:" + userInfo.getUserName(), font),
  document.left(),
  document.top() + 3, 0);
// 獲得一個名為“art”的盒子 
  Rectangle rect = writer.getBoxSize("art");
// 設(shè)置頁碼:這里的頁碼位置已經(jīng)設(shè)置好,大家可直接使用,至于1/20這種效果的頁碼實現(xiàn)則十分復(fù)雜,如有需求請自行百度/谷歌
  ColumnText.showTextAligned(writer.getDirectContent(),
  Element.ALIGN_CENTER, 
  new Phrase(String.format("%d", writer.getPageNumber())),
  (rect.getLeft() + rect.getRight()) / 2, 
  rect.getBottom() - 18, 0);
 }

 public static Font getPdfChineseFont() throws Exception {
  BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H",
    BaseFont.NOT_EMBEDDED);
  Font fontChinese = new Font(bfChinese, 12, Font.NORMAL);
  fontChinese.setColor(BaseColor.BLACK);
  fontChinese.setSize(11);
  return fontChinese;
 }
}
 
接下來就很簡單了,將我們的HeaderFooter設(shè)置給PdfWriter對象即可:

// 新建HeaderFooter并傳遞需要的參數(shù)
HeaderFooter headerFooter = new HeaderFooter(recordDto, userInfo);
// 新建一個盒子
Rectangle rect = new Rectangle(36, 54, 559, 788);
// 設(shè)置名稱為“art”,上面get的就是這個盒子了
writer.setBoxSize("art", rect);
writer.setPageEvent(headerFooter);
// 這個可以設(shè)置內(nèi)容的margin
document.setMargins(45f, 45f, 65f, 50f);

7.效果展示

Java使用itext5實現(xiàn)PDF表格文檔導(dǎo)出的示例分析

感謝各位的閱讀!關(guān)于“Java使用itext5實現(xiàn)PDF表格文檔導(dǎo)出的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI