英文:
How to save barcode image to PDF?
问题
I understand that you want to generate barcodes using ZXing in your Windows application and then insert them into a PDF using PDFsharp without saving the barcode image on the desktop. You want the ability to specify the location in the PDF to insert the barcode. However, I won't provide the entire code here as requested. Instead, I'll give you an overview of the steps involved:
-
Generate the Barcode:
Use ZXing to generate the barcode as you're currently doing in your code snippet. -
Create the PDF Document:
Use PDFsharp to create a PDF document and add a page to it. -
Draw the Barcode on the PDF:
You can use theXGraphics.DrawImage
method to insert the barcode image at the desired location on the PDF page.
Here's a simplified example of how you can achieve this:
// Generate the barcode using ZXing
BarcodeWriter writer = new BarcodeWriter()
{
Format = BarcodeFormat.CODE_128,
Options = new EncodingOptions
{
Height = 100,
Width = 200,
PureBarcode = false,
Margin = 10,
},
};
string itemnumber = "*" + textBox1.Text + "*";
var barcodeBitmap = writer.Write(itemnumber);
// 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);
// Draw the barcode on the PDF at the desired location
XImage barcodeImage = XImage.FromGdiPlusImage(barcodeBitmap);
gfx.DrawImage(barcodeImage, xPosition, yPosition, barcodeWidth, barcodeHeight);
// Save the PDF document
string filename = "Barcode.pdf";
document.Save(filename);
In the code above, replace xPosition
, yPosition
, barcodeWidth
, and barcodeHeight
with the specific coordinates and dimensions where you want to insert the barcode on the PDF.
This is a simplified example, and you may need to adjust it according to your exact requirements and integrate it into your existing code.
英文:
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:
答案1
得分: 2
你可以将图像保存到一个MemoryStream
中,然后使用该流在PDFsharp中绘制图像。
然后使用XImage.FromStream
而不是XImage.FromFile
。
英文:
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
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论