java使用Abobe Acrobat DC生成模板

一、利用word生成一個文檔轉成pdf

word模板

說明:轉換成pdf格式

二、Abobe Acrobat DC圖解

利用Abobe Acrobat DC打開pdf

步驟:文件 —->創建——>創建表單—–>選擇文件(你轉換成pdf文件)

這裡是所要增加的文本域、圖片域等功能按鈕

在這裡插入圖片描述

在你需要的位置增加你說要的功能完成之後進行保存

在這裡插入圖片描述

三、java後臺代碼

環境maven

<dependency>
			<groupId>com.itextpdf</groupId>
			<artifactId>itext7-core</artifactId>
			<version>7.1.13</version>
			<type>pom</type>
		</dependency>

生成的模板文件放到適當的位置

在這裡插入圖片描述

所需要生成新的pdf代碼

public @ResponseBody Object getFaceVerificationPdf(GzFaceVerification gzFaceVerification, HttpServletRequest request) throws IOException {
        try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
            try (PdfDocument document = new PdfDocument(new PdfReader(
                    new ClassPathResource("static/moban.pdf").getInputStream()), new PdfWriter(outputStream))) {

                    PdfAcroForm pdfAcroForm = PdfAcroForm.getAcroForm(document, true);
                    //對圖片進行處理(如果不處理在模板中顯示不出圖片)
                    PdfFormField pdfFormField = pdfAcroForm.getField("frontIdCardUrl");
                    InputStream is = new FileInputStream(new File(gzFaceVerification.getFrontIdCardUrl()));
                    String str = Base64.encodeBytes(StreamUtil.inputStreamToArray(is));
                    pdfFormField.setValue(str);
                    pdfAcroForm.addField(pdfFormField);

                    PdfFormField pdfFormField1 = pdfAcroForm.getField("reverseIdCardUrl");
                    InputStream is1 = new FileInputStream(new File(gzFaceVerification.getReverseIdCardUrl()));
                    String str1 = Base64.encodeBytes(StreamUtil.inputStreamToArray(is1));
                    pdfFormField1.setValue(str1);
                    pdfAcroForm.addField(pdfFormField1);

                    PdfFormField pdfFormField2 = pdfAcroForm.getField("photo");
                    InputStream is2 = new FileInputStream(new File(gzFaceVerification.getPhoto()));
                    String str2 = Base64.encodeBytes(StreamUtil.inputStreamToArray(is2));
                    pdfFormField2.setValue(str2);
                    pdfAcroForm.addField(pdfFormField2);

                    pdfAcroForm.getField("frontIdCardUrl").setValue(gzFaceVerification.getFrontIdCardUrl());
                    pdfAcroForm.getField("reverseIdCardUrl").setValue(gzFaceVerification.getReverseIdCardUrl());
                    pdfAcroForm.getField("photo").setValue(gzFaceVerification.getPhoto());
                    pdfAcroForm.getField("comparisonResults").setValue(gzFaceVerification.getComparisonResults());
                    pdfAcroForm.getField("createTime").setValue(gzFaceVerification.getCreateTime());
                    pdfAcroForm.flattenFields();

            }
            HttpHeaders httpHeaders = new HttpHeaders();
            httpHeaders.setContentType(MediaType.APPLICATION_PDF);
            httpHeaders.setContentDispositionFormData("attachment", "test.pdf", Charset.forName("UTF-8"));

            return ResponseEntity.ok().headers(httpHeaders).body(outputStream.toByteArray());
        }
    }

到此這篇關於java使用Abobe Acrobat DC生成模板的文章就介紹到這瞭,更多相關Abobe Acrobat DC生成模板內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀:

    None Found