国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

itext5創(chuàng)建pdf表格及遇到的一些問(wèn)題

這篇具有很好參考價(jià)值的文章主要介紹了itext5創(chuàng)建pdf表格及遇到的一些問(wèn)題。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

0. 核心依賴(lài):
<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.10</version>
</dependency>
<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itext-asian</artifactId>
    <version>5.2.0</version>
</dependency>
1. 設(shè)置頁(yè)眉圖片及下劃線(xiàn)

通過(guò)PdfPageEventHelper事件可以動(dòng)態(tài)的創(chuàng)建頁(yè)眉,數(shù)據(jù)構(gòu)建出多少頁(yè)pdf就有多少頁(yè)頁(yè)眉

package com.example.pdf;

import com.example.pdf.vo.RenovationDocNameEnum;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;

/**
 * 內(nèi)部類(lèi) 添加頁(yè)眉、頁(yè)腳
 */
public class PdfEvent extends PdfPageEventHelper {

    // 一頁(yè)加載完成觸發(fā),寫(xiě)入頁(yè)眉和頁(yè)腳
    @Override
    public void onEndPage(PdfWriter writer, Document document) {
        PdfPTable head = new PdfPTable(1);
        PdfObject businessType = document.getAccessibleAttribute(new PdfName("businessType"));
        PdfObject flag = document.getAccessibleAttribute(new PdfName("flag"));
        try {
            if (RenovationDocNameEnum.CB.getName().equals(businessType.toString())
                    || (RenovationDocNameEnum.SG.getName().equals(businessType.toString()) && flag != null && "1".equals(flag.toString()))) {
                setCbPageHead(head, writer, document);
            } else if (RenovationDocNameEnum.SG.getName().equals(businessType.toString()) || RenovationDocNameEnum.JC.getName().equals(businessType.toString())) {
                setSinglePageHead(head, writer, document);
            }
        } catch (Exception e) {
            throw new ExceptionConverter(e);
        }
    }

    /**
     * Chunk的offsetX控制頁(yè)眉圖片水平位置,offsetY會(huì)影響到圖片大??; 圖片上下位置可通過(guò)showTextAligned的y來(lái)控制
     * 頁(yè)眉下劃線(xiàn)通過(guò)setTotalWidth控制先長(zhǎng)度,writeSelectedRows的xPos控制線(xiàn)的起始水平位置
     *
     * @param head
     * @param writer
     * @param document
     * @throws DocumentException
     */
    private void setSinglePageHead(PdfPTable head, PdfWriter writer, Document document) throws DocumentException {
        Font font = PdfUtil.getFont(null);
        // 通過(guò)表格構(gòu)建頁(yè)眉下劃線(xiàn)
        head.setTotalWidth(PageSize.A4.getWidth() - 105);
        head.setWidths(new int[]{24});
        head.setLockedWidth(true);
        head.getDefaultCell().setFixedHeight(-10);
        head.getDefaultCell().setBorder(Rectangle.BOTTOM);
        head.getDefaultCell().setBorderWidth(0.5f);
        head.addCell(new Paragraph(" ", font));
        // 將頁(yè)眉寫(xiě)到document中,位置可以指定,指定到下面就是頁(yè)腳
        head.writeSelectedRows(0, -1, 55, PageSize.A4.getHeight() - 20, writer.getDirectContent());
        PdfContentByte directContent = writer.getDirectContent();
        // 最重要的是這個(gè),如果頁(yè)眉需要設(shè)置圖片的話(huà),需要在Phrase對(duì)象中添加一個(gè)Chunk對(duì)象,在Chunk對(duì)象中添加圖片信息即可
        Phrase phrase = new Phrase("", font);
        Image img = PdfUtil.getImg();
        if (img != null) {
            phrase.add(new Chunk(img, 30, -150));
        }
        // 寫(xiě)入頁(yè)眉
        ColumnText.showTextAligned(directContent, Element.ALIGN_RIGHT, phrase, document.right(), PageSize.A4.getHeight() + 48, 0);
    }
  • 通過(guò)PdfPTable來(lái)構(gòu)建下劃線(xiàn)
  • 通過(guò)new Chunk來(lái)顯示圖片,并通過(guò)ColumnText.showTextAligned來(lái)展示在頁(yè)眉
  • 詳細(xì)見(jiàn)setSinglePageHead方法的注釋?zhuān)秸褪且活D調(diào),知道合適位置
