本文由 发布,转载请注明出处,如有问题请联系我们! 发布时间: 2021-05-28【玩转PDF】贼稳,产品要做一个三方合同签署,我方了!

加载中

【轻松玩PDF】贼稳,商品要做一个三方合同签定,我方了!

一、序言

事儿是这一模样的,小农的企业,以前有一个作用必须签定来开展一系列的实际操作,因此大家引进了一个三方服务平台的签定——上吉签,可是有一个较为难堪的点便是,它不兼容合同书在电脑浏览器上和配件一起浏览的,大家要想的是必须将配件拼凑在合同书主文档中一起展现,可是它不兼容,因此大家就开过一个要求会。。。

商品说,我们要做一个网上合同书签定的作用,不借助第三方来进行,能够电脑浏览器上浏览和免费下载合同书,小农,你这里能做吗?

我一听,这个啊,这一有点儿难度系数啊(我需要時间),不大好做,以前大家连接的第三方就沒有彻底进行电脑浏览器浏览的作用,等同于大家做一个和这一第三方一模一样的物品,并且还需要比它那一个兼容大量的作用,不大好做(的确有点儿不大好做),再加上以前都没有做了,内心沒有底。

商品说,这一没有办法(你做也得做,不做也得做),是领导干部规定的(上边规定的,你只有做),你看看下进行这种作用大约必须多长时间?

因此只有咬着牙到了,因此给了一个大约的時间后,就逐渐科学研究了,什么叫快乐星球,假如你想要知道得话,那我也陪你科学研究,what???这些,方向跑偏了,回家回家。
在这里插入图片描述

二、那我也陪你科学研究

科学研究哪些?什么叫快乐星球[手动狗头],哼哼,忽悠了,你要马上终止你的傻*个人行为。

我们知道,如果是要想实际操作PDF得话(由于签订合同一般全是用的PDF,同志们为大家答疑解惑了,欢呼声能够响起来了),因此一般全是用iText(PDF 实际操作类库)实际操作类库??? ,哼哼,你是怎么回事?
在这里插入图片描述

大家一般全是要应用 Adobe专用工具设计方案表格和iText 来开展內容实际操作,这也是大家今日必须解读的行为主体,知道用哪种,大家而言一下大家的要求是啥?工作中后的小伙伴们是否有感觉很恐怖,“大家而言一下要求”,最先必须完成的是 根据PDF模版添充大家相匹配的甲承包方基本上数据信息后,转化成PDF文档,随后再将数据库查询的特殊数据信息拼凑在PDF里边一起,根据甲承包方依次签定后,随后让该合同书开展起效实际操作!能够浏览和免费下载。

规定便是那么个规定,听着倒是不会太难,主要是以前沒有做了,内心不太有谱,可是做完以后,迫不得已呼自身简直个奇才啊,我可真聪慧,想到小农从见习的情况下便是做的PDF,现如今工作中这么多年了或是在做PDF的,简直漂(cao)亮(dan),那么就做呗,谁怕谁啊!

三、Adobe专用工具

磨刀不误砍柴工工欲善其事,最先假如要想添充PDF模版里边的內容得话,大家必须应用到Adobe Acrobat Peo DC这一专用工具

下载链接:

连接:https://pan.baidu.com/s/1JdeKr7-abc4bajhVxoiYWg
提取码:6小时0i

1、开启PDF文档

当我们下载好Adobe Acrobat Peo DC后,用它开启PDF文档,随后点一下 提前准备表格
在这里插入图片描述

2、加上文本域

点一下加上文本域
在这里插入图片描述

3、填好用户标识符

这一用户标识符便是大家添充数据信息的主要参数,要一一对应
在这里插入图片描述

4、填好进行后,以下所显示

在这里插入图片描述

3、逐渐分配

别担心朋友们,新项目都给大家做好准备
新项目详细地址:https://GitHub.com/muxiaonong/other/tree/master/pdf_sign_demo

jar文件:

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>5.5.5</version>
        </dependency>
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>layout</artifactId>
            <version>7.1.15</version>
        </dependency>

        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itext-asian</artifactId>
            <version>5.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
        </dependency>

        <!--itext转化成word文本文档,必须下边dependency-->      <dependency>
            <groupId>com.lowagie</groupId>
            <artifactId>iText-rtf</artifactId>
            <version>2.1.4</version>
        </dependency>
        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
            <version>3.2.2</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>

依据PDF模版转化成文档和拼凑商品的数据信息

    /**
     * 依据PDF模版转化成PDF文档
     * @return
     */
    @GetMapping("generatePdf")
    public String generatePdf() throws Exception{
//        File file = ResourceUtils.getFile("classpath:" SAVE_PATH);
        File pdfFile = new File(ResourceUtils.getURL("classpath:").getPath() SAVE_PATH);
        try {
            PdfReader pdfReader;
            PdfStamper pdfStamper;
            ByteArrayOutputStream baos;

            Document document = new Document();
//

            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弄2号楼");
                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<File> 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(File6byte(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~一百万-5.7%,限制:五百元,低限:二十元",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("一百万~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万~三百万-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(File6byte(tempFile));
//        log.info("url:" url);

        //如果是提交网络服务器后,要删掉信息内容
        //当地不必删掉,不然沒有文档
//        if(tempFile.exists()){
//            tempFile.delete();
//        }

        return "success";
    }

PDFjava工具:

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<File> 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[] File6byte(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;
    }
}

演试

在我们撰写进行以后,就赶到了最重要的地区,检测了,内心还有点儿小兴奋,应当不容易有BUG的

在这里插入图片描述

最先大家键入http://localhost:8080/generatePdf,转化成添充模版,转化成新的PDF文档并合并文件,转化成进行以后大家会在新项目的class文件目录下见到这一文档

在这里插入图片描述

开启大家的文档,就可以见到,相匹配的数据信息,到这儿安然无恙,就查最后一步签订合同了
在这里插入图片描述

到这儿假如可以把签定的信息内容,填好到合同书签定的部位上,那大家就可以说做好了了,大家键入签定的详细地址http://localhost:8080/addContent,在我们在文件目录下见到 签定检测.PDF的情况下就表明大家做好了了

在这里插入图片描述
在这里插入图片描述
我们可以见到相匹配的签定信息内容早已被大家加上上来了,除开沒有第三方验证,该有的作用都是有了,太出色了啊!

这个时候我免不了想到了,诗仙李白那句 “大笑一声外出去,我辈岂是蓬蒿人”,天晴了,雨停了,我认为我又行了,我还早已想到到商品漂亮小姐姐钦佩的小目光了,风采红心闪闪,你要千万别痴迷哥!别老饮酒啊,吃点餐啊,几个菜喝成那样
在这里插入图片描述

汇总

这一作用,耗费了小农三天的情况下,假如这一作用早已可以在企业新项目中一切正常应用了,之上就是这个作用的详尽编码和表明,假如有疑问或是也在做这一作用的小伙伴们能够留言板留言,小农看到了会第一时间回应

假如大伙儿感觉非常好,还记得一键三连~

关注点赞过百,我是怀二胎也出下一篇

我是牧小农,害怕真知无限,进一步有进一步的开心,大伙儿给油!

评论(0条)

刀客源码 游客评论