Custom tag in HTML

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

28th December 2015
admin-marketing

Switch code for this example

BarcodeTagExample.java
  1. /*
  2.  * This example was written by Bruno Lowagie.
  3.  */
  4. package sandbox.xmlworker;
  5.  
  6. import com.itextpdf.text.Chunk;
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.Element;
  10. import com.itextpdf.text.ExceptionConverter;
  11. import com.itextpdf.text.Image;
  12. import com.itextpdf.text.pdf.BarcodeEAN;
  13. import com.itextpdf.text.pdf.PdfWriter;
  14. import com.itextpdf.tool.xml.NoCustomContextException;
  15. import com.itextpdf.tool.xml.Tag;
  16. import com.itextpdf.tool.xml.WorkerContext;
  17. import com.itextpdf.tool.xml.XMLWorker;
  18. import com.itextpdf.tool.xml.XMLWorkerHelper;
  19. import com.itextpdf.tool.xml.html.Span;
  20. import com.itextpdf.tool.xml.html.TagProcessorFactory;
  21. import com.itextpdf.tool.xml.html.Tags;
  22. import com.itextpdf.tool.xml.parser.XMLParser;
  23. import com.itextpdf.tool.xml.pipeline.css.CSSResolver;
  24. import com.itextpdf.tool.xml.pipeline.css.CssResolverPipeline;
  25. import com.itextpdf.tool.xml.pipeline.ctx.MapContext;
  26. import com.itextpdf.tool.xml.pipeline.end.PdfWriterPipeline;
  27. import com.itextpdf.tool.xml.pipeline.html.HtmlPipeline;
  28. import com.itextpdf.tool.xml.pipeline.html.HtmlPipelineContext;
  29. import java.io.File;
  30. import java.io.FileInputStream;
  31. import java.io.FileOutputStream;
  32. import java.io.IOException;
  33. import java.util.ArrayList;
  34. import java.util.List;
  35. import java.util.Map;
  36. import sandbox.WrapToTest;
  37.  
  38. /**
  39.  * @author iText
  40.  */
  41. @WrapToTest
  42. public class BarcodeTagExample {
  43.    
  44.     public static final String HTML = "resources/xml/barcode.html";
  45.     public static final String DEST = "results/xmlworker/barcode.pdf";
  46.    
  47.     public static void main(String[] args) throws IOException, DocumentException {
  48.         File file = new File(DEST);
  49.         file.getParentFile().mkdirs();
  50.         new BarcodeTagExample().createPdf(DEST);
  51.     }
  52.    
  53.     /**
  54.      * Creates a PDF with the words "Hello World"
  55.      * @param file
  56.      * @throws IOException
  57.      * @throws DocumentException
  58.      */
  59.     public void createPdf(String file) throws IOException, DocumentException {
  60.         // step 1
  61.         Document document = new Document();
  62.         // step 2
  63.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
  64.         // step 3
  65.         document.open();
  66.         // step 4
  67.        
  68.         // CSS
  69.         CSSResolver cssResolver =
  70.                 XMLWorkerHelper.getInstance().getDefaultCssResolver(false);
  71.        
  72.         // HTML
  73.         HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
  74.         TagProcessorFactory factory = Tags.getHtmlTagProcessorFactory();
  75.         factory.addProcessor(
  76.             new Span(){
  77.                 @Override
  78.                 public List end(WorkerContext ctx, Tag tag, List l) {
  79.                     List list = new ArrayList(1);
  80.                     list.add(getEanBarcodeChunk(ctx, tag.getAttributes()));
  81.                     return list;
  82.                 }
  83.             },
  84.             "ean");
  85.         htmlContext.setTagFactory(factory);
  86.         htmlContext.autoBookmark(false);
  87.        
  88.         // Pipelines
  89.         PdfWriterPipeline pdf = new PdfWriterPipeline(document, writer);
  90.         HtmlPipeline html = new HtmlPipeline(htmlContext, pdf);
  91.         CssResolverPipeline css = new CssResolverPipeline(cssResolver, html);
  92.        
  93.         // XML Worker
  94.         XMLWorker worker = new XMLWorker(css, true);
  95.         XMLParser p = new XMLParser(worker);
  96.         p.parse(new FileInputStream(HTML));
  97.        
  98.         // step 5
  99.         document.close();
  100.     }
  101.    
  102.     public Chunk getEanBarcodeChunk(WorkerContext ctx, Map attributes) {
  103.         MapContext mc;
  104.         try {
  105.             mc = (MapContext)ctx.get("com.itextpdf.tool.xml.pipeline.end.PdfWriterPipeline");
  106.         } catch (NoCustomContextException ex) {
  107.             throw new ExceptionConverter(ex);
  108.         }
  109.         PdfWriter writer = (PdfWriter) mc.get("WRITER");
  110.         BarcodeEAN barcode = new BarcodeEAN();
  111.         String type = attributes.get("type");
  112.         if ("EAN8".equals(type))
  113.             barcode.setCodeType(BarcodeEAN.EAN8);
  114.         else
  115.             barcode.setCodeType(BarcodeEAN.EAN13);
  116.         barcode.setCode(attributes.get("value"));
  117.         Image img = barcode.createImageWithBarcode(writer.getDirectContent(), null, null);
  118.         return new Chunk(img, 0, 0, true);
  119.     }
  120. }

Resources

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