根据字段传入生成Pdf文件 改变字体样式

根据字段传入生成Pdf文件 改变字体样式,第1张

根据字段传入生成Pdf文件 改变字体样式
package com.glxh.itcc.common.utils;

import com.glxh.itcc.admin.Dao.AccessoryDao;
import com.glxh.itcc.admin.Dao.GroupsVipDao;
import com.glxh.itcc.admin.Dao.GroupsVipTempDao;
import com.glxh.itcc.admin.Dao.UserDao;
import com.glxh.itcc.admin.Service.Impl.GroupsVipTempServiceImpl;
import com.glxh.itcc.admin.Service.PdfService;
import com.glxh.itcc.admin.Service.SendMessageService;
import com.glxh.itcc.common.constant.NumberConstant;
import com.glxh.itcc.common.pojo.GroupsVipTemp;
import com.glxh.itcc.common.pojo.Pdfmessage;
import com.itextpdf.text.document;
import com.itextpdf.text.documentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.pdf.*;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@SpringBootTest
public class CjxhPdf {
    
    @Value("${path.accessoryPath}")
    String accessoryPath;

    @Autowired
    RedisUtils redisUtils;

    @Autowired
    private PdfService pdfService;

    @Autowired
    private AccessoryDao accessoryDao;

    @Autowired
    private SendMessageService sendMessageService;

    @Autowired
    private GroupsVipTempDao groupsVipTempDao;

    @Autowired
    private UserDao userDao;

    @Autowired
    private GroupsVipDao groupsVipDao;


    Logger log = LoggerFactory.getLogger(GroupsVipTempServiceImpl.class);
    @Test
    void cjxh() {
        for (int k = 41; k <= 42; k++) {
            String j = String.valueOf(k);
            GroupsVipTemp vt = groupsVipTempDao.getGroupsVipTempINfoById(j);
            Object o = redisUtils.get(NumberConstant.cjxh);
            if (o == null) {
                o = "00000";
            }


            Map map2 = IDnumberUtlis.GetGroupsVipNumber(o.toString());
            String number = map2.get("number");
            String newNumbers = map2.get("newEquipmentNo");

            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            Date startTime1 = vt.getStartTime();
            String startTime = df.format(vt.getStartTime());
            String numbers = newNumbers;
            String companyname = vt.getCompanyname();
            String learnPost = vt.getLearnPost();
            String year = startTime.substring(0, 4);
            String month = startTime.substring(5, 7);
            String day = startTime.substring(8, 10);
            Long uid = vt.getId();
            Integer vipId = vt.getId().intValue();

            String templatePath = "";
            String newName = "";
            String newPDFPath = "";
            if (learnPost.equals("副会长单位")) {

                // 模板路径
                templatePath = "pdf/副会长单位.pdf";
                // 生成的新文件路径
                newName = "副会长单位" + System.currentTimeMillis() + ".pdf";
                newPDFPath = "C:/glxh/vipFile/" + newName;


            } else if (learnPost.equals("会长单位")) {
                // 模板路径
                templatePath = "pdf/会长单位.pdf";
                // 生成的新文件路径
                newName = "会长单位" + System.currentTimeMillis() + ".pdf";
                newPDFPath = "C:/glxh/vipFile/" + newName;

            } else if (learnPost.equals("监事长单位")) {
                // 模板路径
                templatePath = "pdf/监事长单位.pdf";
                // 生成的新文件路径
                newName = "监事长单位" + System.currentTimeMillis() + ".pdf";
                newPDFPath = "C:/glxh/vipFile/" + newName;
            } else if (learnPost.equals("普通会员单位")) {
                // 模板路径
                templatePath = "pdf/会员单位.pdf";
                // 生成的新文件路径
                newName = "会员单位" + System.currentTimeMillis() + ".pdf";
                newPDFPath = "C:/glxh/vipFile/" + newName;
            } else if (learnPost.equals("常务理事单位")) {
                // 模板路径
                templatePath = "pdf/常务理事单位.pdf";
                // 生成的新文件路径
                newName = "常务理事单位" + System.currentTimeMillis() + ".pdf";
                newPDFPath = "C:/glxh/vipFile/" + newName;
            } else if (learnPost.equals("理事单位")) {
                // 模板路径
                templatePath = "pdf/理事单位.pdf";
                // 生成的新文件路径
                newName = "理事单位" + System.currentTimeMillis() + ".pdf";
                newPDFPath = "C:/glxh/vipFile/" + newName;
            }
            System.out.println(newPDFPath);
            //存pdf文件基本信息到数据库
            Pdfmessage pdfmessage = new Pdfmessage();
            pdfmessage.setNewName(newName);
            pdfmessage.setPdf_uid(uid);
            pdfmessage.setPdf_url(newPDFPath);
            pdfmessage.setPdfName(companyname);
            pdfmessage.setStartTime(startTime1);
            pdfmessage.setVipId(vipId);
            pdfService.insertPdf(pdfmessage);

            //pdf生成
            PdfReader reader;
            FileOutputStream out;
            ByteArrayOutputStream bos;
            PdfStamper stamper;

            try {
                baseFont bf = baseFont.createFont("static/font/simkai.ttf", baseFont.IDENTITY_H, baseFont.NOT_EMBEDDED);
                Font font = new Font(bf, 40, Font.NORMAL);


                out = new FileOutputStream(newPDFPath);// 输出流
                reader = new PdfReader(templatePath);// 读取pdf模板
                bos = new ByteArrayOutputStream();
                stamper = new PdfStamper(reader, bos);
                AcroFields form = stamper.getAcroFields();

//                int i = 0;
                java.util.Iterator it = form.getFields().keySet().iterator();
                while (it.hasNext()) {
                    String name = it.next().toString();
                    System.out.println(name);
                }
                form.setFieldProperty("companyName", "textsize", 30f, null);
                form.setFieldProperty("companyName", "textfont", bf, null);//设置字体
                form.setField("companyName", companyname);
                form.setFieldProperty("companyName", "textfont", bf, null);
                form.setField("companyName", companyname);
                form.setFieldProperty("number", "textsize", 20f, null);
                form.setFieldProperty("number", "textfont", bf, null);
                form.setField("number", numbers);
                form.setFieldProperty("year", "textfont", bf, null);
                form.setField("year", year);
                form.setFieldProperty("month", "textfont", bf, null);
                form.setField("month", month);
                form.setFieldProperty("day", "textfont", bf, null);
                form.setField("day", day);
                form.setFieldProperty("dateTime", "textsize", 30f, null);
                form.setFieldProperty("dateTime", "textfont", bf, null);//设置字体
                form.setField("dateTime", startTime);


                stamper.setFormFlattening(true);// 如果为false那么生成的PDF文件还能编辑,一定要设为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 (IOException e) {
                System.out.println(1);
            } catch (documentException e) {
                System.out.println(2);
            }
            vt.setNumbers(newNumbers);
            Integer integer = groupsVipDao.insertGroupsVip(vt);
            if (integer > 0) {
                redisUtils.set(NumberConstant.cjxh, number, -1);
            }
        }
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5678217.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存