Java實現(xiàn)PDF導(dǎo)出功能的示例代碼
更新時間:2023年09月18日 14:07:17 作者:愛打羽球的碼猿
這篇文章主要為大家詳細介紹了Java實現(xiàn)PDF導(dǎo)出功能的相關(guān)知識,文中的示例代碼講解詳細,具有一定的學(xué)習(xí)價值,感興趣的小伙伴可以了解下
一、添加依賴
<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)建文檔,設(shè)置頁面大小、左右上下邊距
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;
//設(shè)置表格每一格的寬度
tableTitle.setWidths(widthsHeaderTitle);
codeTitle.setWidths(widthsCodeTitle);
table.setWidths(widthsHeader);
//設(shè)置表格總體寬度
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("導(dǎo)出人:行如火", 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)辦人員 ", "提交時間 ","復(fù)核人員 ", "復(fù)核時間 ", "情況補充 "};
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 = "待復(fù)核";
}
if ("2".equals(map.get("status"))) {
status = "已復(fù)核";
}
}
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);
//設(shè)置水平居中
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
//設(shè)置垂直居中
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);
//設(shè)置水平
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
//設(shè)置垂直
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);
//設(shè)置水平居中
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
//設(shè)置垂直居中
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);
//設(shè)置水平
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
//設(shè)置垂直
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", "疾如風(fēng)");
map.put("updateTime", "2022/10/11");
if(i == 0) {
map.put("status", "0");
map.put("description", "測試導(dǎo)出PDF");
} else if (i < 4){
map.put("status", "1");
map.put("description", "測試導(dǎo)出PDF"+i);
} else {
map.put("status", "2");
map.put("description", "測試導(dǎo)出PDF"+i);
}
result.add(map);
}
System.out.println(result);
return result;
}
}三、效果展示
對應(yīng)目錄下生成test.pdf 文件

生成效果如下所示:

以上就是Java實現(xiàn)PDF導(dǎo)出功能的示例代碼的詳細內(nèi)容,更多關(guān)于Java導(dǎo)出PDF的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
fastjson全局日期序列化設(shè)置導(dǎo)致JSONField失效問題解決方案
這篇文章主要介紹了fastjson通過代碼指定全局序列化返回時間格式,導(dǎo)致使用JSONField注解標注屬性的特殊日期返回格式失效問題的解決方案2023-01-01
springboot如何讀取application.yml文件
這篇文章主要介紹了springboot如何讀取application.yml文件的方法,幫助大家更好的理解和使用springboot框架,感興趣的朋友可以了解下2020-12-12
淺析Java如何高效將PDF轉(zhuǎn)換為高質(zhì)量TIFF圖片
在文檔處理和歸檔系統(tǒng)中,將PDF文件轉(zhuǎn)換為TIFF格式是一項非常常見的需求,本文將介紹如何使用Java通過Spire.PDF?for?Java庫,快速實現(xiàn)PDF到TIFF的轉(zhuǎn)換,希望對大家有所幫助2026-04-04
使用Java代碼獲取服務(wù)器性能信息及局域網(wǎng)內(nèi)主機名
這篇文章主要介紹了使用Java代碼獲取服務(wù)器性能信息及局域網(wǎng)內(nèi)主機名的方法,方便對服務(wù)器的遠程管理和團隊協(xié)作時用到,而且文中的方法無需調(diào)用jni,需要的朋友可以參考下2015-11-11
基于Apache組件分析對象池原理的實現(xiàn)案例分析
本文從對象池的一個簡單案例切入,主要分析common-pool2組件關(guān)于:池、工廠、配置、對象管理幾個角色的源碼邏輯,并且參考其在Redis中的實踐,對Apache組件分析對象池原理相關(guān)知識感興趣的朋友一起看看吧2022-04-04

