Step-by-Step Guide to Add QR Code to Document in Alfresco

Step-by-Step Guide to Add QR Code to Document in Alfresco

The QR code is no longer a new term for techies as well as non-technical persons. Out of many possible applications of QR code, one is to apply it to the documents that have stored or associated information. For those who want a technical definition of a QR code, we can mention here what Wikipedia says: A QR code is an optical and machine-readable representation of data; here, the data describes something about the object that carries the QR code.

Alfresco, as a document management system, requires the documents to be printed with QR codes. These QR codes contain some associated data of the said documents.

We, at ContCentric, have developed a solution wherein you can directly pick the document metadata from PDF files in the form of the QR code in Alfresco.

Related Blog: How to Add Barcode to Documents in Alfresco

Here is the technical approach on how to generate a QR code in Alfresco:

  • Java action – To allow users to generate and decide where to place the QR code in the document
  • Evaluator – To show action only on PDF documents (share side class)
  • A Java PDF library (iText) – Provides classes and functions to edit PDF documents

Java Action

Create a Java class with following code at alfresco/src/main/java

public class BarcodeQRCodePdfActionExecuter extends ActionExecuterAbstractBase {
	private static Log logger = LogFactory.getLog(BarcodeQRCodePdfActionExecuter.class);
	private ServiceRegistry serviceRegistry;

	public static final String PARAM_PAGE_NO = "page-no";

	public void setServiceRegistry(ServiceRegistry serviceRegistry) {
		this.serviceRegistry = serviceRegistry;
	}

	@Override
	protected void addParameterDefinitions(List paramList) {
		for (String s : new String[] {PARAM_PAGE_NO}) {
			paramList.add(new ParameterDefinitionImpl(s, DataTypeDefinition.TEXT, true, getParamDisplayLabel(s)));
		}
	}

	@Override
	protected void executeImpl(Action action, NodeRef actionedUponNodeRef) {

		logger.debug("Page No " + action.getParameterValue(PARAM_PAGE_NO));
		
		Map<QName, Serializable> props = serviceRegistry.getNodeService().getProperties(actionedUponNodeRef);
		String qrCodeString = new String();
		for (Map.Entry<QName,Serializable> entry : props.entrySet()){ 
            qrCodeString += entry.getKey().getLocalName()+" : "+entry.getValue()+"\n";
		}
		
		ContentWriter writer = serviceRegistry.getContentService().getWriter(actionedUponNodeRef,
				ContentModel.PROP_CONTENT, true);
		ContentReader reader = serviceRegistry.getContentService().getReader(actionedUponNodeRef,
				ContentModel.PROP_CONTENT);
		int pageNo = Integer.parseInt((String) action.getParameterValue(PARAM_PAGE_NO));
		try {

			PdfReader pdfReader = new PdfReader(reader.getContentInputStream());
			PdfStamper stamper = new PdfStamper(pdfReader, writer.getContentOutputStream());
			PdfContentByte over = stamper.getOverContent(pageNo);
			BarcodeQRCode barcodeQRCode = new BarcodeQRCode(qrCodeString, 1, 1, null);
			Image qrcodeImage = barcodeQRCode.getImage();
			qrcodeImage.setAbsolutePosition(10,10);
			over.addImage(qrcodeImage);
			stamper.close();
			pdfReader.close();

		} catch (ContentIOException e) {

			e.printStackTrace();
		} catch (IOException e) {

			e.printStackTrace();
		} catch (DocumentException e) {

			e.printStackTrace();
		}
	}
}

Create a separate <bean> at alfresco/src/main/amp/config/alfresco/module/${project-name}/context/ to register action class.

	<bean id="barcodeQRCode-pdf"
		class="com.contcentric.bar_repo.BarcodeQRCodePdfActionExecuter"
		parent="action-executer">
		<property name="serviceRegistry">
			<ref bean="ServiceRegistry" />
		</property>
	</bean>

