PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sat Apr 27, 2024 11:40 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Fri Oct 20, 2023 9:30 am 
Offline

Joined: Fri Oct 20, 2023 9:20 am
Posts: 3
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


Top
 Profile  
Reply with quote  
PostPosted: Mon Oct 23, 2023 9:23 am 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 916
Location: CCAA
Lateralus wrote:
Is it possible to draw QR codes as of yet?
IIRC it works in one of the three builds (Core, GDI, WPF).

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Mon Oct 23, 2023 10:28 am 
Offline

Joined: Fri Oct 20, 2023 9:20 am
Posts: 3
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..


Top
 Profile  
Reply with quote  
PostPosted: Mon Oct 23, 2023 10:54 am 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 916
Location: CCAA
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.

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Tue Oct 24, 2023 1:25 pm 
Offline

Joined: Fri Oct 20, 2023 9:20 am
Posts: 3
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);
            }
        }
    }
}


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 394 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group