亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時間:2023年09月18日 14:07:17   作者:愛打羽球的碼猿  
這篇文章主要為大家詳細(xì)介紹了Java實現(xiàn)PDF導(dǎo)出功能的相關(guān)知識,文中的示例代碼講解詳細(xì),具有一定的學(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();
            //標(biāo)題
            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());
            //添加標(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 = {"報表生成日期 ","狀態(tài) ", "經(jīng)辦人員 ", "提交時間 ","復(fù)核人員 ", "復(fù)核時間 ", "情況補(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", "測試導(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)出功能的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于Java導(dǎo)出PDF的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • FileUtils擴(kuò)展readURLtoString讀取url內(nèi)容

    FileUtils擴(kuò)展readURLtoString讀取url內(nèi)容

    這篇文章主要介紹了FileUtils擴(kuò)展readURLtoString使用其支持讀取URL內(nèi)容為String,支持帶POST傳大量參數(shù),大家參考使用吧
    2014-01-01
  • Java布爾值Boolean和boolean之間轉(zhuǎn)換實例用法

    Java布爾值Boolean和boolean之間轉(zhuǎn)換實例用法

    在本篇文章里小編給大家整理的是一篇關(guān)于Java布爾值Boolean和boolean之間轉(zhuǎn)換實例用法內(nèi)容,有需要的朋友們跟著學(xué)習(xí)參考下。
    2021-06-06
  • springBoot的事件機(jī)制GenericApplicationListener用法解析

    springBoot的事件機(jī)制GenericApplicationListener用法解析

    這篇文章主要介紹了springBoot的事件機(jī)制GenericApplicationListener用法解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值的相關(guān)資料
    2019-09-09
  • 解決idea使用過程中讓你覺得不爽的一些問題(小結(jié))

    解決idea使用過程中讓你覺得不爽的一些問題(小結(jié))

    這篇文章主要介紹了解決idea使用過程中讓你覺得不爽的一些問題,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • 解決spring boot hibernate 懶加載的問題

    解決spring boot hibernate 懶加載的問題

    這篇文章主要介紹了解決spring boot hibernate 懶加載的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-10-10
  • Mybatis SQL運(yùn)行流程源碼詳解

    Mybatis SQL運(yùn)行流程源碼詳解

    這篇文章主要介紹了Mybatis SQL運(yùn)行流程源碼詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-10-10
  • 詳解如何使用java實現(xiàn)Open Addressing

    詳解如何使用java實現(xiàn)Open Addressing

    這篇文章主要介紹了詳解如何使用java實現(xiàn)Open Addressing,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • 解決eclipse上傳svn忽略target文件夾的坑

    解決eclipse上傳svn忽略target文件夾的坑

    這篇文章主要介紹了解決eclipse上傳svn忽略target文件夾的坑,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01
  • 詳解Java中兩種分頁遍歷的使用姿勢

    詳解Java中兩種分頁遍歷的使用姿勢

    這篇文章主要介紹了詳解Java中兩種分頁遍歷的使用姿勢,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • Java調(diào)用參數(shù)類型是application/x-www-form-urlencoded的API問題

    Java調(diào)用參數(shù)類型是application/x-www-form-urlencoded的API問題

    在使用Postman進(jìn)行接口測試時,對于POST請求,需將請求頭設(shè)置為application/x-www-form-urlencoded,并將參數(shù)轉(zhuǎn)為String類型,通常在GET請求中,參數(shù)直接拼接在URL后,本文通過具體實例,詳細(xì)講解了參數(shù)處理的方法,適合API開發(fā)者參考
    2024-09-09

最新評論