Create an extension module at share/src/main/amp/config/alfresco/web-extension/site-data/extensions/ in separate or existing xml file.

<config evaluator="string-compare" condition="DocLibActions">
	<actions>
		<action id="alfresco.doclib.action.barcodeQRCodePdf"
			icon="qrcode" type="javascript"
			label="alfresco.doclib.action.barcodeQRCodePdf.label">
			<param name="function">onActionFormDialog</param>
			<param name="itemKind">action</param>
			<param name="itemId">barcodeQRCode-pdf</param> <!-- Repository action id = Spring Bean id -->
			<param name="mode">create</param>
			<param name="destination">{node.nodeRef}</param>
			<param name="successMessage">alfresco.doclib.action.barcodeQRCodePdf.msg.success
			</param>
			<param name="failureMessage">alfresco.doclib.action.barcodeQRCodePdf.msg.failure
			</param>
			<evaluator>alfresco.barcodeQRCodePdf.evaluator.checkPDFFileType
			</evaluator>
		</action>
	</actions>

	<actionGroups>
		<actionGroup id="document-browse">
			<action index="400" id="alfresco.doclib.action.barcodeQRCodePdf" />
		</actionGroup>
		<actionGroup id="document-details">
			<action index="400" id="alfresco.doclib.action.barcodeQRCodePdf" />
		</actionGroup>
	</actionGroups>
</config>

<config evaluator="string-compare" condition="barcodeQRCode-pdf"> ID for the Repository Action that this form is associated with
	<forms>
		<form>
			<field-visibility>
				<show id="page-no" />
			</field-visibility>
			<appearance>
				<field id="page-no"
					label-id="alfresco.doclib.action.barcodeQRCodePdf.form.field.pageNo">
				</field>
			</appearance>
		</form>
	</forms>
</config>

Create a properties file for Label at share/src/main/amp/config/alfresco/web-extension/messages/

alfresco.doclib.action.barcodeQRCodePdf.label=QRCode Pdf
alfresco.doclib.action.barcodeQRCodePdf.msg.success=Pdf have been QRcoded.
alfresco.doclib.action.barcodeQRCodePdf.msg.failure=Problem in qrcoding Pdf, please contact Administrator
alfresco.doclib.action.barcodeQRCodePdf.form.field.pageNo=Page Number

 

Note: You have to add images for the action icon at

share/src/main/amp/web/components/documentlibrary/actions/

Evaluator (Shows action only on PDF document)

Create a separate <bean> with the following code at

share/src/main/amp/config/alfresco/web-extension/
<bean id="alfresco.barcodeQRCodePdf.evaluator.checkPDFFileType"
	parent="evaluator.doclib.action.isMimetype">
	<property name="mimetypes">
		<list>
			<value>application/pdf</value>
		</list>
	</property>
</bean>

iText Library

Add the following dependency to pom.xml to use this library.

<!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf -->
<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.0.6</version>
</dependency>

12

aaaa

 

That’s it for now! If you have any feedback or suggestions regarding this approach, we would love to hear from you. We certainly hope that this article will guide you when you need to address QR code document management requirement in Alfresco. Though you must have come across an online barcode generator, we believe that this technique will add value to the Alfresco practice.

What’s more, as Alfresco deals with digitization projects, integration with Ephesoft helps you achieve OCR indexing, i.e. to extract values from the scanned documents and pushing it to the database. The application is widely used in Purchase Document Management and Pharmaceutical Document Management.

ContCentric team would be happy to help! Please feel free to approach us for a free consultation of the high-level requirement understanding.

Thanks for reading. Happy Developing!

    Have a Business Inquiry ?

    Let us collaborate and break yet another barrier to digitalization. Connect with us to discuss the project!





    Let us connect the digital dots!

    We are seeking dynamic professionals and unstoppable talents to craft distinct solutions for our clients to enhance their businesses. Come, join our fair & focused, optimistic & thoughtful world and deliver excellence together.

    Discipline

    Innovation

    Growth