在类路径中保存已编辑的pdf并使用spring框架转换为字节

时间:2017-05-08 07:01:43

标签: spring pdfbox

使用spring和pdfbox apache库我已在控制器中编辑了PDF,但也不确定如何将此pdf保存到classpath,以便我可以将保存的pdf转换为字节并将其传递给服务。

感谢任何帮助。谢谢。

我的控制器代码如下:

 @RequestMapping("/individual/load/editablePDF.do")
public void getFile(HttpServletRequest request,HttpServletResponse response) throws IOException {
    Resource resource = new ClassPathResource("EditableFile.pdf");
    InputStream resourceInputStream = resource.getInputStream();
    PDDocument pdfDoc = PDDocument.load(resourceInputStream);
    PDDocumentCatalog docCatalog = pdfDoc.getDocumentCatalog();
    PDAcroForm acroForm = docCatalog.getAcroForm();
    List<PDField> fieldList = acroForm.getFields();
    String[] fieldArray = new String[fieldList.size()];
    int i = 0;
    for (PDField sField : fieldList) {
        fieldArray[i] = sField.getFullyQualifiedName();
        i++;
    }
    for (String f : fieldArray) {
        PDField field = acroForm.getField(f);

        System.out.println("f is: " + f);
        if (f.contains("company name")) {
            String value = "Discovery";
            field.setValue(value);
            System.out.println("printed: " + value + " to: " + f);
        }

    }
    try {
        pdfDoc.save("D:\\workspace\\TestSamples\\src\\Editable_ Discovery wellness days application form 2017-SAVED.pdf");//Not sure how to achieve this?
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    pdfDoc.close();
}

0 个答案:

没有答案