SpringBoot使用iTest包對PDF表單填充

使用itest對錶單填充

使用簡介

首先: 準備PDF文件, 使用Adobe Acrobat DC軟件編寫表單域, 並記錄表單域字段, 後將使用表單Key查找Value填充PDFjava

準備pom座標(itest)

<dependency>
       <groupId>com.itextpdf</groupId>
       <artifactId>kernel</artifactId>
       <version>${itextpdf.version}</version>
   </dependency>
   <dependency>
       <groupId>com.itextpdf</groupId>
       <artifactId>forms</artifactId>
       <version>${itextpdf.version}</version>
   </dependency>
   <dependency>
       <groupId>com.itextpdf</groupId>
       <artifactId>font-asian</artifactId>
       <version>${itextpdf.version}</version>
   </dependency>

Resource層

import com.fasterxml.jackson.databind.ObjectMapper;
import com.lit.service.PdfMergeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayOutputStream;
import java.util.Map;

/**
 * @author LiT
 * @date 2020/8/14 16:28
 */
@RequestMapping("/api")
@RestController
public class PdfMergeResource {

    @Autowired
    private PdfMergeService pdfMergeService;
    @Autowired
    private ObjectMapper objectMapper;

    @PostMapping("/pdfmerge")
    public ResponseEntity<byte[]> merge(@RequestParam(name = "pdfFile") MultipartFile pdfFile, @RequestParam String json) throws Exception {
        Map<String, String> m = objectMapper.readValue(json, Map.class);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        pdfMergeService.merge(pdfFile.getInputStream(), m, baos);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_PDF);
        headers.setContentDispositionFormData("attachment", pdfFile.getOriginalFilename());
        return ResponseEntity.ok().headers(headers).body(baos.toByteArray());
    }

}

Service

import com.itextpdf.forms.PdfAcroForm;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.*;
import java.util.Map.Entry;

/**
 * @author LiT
 * @date 2020/8/14 16:28
 */
@Service
public class PdfMergeService {

    // 日誌服務
    private final Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * PDF 填充表單
     *
     * @param input
     * @param acrobatContext
     * @param output
     * @throws Exception
     */
    public void merge(InputStream input, Map<String, String> acrobatContext, OutputStream output) throws IOException {
        PdfReader reader = new PdfReader(input);
        PdfWriter writer = new PdfWriter(output);
        PdfDocument pdfDoc = new PdfDocument(reader, writer);
        PdfFont f2 = PdfFontFactory.createFont("STSong-Light", "UniGB-UCS2-H", true);
        PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
        for (Entry<String, String> entry : acrobatContext.entrySet()) {
            form.getField(entry.getKey()).setFont(f2).setValue(entry.getValue());
        }
        form.flattenFields();
        pdfDoc.close();
    }

}

至此已完成簡單表單填充, 感謝閱讀

若有須要歡迎留言QAQ

相關文章
相關標籤/搜索