I am using ZXing to generate barcodes in my windows application. I am successfully able to save the barcode as an image on my desktop, but I want to convert the barcode into an image in the code itself, paste it into PDF at a specific location. My code:
BarcodeWriter writer = new BarcodeWriter()
{
Format = BarcodeFormat.CODE_128,
Options = new EncodingOptions
{
Height = 100,
Width = 200,
PureBarcode = false,
Margin = 10,
},
};
string itemnumber = @"*\" + textBox1.Text + "*";
var bitmap = writer.Write(itemnumber);
//bitmap.Save( textBox1.Text , System.Drawing.Imaging.ImageFormat.Png);
// This line opens the image in your default image viewer
//System.Diagnostics.Process.Start("bitmap.png");
bitmap.Save(@"E:\Image.png" , System.Drawing.Imaging.ImageFormat.Png);
Now, I am reading this image and printing it in pdf using PDFsharp.
// 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);
// Create a font
//XFont font = new XFont("Verdana", 20, XFontStyle.Bold);
const string PngSamplePath = @"E:\Image.png";
XImage image = XImage.FromFile(PngSamplePath);
gfx.DrawImage(image, 220, 120, 50, 50);
// Save the document...
string filename = "Barcode.pdf";
document.Save(filename);
// ...and start a viewer.
Process.Start(filename);
But I don’t want to save the image on desktop. I want to directly convert the barcode to image in the code and then paste it in the PDF.
I want something like this, location to print the barcode can be any that would be specified by the end user:
You can save the image to a MemoryStream
and use that stream to draw the image in PDFsharp.
Then use XImage.FromStream
instead of XImage.FromFile
.