2. document參數(shù)傳遞:
  • 像下述這樣定義一個(gè)字符串參數(shù)于document,就可以像上述PdfEvent中獲取做業(yè)務(wù)判斷
  • 使用的時(shí)候注意toString()
PdfName businessType = new PdfName("businessType");
document.setAccessibleAttribute(businessType, new PdfString(type));
3. 生成的pdf文件轉(zhuǎn)base64編碼:
  • 主要以下兩個(gè)方法,FileUtil是hutool的工具類(lèi),Base64是java.util的工具類(lèi)

	PdfUtil.getBase64(FileUtil.readBytes(file));
	
    public static String getBase64(byte[] buffer) {
        return Base64.getEncoder().encodeToString(buffer);
    }
4. 平方2上標(biāo)顯示問(wèn)題:

本人生成pdf時(shí)用的是simkai.ttf字體,輸出的pdf沒(méi)有顯示2上標(biāo)文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-561913.html

  • 要么可以換字體,百度即可
  • 要么像如下這樣,通過(guò)setTextRise來(lái)控制文字位置,來(lái)大概展示成2效果
  • 方法正值為上標(biāo),負(fù)值可為下標(biāo),注意上標(biāo)的2的文字specialFont字體要定義小點(diǎn),本人定義5
 public static PdfPCell getPDFCellSpecial(String name, Font font, Integer alignment) {
        PdfPCell cell = new PdfPCell();
        if (name == null) {
            name = " ";
        }
        Paragraph p = new Paragraph(name, font);
        Font specialFont = PdfUtil.getSpecialFont(null);
        Chunk chunk = new Chunk("2", specialFont);
        chunk.setTextRise(5f);
        p.add(chunk);
        p.add(new Chunk(")", font));

        if (alignment == null) {
            p.setAlignment(Element.ALIGN_CENTER);
        } else {
            p.setAlignment(alignment);
        }
        cell.setUseAscender(true);
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.addElement(p);
        return cell;
    }
5. 壓縮包的文件流InputStream輸出文件:
  • 業(yè)務(wù)上從別的接口獲取到一個(gè)zip包的文件流,需要將zip包的文件輸出到目錄,如下代碼:
  • 涉及IO流要注意關(guān)流

    private static List<String> extractAllByInputStream(InputStream inputStream, String pdfPath) {
        ArrayList<String> list = Lists.newArrayList();
        try {
            dealZipInputStream(inputStream, pdfPath, list);
        } catch (IOException e) {
            LOGGER.info("下載pdf到本地異常,異常信息:{}", e);
        }

        return list;
    }

    private static void dealZipInputStream(InputStream inputStream, String pdfPath, ArrayList<String> list) throws IOException {
        byte[] buffer = new byte[1024];
        ZipEntry zipEntry;
        try (ZipInputStream zipInputStream = new ZipInputStream(inputStream)) {
            while ((zipEntry = zipInputStream.getNextEntry()) != null) {
                String entryName = zipEntry.getName();
                if (!zipEntry.isDirectory()) {
                    String fileName;
                    if (entryName.contains(RenovationDocNameEnum.CB.getName())) {
                        fileName = pdfPath + RenovationDocNameEnum.CB.getName() + ".pdf";
                    } else if (entryName.contains(RenovationDocNameEnum.JC.getName())) {
                        fileName = pdfPath + RenovationDocNameEnum.JC.getName() + ".pdf";
                    } else if (entryName.contains(RenovationDocNameEnum.SG.getName())) {
                        fileName = pdfPath + RenovationDocNameEnum.SG.getName() + ".pdf";
                    } else {
                        fileName = pdfPath + UUID.randomUUID() + ".pdf";
                    }
                    list.add(fileName);
                    File subFile = new File(fileName);
                    subFile.createNewFile();
                    readSubFile(subFile, zipInputStream, buffer);

                }
                zipInputStream.closeEntry();
            }
        } finally {
            IOUtils.closeQuietly(inputStream);
        }
    }

    private static void readSubFile(File subFile, ZipInputStream zipInputStream, byte[] buffer) {
        int len;
        try (FileOutputStream fileOut = new FileOutputStream(subFile)) {
            while ((len = zipInputStream.read(buffer)) > 0) {
                fileOut.write(buffer, 0, len);
            }
        } catch (IOException e) {
            throw new ExceptionConverter(e);
        }
    }

