1

IronBarcode (preferred)

We want to print a QR Code in a Label with iTextSharp. We use IronBarcode to generate the Barcode. Also see: IronBarcode Tutorial

var label = new Label(Enums.Alignment.CENTER);

// label.AddText("Nr.     " + index.ToString("000000"), "Verdana", 12, embedFont: true); // YEAH this works fine

var qrcode = QRCodeWriter.CreateQrCode(index.ToString(), 100);
var image = qrcode.GetInstance(); image.ScaleToFitHeight = false;
label.AddImage(image);

labelCreator.AddLabel(label);

Error:

"GeneratedBarcode" contain no defintion for "GetInstance", and there is no method who would accept an argument from typ "GeneratedBarcode".

QRCoder

Also we tried it with QRCoder / Found here: QRCoder Tutorial

var label = new Label(Enums.Alignment.CENTER);

// label.AddText("Nr.     " + index.ToString("000000"), "Verdana", 12, embedFont: true); // YEAH this works fine

var qrGenerator = new QRCodeGenerator();
var qrCodeData = qrGenerator.CreateQrCode(index.ToString(), QRCodeGenerator.ECCLevel.Q);
var qrCode = new Base64QRCode(qrCodeData);
var qrCodeImageAsBase64 = qrCode.GetGraphic(8);
var imageBytes = Convert.FromBase64String(qrCodeImageAsBase64);

var image = Image.GetInstance(imageBytes);
image.ScaleAbsoluteWidth(40);
image.ScaleAbsoluteHeight(40);

label.AddImage(image);

labelCreator.AddLabel(label);

Error:

"Image" contain no defintion for "GetInstance".

Other try

If we use using iTextSharp.text; and using iTextSharp.text.pdf;

Error:

Argument "1": Convert from "iTextSharp.text.Image" to "System.IO.Stream" not possible

How we can fix this problem? Thanks for your input!

~ edit

This is our function to add the Image from label.cs. Have a look to this project we use as basic: SharpPDFLabel.

public void AddImage(Stream img)
{
var mem = new System.IO.MemoryStream();
CopyStream(img, mem);
_images.Add(mem.GetBuffer());
}

In a second step we want to add an image to our barcode, preferred is the solution of IronBarcode.

  • **IronBarcode** - the example code you linked to does not claim that there is a `GetInstance` method in their class. Why do you think so? **QRCoder** There appear to be `Image` classes from different namespaces in use here. Disambiguate by using the fully qualified class name, i.e. namespace + class name. – mkl Feb 20 '22 at 16:05
  • Hi mkl, we changed the namespace from "Image.GetInstance()" to "iTextSharp.text.Image.GetInstance()" and this part is now ok. But we have always the problem with "Label.AddImage(image);" and the missing convertion >>> Argument "1": Convert from "iTextSharp.text.Image" to "System.IO.Stream" not possible << – Julian Johannsen Feb 20 '22 at 17:05
  • That means that you still have other class name clashes. Look into the code lines where the error is reported and disambiguate. And if that doesn't help, check whether the classes and methods really match... – mkl Feb 20 '22 at 20:28

4 Answers4

3

If you using iTextSharp Why don't do it all the way in iTextSharp?

        GeneratedPdf generatedPDF = new GeneratedPdf();
        Document document = new Document();

        string path = @"C:\Temp\";
        string originalFileName = "qr.pdf";

        PdfWriter pdfWriter = PdfWriter.GetInstance(document, new FileStream(path + originalFileName, FileMode.Create));
        document.Open();
        string strBarCodeValue = "hello world";
        BarcodeQRCode barcodeQRCode = new BarcodeQRCode(strBarCodeValue, 20, 20, null) ;
        
        document.Add(barcodeQRCode.GetImage());
        document.Close();
