PDF模板寫入數據

導入Maven pom依賴

        <!-- pdf樣式 -->
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itext-asian</artifactId>
            <version>5.2.0</version>
        </dependency>
        <!--pdf相關操作-->
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>5.5.3</version>
        </dependency>

 

編輯pdf,寫入編輯表單域

 

package com.ruoyi;

import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.*;
import com.ruoyi.common.config.Global;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: ljt
 * @Version 1.0
 * @Date: 2020/06/05/16:18
 * @Description:
 */
public class CreatePDFUtil {


    public static void main(String[] args) {
        //pdf模板路徑
        String templatePdf = "d:\\社區證明_20200605163523.pdf";
        //生成新寫入數據pdf路徑
        String newPdfPath = "d:\\new社區證明_20200605163523.pdf";
        //自定義字體路徑
        String fontPath = "";

        Map<String,String> map = convertMap();
        getBillByPdf(templatePdf,newPdfPath,fontPath,map);

    }



    public static Map<String, String> convertMap()   {
        Map<String, String> map = new HashMap<>();
        map.put("year", "2020");
        map.put("month", "10");
        map.put("day", "24");
        map.put("userName", "李某人");
        map.put("singleCommunity","1024");
        map.put("sex","未知");
        map.put("nation","1024");
        map.put("birthday","1024");
        map.put("number","1024");
        map.put("address","1024地區");
        map.put("singleCommunity2","1024");

        map.put("png", "d://201411281904190406.png");

        return map;
    }


    /**
     *
     * @param templatePdf  模板路徑
     * @param newPdfPath   新pdf路徑
     * @param fontPath   字體路徑
     * @param map  參數
     */
    public static void getBillByPdf(String  templatePdf ,String newPdfPath, String fontPath,Map<String,String> map) {
        PdfReader reader = null;
        FileOutputStream out = null;
        ByteArrayOutputStream bos = null;
        PdfStamper stamper = null;
        File file = new File(Global.getUploadPath());
        if (!file.exists() && !file.isDirectory()) {
            file.mkdir();
        }
        try {
            out = new FileOutputStream(newPdfPath);
            reader = new PdfReader(templatePdf);
            bos = new ByteArrayOutputStream();
            stamper = new PdfStamper(reader, bos);
            AcroFields form = stamper.getAcroFields();
            //注意:加入字體輸出的模板太大,可以用下面這個不帶字體的
            BaseFont bfChinese = null;
            if("".equals(fontPath)){
                 bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
            }else{
                //導入指定字體,
                 bfChinese = BaseFont.createFont(fontPath, BaseFont.IDENTITY_H,
                        BaseFont.NOT_EMBEDDED);
            }

            Map<String, AcroFields.Item> fieldMap = form.getFields();
            for (Map.Entry<String, AcroFields.Item> entry : fieldMap.entrySet()) {
                String name = entry.getKey();
                if("image".equals(name)){
                    int pageNo = form.getFieldPositions(name).get(0).page;
                    Rectangle signRect = form.getFieldPositions(name).get(0).position;
                    float x = signRect.getLeft();
                    float y = signRect.getBottom();
                    // 讀圖片
                    Image image = Image.getInstance(map.get(name));
                    // 獲取操作的頁面
                    PdfContentByte under = stamper.getOverContent(pageNo);
                    // 根據域的大小縮放圖片
                    image.scaleToFit(signRect.getWidth(), signRect.getHeight());
                    // 添加圖片
                    image.setAbsolutePosition(x, y);
                    under.addImage(image);
                }else{
                    form.setField(name, map.get(name));
                }
            }
            form.addSubstitutionFont(bfChinese);
            form.setFieldProperty("content", "textfont", bfChinese, null);
            //true代表生成的PDF文件不可編輯
            stamper.setFormFlattening(true);
            stamper.close();
            Document doc = new Document();
            PdfCopy copy = new PdfCopy(doc, out);
            doc.open();
            PdfImportedPage importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), 1);
            copy.addPage(importPage);
            doc.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }




}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章