6. itext5進(jìn)行pdf合并:
  • 本人文件合并后要?jiǎng)h除之前的文件,這時(shí)需要注意new PdfReader(inputStream)時(shí)使用inputStream
  • 不然可能出現(xiàn):別的程序正在使用而無(wú)法刪除情況

    /* 合并pdf文件
     * @param files 要合并文件數(shù)組(絕對(duì)路勁{ "D:\\a.pdf", "D:\\b.pdf" })
     * @param savePath 合并后新產(chǎn)生的文件絕對(duì)路徑如D:\\temp.pdf
     */
    public static Integer mergePdfFiles(String[] files, String savePath) {
        Document document = null;
        PdfCopy copy = null;
        try (FileOutputStream outputStream = new FileOutputStream(savePath)) {
            // 創(chuàng)建一個(gè)與a.pdf相同紙張大小的document
            document = new Document(new PdfReader(files[0]).getPageSize(1));
            copy = new PdfCopy(document, outputStream);
            document.open();
            for (int i = 0; i < files.length; i++) {
                // 一個(gè)一個(gè)的遍歷現(xiàn)有的PDF
                dealFile(files[i], copy, document);
            }
            copy.close();
            document.close();
            return 1;
        } catch (IOException | DocumentException e) {
            LOGGER.info("合并pdf失敗,異常信息:{}", e);
        } finally {
            if (document != null) {
                document.close();
            }
            if (copy != null) {
                copy.close();
            }
        }
        return 0;
    }

    private static void dealFile(String file, PdfCopy copy, Document document) {
        PdfReader reader = null;
        try (FileInputStream inputStream = new FileInputStream(file)) {
            reader = new PdfReader(inputStream);
            int n = reader.getNumberOfPages();// PDF文件總共頁(yè)數(shù)
            for (int j = 1; j <= n; j++) {
                document.newPage();
                PdfImportedPage page = copy.getImportedPage(reader, j);
                copy.addPage(page);
            }
            reader.close();
        } catch (IOException | BadPdfFormatException e) {
            throw new ExceptionConverter(e);
        } finally {
            if (reader != null) {
                reader.close();
            }
        }

    }

