Call Us India :- +91 9925144200       US :- +1 (732) 927-5544       Email us : hello@contcentric.com

Blog

Dec 20

How to Add Barcode to Documents in Alfresco

When the world is heading towards digitization, a barcode is no longer a new term for the enterprises. Tracking and storing information about goods, from individual items to large stocks of millions of items, barcode has provided ease and relief to the system. Barcode allows you to accurately track large stocks and also look up any single piece of merchandise in a matter of second. There are many other cases where the barcode is applied to the documents. We, at ContCentric, have come up with the solution to add the barcode to the document stored in the Alfresco system.

Here is the technical approach on how to generate a barcode in Alfresco. If you want to explore the QR code, then please visit this link.

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

Related blog: Step-by-Step Guide to Add QR Code to Document in Alfresco

1.Java Action

  • Create a Java class with following code at alfresco/src/main/java
public class BarcodePdfActionExecuter extends ActionExecuterAbstractBase {
      private static Log logger = LogFactory.getLog(BarcodePdfActionExecuter.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));

       String barcodeString = "abcd0001";
    
       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(inputStream);
       PdfStamper stamper = new PdfStamper(pdfReader, outputStream);
       PdfContentByte over = stamper.getOverContent(1);
      
       Barcode128 barcode128 = new Barcode128();
       barcode128.setCode(barcodeString);
       barcode128.setCodeType(Barcode.CODE128);
       Image code128Image = barcode128.createImageWithBarcode(over, null, null);
       code128Image.setAbsolutePosition(10,10);
       over.addImage(code128Image);
       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="barcode-pdf"
      class="com.contcentric.bar_repo.BarcodePdfActionExecuter"
      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.barcodePdf"
              icon="barcode" type="javascript"
              label="alfresco.doclib.action.barcodePdf.label">
              <param name="function">onActionFormDialog</param>
              <param name="itemKind">action</param>
              <param name="itemId">barcode-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.barcodePdf.msg.success
              </param>
              <param name="failureMessage">alfresco.doclib.action.barcodePdf.msg.failure
              </param>
              <evaluator>alfresco.barcodePdf.evaluator.checkPDFFileType
              </evaluator>
         </action>
</actions>
<actionGroups>
         <actionGroup id="document-browse">
              <action index="400" id="alfresco.doclib.action.barcodePdf" />
         </actionGroup>
         <actionGroup id="document-details">
               <action index="400" id="alfresco.doclib.action.barcodePdf" />
         </actionGroup>
    </actionGroups>
</config>
<config evaluator="string-compare" condition="barcode-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.barcodePdf.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.barcodePdf.label=Barcode Pdf
alfresco.doclib.action.barcodePdf.msg.success=Pdf have been barcoded.
alfresco.doclib.action.barcodePdf.msg.failure=Problem in barcoding Pdf, please contact Administrator
alfresco.doclib.action.barcodePdf.form.field.pageNo=Page Number
  • You have to add images for the action icon at

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

2.Evaluator

  • Create a separate <bean> with following code at

share/src/main/amp/config/alfresco/web-extension/

<bean id="alfresco.barcodePdf.evaluator.checkPDFFileType"
       parent="evaluator.doclib.action.isMimetype">
       <property name="mimetypes">
             <list>
                  <value>application/pdf</value>
             </list>
       </property>
</bean>

3. A Java PDF library (iText)

  • 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>

document management system

Feel free to contact us if there is any help you need while executing this code.

Vidhi P

About The Author

Vidhi is involved in the ECM project development at ContCentric. She is an ACE501, Alfresco Certified Engineer.

Leave a reply

Your email address will not be published. Required fields are marked *