Skip to content

3.7 Draw Barcodes

OgreTransporter edited this page Mar 2, 2020 · 1 revision

The DrawBarcode method is an example of drawing two barcodes EAN-13 and Code-128.

// Draw Barcode
private void DrawBarcode()
{
    // save graphics state
    Contents.SaveGraphicsState();

    // draw EAN13 barcode
    BarcodeEAN13 Barcode1 = new BarcodeEAN13("1234567890128");
    Contents.DrawBarcode(1.3, 7.05, 0.012, 0.75, Barcode1, ArialNormal, 8.0);

    // create QRCode barcode
    QREncoder QREncoder = new QREncoder();

    // set error correction code
    QREncoder.ErrorCorrection = ErrorCorrection.M;

    // set module size in pixels
    QREncoder.ModuleSize = 1;

    // set quiet zone in pixels
    QREncoder.QuietZone = 4;

    // encode your text or byte array
    QREncoder.Encode(ArticleLink);

    // convert QRCode to black and white image
    PdfImage BarcodeImage = new PdfImage(Document);
    BarcodeImage.LoadImage(QREncoder);

    // draw image (height is the same as width for QRCode)
    Contents.DrawImage(BarcodeImage, 6.0, 6.8, 1.2);

    // define a web link area coinsiding with the qr code
    Page.AddWebLink(6.0, 6.8, 7.2, 8.0, ArticleLink);

    // restore graphics sate
    Contents.RestoreGraphicsState();
    return;
}
Clone this wiki locally