pdfHTML: QRCode Example

This is a code example of iText PDF, discover more.

8th March 2017
iText PDF

Switch code for this example

QRCodeTagCssApplierFactory.java
  1. /*
  2.     This file is part of the iText (R) project.
  3.     Copyright (c) 1998-2019 iText Group NV
  4.     Authors: iText Software.
  5.  
  6.     For more information, please contact iText Software at this address:
  7.     sales@itextpdf.com
  8.  */
  9. package com.itextpdf.samples.pdfHTML;
  10.  
  11.  
  12.  
  13. import com.itextpdf.html2pdf.css.apply.ICssApplier;
  14. import com.itextpdf.html2pdf.css.apply.impl.BlockCssApplier;
  15. import com.itextpdf.html2pdf.css.apply.impl.DefaultCssApplierFactory;
  16. import com.itextpdf.styledxmlparser.node.IElementNode;
  17.  
  18. /**
  19.  * Example of a custom CssApplier factory for pdfHTML
  20.  * The tag  is mapped on a BlockCssApplier. Every other tag is mapped to the default.
  21.  */
  22. public class QRCodeTagCssApplierFactory extends DefaultCssApplierFactory {
  23.  
  24.     @Override
  25.     public ICssApplier getCustomCssApplier(IElementNode tag) {
  26.         if (tag.name().equals("qr")) {
  27.             return new BlockCssApplier();
  28.         }
  29.         return null;
  30.     }
  31. }
QRCodeTagWorker.java
  1. /*
  2.     This file is part of the iText (R) project.
  3.     Copyright (c) 1998-2019 iText Group NV
  4.     Authors: iText Software.
  5.  
  6.     For more information, please contact iText Software at this address:
  7.     sales@itextpdf.com
  8.  */
  9. package com.itextpdf.samples.pdfHTML;
  10.  
  11. import com.itextpdf.barcodes.BarcodeQRCode;
  12. import com.itextpdf.barcodes.qrcode.EncodeHintType;
  13. import com.itextpdf.barcodes.qrcode.ErrorCorrectionLevel;
  14. import com.itextpdf.html2pdf.attach.ITagWorker;
  15. import com.itextpdf.html2pdf.attach.ProcessorContext;
  16. import com.itextpdf.layout.IPropertyContainer;
  17. import com.itextpdf.layout.element.Image;
  18. import com.itextpdf.styledxmlparser.node.IElementNode;
  19.  
  20. import java.util.HashMap;
  21. import java.util.Map;
  22.  
  23. /**
  24.  * Example of a custom tagworker implementation for pdfHTML.
  25.  * The tagworker processes a  tag using iText Barcode functionality
  26.  */
  27. public class QRCodeTagWorker implements ITagWorker {
  28.     private static String[] allowedErrorCorrection = {"L","M","Q","H"};
  29.     private static String[] allowedCharset = {"Cp437","Shift_JIS","ISO-8859-1","ISO-8859-16"};
  30.     private BarcodeQRCode qrCode;
  31.     private Image qrCodeAsImage;
  32.  
  33.     public QRCodeTagWorker(IElementNode element, ProcessorContext context){
  34.         //Retrieve all necessary properties to create the barcode
  35.         Map hints = new HashMap();
  36.         //Character set
  37.         String charset = element.getAttribute("charset");
  38.         if(checkCharacterSet(charset )){
  39.             hints.put(EncodeHintType.CHARACTER_SET, charset);
  40.         }
  41.         //Error-correction level
  42.         String errorCorrection = element.getAttribute("errorcorrection");
  43.         if(checkErrorCorrectionAllowed(errorCorrection)){
  44.             ErrorCorrectionLevel errorCorrectionLevel = getErrorCorrectionLevel(errorCorrection);
  45.             hints.put(EncodeHintType.ERROR_CORRECTION, errorCorrectionLevel);
  46.         }
  47.         //Create the QR-code
  48.         qrCode = new BarcodeQRCode("placeholder",hints);
  49.  
  50.     }
  51.  
  52.     @Override
  53.     public void processEnd(IElementNode element, ProcessorContext context) {
  54.         //Transform barcode into image
  55.         qrCodeAsImage = new Image(qrCode.createFormXObject(context.getPdfDocument()));
  56.  
  57.     }
  58.  
  59.     @Override
  60.     public boolean processContent(String content, ProcessorContext context) {
  61.         //Add content to the barcode
  62.         qrCode.setCode(content);
  63.         return true;
  64.     }
  65.  
  66.     @Override
  67.     public boolean processTagChild(ITagWorker childTagWorker, ProcessorContext context) {
  68.         return false;
  69.     }
  70.  
  71.     @Override
  72.     public IPropertyContainer getElementResult() {
  73.  
  74.         return qrCodeAsImage;
  75.     }
  76.  
  77.     private static boolean checkErrorCorrectionAllowed(String toCheck){
  78.         for(int i = 0; i
QRCodeTagWorkerFactory.java
  1. /*
  2.     This file is part of the iText (R) project.
  3.     Copyright (c) 1998-2019 iText Group NV
  4.     Authors: iText Software.
  5.  
  6.     For more information, please contact iText Software at this address:
  7.     sales@itextpdf.com
  8.  */
  9. package com.itextpdf.samples.pdfHTML;
  10.  
  11. import com.itextpdf.html2pdf.attach.ITagWorker;
  12. import com.itextpdf.html2pdf.attach.ProcessorContext;
  13. import com.itextpdf.html2pdf.attach.impl.DefaultTagWorkerFactory;
  14. import com.itextpdf.styledxmlparser.node.IElementNode;
  15.  
  16. /**
  17.  * Example of a custom tagworkerfactory for pdfHTML
  18.  * The tag  is mapped on a QRCode tagworker. Every other tag is mapped to the default.
  19.  */
  20. public class QRCodeTagWorkerFactory extends DefaultTagWorkerFactory {
  21.  
  22.     @Override
  23.     public ITagWorker getCustomTagWorker(IElementNode tag, ProcessorContext context) {
  24.         if(tag.name().equals("qr")){
  25.             return new QRCodeTagWorker(tag, context);
  26.         }
  27.         return null;
  28.     }
  29. }
Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now