常見的使用案例是將xdp轉譯為PDF,並將Reader擴充功能套用至轉譯的PDF。
例如,在AEM Forms的表單入口網站中,當使用者點按XDP時,我們可以將XDP轉譯為PDF,並透過閱讀器擴充PDF。
若要完成此使用案例,我們必須執行下列動作。
將Reader擴充功能憑證新增至「fd-service」使用者。 列出新增Reader擴充功能憑證的步驟 此處
您也可以參閱 配置Reader擴展憑據
建立可轉譯及套用使用權限的自訂OSGi服務。 完成此作業的程式碼列於下方
第7行:我們會使用FormsService的renderPDFForm,從XDP產生PDF。
第8-14行:已設定適當的使用權限。 這些使用權限會從OSGi組態設定中擷取。
第20行:使用與服務使用者fd-service相關聯的資源解析器
第24行:DocumentAssuranceService的secureDocument方法用於應用使用權限
public Document renderAndExtendXdp(String xdpPath) {
// TODO Auto-generated method stub
log.debug("In renderAndExtend xdp the alias is " + docConfig.ReaderExtensionAlias());
PDFFormRenderOptions renderOptions = new PDFFormRenderOptions();
renderOptions.setAcrobatVersion(AcrobatVersion.Acrobat_11);
try {
Document xdpRenderedAsPDF = formsService.renderPDFForm("crx://" + xdpPath, null, renderOptions);
UsageRights usageRights = new UsageRights();
usageRights.setEnabledBarcodeDecoding(docConfig.BarcodeDecoding());
usageRights.setEnabledFormFillIn(docConfig.FormFill());
usageRights.setEnabledComments(docConfig.Commenting());
usageRights.setEnabledEmbeddedFiles(docConfig.EmbeddingFiles());
usageRights.setEnabledDigitalSignatures(docConfig.DigitialSignatures());
usageRights.setEnabledFormDataImportExport(docConfig.FormDataExportImport());
ReaderExtensionsOptionSpec reOptionsSpec = new ReaderExtensionsOptionSpec(usageRights, "Sample ARES");
UnlockOptions unlockOptions = null;
ReaderExtensionOptions reOptions = ReaderExtensionOptions.getInstance();
reOptions.setCredentialAlias(docConfig.ReaderExtensionAlias());
log.debug("set the credential");
reOptions.setResourceResolver(getResolver.getFormsServiceResolver());
reOptions.setReOptions(reOptionsSpec);
log.debug("set the resourceResolver and re spec");
xdpRenderedAsPDF = docAssuranceService.secureDocument(xdpRenderedAsPDF, null, null, reOptions,
unlockOptions);
return xdpRenderedAsPDF;
} catch (FormsServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
以下螢幕擷圖顯示公開的設定屬性。 大部分的常見使用權限會透過此設定公開。
下列程式碼會顯示用來建置OSGi組態設定的程式碼
package com.aemformssamples.configuration;
import org.osgi.service.metatype.annotations.AttributeDefinition;
import org.osgi.service.metatype.annotations.AttributeType;
import org.osgi.service.metatype.annotations.ObjectClassDefinition;
@ObjectClassDefinition(name = "AEM Forms Samples Doc Services Configuration", description = "AEM Forms Samples Doc Services Configuration")
public @interface DocSvcConfiguration {
@AttributeDefinition(name = "Allow Form Fill", description = "Allow Form Fill", type = AttributeType.BOOLEAN)
boolean FormFill() default false;
@AttributeDefinition(name = "Allow BarCode Decoding", description = "Allow BarCode Decoding", type = AttributeType.BOOLEAN)
boolean BarcodeDecoding() default false;
@AttributeDefinition(name = "Allow File Embedding", description = "Allow File Embedding", type = AttributeType.BOOLEAN)
boolean EmbeddingFiles() default false;
@AttributeDefinition(name = "Allow Commenting", description = "Allow Commenting", type = AttributeType.BOOLEAN)
boolean Commenting() default false;
@AttributeDefinition(name = "Allow DigitialSignatures", description = "Allow File DigitialSignatures", type = AttributeType.BOOLEAN)
boolean DigitialSignatures() default false;
@AttributeDefinition(name = "Allow FormDataExportImport", description = "Allow FormDataExportImport", type = AttributeType.BOOLEAN)
boolean FormDataExportImport() default false;
@AttributeDefinition(name = "Reader Extension Alias", description = "Alias of your Reader Extension")
String ReaderExtensionAlias() default "";
}
下一步是使用GET方法建立servlet,將讀取器擴展PDF返回給用戶。 在這種情況下,會要求使用者將PDF儲存至其檔案系統。 這是因為PDF會以動態PDF呈現,而隨瀏覽器提供的pdf檢視器不會處理動態PDF。
以下是servlet的程式碼。 我們會將CRX存放庫中XDP的路徑傳遞至此servlet。
接著,我們會呼叫com.aemformssamples.documentservices.core.DocumentServices的renderAndExtendXdp方法。
然後,將讀取器擴展PDF流化到呼叫應用程式
package com.aemformssamples.documentservices.core.servlets;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.Servlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.adobe.aemfd.docmanager.Document;
import com.adobe.fd.forms.api.FormsService;
import com.aemformssamples.documentservices.core.DocumentServices;
@Component(service = Servlet.class, property = {
"sling.servlet.methods=get",
"sling.servlet.paths=/bin/renderandextend"
})
public class RenderAndReaderExtend extends SlingSafeMethodsServlet {
@Reference
FormsService formsService;
@Reference
DocumentServices documentServices;
private static final Logger log = LoggerFactory.getLogger(RenderAndReaderExtend.class);
private static final long serialVersionUID = 1L;
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) {
log.debug("The path of the XDP I got was " + request.getParameter("xdpPath"));
Document renderedPDF = documentServices.renderAndExtendXdp(request.getParameter("xdpPath"));
response.setContentType("application/pdf");
response.addHeader("Content-Disposition", "attachment; filename=AemFormsRocks.pdf");
try {
response.setContentLength((int) renderedPDF.length());
InputStream fileInputStream = null;
fileInputStream = renderedPDF.getInputStream();
OutputStream responseOutputStream = null;
responseOutputStream = response.getOutputStream();
int bytes;
while ((bytes = fileInputStream.read()) != -1) {
{
responseOutputStream.write(bytes);
}
}
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
}
}
若要在本機伺服器上測試,請執行下列步驟
將Reader擴充功能憑證新增至「fd-service」使用者
將瀏覽器指向 入口網站頁面
按一下pdf圖示,將xdp轉譯為已套用使用權限的pdf檔案。