欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2025年06月26日 10:33:22   作者:ciku  
這篇文章主要為大家詳細(xì)介紹了Java如何使用itextpdf實現(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)建表格

  /**
     * 獲取一個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)建一個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(), // 所有者密碼,修改時需要該密碼
                    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.合并列

   /**
     * 增加一個合并列
     * @param table
     * @param colsNum 合并幾列
     * @param startKey 從valueMap 那個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.增加超鏈接

   /**
     * 增加一個超鏈接的單元格
     * @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ù)以上方法可以實現(xiàn)一個簡單的form表單導(dǎo)出成pdf,并實現(xiàn)超鏈接可跳轉(zhuǎn)或下載、文檔密碼設(shè)置、可編輯修改復(fù)制權(quán)限。具體代碼以具體業(yè)務(wù)為主,僅供參考。

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

相關(guān)文章

  • 淺析SpringBoot中的過濾器和攔截器

    淺析SpringBoot中的過濾器和攔截器

    過濾器和攔截器都是為了在請求到達(dá)目標(biāo)處理器(Servlet或Controller)之前或者之后插入自定義的處理邏輯,下面就跟隨小編來看看它們二者的區(qū)別和具體使用吧
    2024-03-03
  • spring cloud gateway如何獲取請求的真實地址

    spring cloud gateway如何獲取請求的真實地址

    這篇文章主要介紹了spring cloud gateway如何獲取請求的真實地址問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • 詳解Junit 測試之 Spring Test

    詳解Junit 測試之 Spring Test

    本篇文章主要介紹了Junit 測試之 Spring Test,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-10-10
  • springboot集成redis并使用redis生成全局唯一索引ID

    springboot集成redis并使用redis生成全局唯一索引ID

    本文主要介紹了springboot集成redis并使用redis生成全局唯一索引ID,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • Java如何接收XML格式參數(shù)并轉(zhuǎn)換為JSON

    Java如何接收XML格式參數(shù)并轉(zhuǎn)換為JSON

    在 Java 應(yīng)用程序中,處理 XML 數(shù)據(jù)并將其轉(zhuǎn)換為 JSON 格式是很常見的任務(wù),這篇文章為大家整理了一下具體的實現(xiàn)方法,希望對大家有所幫助
    2025-03-03
  • java中的轉(zhuǎn)義字符介紹

    java中的轉(zhuǎn)義字符介紹

    普通的轉(zhuǎn)義字符序列和八進(jìn)制轉(zhuǎn)義字符都比Unicode轉(zhuǎn)義字符要好得多,因為與Unicode轉(zhuǎn)義字符不同,轉(zhuǎn)義字符序列是在程序被解析為各種符號之后被處理的
    2013-09-09
  • 基于Ok+Rxjava實現(xiàn)斷點續(xù)傳下載

    基于Ok+Rxjava實現(xiàn)斷點續(xù)傳下載

    這篇文章主要為大家詳細(xì)介紹了基于Ok+Rxjava實現(xiàn)斷點續(xù)傳下載,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-06-06
  • mybatis映射文件操作存儲過程的實現(xiàn)

    mybatis映射文件操作存儲過程的實現(xiàn)

    本文主要介紹了mybatis映射文件操作存儲過程的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03
  • IntelliJ IDEA窗口組件具體操作方法

    IntelliJ IDEA窗口組件具體操作方法

    IDEA剛接觸不久,各種常用工具窗口找不到,不小心關(guān)掉不知道從哪里打開,今天小編給大家分享這個問題的解決方法,感興趣的朋友一起看看吧
    2021-09-09
  • 將Java的List結(jié)構(gòu)通過GSON庫轉(zhuǎn)換為JSON的方法示例

    將Java的List結(jié)構(gòu)通過GSON庫轉(zhuǎn)換為JSON的方法示例

    GONS是Google在GitHub上開源的Java類庫,提供各種Java對象和JSON格式對象之間的轉(zhuǎn)換功能,將Java的List結(jié)構(gòu)通過GSON庫轉(zhuǎn)換為JSON的方法示例
    2016-06-06

最新評論