2017-05-08 10 views
0

springとpdfbox apacheライブラリを使用していますが、PDFをコントローラで編集しましたが、保存されたpdfをバイトパスに変換できるよう、それをサービスに渡します。保存された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

OutputStreamに直接保存することもできます。 –

+0

それは今働いています。ティルマンありがとうございます:) –

+0

それを聞いてうれしいです。質問を削除するか、自分で回答してください(これは許可されています)。私はヒントのどれが役に立つのか分からない。 –

答えて

1

は、これは私のために働きました。

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 
document.save(byteArrayOutputStream); 
document.close(); 
InputStream inputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray()); 
関連する問題