Skip to content

Instantly share code, notes, and snippets.

@conholdate-gists
Last active August 15, 2022 17:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save conholdate-gists/cf9590649dfc71d60384207ef53862f2 to your computer and use it in GitHub Desktop.
Save conholdate-gists/cf9590649dfc71d60384207ef53862f2 to your computer and use it in GitHub Desktop.
Create BarCode using C#
int Resolution = 300;//300 dpi high resolution of the barcode image
int leftBarcodePosition = 10;//left position of the barcode image
int topBarcodePosition = 20;//top position of the barcode image
//create a PDF document with a new page by creating an instance of Aspose.Pdf.Document() class
Aspose.Pdf.Document pdfDoc = new Aspose.Pdf.Document();
Aspose.Pdf.Page pdfPage = pdfDoc.Pages.Add();
//convert the barcode image to a PNG stream by instantiating an instance of BarcodeGenerator class
BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.Pdf417, "Aspose.Barcode Example");
// Invoke the Resolution property to set the barcode image resolution
generator.Parameters.Resolution = Resolution;
// Generate the barcode image by calling GenerateBarCodeImage method and assign to the Bitmap object
Bitmap image = generator.GenerateBarCodeImage();
// Initialize an object of MemoryStream class
MemoryStream imageStream = new MemoryStream();
// Save barcode image to stream in PNG format.
generator.Save(imageStream, BarCodeImageFormat.Png);
imageStream.Position = 0;
Rectangle imageRect = new Rectangle(leftBarcodePosition, topBarcodePosition, (image.Width * 72) / Resolution, (image.Height * 72) / Resolution);
//Create an object of Rectangle where the image will be placed in the top/left corner
Aspose.Pdf.Rectangle pdfRect = new Aspose.Pdf.Rectangle(imageRect.Left, pdfPage.Rect.Height - imageRect.Bottom, imageRect.Right, pdfPage.Rect.Height - imageRect.Top);
//add the image to the created PDF page by calling the AddImage method
pdfPage.AddImage(imageStream, pdfRect);
//Invoke the save method to save the PDF document
pdfDoc.Save("AddBarcodeToPDFDocumentDirectly.pdf");
// Instantiate an instance of the Document class to create a PDF document
using (Aspose.Pdf.Document pdfDoc = new Aspose.Pdf.Document("AddBarcodeToPDFDocumentDirectly.pdf"))
{
// Initialize the constructor of the PdfConverter class with a PDF file
Aspose.Pdf.Facades.PdfConverter pdfConverter = new Aspose.Pdf.Facades.PdfConverter(pdfDoc);
// set the barcode optimization mode by setting the BarcodeOptimization property
pdfConverter.RenderingOptions.BarcodeOptimization = true;
//set resolution to the page by setting the Resolution property
pdfConverter.Resolution = new Aspose.Pdf.Devices.Resolution(300);
//set all pages to render into images starting from 1st page
pdfConverter.StartPage = 1;
pdfConverter.EndPage = pdfConverter.Document.Pages.Count;
// Call this DoConvert method to render selected pages to the images
pdfConverter.DoConvert();
while (pdfConverter.HasNextImage())
{
//render current page to memory stream as png image
MemoryStream ms = new MemoryStream();
pdfConverter.GetNextImage(ms, Aspose.Pdf.PageSize.A0);
ms.Position = 0;
//Initializes a new instance of the BarCodeReader class with default values to detect barcodes.
BarCodeReader reader = new BarCodeReader(ms, DecodeType.Pdf417, DecodeType.QR, DecodeType.DataMatrix);
foreach (BarCodeResult result in reader.ReadBarCodes())
Console.WriteLine($"Barcode type:{result.CodeTypeName}, Barcode Data:{result.CodeText}");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment