PDFsharp & MigraDoc Foundation http://forum.pdfsharp.com/ |
|
Drawing QR codes (CodeDataMatrix) http://forum.pdfsharp.com/viewtopic.php?f=2&t=4493 |
Page 1 of 1 |
Author: | Lateralus [ Fri Oct 20, 2023 9:30 am ] |
Post subject: | Drawing QR codes (CodeDataMatrix) |
Is it possible to draw QR codes as of yet? This is not working for me: Code: var currentDir = Directory.GetCurrentDirectory(); var fileLocation = currentDir + @"\Data\Resources\sample_fillable_pdf.pdf"; var document = PdfReader.Open(fileLocation, PdfDocumentOpenMode.Modify); string qrString = "encode-me"; CodeDataMatrix qrCode = new CodeDataMatrix(qrString, 26, 26); XGraphics graphicsOnFirstPage = XGraphics.FromPdfPage(document.Pages[0]); graphicsOnFirstPage.DrawMatrixCode(qrCode, new(50, 500)); Error message: Code: Message: System.InvalidOperationException : 'XImage' must not be null here. Stack Trace: NRT.ThrowOnNull[TResult](String message) MatrixCode.get_MatrixImage() CodeDataMatrix.Render(XGraphics gfx, XBrush brush, XPoint position) XGraphics.DrawMatrixCode(MatrixCode matrixcode, XPoint position) PdfSampleReportManagerTests.TestFillPdfForm() line 38 RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor) MethodInvoker.Invoke(Object obj, IntPtr* args, BindingFlags invokeAttr) I think the issue is here? : https://github.com/empira/PDFsharp/blob ... ge.cs#L728 This was already not possible in the PdfSharpCore nuget: https://github.com/ststeiger/PdfSharpCore/issues/361 Any way this would be possible? Thanks & Best regards, Stephan |
Author: | TH-Soft [ Mon Oct 23, 2023 9:23 am ] |
Post subject: | Re: Drawing QR codes (CodeDataMatrix) |
Lateralus wrote: Is it possible to draw QR codes as of yet? IIRC it works in one of the three builds (Core, GDI, WPF).
|
Author: | Lateralus [ Mon Oct 23, 2023 10:28 am ] |
Post subject: | Re: Drawing QR codes (CodeDataMatrix) |
Well, we need it for .NET Core (drawing in .NET MAUI in a mobile device context) We have implemented it ourselves by drawing manually, using the GDI code as a blueprint. Just strange that it is still not implemented, since it seems so straightforward.. |
Author: | TH-Soft [ Mon Oct 23, 2023 10:54 am ] |
Post subject: | Re: Drawing QR codes (CodeDataMatrix) |
Lateralus wrote: Just strange that it is still not implemented, since it seems so straightforward.. We were dealing with other things.If you want to contribute your implementation, we'll have a look at it. |
Author: | Lateralus [ Tue Oct 24, 2023 1:25 pm ] |
Post subject: | Re: Drawing QR codes (CodeDataMatrix) |
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); } } } } |
Page 1 of 1 | All times are UTC |
Powered by phpBB® Forum Software © phpBB Group https://www.phpbb.com/ |