pdfHTML: QRCode Examples
qrcodetagcssapplierfactory
JAVA
JAVA
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2023 Apryse Group NV
Authors: Apryse Software.
For more information, please contact iText Software at this address:
sales@itextpdf.com
*/
package com.itextpdf.samples.sandbox.pdfhtml.qrcodetag;
import com.itextpdf.html2pdf.css.apply.ICssApplier;
import com.itextpdf.html2pdf.css.apply.impl.BlockCssApplier;
import com.itextpdf.html2pdf.css.apply.impl.DefaultCssApplierFactory;
import com.itextpdf.styledxmlparser.node.IElementNode;
/**
* Example of a custom CssApplier factory for pdfHTML
* The tag <qr> is mapped on a BlockCssApplier. Every other tag is mapped to the default.
*/
public class QRCodeTagCssApplierFactory extends DefaultCssApplierFactory {
@Override
public ICssApplier getCustomCssApplier(IElementNode tag) {
if (tag.name().equals("qr")) {
return new BlockCssApplier();
}
return null;
}
}
C#
C#
using iText.Html2pdf.Css.Apply;
using iText.Html2pdf.Css.Apply.Impl;
using iText.StyledXmlParser.Node;
namespace iText.Samples.Sandbox.Pdfhtml.Qrcodetag
{
/// <summary>
/// Example of a custom CssApplier factory for pdfHTML
/// </summary>
/// <remarks>
/// The tag <bold>qr</bold> is mapped on a BlockCssApplier. Every other tag is mapped to the default.
/// </remarks>
public class QRCodeTagCssApplierFactory : DefaultCssApplierFactory
{
public override ICssApplier GetCustomCssApplier(IElementNode tag)
{
if (tag.Name().Equals("qr"))
{
return new BlockCssApplier();
}
return null;
}
}
}
qrcodetagworker
JAVA
JAVA
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2023 Apryse Group NV
Authors: Apryse Software.
For more information, please contact iText Software at this address:
sales@itextpdf.com
*/
package com.itextpdf.samples.sandbox.pdfhtml.qrcodetag;
import com.itextpdf.barcodes.BarcodeQRCode;
import com.itextpdf.barcodes.qrcode.EncodeHintType;
import com.itextpdf.barcodes.qrcode.ErrorCorrectionLevel;
import com.itextpdf.html2pdf.attach.ITagWorker;
import com.itextpdf.html2pdf.attach.ProcessorContext;
import com.itextpdf.layout.IPropertyContainer;
import com.itextpdf.layout.element.Image;
import com.itextpdf.styledxmlparser.node.IElementNode;
import java.util.HashMap;
import java.util.Map;
/**
* Example of a custom tagworker implementation for pdfHTML.
* The tagworker processes a <qr> tag using iText Barcode functionality
*/
public class QRCodeTagWorker implements ITagWorker {
private static String[] allowedErrorCorrection = {"L", "M", "Q", "H"};
private static String[] allowedCharset = {"Cp437", "Shift_JIS", "ISO-8859-1", "ISO-8859-16"};
private BarcodeQRCode qrCode;
private Image qrCodeAsImage;
public QRCodeTagWorker(IElementNode element, ProcessorContext context) {
// Retrieve all necessary properties to create the barcode
Map<EncodeHintType, Object> hints = new HashMap<>();
// Character set
String charset = element.getAttribute("charset");
if (checkCharacterSet(charset)) {
hints.put(EncodeHintType.CHARACTER_SET, charset);
}
// Error-correction level
String errorCorrection = element.getAttribute("errorcorrection");
if (checkErrorCorrectionAllowed(errorCorrection)) {
ErrorCorrectionLevel errorCorrectionLevel = getErrorCorrectionLevel(errorCorrection);
hints.put(EncodeHintType.ERROR_CORRECTION, errorCorrectionLevel);
}
// Create the QR-code
qrCode = new BarcodeQRCode("placeholder", hints);
}
@Override
public void processEnd(IElementNode element, ProcessorContext context) {
// Transform barcode into image
qrCodeAsImage = new Image(qrCode.createFormXObject(context.getPdfDocument()));
}
@Override
public boolean processContent(String content, ProcessorContext context) {
// Add content to the barcode
qrCode.setCode(content);
return true;
}
@Override
public boolean processTagChild(ITagWorker childTagWorker, ProcessorContext context) {
return false;
}
@Override
public IPropertyContainer getElementResult() {
return qrCodeAsImage;
}
private static boolean checkErrorCorrectionAllowed(String toCheck) {
for (int i = 0; i < allowedErrorCorrection.length; i++) {
if (toCheck.toUpperCase().equals(allowedErrorCorrection[i])) {
return true;
}
}
return false;
}
private static boolean checkCharacterSet(String toCheck) {
for (int i = 0; i < allowedCharset.length; i++) {
if (toCheck.equals(allowedCharset[i])) {
return true;
}
}
return false;
}
private static ErrorCorrectionLevel getErrorCorrectionLevel(String level) {
switch (level) {
case "L":
return ErrorCorrectionLevel.L;
case "M":
return ErrorCorrectionLevel.M;
case "Q":
return ErrorCorrectionLevel.Q;
case "H":
return ErrorCorrectionLevel.H;
}
return null;
}
}
C#
C#
using System;
using System.Collections.Generic;
using iText.Barcodes;
using iText.Barcodes.Qrcode;
using iText.Html2pdf.Attach;
using iText.Layout;
using iText.Layout.Element;
using iText.StyledXmlParser.Node;
namespace iText.Samples.Sandbox.Pdfhtml.Qrcodetag
{
/// <summary>
/// Example of a custom tagworker implementation for pdfHTML.
/// </summary>
/// <remarks>
/// The tagworker processes a <bold>qr</bold> tag using iText Barcode functionality
/// </remarks>
public class QRCodeTagWorker : ITagWorker
{
private static string[] allowedErrorCorrection = {"L", "M", "Q", "H"};
private static string[] allowedCharset = {"Cp437", "Shift_JIS", "ISO-8859-1", "ISO-8859-16"};
private BarcodeQRCode qrCode;
private Image qrCodeAsImage;
public QRCodeTagWorker(IElementNode element, ProcessorContext context)
{
// Retrieve all necessary properties to create the barcode
IDictionary<EncodeHintType, Object> hints = new Dictionary<EncodeHintType, object>();
// Character set
string charset = element.GetAttribute("charset");
if (CheckCharacterSet(charset))
{
hints[EncodeHintType.CHARACTER_SET] = charset;
}
// Error-correction level
string errorCorrection = element.GetAttribute("errorcorrection");
if (CheckErrorCorrectionAllowed(errorCorrection))
{
ErrorCorrectionLevel errorCorrectionLevel = GetErrorCorrectionLevel(errorCorrection);
hints[EncodeHintType.ERROR_CORRECTION] = errorCorrectionLevel;
}
// Create the QR-code
qrCode = new BarcodeQRCode("placeholder", hints);
}
public virtual void ProcessEnd(IElementNode element, ProcessorContext context)
{
// Transform barcode into image
qrCodeAsImage = new Image(qrCode.CreateFormXObject(context.GetPdfDocument()));
}
public virtual bool ProcessContent(string content, ProcessorContext context)
{
// Add content to the barcode
qrCode.SetCode(content);
return true;
}
public virtual bool ProcessTagChild(ITagWorker childTagWorker, ProcessorContext context)
{
return false;
}
public virtual IPropertyContainer GetElementResult()
{
return qrCodeAsImage;
}
private static bool CheckErrorCorrectionAllowed(string toCheck)
{
for (int i = 0; i < allowedErrorCorrection.Length; i++)
{
if (toCheck.ToUpper().Equals(allowedErrorCorrection[i]))
{
return true;
}
}
return false;
}
private static bool CheckCharacterSet(string toCheck)
{
for (int i = 0; i < allowedCharset.Length; i++)
{
if (toCheck.Equals(allowedCharset[i]))
{
return true;
}
}
return false;
}
private static ErrorCorrectionLevel GetErrorCorrectionLevel(string level)
{
switch (level)
{
case "L":
return ErrorCorrectionLevel.L;
case "M":
return ErrorCorrectionLevel.M;
case "Q":
return ErrorCorrectionLevel.Q;
case "H":
return ErrorCorrectionLevel.H;
}
return null;
}
}
}
qrcodetagworkerfactory
JAVA
JAVA
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2023 Apryse Group NV
Authors: Apryse Software.
For more information, please contact iText Software at this address:
sales@itextpdf.com
*/
package com.itextpdf.samples.sandbox.pdfhtml.qrcodetag;
import com.itextpdf.html2pdf.attach.ITagWorker;
import com.itextpdf.html2pdf.attach.ProcessorContext;
import com.itextpdf.html2pdf.attach.impl.DefaultTagWorkerFactory;
import com.itextpdf.styledxmlparser.node.IElementNode;
/**
* Example of a custom tagworkerfactory for pdfHTML
* The tag <qr> is mapped on a QRCode tagworker. Every other tag is mapped to the default.
*/
public class QRCodeTagWorkerFactory extends DefaultTagWorkerFactory {
@Override
public ITagWorker getCustomTagWorker(IElementNode tag, ProcessorContext context) {
if (tag.name().equals("qr")) {
return new QRCodeTagWorker(tag, context);
}
return null;
}
}
C#
C#
using iText.Html2pdf.Attach;
using iText.Html2pdf.Attach.Impl;
using iText.StyledXmlParser.Node;
namespace iText.Samples.Sandbox.Pdfhtml.Qrcodetag
{
/// <summary>
/// Example of a custom tagworkerfactory for pdfHTML
/// </summary>
/// <remarks>
/// The tag <bold>qr</bold> is mapped on a QRCode tagworker. Every other tag is mapped to the default.
/// </remarks>
public class QRCodeTagWorkerFactory : DefaultTagWorkerFactory
{
public override ITagWorker GetCustomTagWorker(IElementNode tag, ProcessorContext context)
{
if (tag.Name().Equals("qr"))
{
return new QRCodeTagWorker(tag, context);
}
return null;
}
}
}