如何将条形码图像保存为PDF?

huangapple go评论51阅读模式
英文:

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:

  1. Generate the Barcode:
    Use ZXing to generate the barcode as you're currently doing in your code snippet.

  2. Create the PDF Document:
    Use PDFsharp to create a PDF document and add a page to it.

  3. Draw the Barcode on the PDF:
    You can use the XGraphics.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:

如何将条形码图像保存为PDF?

答案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.

huangapple
  • 本文由 发表于 2023年7月4日 20:03:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76612425.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定