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

Java使用itextpdf實(shí)現(xiàn)表單導(dǎo)出為pdf

 更新時(shí)間:2025年06月26日 10:33:22   作者:ciku  
這篇文章主要為大家詳細(xì)介紹了Java如何使用itextpdf實(shí)現(xiàn)form表單導(dǎo)出為pdf,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

下文將簡述如何通過itextpdf 導(dǎo)出form表單,涉及的內(nèi)容有字體設(shè)置、創(chuàng)建表格、表格樣式設(shè)置、安全性設(shè)置、表頭設(shè)置、增加一行包括內(nèi)容、增加多行、合并列、增加超鏈接

1.引入依賴

       <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>5.5.13.2</version>
        </dependency>

2.字體設(shè)置

對于導(dǎo)出的內(nèi)容包含中文需要設(shè)置字體,windows系統(tǒng)自帶的中文字體文件一般在C:/Windows/Fonts/下,不使用中文字體將顯示空白

//獲取基礎(chǔ)字體
    public static BaseFont getBaseFont(String path){
        if(baseFont!=null){
            return baseFont;
        }
        BaseFont baseFont=null;
        try {
            baseFont= BaseFont.createFont(path,
                    BaseFont.IDENTITY_H,
                    BaseFont.EMBEDDED);

        }catch (Exception e){
            e.printStackTrace();
        }
        return baseFont;
    }
    /**
     * 設(shè)置中文字體大小
     * @param path 字體路徑
     * @param fontSize 字體大小
     * @param style 樣式
     * @return
     */
    public static Font getChineseFont(String path,int fontSize,int style){
        Font chineseFont = null;
        try {
            BaseFont baseFont = getBaseFont(path);
            chineseFont = new Font(baseFont, fontSize);
            chineseFont.setStyle(style);
        }catch (Exception e){
            e.printStackTrace();
        }
        return chineseFont;
    }

3.創(chuàng)建表格

  /**
     * 獲取一個(gè)table
     * @param tableColNum 列數(shù)
     * @param widthPercentage 寬度顯示百分比
     * @param spacingBefore 設(shè)置前間距
     * @param spacingAfter 設(shè)置后間距
     * @param columnWidths 設(shè)置表格列寬,格式{2f,2f},注意集合數(shù)量與列數(shù)相同,按順序定義列寬
     * @return
     */
    public static PdfPTable getTable(Integer tableColNum,float widthPercentage,float spacingBefore,float spacingAfter, float[] columnWidths){
        if(tableColNum==null||tableColNum==0){
            return null;
        }
        PdfPTable table = new PdfPTable(tableColNum); //創(chuàng)建一個(gè)tableColNum的表格
        table.setWidthPercentage(widthPercentage);//設(shè)置百分比
        table.setSpacingBefore(spacingBefore);//設(shè)置前間距
        table.setSpacingAfter(spacingAfter);//設(shè)置后間距
        // 設(shè)置表格列寬
        try {
            table.setWidths(columnWidths);
        } catch (DocumentException e) {
            throw new RuntimeException(e);
        }
        return table;
    }

4.設(shè)置表格無邊框

