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 Tridhya Tech, have developed a solution wherein you can directly pick the document metadata from PDF files in the form of the QR code 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 props = serviceRegistry.getNodeService().getProperties(actionedUponNodeRef);
String qrCodeString = new String();
for (Map.Entry 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 at alfresco/src/main/amp/config/alfresco/module/${project-name}/context/ to register action class.
Create an extension module at share/src/main/amp/config/alfresco/web-extension/site-data/extensions/ in separate or existing xml file.
onActionFormDialogactionbarcodeQRCode-pdf create{node.nodeRef}alfresco.doclib.action.barcodeQRCodePdf.msg.successalfresco.doclib.action.barcodeQRCodePdf.msg.failure alfresco.barcodeQRCodePdf.evaluator.checkPDFFileType ID for the Repository Action that this form is associated with
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 with the following code at
share/src/main/amp/config/alfresco/web-extension/
application/pdf
iText Library
Add the following dependency to pom.xml to use this library.
com.itextpdf
itextpdf
5.0.6
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 development 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.
Tridhya Tech team would be happy to help! Please feel free to approach us for a free consultation of the high-level requirement understanding.
#CTA-1#