I have a .csv file that I am reading –
Item,Position
A,1
B,5
C,7
I want to generate barcodes of item and placed them on a pdf page based on the position.
Here is my code –
while (!parser.EndOfData)
{
fields = parser.ReadFields();
string item = fields[0];
int position = Convert.ToInt32(fields[1]);
BarcodeWriter writer = new BarcodeWriter()
{
Format = BarcodeFormat.CODE_128,
Options = new EncodingOptions
{
Height = 100,
Width = 300,
PureBarcode = false,
Margin = 10,
},
};
string item = @"*" + item + "*";
var bitmap = writer.Write(item);
bitmap.SetResolution(300,300);
//bitmap.Save(@"E:\Image.png", System.Drawing.Imaging.ImageFormat.Png);
using (MemoryStream ms = new MemoryStream())
{
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
hastable.Add(position, ms);
}
}
GeneratePDF(hastable);
}
The GeneratePDF code :
PdfDocument document = new PdfDocument();
// Create an empty page
PdfPage page = document.AddPage();
// Get an XGraphics object for drawing
XGraphics gfx = XGraphics.FromPdfPage(page);
MemoryStream ms = new MemoryStream();
string filename = "Barcode.pdf";
foreach (DictionaryEntry values in hashtable)
{
int position = Convert.ToInt32(values.Key);
ms = (MemoryStream)values.Value;
XImage pic = XImage.FromStream(ms);
pic.Interpolate = false;
string points = getCoordinates(position);
string[] axis = points.Split(',');
int x = Convert.ToInt32(axis[0]);
int y = Convert.ToInt32(axis[1]);
gfx.DrawImage(pic, x, y, pic.PointWidth, pic.PointHeight);
I am getting an exception when I am trying to call GeneratePDF method. The exception says Cannot access a closed stream.
I am successfully able to print one barcode at a time. This is how I am doing it –
BarcodeWriter writer = new BarcodeWriter()
{
Format = BarcodeFormat.CODE_128,
Options = new EncodingOptions
{
Height = 100,
Width = 150,
PureBarcode = false,
Margin = 10,
},
};
string item = @"*" + textBox1.Text + "*";
var bitmap = writer.Write(item);
//bitmap.Save(@"E:\Image.png", System.Drawing.Imaging.ImageFormat.Png);
using (MemoryStream ms = new MemoryStream())
{
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
GeneratePDF(ms);
}
GeneratePDF :
// Create a new PDF document
PdfDocument document = new PdfDocument();
// Create an empty page
PdfPage page = document.AddPage();
// Get an XGraphics object for drawing
XGraphics gfx = XGraphics.FromPdfPage(page);
XImage pic = XImage.FromStream(ms);
gfx.DrawImage(pic, 220, 120, 110, 30);
// Save the document...
string filename = "Barcode.pdf";
document.Save(@"E:\" + filename);
MessageBox.Show("PDF saved successfully!");
Is this the right approach to print multiple barcodes at a same time?
>Solution :
using (MemoryStream ms = new MemoryStream())
This will close the stream after the bitmap has been saved, so when the stream is used at a later time it is closed, and cannot be used.
The simple option would be to just remove the using, while the rule of thumb is to always dispose disposable objects, you can be fairly sure that MemoryStream only holds managed memory that will be cleaned up by the garbage collector.
However, I see little point in your hashtable, Just keep a List<(Bitmap Image,int Position)>, and add a helper method to convert the bitmap to a XImage when you need to.