需要創(chuàng)建完所有行后,再設(shè)置為無邊框

    /**
     * 設(shè)置表格為無邊框
     * @param table
     */
    public static void setNoneBorderTable(PdfPTable table){
        for (PdfPRow row : table.getRows()) {
            for (PdfPCell cell : row.getCells()) {
                if (cell != null) {
                    cell.setBorder(Rectangle.NO_BORDER);
                }
            }
       

5.安全性設(shè)置

    /**
     * 設(shè)置文檔的安全性
     * @param document
     * @param pdfPath
     * @return
     */
    public static PdfWriter  getSecurityWriter( Document document,String pdfPath,String ownerPass)  {
        PdfWriter instance = null;
        try {
            instance = PdfWriter.getInstance(document, new FileOutputStream(pdfPath));
            if(StringUtils.isEmpty(ownerPass)){
                ownerPass="ssc-logistic";
            }
            instance.setEncryption(
                    null,               // 用戶密碼(空表示無需密碼)
                    ownerPass.getBytes(), // 所有者密碼,修改時(shí)需要該密碼
                    PdfWriter.ALLOW_PRINTING |//允許打印
                            PdfWriter.ALLOW_COPY |//允許復(fù)制
                            PdfWriter.ALLOW_SCREENREADERS,//允許屏幕閱讀器訪問
                    PdfWriter.ENCRYPTION_AES_256//AES 256加密
            );
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return instance;

    }

6.設(shè)置表頭

 /**
     * 設(shè)置表頭
     * @param table
     * @param headerMap 按照put順序
     * @param fontPath 從配置文件帳獲取
     * @param language 語言
     */
    public  static void addTableHeader(PdfPTable table, LinkedHashMap<String,String> headerMap, String language,String fontPath){
        Font chineseFont=getChineseFont(fontPath);
        if(headerMap!=null){
            for(String key:headerMap.keySet()){
                PdfPCell headerCell = new PdfPCell(new Phrase(headerMap.get(key), chineseFont));
                headerCell.setBackgroundColor(BaseColor.LIGHT_GRAY);//背景顏色
                headerCell.setHorizontalAlignment(Element.ALIGN_CENTER);//水平對齊方式
                headerCell.setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直對齊方式
                headerCell.setFixedHeight(40f);//行高
                table.addCell(headerCell);
            }
        }
    }

7.增加一行內(nèi)容

  /**
     * 增加表格內(nèi)容行
     * @param table
     * @param rowMap 格式為k-v如k:field1 v:test
     * @param language
     */
    public static void addTableRole(PdfPTable table,LinkedHashMap<String,String> rowMap,String fontPath){
        Font chineseFont = getChineseFont(fontPath);
        Font font = new Font(Font.FontFamily.COURIER, 10);
        if(rowMap!=null){
            for(String key:rowMap.keySet()){
                String fieldValue=rowMap.get(key);
                PdfPCell cell = new PdfPCell(new Phrase(fieldValue,chineseFont));
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                cell.setFixedHeight(30f);//行高
                table.addCell(cell);
            }
        }
    }

8.增加多行內(nèi)容

  /**
     * 增肌多列內(nèi)容
     * @param table
     * @param rowsMapList 多行的list
     * @param language 語言
     * @param fontPath 字體路徑
     */
    public static void addTableRoles(PdfPTable table,List<LinkedHashMap<String,String>> rowsMapList,String language,String fontPath){
        Font chineseFont = getChineseFont(fontPath);
        Font font = new Font(Font.FontFamily.COURIER, 10);
        if(!CollectionUtils.isEmpty(rowsMapList)){
            for(LinkedHashMap<String,String> rowMap:rowsMapList){
                for(String key:rowMap.keySet()){
                    String fieldValue=rowMap.get(key);
                    PdfPCell cell=null;
                    if("zh-CN".equals(language)){
                        cell = new PdfPCell(new Phrase(fieldValue,chineseFont));
                    }else{
                        cell = new PdfPCell(new Phrase(fieldValue,font));
                    }
                    cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                    cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                    cell.setFixedHeight(30f);//行高
                    table.addCell(cell);
                }
            }

        }
    }

9.合并列

   /**
     * 增加一個(gè)合并列
     * @param table
     * @param colsNum 合并幾列
     * @param startKey 從valueMap 那個(gè)key開始
     * @param valueMap filed1:fieldValue1,field2:fieldValue2
     */
    public static void addColSpanRow(PdfPTable table,int colsNum,String startKey,LinkedHashMap<String,String> valueMap){
        if(!valueMap.isEmpty()){
            for(String key:valueMap.keySet()){
                String fieldValue = valueMap.get(key);
                PdfPCell cell = new PdfPCell(new Phrase(fieldValue,chineseFont));
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                if(startKey.equals(key)){
                    cell.setColspan(colsNum); // 設(shè)置跨列
                }
                cell.setFixedHeight(30f);//行高
                table.addCell(cell);
            }
        }
    }

10.增加超鏈接

   /**
     * 增加一個(gè)超鏈接的單元格
     * @param table
     * @param fieldFixedValue 固定單元格內(nèi)容
     * @param linkedList List<Map<String,String>> 格式,包含linkName、url,分別表示顯示的名稱和超鏈接地址
     */
    public static void addLinkCell(PdfPTable table, String fieldFixedValue,int colspanNum,String fontPath, List<Map<String,String>> linkedList){
        BaseFont baseFont = getBaseFont(fontPath);
        Font chineseFont = getChineseFont(fontPath);
        Font linkFont = new Font(baseFont, 10, Font.UNDERLINE, new BaseColor(0,0,255));
        Paragraph mixPara = new Paragraph();
        mixPara.add(new Chunk(fieldFixedValue, chineseFont));  // 普通中文
        //設(shè)置超鏈接
        int size=0;
        if(!CollectionUtils.isEmpty(linkedList)){
            for(Map<String,String> linkedMap:linkedList){
                String linkName = linkedMap.get("linkName");
                String url=linkedMap.get("url");
                if(!StringUtils.isEmpty(linkName)&&!StringUtils.isEmpty(url)){
                    if(size!=0&&size!=linkedList.size()){//增加分割線
                        Chunk sep = new Chunk(" | ", new Font(Font.FontFamily.HELVETICA, 12));
                        mixPara.add(sep);
                    }
                    Chunk linkChunk = new Chunk(linkName, linkFont);
                    linkChunk.setAction(new PdfAction(url));
                    mixPara.add(linkChunk);
                }
                size++;
            }
        }
        PdfPCell cell = new PdfPCell(mixPara);
        cell.setBorder(Rectangle.NO_BORDER);//無邊框
        cell.setColspan(colspanNum);//合并單元格
        cell.setPaddingTop(10f);//上間距
        cell.setFixedHeight(30f);//行高
        table.addCell(cell);
    }

根據(jù)以上方法可以實(shí)現(xiàn)一個(gè)簡單的form表單導(dǎo)出成pdf,并實(shí)現(xiàn)超鏈接可跳轉(zhuǎn)或下載、文檔密碼設(shè)置、可編輯修改復(fù)制權(quán)限。具體代碼以具體業(yè)務(wù)為主,僅供參考。

到此這篇關(guān)于Java使用itextpdf實(shí)現(xiàn)表單導(dǎo)出為pdf的文章就介紹到這了,更多相關(guān)Java itextpdf導(dǎo)出表單為pdf內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • java實(shí)現(xiàn)的DES加密算法詳解

    java實(shí)現(xiàn)的DES加密算法詳解

    這篇文章主要介紹了java實(shí)現(xiàn)的DES加密算法,結(jié)合實(shí)例形式詳細(xì)分析了java實(shí)現(xiàn)DES加密操作的原理、實(shí)現(xiàn)技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下
    2017-06-06
  • Java?CompletableFuture實(shí)現(xiàn)原理分析詳解

    Java?CompletableFuture實(shí)現(xiàn)原理分析詳解

    CompletableFuture是Java8并發(fā)新特性,本文我們主要來聊一聊CompletableFuture的回調(diào)功能以及異步工作原理是如何實(shí)現(xiàn)的,需要的可以了解一下
    2022-09-09
  • Java中前臺往后臺傳遞多個(gè)id參數(shù)的實(shí)例

    Java中前臺往后臺傳遞多個(gè)id參數(shù)的實(shí)例

    下面小編就為大家?guī)硪黄狫ava中前臺往后臺傳遞多個(gè)id參數(shù)的實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-07-07
  • RabbitMQ?延遲隊(duì)列實(shí)現(xiàn)訂單支付結(jié)果異步階梯性通知(實(shí)例代碼)

    RabbitMQ?延遲隊(duì)列實(shí)現(xiàn)訂單支付結(jié)果異步階梯性通知(實(shí)例代碼)

    這篇文章主要介紹了RabbitMQ?延遲隊(duì)列實(shí)現(xiàn)訂單支付結(jié)果異步階梯性通知,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-02-02
  • SpringBoot配置文件格式詳細(xì)介紹

    SpringBoot配置文件格式詳細(xì)介紹

    這篇文章主要為大家詳細(xì)介紹了SpringBoot配置文件格式,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)SpringBoot有一定幫助,需要的可以參考一下
    2022-09-09
  • Java入門教程--帶包的類如何編譯與運(yùn)行

    Java入門教程--帶包的類如何編譯與運(yùn)行

    我們一般都是通過IDE(如Eclipse、Intellij Idea,STS等)來開發(fā),調(diào)試java項(xiàng)目。在不借助IDE的情況下,如何編譯、運(yùn)行Java程序。打包編譯時(shí),會自動創(chuàng)建包目錄,不需要自己新建包名文件夾。
    2022-12-12
  • Java多線程中線程池常見7個(gè)參數(shù)的詳解以及執(zhí)行流程

    Java多線程中線程池常見7個(gè)參數(shù)的詳解以及執(zhí)行流程

    本文主要介紹了Java多線程中線程池常見7個(gè)參數(shù)的詳解以及執(zhí)行流程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • SpringBoot之控制器的返回值處理方式

    SpringBoot之控制器的返回值處理方式

    這篇文章主要介紹了SpringBoot之控制器的返回值處理方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • Java后臺批量生產(chǎn)echarts圖表并保存圖片

    Java后臺批量生產(chǎn)echarts圖表并保存圖片

    這篇文章主要介紹了Java后臺批量生產(chǎn)echarts圖表并保存圖片,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-05-05
  • java根據(jù)富文本生成pdf文件過程解析

    java根據(jù)富文本生成pdf文件過程解析

    這篇文章主要介紹了java根據(jù)富文本生成pdf文件過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-10-10

最新評論

石家庄市| 重庆市| 临武县| 休宁县| 崇仁县| 兖州市| 定州市| 友谊县| 西乌珠穆沁旗| 泾阳县| 吴旗县| 基隆市| 万源市| 闽清县| 衢州市| 德庆县| 青铜峡市| 胶南市| 潼南县| 海伦市| 建阳市| 高雄市| 达日县| 礼泉县| 通城县| 固始县| 从化市| 自贡市| 林口县| 安庆市| 龙里县| 天柱县| 汾阳市| 伊金霍洛旗| 义乌市| 开鲁县| 昂仁县| 依安县| 泸西县| 霍林郭勒市| 丰原市|