Java實現(xiàn)PDF導出功能的示例代碼
更新時間:2023年09月18日 14:07:17 作者:愛打羽球的碼猿
這篇文章主要為大家詳細介紹了Java實現(xiàn)PDF導出功能的相關知識,文中的示例代碼講解詳細,具有一定的學習價值,感興趣的小伙伴可以了解下
一、添加依賴
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
<version>2.1.5</version>
</dependency>二、實現(xiàn)示例代碼
如下代碼中使用了 【SIMYOU.TTF】幼圓字體,根據(jù)需要可以自行下載
package com.lyp;
import com.lowagie.text.*;
import com.lowagie.text.Font;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.List;
public class downLoadPDF {
public static void main(String[] args) {
String fileName = "test.pdf";
String path = "D:pdf/";
try {
//創(chuàng)建文檔,設置頁面大小、左右上下邊距
Document document = new Document();
//處理中文顯示問題,使用資源字體
BaseFont bfChinese = BaseFont.createFont("/font/SIMYOU.TTF",BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
Font dateTitle = new Font(bfChinese,7,Font.NORMAL);
Font title = new Font(bfChinese,12,Font.BOLD);//文字加粗
Font textFont = new Font(bfChinese,7,Font.NORMAL);//文字正常
//給出輸出路徑
PdfWriter.getInstance(document, new FileOutputStream(path+fileName));
//打開文檔
document.open();
//標題
PdfPTable tableTitle = new PdfPTable(3);
PdfPTable codeTitle = new PdfPTable(1);
//生成一個7列的表格
PdfPTable table = new PdfPTable(7);
//定義每個單元格的寬度
float[] widthsHeaderTitle = {1f,1f,1f};
float[] widthsCodeTitle = {1f};
float[] widthsHeader = {1f,1f,1f,1f,1f,1f,2f};
float lineHeight = (float)20.0;
//設置表格每一格的寬度
tableTitle.setWidths(widthsHeaderTitle);
codeTitle.setWidths(widthsCodeTitle);
table.setWidths(widthsHeader);
//設置表格總體寬度
tableTitle.setWidthPercentage(100);
codeTitle.setWidthPercentage(100);
table.setWidthPercentage(100);
int colSpan = 1;
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date(System.currentTimeMillis());
//添加標題
createTableCellLeft("導出人:行如火", textFont, tableTitle, lineHeight, colSpan);
createTableCellCenter("確認出差記錄表", title, tableTitle, lineHeight, colSpan);
createTableCellRight(formatter.format(date), dateTitle, tableTitle, lineHeight, colSpan);
document.add(tableTitle);
createTableCellRight("功能碼: XXXXXXXXX",textFont, codeTitle, lineHeight, colSpan);
document.add(codeTitle);
document.add(new Paragraph("\n"));
String[] array = {"報表生成日期 ","狀態(tài) ", "經(jīng)辦人員 ", "提交時間 ","復核人員 ", "復核時間 ", "情況補充 "};
for (String s : array) {
createTableCell(s, textFont, table, lineHeight, colSpan);
}
List<Map<String, Object>> dataResult = getResult();
for (Map<String, Object> map : dataResult) {
String dataTime = map.get("dateTime") != null?map.get("dateTime").toString():"0";
String status = "0";
if (null != map.get("status")) {
if ("0".equals(map.get("status"))) {
status = "待確認";
}
if ("1".equals(map.get("status"))) {
status = "待復核";
}
if ("2".equals(map.get("status"))) {
status = "已復核";
}
}
String creator = map.get("creator") != null?map.get("creator").toString():"0";
String createTime = map.get("createTime") != null?map.get("createTime").toString():"0";
String updator = map.get("updator") != null?map.get("updator").toString():"0";
String updateTime = map.get("updateTime") != null?map.get("updateTime").toString():"0";
String description = map.get("description") != null?map.get("description").toString():"0";
createTableCell(dataTime, textFont, table, lineHeight, colSpan);
createTableCell(status, textFont, table, lineHeight, colSpan);
createTableCell(creator, textFont, table, lineHeight, colSpan);
createTableCell(createTime, textFont, table, lineHeight, colSpan);
createTableCell(updator, textFont, table, lineHeight, colSpan);
createTableCell(updateTime, textFont, table, lineHeight, colSpan);
createTableCell(description, textFont, table, lineHeight, colSpan);
}
document.add(table);
document.close();
} catch (DocumentException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
File file = new File(fileName);
System.out.println(file);
}
private static void createTableCell(String text, Font font, PdfPTable table, float lineHeight, int colSapn) {
PdfPCell cell;
cell = new PdfPCell(new Paragraph(text,font));
//合并單元格列
cell.setColspan(colSapn);
//設置水平居中
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
//設置垂直居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setFixedHeight(lineHeight);
//將單元格內(nèi)容添加到表格中
table.addCell(cell);
}
private static void createTableCellLeft(String text, Font font, PdfPTable table, float lineHeight ,int colSapn) {
PdfPCell cell;
cell = new PdfPCell(new Paragraph(text,font));
//合并單元格列
cell.setColspan(colSapn);
//設置水平
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
//設置垂直
cell.setVerticalAlignment(Element.ALIGN_BOTTOM);
cell.setFixedHeight(lineHeight);
cell.setBorderWidthRight(0);
cell.setBorderWidthBottom(0);
cell.setBorderWidthLeft(0);
cell.setBorderWidthTop(0);
//將單元格內(nèi)容添加到表格中
table.addCell(cell);
}
private static void createTableCellCenter(String text, Font font, PdfPTable table, float lineHeight ,int colSapn) {
PdfPCell cell;
cell = new PdfPCell(new Paragraph(text,font));
//合并單元格列
cell.setColspan(colSapn);
//設置水平居中
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
//設置垂直居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setFixedHeight(lineHeight);
cell.setBorderWidthLeft(0);
cell.setBorderWidthRight(0);
cell.setBorderWidthBottom(0);
cell.setBorderWidthTop(0);
//將單元格內(nèi)容添加到表格中
table.addCell(cell);
}
private static void createTableCellRight(String text, Font font, PdfPTable table, float lineHeight , int colSapn) {
PdfPCell cell;
cell = new PdfPCell(new Paragraph(text,font));
//合并單元格列
cell.setColspan(colSapn);
//設置水平
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
//設置垂直
cell.setVerticalAlignment(Element.ALIGN_TOP);
cell.setFixedHeight(lineHeight);
//將單元格內(nèi)容添加到表格中
//去除邊框
cell.setBorderWidthLeft(0);
cell.setBorderWidthBottom(0);
cell.setBorderWidthRight(0);
cell.setBorderWidthTop(0);
table.addCell(cell);
}
private static List<Map<String, Object>> getResult() {
List<Map<String, Object>> result = new ArrayList<>();
for (int i = 0; i < 10; i++) {
Map<String, Object> map = new HashMap<>();
map.put("dateTime", "2022/10/11");
map.put("creator", "行如火");
map.put("createTime", "2022/10/10");
map.put("updator", "疾如風");
map.put("updateTime", "2022/10/11");
if(i == 0) {
map.put("status", "0");
map.put("description", "測試導出PDF");
} else if (i < 4){
map.put("status", "1");
map.put("description", "測試導出PDF"+i);
} else {
map.put("status", "2");
map.put("description", "測試導出PDF"+i);
}
result.add(map);
}
System.out.println(result);
return result;
}
}三、效果展示
對應目錄下生成test.pdf 文件

生成效果如下所示:

以上就是Java實現(xiàn)PDF導出功能的示例代碼的詳細內(nèi)容,更多關于Java導出PDF的資料請關注腳本之家其它相關文章!
相關文章
FileUtils擴展readURLtoString讀取url內(nèi)容
這篇文章主要介紹了FileUtils擴展readURLtoString使用其支持讀取URL內(nèi)容為String,支持帶POST傳大量參數(shù),大家參考使用吧2014-01-01
Java布爾值Boolean和boolean之間轉換實例用法
在本篇文章里小編給大家整理的是一篇關于Java布爾值Boolean和boolean之間轉換實例用法內(nèi)容,有需要的朋友們跟著學習參考下。2021-06-06
springBoot的事件機制GenericApplicationListener用法解析
這篇文章主要介紹了springBoot的事件機制GenericApplicationListener用法解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值的相關資料2019-09-09
解決spring boot hibernate 懶加載的問題
這篇文章主要介紹了解決spring boot hibernate 懶加載的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10
詳解如何使用java實現(xiàn)Open Addressing
這篇文章主要介紹了詳解如何使用java實現(xiàn)Open Addressing,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-12-12
Java調(diào)用參數(shù)類型是application/x-www-form-urlencoded的API問題
在使用Postman進行接口測試時,對于POST請求,需將請求頭設置為application/x-www-form-urlencoded,并將參數(shù)轉為String類型,通常在GET請求中,參數(shù)直接拼接在URL后,本文通過具體實例,詳細講解了參數(shù)處理的方法,適合API開發(fā)者參考2024-09-09

