【玩转PDF】贼稳,产品要做一个三方合同签署,我方了!

//

PdfSmartCopy pdfSmartCopy = new PdfSmartCopy(document,

new FileOutputStream(pdfFile));

document.open();

File file = ResourceUtils.getFile(“classpath:”+templatePath);

pdfReader = new PdfReader(file.getPath());

int n = pdfReader.getNumberOfPages();

log.info(“页数:”+n);

baos = new ByteArrayOutputStream();

pdfStamper = new PdfStamper(pdfReader, baos);

for(int i = 1; i <= n; i++) {

AcroFields acroFields = pdfStamper.getAcroFields();

//key statement 1

acroFields.setGenerateAppearances(true);

//acroFields.setExtraMargin(5, 5);

acroFields.setField(“customerAddress”, “上海市浦东新区田子路520弄1号楼”);

acroFields.setField(“customerCompanyName”, “上海百度有限公司”);

acroFields.setField(“customerName”, “张三”);

acroFields.setField(“customerPhone”, “15216667777”);

acroFields.setField(“customerMail”, “123456789@sian.com”);

acroFields.setField(“vendorAddress”, “上海市浦东新区瑟瑟发抖路182号”);

acroFields.setField(“vendorCompanyName”, “牧小农科技技术有限公司”);

acroFields.setField(“vendorName”, “王五”);

acroFields.setField(“vendorPhone”, “15688886666”);

acroFields.setField(“vendorMail”, “123567@qq.com”);

acroFields.setField(“effectiveStartTime”, “2021年05月25”);

acroFields.setField(“effectiveEndTime”, “2022年05月25”);

//true代表生成的PDF文件不可编辑

pdfStamper.setFormFlattening(true);

pdfStamper.close();

pdfReader = new PdfReader(baos.toByteArray());

pdfSmartCopy.addPage(pdfSmartCopy.getImportedPage(pdfReader, i));

pdfSmartCopy.freeReader(pdfReader);

pdfReader.close();

}

pdfReader.close();

document.close();

} catch(DocumentException dex) {

dex.printStackTrace();

} catch(IOException ex) {

ex.printStackTrace();

}

//创建PDF文件

createPdf();

File file3 = new File(ResourceUtils.getURL(“classpath:”).getPath()+TEMP_PATH);

File file1 = new File(ResourceUtils.getURL(“classpath:”).getPath()+outputFileName);

List files = new ArrayList<>();

files.add(pdfFile);

files.add(file3);

try {

PdfUtil pdfUtil = new PdfUtil();

pdfUtil.mergeFileToPDF(files,file1);

} catch (Exception e) {

e.printStackTrace();

}

//如果你是上传文件服务器上,这里可以上传文件

// String url = fileServer.uploadPdf(File2byte(file1));

//删除总文件

//如果是你本地预览就不要删除了,删了就看不到了

// if(file1.exists()){

// file1.delete();

// }

//删除模板文件

if(pdfFile.exists()){

System.gc();

pdfFile.delete();

}

//删除产品文件

if(file3.exists()){

file3.delete();

}

return “success”;

}

创建PDF附件信息拼接到主文件中:

/**

  • 创建PDF附件信息

*/

