分享

itext pdf 设计表单,添加文字及图片内容

dck 2023-9-11 09:45:08 发表于 入门帮助 [显示全部楼层] 回帖奖励 阅读模式 关闭右栏 0 263
  1. package com.app.util;
  2. import com.itextpdf.text.Rectangle;
  3. import com.itextpdf.text.pdf.*;
  4. import javax.imageio.ImageIO;
  5. import java.awt.image.BufferedImage;
  6. import java.io.*;
  7. import java.util.Base64;
  8. import java.util.HashMap;
  9. import java.util.Map;
  10. public class TestPdf {
  11.     public static void main(String[] args) throws Exception {
  12.         testPdf();
  13.     }
  14.     static void testPdf() throws Exception {
  15.         // 模板文件路径
  16.         String inputFileName = "D:\\pdfWork\\1.pdf";
  17.         // 生成的文件路径
  18.         String outputFileName = "D:\\pdfWork\\2.pdf";
  19.         OutputStream os = null;
  20.         PdfStamper ps = null;
  21.         PdfReader reader = null;
  22.         PdfStamper stamper = null;
  23.         try {
  24.             os = new FileOutputStream(new File(outputFileName));
  25.             // 2 读入pdf表单
  26.             reader = new PdfReader(inputFileName);
  27.             // 3 根据表单生成一个新的Pdf
  28.             ps = new PdfStamper(reader, os);
  29.             String password = "123456";
  30.             //pdf权限,值为"PdfWriter.ALLOW_PRINTING"
  31.             int permission = 0;
  32.             ps.setEncryption(password.getBytes(),
  33.                     password.getBytes(),PdfWriter.ALLOW_SCREENREADERS, false);
  34.             // 4 获取pdf表单
  35.             AcroFields form = ps.getAcroFields();
  36.             // 5给表单添加中文字体
  37. //            BaseFont bf = BaseFont.createFont("Font/SIMYOU.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
  38. //            form.addSubstitutionFont(bf);
  39.             // 6查询数据================================================
  40.             Map<String, Object> data = new HashMap<String, Object>();
  41.             data.put("AName", "张三");
  42.             data.put("BName", "李四");
  43.             data.put("ACode", "Z3");
  44.             data.put("BCode", "L4");
  45.             data.put("Sign_Date", "2023-9-9");
  46.             //图片地址
  47.             String imageUrl = "D:\\pdfWork\\1.png";
  48.             String imgBase64 = convertImageToBase64Str(imageUrl);
  49.             //String img2Base64 = convertImageToBase64Str(imgUrl);
  50.             Map<String, String> templateImageMap = new HashMap<>();
  51.             data.put("AImg", imgBase64);
  52.             data.put("BImg", imgBase64);
  53.             ps.setFormFlattening(true);
  54.             System.out.println("===============PDF导出成功=============");
  55.         } catch (Exception e) {
  56.             System.out.println("===============PDF导出失败=============");
  57.             e.printStackTrace();
  58.         } finally {
  59.             try {
  60.                 ps.close();
  61.                 reader.close();
  62.                 os.close();
  63.             } catch (Exception e) {
  64.                 e.printStackTrace();
  65.             }
  66.         }
  67.     }
  68.     /**
  69.      * 图片转Base64字符串
  70.      * @param imageFileName
  71.      * @return
  72.      */
  73.     public static String convertImageToBase64Str(String imageFileName) {
  74.         ByteArrayOutputStream baos = null;
  75.         try {
  76.             //获取图片类型
  77.             String suffix = imageFileName.substring(imageFileName.lastIndexOf(".") + 1);
  78.             //构建文件
  79.             File imageFile = new File(imageFileName);
  80.             //通过ImageIO把文件读取成BufferedImage对象
  81.             BufferedImage bufferedImage = ImageIO.read(imageFile);
  82.             //构建字节数组输出流
  83.             baos = new ByteArrayOutputStream();
  84.             //写入流
  85.             ImageIO.write(bufferedImage, suffix, baos);
  86.             //通过字节数组流获取字节数组
  87.             byte[] bytes = baos.toByteArray();
  88.             //获取JDK8里的编码器Base64.Encoder转为base64字符
  89.             return Base64.getEncoder().encodeToString(bytes);
  90.         } catch (Exception e) {
  91.             e.printStackTrace();
  92.         } finally {
  93.             try {
  94.                 if (baos != null) {
  95.                     baos.close();
  96.                 }
  97.             } catch (IOException e) {
  98.                 e.printStackTrace();
  99.             }
  100.         }
  101.         return null;
  102.     }
  103. }
复制代码

没找到任何评论,期待你打破沉寂

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条