pdfHtml: Custom Tagworker example

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

7th March 2017
iText PDF

Switch code for this example

CustomTagWorkerFactory.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.impl.DefaultTagWorkerFactory;
  12. import com.itextpdf.html2pdf.attach.ITagWorker;
  13. import com.itextpdf.html2pdf.attach.ProcessorContext;
  14. import com.itextpdf.styledxmlparser.node.IElementNode;
  15.  
  16. /**
  17.  * Example of a custom tagworkerfactory for pdfHTML
  18.  * The tag <qr> is mapped on a QRCode tagworker. Every other tag is mapped to the default.
  19.  */
  20. public class CustomTagWorkerFactory extends DefaultTagWorkerFactory {
  21.  
  22.     @Override
  23.     public ITagWorker getCustomTagWorker(IElementNode tag, ProcessorContext context) {
  24.         if ( "qr".equalsIgnoreCase(tag.name()) ) {
  25.             return new QRCodeTagWorker(tag, context);
  26.         }
  27.  
  28.         return null;
  29.     }
  30. }
HtmlParser.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.ConverterProperties;
  12. import com.itextpdf.html2pdf.HtmlConverter;
  13. import com.itextpdf.licensekey.LicenseKey;
  14.  
  15. import java.io.FileInputStream;
  16. import java.io.FileOutputStream;
  17. import java.io.IOException;
  18.  
  19.  
  20. public class HtmlParser {
  21.  
  22.     private static final String BASE_IN = "C:\\Temp\\html2pdf";
  23.  
  24.     public static void main(String[] args) throws IOException {
  25.         LicenseKey.loadLicenseFile("C:\\Code\\iText\\iText-7\\html2pdf\\src\\test\\resources\\com\\itextpdf\\html2pdf\\itextkey-html2pdf_typography.xml");
  26.  
  27.  
  28.         ConverterProperties converterProperties = new ConverterProperties();
  29.         converterProperties.setTagWorkerFactory(new CustomTagWorkerFactory());
  30.  
  31.         HtmlConverter.convertToPdf(
  32.                 new FileInputStream("C:\\Temp\\qr\\qrcode.html"),
  33.                 new FileOutputStream("C:\\Temp\\qr\\out.pdf"),
  34.                 converterProperties);
  35.     }
  36. }
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 <qr> 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<EncodeHintType, Object> 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<allowedErrorCorrection.length;i++){
  79.             if(toCheck.toUpperCase().equals(allowedErrorCorrection[i])){
  80.                 return true;
  81.             }
  82.         }
  83.         return false;
  84.     }
  85.  
  86.     private static boolean checkCharacterSet(String toCheck){
  87.         for(int i = 0; i<allowedCharset.length;i++){
  88.             if(toCheck.equals(allowedCharset[i])){
  89.                 return true;
  90.             }
  91.         }
  92.         return false;
  93.     }
  94.  
  95.     private static ErrorCorrectionLevel getErrorCorrectionLevel(String level){
  96.         switch(level) {
  97.             case "L":
  98.                 return ErrorCorrectionLevel.L;
  99.             case "M":
  100.                 return ErrorCorrectionLevel.M;
  101.             case "Q":
  102.                 return ErrorCorrectionLevel.Q;
  103.             case "H":
  104.                 return ErrorCorrectionLevel.H;
  105.         }
  106.         return null;
  107.  
  108.     }
  109. }
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