public static void createPdf() {

Document doc = null;

try {

doc = new Document();

PdfWriter.getInstance(doc, new FileOutputStream(ResourceUtils.getURL(“classpath:”).getPath()+TEMP_PATH));

doc.open();

BaseFont bfChi = BaseFont.createFont(“STSong-Light”,“UniGB-UCS2-H”, BaseFont.NOT_EMBEDDED);

Font fontChi = new Font(bfChi, 8, Font.NORMAL);

PdfPTable table = new PdfPTable(5);

Font fontTitle = new Font(bfChi, 15, Font.NORMAL);

PdfPCell cell = new PdfPCell(new Paragraph(“货运运输服务协议-附件1 运输费用报价”,fontTitle));

cell.setColspan(5);

table.addCell(cell);

// “序号”

table.addCell(new Paragraph(“序号”,fontChi));

table.addCell(new Paragraph(“品类”,fontChi));

table.addCell(new Paragraph(“名称”,fontChi));

table.addCell(new Paragraph(“计算方式”,fontChi));

table.addCell(new Paragraph(“费率”,fontChi));

table.addCell(new Paragraph(“1”,fontChi));

table.addCell(new Paragraph(“货运”,fontChi));

table.addCell(new Paragraph(“费率1.0”,fontChi));

table.addCell(new Paragraph(“算”,fontChi));

table.addCell(new Paragraph(“0~100万-5.7%,上限:500元,下限:20元”,fontChi));

table.addCell(new Paragraph(“2”,fontChi));

table.addCell(new Paragraph(“货运”,fontChi));

table.addCell(new Paragraph(“费率1.0”,fontChi));

table.addCell(new Paragraph(“倒”,fontChi));

table.addCell(new Paragraph(“100万~200万-5.6%,无上限、下限”,fontChi));

table.addCell(new Paragraph(“3”,fontChi));

table.addCell(new Paragraph(“货运”,fontChi));

table.addCell(new Paragraph(“费率1.0”,fontChi));

table.addCell(new Paragraph(“算”,fontChi));

table.addCell(new Paragraph(“200万~300万-5.5%,无上限、下限”,fontChi));

doc.add(table);

// doc.add(new Paragraph(“Hello World,看看中文支持不…aaaaaaaaaaaaaaaaa”,fontChi));

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (DocumentException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally {

doc.close();

}

}

合同签署:

/**

  • 签署合同

  • @return

  • @throws IOException

  • @throws DocumentException

*/

@GetMapping(“addContent”)

public String addContent() throws IOException, DocumentException {

BaseFont baseFont = BaseFont.createFont(“STSong-Light”, “UniGB-UCS2-H”, BaseFont.NOT_EMBEDDED);

Font font = new Font(baseFont);

//这里可以填写本地地址,也可以是服务器上的文件地址

PdfReader reader = new PdfReader(ResourceUtils.getURL(“classpath:”).getPath()+outputFileName);

PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(ResourceUtils.getURL(“classpath:”).getPath()+endPdf));

//

PdfContentByte over = stamper.getOverContent(1);

ColumnText columnText = new ColumnText(over);

PdfContentByte over1 = stamper.getOverContent(1);

ColumnText columnText1 = new ColumnText(over1);

PdfContentByte over2 = stamper.getOverContent(1);

ColumnText columnText2 = new ColumnText(over2);

PdfContentByte over3 = stamper.getOverContent(1);

ColumnText columnText3 = new ColumnText(over3);

// llx 和 urx 最小的值决定离左边的距离. lly 和 ury 最大的值决定离下边的距离

// llx 左对齐

// lly 上对齐

// urx 宽带

// ury 高度

columnText.setSimpleColumn(29, 117, 221, 16);

Paragraph elements = new Paragraph(0, new Chunk(“上海壹站供应链有限公司”));

columnText1.setSimpleColumn(26, 75, 221, 16);

Paragraph elements1 = new Paragraph(0, new Chunk(“2021年03月03日”));

columnText2.setSimpleColumn(800, 120, 200, 16);

Paragraph elements2 = new Paragraph(0, new Chunk(“壹汇(江苏)供应链管理有限公司芜湖分公司”));

columnText3.setSimpleColumn(800, 74, 181, 16);

Paragraph elements3 = new Paragraph(0, new Chunk(“2022年03月03日”));

// acroFields.setField(“customerSigntime”, “2021年03月03日”);

// acroFields.setField(“vendorSigntime”, “2021年03月09日”);

// 设置字体,如果不设置添加的中文将无法显示

elements.setFont(font);

columnText.addElement(elements);

columnText.go();

elements1.setFont(font);

columnText1.addElement(elements1);

columnText1.go();

elements2.setFont(font);

columnText2.addElement(elements2);

columnText2.go();

elements3.setFont(font);

columnText3.addElement(elements3);

columnText3.go();

stamper.close();

File tempFile = new File(ResourceUtils.getURL(“classpath:”).getPath()+“签署测试.pdf”);

//如果是你要上传到服务器上,填写服务器的地址

// String url = fileServer.uploadPdf(File2byte(tempFile));

// log.info(“url:”+url);

//如果是上传服务器后,要删除信息

//本地不要删除,否则没有文件

// if(tempFile.exists()){

// tempFile.delete();

// }

return “success”;

}

PDF工具类:

import com.itextpdf.text.*;

import com.itextpdf.text.pdf.*;

import lombok.extern.slf4j.Slf4j;

import org.apache.tomcat.util.http.fileupload.IOUtils;

import org.springframework.stereotype.Component;

import java.io.*;

import java.util.List;

/***

  • pdf 相关操作

  • @author mxn

*/

@Slf4j

@Component

public class PdfUtil {

/**

  • 合并PDF文件

  • @param files 文件列表

  • @param output 输出的PDF文件

*/

public void mergeFileToPDF(List files, File output) {

Document document = null;

PdfCopy copy = null;

OutputStream os = null;

try {

os = new FileOutputStream(output);

document = new Document();

copy = new PdfCopy(document, os);

document.open();

for (File file : files) {

if (!file.exists()) {

continue;

}

String fileName = file.getName();

if (fileName.endsWith(“.pdf”)) {

PdfContentByte cb = copy.getDirectContent();

PdfOutline root = cb.getRootOutline();

new PdfOutline(root, new PdfDestination(PdfDestination.XYZ), fileName

.substring(0, fileName.lastIndexOf(“.”)));

// 不使用reader来维护文件,否则删除不掉文件,一直被占用

try (InputStream is = new FileInputStream(file)) {

PdfReader reader = new PdfReader(is);

int n = reader.getNumberOfPages();

for (int j = 1; j <= n; j++) {

document.newPage();

PdfImportedPage page = copy.getImportedPage(reader, j);

copy.addPage(page);

}

} catch(Exception e) {

log.warn(“error to close file : {}” + file.getCanonicalPath(), e);

// e.printStackTrace();

}

} else {

log.warn(“file may not be merged to pdf. name:” + file.getCanonicalPath());

}

}

} catch (Exception e) {

e.printStackTrace();

} finally {

if (document != null) {

document.close();

}

if (copy != null) {

copy.close();

}

if (os != null) {

IOUtils.closeQuietly(os);

}

}

}

/**

  • 将文件转换成byte数组

  • @param file

  • @return

  • **/

public static byte[] File2byte(File file){

byte[] buffer = null;

try {

FileInputStream fis = new FileInputStream(file);

ByteArrayOutputStream bos = new ByteArrayOutputStream();

byte[] b = new byte[1024];

int n;

while ((n = fis.read(b)) != -1) {

bos.write(b, 0, n);

}

fis.close();

bos.close();

buffer = bos.toByteArray();

}catch (FileNotFoundException e){

e.printStackTrace();

}catch (IOException e){

e.printStackTrace();

}

return buffer;

}

}

演示

最后

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Java工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。

因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
IOException e){

e.printStackTrace();

}

return buffer;

}

}

演示

最后

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Java工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。

因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

[外链图片转存中…(img-tGuewah9-1715164476826)]

[外链图片转存中…(img-ePup1XgR-1715164476827)]

[外链图片转存中…(img-kuCUkINo-1715164476827)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

  • 8
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值