到了這里,關(guān)于itext5創(chuàng)建pdf表格及遇到的一些問(wèn)題的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶(hù)投稿,該文觀(guān)點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 在 VScode 終端上創(chuàng)建 nuxtjs 項(xiàng)目遇到的問(wèn)題以及使用 GitHub 遇到的問(wèn)題和一些個(gè)人筆記

    在 VScode 終端上創(chuàng)建 nuxtjs 項(xiàng)目遇到的問(wèn)題以及使用 GitHub 遇到的問(wèn)題和一些個(gè)人筆記

    這篇文章是關(guān)于在vscode終端中創(chuàng)建 nuxtjs項(xiàng)目 的一些步驟,同時(shí)還包括了使用 Git、GitHub 的一些操作,以此文章作為筆記,僅供參考。(前提:已經(jīng)安裝nodejs、git) 關(guān)于nuxtjs、ssr、服務(wù)端渲染、nuxtjs項(xiàng)目結(jié)構(gòu)等等相關(guān)知識(shí)點(diǎn)這篇文章就不多多介紹了,在后續(xù)的文章或筆記中也

    2024年02月09日
    瀏覽(87)
  • 【Java】使用iText生成PDF文件

    【Java】使用iText生成PDF文件

    iText介紹 iText是著名的開(kāi)放源碼的站點(diǎn)sourceforge一個(gè)項(xiàng)目,是用于生成PDF文檔的一個(gè)java類(lèi)庫(kù)。通過(guò)iText不僅可以生成PDF或rtf的文檔,而且可以將XML、Html文件轉(zhuǎn)化為PDF文件。 項(xiàng)目要使用iText,必須引入jar包。才能使用,maven依賴(lài)如下: 輸出中文,還要引入下面itext-asian.jar包: ?

    2024年02月10日
    瀏覽(19)
  • Java 使用 itext 向PDF插入數(shù)據(jù)和圖片

    Java 使用 itext 向PDF插入數(shù)據(jù)和圖片

    一、下載Adobe Acrobat DC 二、制作模板 1、準(zhǔn)備一個(gè)word模板,并轉(zhuǎn)換成PDF格式 2、使用Adobe Acrobat DC打開(kāi)PDF文檔,并在右側(cè)搜索框搜索表單,點(diǎn)擊準(zhǔn)備表單 3、點(diǎn)擊開(kāi)始,制作PDF表單 4、掃描完成后如下圖,藍(lán)白色框就是可編輯表單 5、點(diǎn)擊表單編輯表單名稱(chēng)以及插入時(shí)的字體大小

    2024年02月09日
    瀏覽(19)
  • Java中Itext生成Pdf,并給PdfCell添加圖片
  • java中使用Jsoup和Itext實(shí)現(xiàn)將html轉(zhuǎn)換為PDF

    java中使用Jsoup和Itext實(shí)現(xiàn)將html轉(zhuǎn)換為PDF

    1.在build.gradle中安裝所需依賴(lài): 2.創(chuàng)建工具類(lèi),實(shí)現(xiàn)轉(zhuǎn)換方法 3.base64過(guò)濾類(lèi): 4.字體類(lèi)代碼,window用戶(hù)可在C:windowsfont中尋找自己所需字體即可。我這里用的為黑體: simhei.ttf 效果如下: html頁(yè)面預(yù)覽: pdf頁(yè)面預(yù)覽: ? ?

    2024年02月14日
    瀏覽(25)
  • 【Java】OpenPDF、iText、PDFBox 是三種常用的 PDF 處理庫(kù)

    OpenPDF、iText、PDFBox 是三種常用的 PDF 處理庫(kù),它們各自具有獨(dú)特的優(yōu)勢(shì)和特點(diǎn),同時(shí)也存在一些局限性和差異。本文將對(duì)這四種庫(kù)進(jìn)行詳細(xì)的比較,并通過(guò)代碼示例來(lái)展示它們的使用。 1、OpenPDF OpenPDF 是一個(gè)用于創(chuàng)建和編輯 PDF 文檔的 Java 庫(kù),它基于 iText 庫(kù)的一個(gè)分支,提供

    2024年02月09日
    瀏覽(34)
  • 關(guān)于Kettle ETL java腳本編寫(xiě)遇到的一些問(wèn)題記錄

    關(guān)于Kettle ETL java腳本編寫(xiě)遇到的一些問(wèn)題記錄

    使用方法**logBasic()**參數(shù)必須是字符串 這部分內(nèi)容會(huì)在ETL的日志窗口顯示 1.獲取上個(gè)節(jié)點(diǎn)傳輸?shù)臄?shù)據(jù) 可以直接在左側(cè)雙擊獲取 2.全局參數(shù)獲取 在啟動(dòng)運(yùn)行的變量設(shè)置參數(shù) 在java代碼中獲取方式 3.獲取當(dāng)前節(jié)點(diǎn)參數(shù) 在當(dāng)前窗口下方有個(gè) 參數(shù) Tab頁(yè),在這里設(shè)置 在java代碼中獲取

    2024年02月12日
    瀏覽(25)
  • Java集成阿里云的實(shí)時(shí)語(yǔ)音識(shí)別遇到的一些問(wèn)題

    集成阿里云的實(shí)時(shí)語(yǔ)音識(shí)別遇到的問(wèn)題: 困擾了一周時(shí)間,主要涉及到流的處理問(wèn)題。 集成是通過(guò)引用maven依賴(lài)加載。 前端錄音通過(guò)流的方式傳到后端,后端再把流上傳到Minio,后端拿到文件地址,調(diào)微服務(wù)(集成語(yǔ)音識(shí)別的是另一個(gè)獨(dú)立的微服務(wù))去Minio獲取輸入流,再上傳到

    2024年02月02日
    瀏覽(23)
  • 使用IText導(dǎo)出復(fù)雜pdf

    使用IText導(dǎo)出復(fù)雜pdf

    ? ? ? ? 需要將發(fā)票導(dǎo)出成pdf,要求每頁(yè)都必須包含發(fā)票信息和表頭行。 ? ? ? ? 使用IText工具實(shí)現(xiàn)PDF導(dǎo)出 ????????IText8文檔:Examples (itextpdf.com) ? ? ?? ? ????????引入Itext依賴(lài),我這里用的是8.0.1版本 ????????測(cè)試一下: ? ? ? ? IText8不支持中文,需要引入外部

    2024年02月12日
    瀏覽(17)
  • 使用gradio創(chuàng)建一個(gè)提取pdf、excel中表格數(shù)據(jù)的demo

    在線(xiàn)體驗(yàn)地址 (https://swanhub.co/patch/TabularScan/demo) 大家可以在上面的鏈接中試用,需求不大也不用自己弄代碼了。 后續(xù)大家如果有一些代碼或功能想快速部署、提供服務(wù),不管是 AI 項(xiàng)目或是 web 項(xiàng)目,也可以直接托管在 swanhub開(kāi)源社區(qū) 上,方便快捷,而且免費(fèi) 最近需要對(duì)pdf、

    2024年02月09日
    瀏覽(38)

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包