This article provides you the assets for demonstrating the ability to drag and drop multiple PDF files and save the assembled pdf file to your file system.

A servlet handles the pdf files upload in the browser. A DDX is created dynamically from the uploaded pdf files. We then use the AssemblerService to assemble the documents. The assembled document is stored in the CRX repository and the path returned to the calling application


To download the assets related to this article, please scroll to the bottom of this page.


protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) {
		System.out.println("In Assemble Uploaded Files");

		Map<String, Object> mapOfDocuments = new HashMap<String, Object>();
		final boolean isMultipart = org.apache.commons.fileupload.servlet.ServletFileUpload.isMultipartContent(request);
		DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder docBuilder = null;
		try {
			docBuilder = docFactory.newDocumentBuilder();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
		org.w3c.dom.Document ddx = docBuilder.newDocument();
		Element rootElement = ddx.createElementNS("", "DDX");

		Element pdfResult = ddx.createElement("PDF");
		pdfResult.setAttribute("result", "GeneratedDocument.pdf");
		if (isMultipart) {
			final java.util.Map<String,[]> params = request
			for (final java.util.Map.Entry<String,[]> pairs : params
					.entrySet()) {
				final String k = pairs.getKey();

				final[] pArr = pairs.getValue();
				final param = pArr[0];

				try {
					if (!param.isFormField()) {
						final InputStream stream = param.getInputStream();
						log.debug("the file name is " + param.getFileName());
						log.debug("Got input Stream inside my servlet####" + stream.available());
						com.adobe.aemfd.docmanager.Document document = new Document(stream);
						mapOfDocuments.put(param.getFileName(), document);
						org.w3c.dom.Element pdfSourceElement = ddx.createElement("PDF");
						pdfSourceElement.setAttribute("source", param.getFileName());
						pdfSourceElement.setAttribute("bookmarkTitle", param.getFileName());
						log.debug("The map size is " + mapOfDocuments.size());
					} else {
						log.debug("The form field is" + param.getString());

				} catch (IOException e) {
					// TODO Auto-generated catch block


		com.adobe.aemfd.docmanager.Document ddxDocument = documentServices.orgw3cDocumentToAEMFDDocument(ddx);
		Document assembledDocument = documentServices.assembleDocuments(mapOfDocuments, ddxDocument);
		String path = documentServices.saveDocumentInCrx("/content/ocrfiles", assembledDocument);
		JSONObject jsonObject = new JSONObject();
		try {
			jsonObject.put("path", path);
			response.setHeader("Cache-Control", "nocache");
			PrintWriter out = null;
			out = response.getWriter();

		} catch (JSONException e) {
			// TODO Auto-generated catch block
		} catch (IOException e) {
			// TODO Auto-generated catch block



To install the assets

  • Download the
  • Upload and install using the package manager.
  • Download and install the Document Services bundle
  • Download and install the Developing With Service User bundle.
  • Point your browser to http://localhost:4502/content/DocumentServices/AssemblePdfs.html.
  • Drag and Drop a couple of files.
  • The assembled files will be saved by default to c:\aemformsdemos
  • The save location is defined. in /etc/clientlibs/AEMFormsDemos/assemblemultiplefiles/js/assemblepdfs.js file


Make sure your AEM Forms install is complete. All your bundles need to be in active state.

Make sure you have added - Boot delegate RSA and BouncyCastle libraries as mentioned in this link

 Caveats for this Demo

    The code does not handle XFA based PDF documents

    Make sure you drag and drop PDF files only

    To change the save location of the assembled file go to


    Change the saveLocation on line 9


This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License  Twitter™ and Facebook posts are not covered under the terms of Creative Commons.

Legal Notices   |   Online Privacy Policy