PDFsharp & MigraDoc Foundation

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

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Fri Dec 22, 2023 1:10 pm 
Offline

Joined: Fri Dec 22, 2023 1:06 pm
Posts: 1
I am trying to print a QR code to a pdf document. I used the QRCoder library to convert a string to png byte array.
Why is the line "XImage qrCodeImage = XImage.FromStream(qrCodeStream);" throwing an "Unsupported image format." exception?
This was working in PDF sharp version 1.50.5147, but after upgrading to version 6.0.0 it stopped working.


public class GeneratePdf
{
byte[] pdfData;

public byte[] PaymentLinkToPngQrCode(string paymentLink)
{
QRCodeGenerator qrGenerator = new QRCodeGenerator();
QRCodeData qrCodeData = qrGenerator.CreateQrCode(paymentLink, QRCodeGenerator.ECCLevel.Q);
PngByteQRCode qrCode = new PngByteQRCode(qrCodeData);
byte[] qrCodeAsPngByteArr = qrCode.GetGraphic(10);

return qrCodeAsPngByteArr;
}
public async Task<byte[]> GeneratePdfWithQrCode(string paymentLink)
{
try
{
// Create a new PDF document
PdfDocument document = new PdfDocument();
PdfPage page = document.AddPage();
XGraphics gfx = XGraphics.FromPdfPage(page);

// Generate the QR code image from the payment link
byte[] qrCodeAsPngByteArr = PaymentLinkToPngQrCode(paymentLink);

// Create a bitmap from the QR code byte array
using (var qrCodeStream = new MemoryStream(qrCodeAsPngByteArr, 0, qrCodeAsPngByteArr.Length, false, true))
{
XImage qrCodeImage = XImage.FromStream(qrCodeStream);

// Draw the QR code image on the PDF page
gfx.DrawImage(qrCodeImage, new XRect(100, 100, 100, 100));

using (MemoryStream pdfStream = new MemoryStream())
{
// Save the PDF document into the MemoryStream
document.Save(pdfStream, false);

// Get the PDF content as a byte array
return pdfStream.ToArray();
}
Console.WriteLine("PDF saved successfully.");
}
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
return null;
}
}


Top
 Profile  
Reply with quote  
PostPosted: Sat Dec 23, 2023 7:45 am 
Offline

Joined: Fri Oct 13, 2023 10:19 pm
Posts: 7
Use XImage to ensure compatibility.Using an image format supported by fromstream such as PNG. Downgrade to PDF Sharp version 1.50.5147 or verify that QR code conversion meets the image format criteria of the newest version.

Thak you.


Top
 Profile  
Reply with quote  
PostPosted: Sat Dec 23, 2023 6:40 pm 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 916
Location: CCAA
As I wrote on GitHub: Use PDFsharp 6.0.0 GDI build to get the full image support of PDFsharp 1.50.

When using PDFsharp 6.0.0 Core build, convert your PNG image to a supported PNG or BMP format.

A list of image formats supported by the Core build can be found here:
https://docs.pdfsharp.net/PDFsharp/Over ... tions.html

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


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 361 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:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group