Quote:
We were dealing with other things.
If you want to contribute your implementation, we'll have a look at it.
Sorry if that came across entitled, that was not my intent.
Sure, I can share what we are doing; we are going through ZXing's BitMatrix as an intermediary format, and then drawing using PdfSharp:
Code:
using PdfSharp.Drawing;
using ZXing;
using ZXing.Common;
namespace OurApp.Utilities.Pdf;
/// <summary> Utilities for working with QR codes </summary>
public static class QrCodeHelper
{
/// <summary> Create a BitMatrix from the given string.
/// Caller must ensure that size (length of QR code square side in pixels) can handle the content to encode,
/// otherwise be prepared to handle <see cref="WriterException"/>
/// </summary>
public static BitMatrix Encode(string qrCodeContent, int size = 50)
{
var qrCodeWriter = new BarcodeWriterGeneric()
{
Format = BarcodeFormat.QR_CODE,
Options = new EncodingOptions { Width = size, Height = size, },
};
BitMatrix bitMatrix = qrCodeWriter.Encode(qrCodeContent);
return bitMatrix;
}
/// <summary>
/// A replacement for XGraphics.DrawMatrixCode() until implementation is finished
/// https://github.com/ststeiger/PdfSharpCore/issues/361,
/// https://forum.pdfsharp.net/viewtopic.php?f=2&t=4493
/// </summary>
public static void DrawQrCode(this XGraphics graphics, XPoint upperLeftCorner, BitMatrix bitMatrix, int pixelSize = 3)
{
for (int x = 0; x < bitMatrix.Width; x++)
{
for (int y = 0; y < bitMatrix.Height; y++)
{
var pixelLocation = new XPoint(upperLeftCorner.X + (x * pixelSize), upperLeftCorner.Y + (y * pixelSize));
var rectangle = new XRect(pixelLocation, new XSize(pixelSize, pixelSize));
var color = bitMatrix[x, y] ? XBrushes.Black : XBrushes.White;
graphics.DrawRectangle(color, rectangle);
}
}
}
}