使用DocAssurance API
DocAssurance服務提供使用PDF檔案執行各種數位簽章或加密作業的功能,例如簽署、認證、新增簽章欄位、加密、解密等。
本文提供Java程式碼片段,協助您開始使用API。程式碼片段會使用存取權杖。 本文說明產生存取權杖所需的步驟
這項功能可在早期採用者方案下使用。 您可以從您的正式電子郵件ID寫信到aem-forms-ea@adobe.com以加入早期採用者計畫,並要求存取此功能
先決條件
- AEM FormsCloud Service體驗
- 使用Apache HTTP元件的經驗
- 存取AEM FormsCloud Service環境
Inspect檔案
使用inspect API擷取指定PDF檔案的安全性型別。 下列程式碼片段應該會幫助您快速上手。
...
File fileToInspect = new File("path_to_your_pdf_file)";
HttpPost httpPost = new HttpPost("<your_aem_forms_instance>/adobe/forms/document/assure/inspect");
httpPost.addHeader("Authorization", "Bearer " + accessToken);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
byte[] fileContent = FileUtils.readFileToByteArray(fileToInspect);
builder.addBinaryBody("document", fileContent, ContentType.create("application/pdf"), "BenefitOverview.pdf");
try
{
HttpEntity entity = builder.build();
httpPost.setEntity(entity);
CloseableHttpClient httpclient = HttpClients.createDefault();
CloseableHttpResponse response = httpclient.execute(httpPost);
if (response.getStatusLine().getStatusCode() == 200)
{
String json = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
log.info("The mode of encryption is " + JsonParser.parseString(json).getAsJsonObject().get("mode").getAsString());
}
}
catch (Exception e)
{
log.error(e.getMessage());
}
...
加密檔案
使用Encrypt API以密碼加密pdf檔案。 下列範常式式碼片段會加密指定PDF。
...
File fileToEncrypt = new File("path_to_your_pdf_file");
HttpPost httpPost = new HttpPost(postURL);
httpPost.addHeader("Authorization", "Bearer " + accessToken ");
MultipartEntityBuilder builder = MultipartEntityBuilder.create(); byte[] fileContent = FileUtils.readFileToByteArray(fileToEncrypt); builder.addBinaryBody("document", fileContent, ContentType.create("application/pdf"), "BenefitOverview.pdf");
String config = "{\"mode\":\"ENCRYPT_WITH_PASSWORD\",\"params\":{\"openPassword\":\"adobe\",\"permPassword\":\"systems\",\"permissions\":[\"ALL_PERM\"]}}";
builder.addTextBody("config", config, ContentType.APPLICATION_JSON);
try
{
HttpEntity entity = builder.build();
httpPost.setEntity(entity);
CloseableHttpClient httpclient = HttpClients.createDefault();
CloseableHttpResponse response = httpclient.execute(httpPost);
if (response.getStatusLine().getStatusCode() == 200)
{
InputStream generatedPDF = response.getEntity().getContent();
byte[] bytes = IOUtils.toByteArray(generatedPDF);
File encryptedFile = new File("c:\\aem_forms_cs_api\\encrypted.pdf");
FileOutputStream outputStream = new FileOutputStream(encryptedFile);
outputStream.write(bytes);
outputStream.close();
}
}
catch (Exception e)
{
log.error(e.getMessage());
}
...
新增簽名欄位至pdf
使用signfield API將簽名新增到提供的PDF。 下列程式碼片段範例在檔案的第4頁新增名為SignHere的簽名欄位
...
File pdfFile = new File(pdfFile1.getPath());
HttpPost httpPost = new HttpPost(postURL);
httpPost.addHeader("Authorization", "Bearer "+accessToken);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
byte[] fileContent = FileUtils.readFileToByteArray(pdfFile);
builder.addBinaryBody("document", fileContent, ContentType.create("application/pdf"), "BenefitOverview.pdf");
builder.addTextBody("field", "SignHere", ContentType.TEXT_PLAIN);
String rectangle = "{\"lowerLeftX\":1,\"lowerLeftY\":40,\"width\":100,\"height\":100}";
builder.addTextBody("rectangle", rectangle, ContentType.APPLICATION_JSON);
移除加密
使用加密API上的PUT操作,從提供的PDF中移除加密。 下列Java程式碼片段應該能協助您快速上手。
...
File fileToDecrypt = new File("path_to_your_pdf_file");
HttpPut httpPut = new HttpPut(putURL);
httpPut.addHeader("Authorization", "Bearer " + accessToken);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
byte[] fileContent = FileUtils.readFileToByteArray(fileToDecrypt);
builder.addBinaryBody("document", fileContent, ContentType.create("application/pdf"), "BenefitOverview.pdf");
builder.addTextBody("config", "systems", ContentType.TEXT_PLAIN);
try {
HttpEntity entity = builder.build();
httpPut.setEntity(entity);
CloseableHttpClient httpclient = HttpClients.createDefault();
CloseableHttpResponse response = httpclient.execute(httpPut);
if (response.getStatusLine().getStatusCode() == 200) {
InputStream generatedPDF = response.getEntity().getContent();
byte[] bytes = IOUtils.toByteArray(generatedPDF);
File encryptionRemoved = new File("c:\\aem_forms_cs_api\\encryption_removed.pdf");
FileOutputStream outputStream = new FileOutputStream(encryptionRemoved);
outputStream.write(bytes);
outputStream.close();
httpclient.close();
}
} catch (Exception e) {
log.error(e.getMessage());
}
...
Postman集合
可以從這裡下載API的Postman集合以進行測試。 您可以使用基本驗證或持有人權杖型別的驗證來叫用API。
recommendation-more-help
4859a77c-7971-4ac9-8f5c-4260823c6f69