Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
Aspose.PDF for .NET是一个非常强大的PDF文档管理产品。它使得将PDF文档中的页面转换为图像变得简单。Aspose.BarCode for .NET同样是一个强大的生成和识别条形码的产品。
Aspose.Pdf.Facades命名空间下的PdfConverter类支持将PDF页面转换为各种图像格式。Aspose.Pdf.Devices命名空间下的PngDevice支持将PDF页面转换为PNG文件。这两个类都可以用于将PDF文件的页面转换为图像。
当页面被转换为图像格式后,我们可以使用Aspose.BarCode for .NET来识别其中的条形码。以下代码示例展示了如何使用PdfConverter或PngDevice转换页面。
PdfConverter类包含一个名为GetNextImage的方法,该方法从当前PDF页面生成图像。为了指定输出图像格式,此方法接受来自System.Drawing.Imaging.ImageFormat枚举的参数。
Aspose.Barcode包含一个命名空间BarCodeRecognition,其中包含BarCodeReader类。BarCodeReader类允许您从图像文件中读取、确定和识别条形码。
为了本示例的目的,首先使用Aspose.Pdf.Facades.PdfConverter将PDF文件中的一页转换为图像。然后使用Aspose.BarCodeRecognition.BarCodeReader类识别图像中的条形码。
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void IdentifyBarcodesConverter()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion();
// Create a PdfConverter object
var converter = new Aspose.Pdf.Facades.PdfConverter();
// Bind PDF document
converter.BindPdf(dataDir + "IdentifyBarcodes.pdf");
// Specify the start page to be processed
converter.StartPage = 1;
// Specify the end page for processing
converter.EndPage = 1;
// Create a Resolution object to specify the resolution of resultant image
converter.Resolution = new Aspose.Pdf.Devices.Resolution(300);
// Initialize the convertion process
converter.DoConvert();
// Create a MemoryStream object to hold the resultant image
using (var imageStream = new MemoryStream())
{
// Check if pages exist and then convert to image one by one
while (converter.HasNextImage())
{
// Save the image in the given image Format
converter.GetNextImage(imageStream, System.Drawing.Imaging.ImageFormat.Png);
// Set the stream position to the beginning of the stream
imageStream.Position = 0;
// Instantiate a BarCodeReader object
var barcodeReader = new Aspose.BarCodeRecognition.BarCodeReader(imageStream, Aspose.BarCodeRecognition.BarCodeReadType.Code39Extended);
// String txtResult.Text = "";
while (barcodeReader.Read())
{
// Get the barcode text from the barcode image
var code = barcodeReader.GetCodeText();
// Write the barcode text to Console output
Console.WriteLine("BARCODE : " + code);
}
// Close the BarCodeReader object to release the image file
barcodeReader.Close();
}
// Close the PdfConverter instance and release the resources
converter.Close();
}
}
在Aspose.Pdf.Devices中,有PngDevice类。此类允许您将PDF文档中的页面转换为PNG图像。
为了本示例的目的,将源PDF文件加载到Document中,并将文档的页面转换为PNG图像。当图像创建完成后,使用Aspose.BarCodeRecognition下的BarCodeReader类识别和读取图像中的条形码。
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void IdentifyBarcodes()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "IdentifyBarcodes.pdf"))
{
// Traverse through the individual pages of the PDF file
for (int pageCount = 1; pageCount <= document.Pages.Count; pageCount++)
{
using (var imageStream = new MemoryStream())
{
// Create a Resolution object
var resolution = new Aspose.Pdf.Devices.Resolution(300);
// Instantiate a PngDevice object while passing a Resolution object as an argument to its constructor
var pngDevice = new Aspose.Pdf.Devices.PngDevice(resolution);
// Convert a particular page and save the image to stream
pngDevice.Process(document.Pages[pageCount], imageStream);
// Set the stream position to the beginning of Stream
imageStream.Position = 0;
// Instantiate a BarCodeReader object
var barcodeReader = new Aspose.BarCodeRecognition.BarCodeReader(imageStream, Aspose.BarCodeRecognition.BarCodeReadType.Code39Extended);
// String txtResult.Text = "";
while (barcodeReader.Read())
{
// Get the barcode text from the barcode image
var code = barcodeReader.GetCodeText();
// Write the barcode text to Console output
Console.WriteLine("BARCODE : " + code);
}
// Close the BarCodeReader object to release the image file
barcodeReader.Close();
}
}
}
}
有关本文中涵盖主题的更多信息,请访问:
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.