java使用iText爲pdf模板生成pdf

  1. 用word導出pdf文件格式
  2. 在線生成pdf模板文件表單,網址:https://www.pdfescape.com 在線生成pdf表單上傳剛剛保存的pdf文件在這裏插入圖片描述在下拉中選擇自己需要的類型在這裏插入圖片描述選中文本框點擊鼠標右鍵選擇 object Properties在這裏插入圖片描述 

在name框中輸入你要填充的字段名稱,其他請根據自身需要選擇,做完表單模板後點擊

在這裏插入圖片描述下載該模板。

3. 使用 iText 來填充表單,在pom文件中加入

<properties>
        <itext.version>RELEASE</itext.version>
</properties>

<dependency>
       <groupId>com.itextpdf</groupId>
       <artifactId>itext7-core</artifactId>
       <version>${itext.version}</version>
       <type>pom</type>
 </dependency>

來導入itext所需包

4. 生成填充工具類

/**
 * pdf文件生成工具類
 * @author ls
 *
 */
public class PdfFileGenerateUtil {
    
    private static Logger logger = LogManager.getLogger(PdfFileGenerateUtil.class);
    /**
        *   文件生成
     * @param inputPdfTempletaPath pdf模板文件路徑
     * @param outputFilePath 要生成pdf文件存放位置
     * @throws IOException 
     */
    public static boolean generatePdf(String inputPdfTempletaPath,String outputFilePath,Map<String,Object> fillParam) {
        if(Tools.isEmpty(inputPdfTempletaPath) || Tools.isEmpty(outputFilePath) || fillParam == null) {
            return false;
        }
        try {
            File outFile = new File(outputFilePath);
            if(!outFile.exists()) {
                if(!outFile.getParentFile().exists()) {
                    outFile.getParentFile().mkdirs();
                }
                outFile.createNewFile();
            }
            PdfReader pdfReader = new PdfReader(new FileInputStream(inputPdfTempletaPath));
            PdfWriter writer = new PdfWriter(new FileOutputStream(outFile));
            fillParam(pdfReader,writer,fillParam);
            pdfReader.close();
            writer.close();
        } catch (Exception e) {
            logger.error("生成pdf文件失敗,消息如下{}",e);
            return false;
        }
        return true;
    }
    /**
                  *   參數填充
     * @param pdfReader
     * @param pdfWriter
     * @param fillParam
     * @throws IOException
     */
    private static void fillParam(PdfReader pdfReader, PdfWriter pdfWriter,Map<String, Object> fillParam) throws IOException{
        //1、創建pdf文件
        PdfDocument pdf = new PdfDocument(pdfReader, pdfWriter);
        //2、創建中文字體
        PdfFont f2 = PdfFontFactory.createFont("STSongStd-Light", "UniGB-UCS2-H", true);
        //3、獲取pdf模板中的域值信息
        PdfAcroForm form = PdfAcroForm.getAcroForm(pdf, true);
        Map<String, PdfFormField> fieldMap = form.getFormFields();
        Iterator<String> paramIterator = fillParam.keySet().iterator();
        while (paramIterator.hasNext()) {
            String key = paramIterator.next();
            String value = fillParam.get(key).toString();
            PdfFormField formField = fieldMap.get(key);
            if (formField == null) {
                continue;
            }
            //4、填充信息
            formField.setValue(value).setFont(f2);
        }
        //5、設置文本不可編輯
        form.flattenFields();
        pdf.close();
    }
}

這裏有個坑 在網上找的設置中文顯示是直接在PdfDocument處設置,但是我未能成功,只能是在formField.setValue(value).setFont(f2);設置後中文纔可以顯示。

發佈了5 篇原創文章 · 獲贊 6 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章