最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java實(shí)現(xiàn)PDF導(dǎo)出功能的示例代碼

 更新時(shí)間:2023年09月18日 14:07:17   作者:愛(ài)打羽球的碼猿  
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)PDF導(dǎo)出功能的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以了解下

一、添加依賴

 <dependency>
            <groupId>com.lowagie</groupId>
            <artifactId>itext</artifactId>
            <version>2.1.5</version>
        </dependency>

二、實(shí)現(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è)置頁(yè)面大小、左右上下邊距
            Document document = new Document();
            //處理中文顯示問(wèn)題,使用資源字體
            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));
            //打開(kāi)文檔
            document.open();
            //標(biāo)題
            PdfPTable tableTitle = new PdfPTable(3);
            PdfPTable codeTitle = new PdfPTable(1);
            //生成一個(gè)7列的表格
            PdfPTable table = new PdfPTable(7);
            //定義每個(gè)單元格的寬度
            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());
            //添加標(biāo)題
            createTableCellLeft("導(dǎo)出人:行如火", textFont, tableTitle, lineHeight, colSpan);
            createTableCellCenter("確認(rèn)出差記錄表", 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 = {"報(bào)表生成日期 ","狀態(tài) ", "經(jīng)辦人員 ", "提交時(shí)間 ","復(fù)核人員 ", "復(fù)核時(shí)間 ", "情況補(bǔ)充 "};
            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 = "待確認(rèn)";
                    }
                    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", "測(cè)試導(dǎo)出PDF");
            } else if (i < 4){
                map.put("status", "1");
                map.put("description", "測(cè)試導(dǎo)出PDF"+i);
            } else {
                map.put("status", "2");
                map.put("description", "測(cè)試導(dǎo)出PDF"+i);
            }
            result.add(map);
        }
        System.out.println(result);
        return result;
    }
}

三、效果展示

對(duì)應(yīng)目錄下生成test.pdf 文件

生成效果如下所示:

以上就是Java實(shí)現(xiàn)PDF導(dǎo)出功能的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于Java導(dǎo)出PDF的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Netty分布式固定長(zhǎng)度解碼器實(shí)現(xiàn)原理剖析

    Netty分布式固定長(zhǎng)度解碼器實(shí)現(xiàn)原理剖析

    這篇文章主要為大家介紹了Netty分布式固定長(zhǎng)度解碼器原理剖析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-03-03
  • fastjson全局日期序列化設(shè)置導(dǎo)致JSONField失效問(wèn)題解決方案

    fastjson全局日期序列化設(shè)置導(dǎo)致JSONField失效問(wèn)題解決方案

    這篇文章主要介紹了fastjson通過(guò)代碼指定全局序列化返回時(shí)間格式,導(dǎo)致使用JSONField注解標(biāo)注屬性的特殊日期返回格式失效問(wèn)題的解決方案
    2023-01-01
  • java線程間通信的通俗解釋及代碼示例

    java線程間通信的通俗解釋及代碼示例

    這篇文章主要介紹了java線程間通信的通俗解釋,介紹了線程通信中的幾個(gè)相關(guān)概念,然后分享了線程通信的實(shí)現(xiàn)方式及代碼示例,具有一定參考價(jià)值 ,需要的朋友可以了解下。
    2017-11-11
  • springboot如何讀取application.yml文件

    springboot如何讀取application.yml文件

    這篇文章主要介紹了springboot如何讀取application.yml文件的方法,幫助大家更好的理解和使用springboot框架,感興趣的朋友可以了解下
    2020-12-12
  • 淺析Java如何高效將PDF轉(zhuǎn)換為高質(zhì)量TIFF圖片

    淺析Java如何高效將PDF轉(zhuǎn)換為高質(zhì)量TIFF圖片

    在文檔處理和歸檔系統(tǒng)中,將PDF文件轉(zhuǎn)換為T(mén)IFF格式是一項(xiàng)非常常見(jiàn)的需求,本文將介紹如何使用Java通過(guò)Spire.PDF?for?Java庫(kù),快速實(shí)現(xiàn)PDF到TIFF的轉(zhuǎn)換,希望對(duì)大家有所幫助
    2026-04-04
  • Maven引入本地Jar包并打包進(jìn)War包中的方法

    Maven引入本地Jar包并打包進(jìn)War包中的方法

    本篇文章主要介紹了Maven引入本地Jar包并打包進(jìn)War包中的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-11-11
  • 使用Java代碼獲取服務(wù)器性能信息及局域網(wǎng)內(nèi)主機(jī)名

    使用Java代碼獲取服務(wù)器性能信息及局域網(wǎng)內(nèi)主機(jī)名

    這篇文章主要介紹了使用Java代碼獲取服務(wù)器性能信息及局域網(wǎng)內(nèi)主機(jī)名的方法,方便對(duì)服務(wù)器的遠(yuǎn)程管理和團(tuán)隊(duì)協(xié)作時(shí)用到,而且文中的方法無(wú)需調(diào)用jni,需要的朋友可以參考下
    2015-11-11
  • Scala方法與函數(shù)使用和定義詳解

    Scala方法與函數(shù)使用和定義詳解

    這個(gè)章節(jié)會(huì)很燒腦,需要認(rèn)真研讀,我會(huì)盡量寫(xiě)的詳細(xì)一些。 方法和函數(shù),看似是兩個(gè)概念,其實(shí)他嚴(yán)格來(lái)說(shuō)也是兩個(gè)概念,但我們大可以理解成是同一個(gè)概念,在使用時(shí)只有語(yǔ)法上的細(xì)微差別,是很類似的,都理解為function即可
    2022-12-12
  • Spring boot GC實(shí)現(xiàn)過(guò)程原理解析

    Spring boot GC實(shí)現(xiàn)過(guò)程原理解析

    這篇文章主要介紹了Spring boot GC實(shí)現(xiàn)過(guò)程原理解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-08-08
  • 基于Apache組件分析對(duì)象池原理的實(shí)現(xiàn)案例分析

    基于Apache組件分析對(duì)象池原理的實(shí)現(xiàn)案例分析

    本文從對(duì)象池的一個(gè)簡(jiǎn)單案例切入,主要分析common-pool2組件關(guān)于:池、工廠、配置、對(duì)象管理幾個(gè)角色的源碼邏輯,并且參考其在Redis中的實(shí)踐,對(duì)Apache組件分析對(duì)象池原理相關(guān)知識(shí)感興趣的朋友一起看看吧
    2022-04-04

最新評(píng)論

天台县| 綦江县| 栖霞市| 罗田县| 霍城县| 山西省| 兴海县| 察哈| 辉县市| 龙山县| 内乡县| 大足县| 合江县| 宣威市| 台江县| 米泉市| 甘洛县| 汉寿县| 芜湖市| 长宁区| 紫阳县| 若尔盖县| 洛阳市| 长海县| 姚安县| 衡阳市| 无为县| 黄大仙区| 福清市| 永清县| 汶川县| 历史| 梁山县| 子洲县| 方城县| 云南省| 五原县| 太湖县| 梅州市| 九寨沟县| 改则县|