rotem
  • 39
  • 5
  • We use [SharpPDFLabel](https://github.com/graboskyc/SharpPDFLabel) as basic and create custom labels. So we have to use the existing classes. I have edited our question. Please have a look. – Julian Johannsen Feb 21 '22 at 09:11
  • @JulianJohannsen how about using the barcodeQRCode.GetImage()) to pass a Image to singleSheetLabele.AddImage – rotem Feb 21 '22 at 09:27
  • thanks, but we prefer IronBarcode ... because we want to add an image within our barcode. first it should be added to the label. – Julian Johannsen Feb 21 '22 at 09:46
0

SOLUTION

We open a MemoryStream and put into them our QR / picture. In our function behind them we want to read a "Stream qrstream". It must be "MemoryStream qrstream".

We use now "Spire Barcode" instead of "IronBarcode" or "QRCoder"

wrong

public void AddImage(Stream qrstream)
        {

            // var mem = new MemoryStream(); // not needed
            // CopyStream(qrstream, mem); // not needed
            _images.Add(qrstream.GetBuffer());
        }

correct

public void AddImage(MemoryStream qrstream)
        {
            // var mem = new MemoryStream(); // not needed
            // CopyStream(qrstream, mem); // not needed
            _images.Add(qrstream.GetBuffer());
        }

function to create stream

               //Create a BarcodeSettings object
               BarcodeSettings settings = new BarcodeSettings();

               //Set barcode type, error correction level, data, etc.
               settings.Type = BarCodeType.QRCode;
               settings.QRCodeECL = QRCodeECL.H;
               settings.X = 1.4f;
               settings.AutoResize = true;
               settings.HasBorder = false;
               settings.ShowText = false;
               string data = "MY TEXT IN QR CODE";
               settings.Data = data;
               settings.Data2D = data;

               //Generate QR image based on the settings
               BarCodeGenerator generator = new BarCodeGenerator(settings);
               Image image = generator.GenerateImage();

               // Put Image to Stream
               var qrstream = new System.IO.MemoryStream();
               image.Save(qrstream, System.Drawing.Imaging.ImageFormat.Png);

               // Put QR Code to Label as Image
               label.AddImage(qrstream);

function to add image to cell

foreach (var img in _images)
            {
                var pdfImg = iTextSharp.text.Image.GetInstance(img);
                cellContent.Add(new Chunk(pdfImg, 0, 0));
            }

Running now, great!

  • Using `qrstream.GetBuffer()` for a `MemoryStream qrstream` is questionable as `GetBuffer()` gives you the whole buffer, both used and unused parts, i.e. the image bytes followed by some trash bytes. Depending on the image format the next processor of the data may reject the image. `ToArray()` on the other hand would return only the used part of the buffer, only the image bytes. – mkl Feb 22 '22 at 14:10
0

IronBarcode don't have any function with this name GetInstance() you can use qrcode.Image property that return System.Drawing.Image object and there is also lot of functions that return different types like qrcode.ToBitmap() , qrcode.ToImage() , qrcode.ToStream and a lot others you can find them all in IronSoftware Website here

I wrote a code snippet that shows how to create a QR Code that contains Logo and annotation

var qrcode = QRCodeWriter.CreateQrCodeWithLogo(index.ToString(), @"MyLogo.png");
qrcode.AddAnnotationTextBelowBarcode(index.ToString());
var image = qrcode.Image;
label.AddImage(image);
labelCreator.AddLabel(label);

you can Know more about IronBarcode IronSoftware Tutorials here

0

I prefer QRCoder as 2D Barcode Generator because its free :).

Here is the example code:

QRCodeGenerator qrGenerator = new QRCodeGenerator();
QRCodeData qrCodeData = qrGenerator.CreateQrCode("The text which should be encoded.", QRCodeGenerator.ECCLevel.Q);
QRCode qrCode = new QRCode(qrCodeData);
Bitmap qrCodeImage = qrCode.GetGraphic(20);
qrCodeImage.Save("Qrcoder.bmp");
Tyler2P
  • 2,324
  • 26
  • 22
  • 31