翻译|使用教程|编辑:胡涛|2022-09-22 11:05:24.860|阅读 382 次
概述:本文将为你介绍如何在 C# 中从 PDF 读取条形码,欢迎查阅~
# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>
相关链接:
Aspose.PDF 是一款高级PDF处理API,可以在跨平台应用程序中轻松生成,修改,转换,呈现,保护和打印文档。无需使用Adobe Acrobat。此外,API提供压缩选项,表创建和处理,图形和图像功能,广泛的超链接功能,图章和水印任务,扩展的安全控件和自定义字体处理。本文将为你介绍如何在 C# 中从 PDF 读取条形码
我们可以生成条形码并将其添加到PDF文档中,如我之前的帖子中所述。在某些情况下,我们可能需要以编程方式检测和读取嵌入到 PDF 文档中的条形码。它有助于解码 PDF 文档(如发票、收据或报告)中的条形码和二维码形式的嵌入信息。在本文中,我们将学习如何使用 C# 从 PDF 文档中读取条形码。
我们将按照两步程序从 PDF 文档中读取条形码。首先,我们将使用Aspose.PDF for .NET API 加载 PDF 文档,然后将其页面渲染为光栅图像。之后,我们将使用Aspose.BarCode for .NET API 从渲染图像中读取条形码。
请下载 API 的 DLL或使用NuGet安装它。
PM> Install-Package Aspose.BarCode PM> Install-Package Aspose.PDF
Aspose.PDF API的类代表一个 PDF 文档。API的函数将 PDF 页面呈现为 PNG 内存流。Aspose.BarCode API的类使我们能够执行操作来检测条形码。BarCodeResult类存储检测的条码信息,例如条码类型、代码文本、区域和其他参数。
我们可以按照以下步骤读取嵌入在 PDF 文档任何页面上的条形码图像:
以下代码示例展示了如何使用 C# 从 PDF 文档中读取条形码。
// This code example demonstrates how to read a barcode from a PDF document using C#.
// The path to the document
string file = @"C:\Files\BarCode\sample-PDF-with-Barcodes.pdf";
// Load a PDF document
Aspose.Pdf.Document pdfDoc = new Aspose.Pdf.Document(file);
// Proceed all PDF pages starting from page 1
for (int i = 1; i <= pdfDoc.Pages.Count; ++i)
{
// Render PDF page to the stream
MemoryStream ms = pdfDoc.Pages[i].ConvertToPNGMemoryStream();
ms.Position = 0;
// Recognize barcodes from the rendered image of the page
BarCodeReader reader = new BarCodeReader(ms);
// Show results
foreach (BarCodeResult result in reader.ReadBarCodes())
{
Console.WriteLine("Codetext found: " + result.CodeText);
Console.WriteLine("Symbology: " + result.CodeType);
Console.WriteLine("-------------------------------");
}
}
Codetext found: Aspose.Barcode Pdf417 Example
Symbology: Pdf417
-------------------------------
Codetext found: Aspose.Barcode QR Example
Symbology: QR
-------------------------------
Codetext found: Aspose.Barcode DataMatrix Example
Symbology: DataMatrix
-------------------------------
请下载本博文中使用的带有条形码的输入 PDF 文档。
类将 PDF 页面转换为图像来从 PDF 文档中读取条形码。它允许将PDF文件的每一页转换为图像,然后我们将从转换后的图像中读取条形码信息。
我们可以按照以下步骤从转换后的 PDF 页面中读取条形码:
以下代码示例展示了如何使用 C# 将 PDF 页面转换为图像并读取条形码。
// The following code example shows how to convert PDF pages into images with PDF Convertor and read barcodes using C#.
// The path to the document
string file = @"C:\Files\BarCode\sample-PDF-with-Barcodes.pdf";
// Load a PDF document
Aspose.Pdf.Document pdfDoc = new Aspose.Pdf.Document(file);
// Initialize a PdfConvertor
Aspose.Pdf.Facades.PdfConverter pdfConverter = new Aspose.Pdf.Facades.PdfConverter(pdfDoc);
// Set barcode optimization
pdfConverter.RenderingOptions.BarcodeOptimization = true;
// Set page resolution
// 300 dpi is standard resolution
pdfConverter.Resolution = new Aspose.Pdf.Devices.Resolution(300);
// Set all pages to render into images
pdfConverter.StartPage = 1; //starts from page 1
pdfConverter.EndPage = pdfConverter.Document.Pages.Count;
// Render selected pages into the images
pdfConverter.DoConvert();
while (pdfConverter.HasNextImage())
{
// Render current page to memory stream image
MemoryStream ms = new MemoryStream();
pdfConverter.GetNextImage(ms, ImageFormat.Png);
ms.Position = 0;
// Recognize barcodes from the rendered image of the page
BarCodeReader reader = new BarCodeReader(ms);
// Show results
foreach (BarCodeResult result in reader.ReadBarCodes())
{
Console.WriteLine("Codetext found: " + result.CodeText);
Console.WriteLine("Symbology: " + result.CodeType);
Console.WriteLine("-------------------------------");
}
}
这是另一种类似于前一种的方法。唯一不同的是,在这个方法中,我们将使用 API 的类将 PDF 文档的页面转换为图像。它允许将 PDF 文档的页面转换为 PNG 图像。
我们可以按照以下步骤将转换后的 PDF 页面中的条形码读取为 PNG 图像:
以下代码示例展示了如何将 PDF 页面转换为 PNG 图像并使用 C# 读取条形码。
// The following code example shows how to convert PDF pages into images with PDF Convertor and read barcodes using C#.
// The path to the document
string file = @"C:\Files\BarCode\sample-PDF-with-Barcodes.pdf";
// Load a PDF document
Aspose.Pdf.Document pdfDoc = new Aspose.Pdf.Document(file);
// Initialize a PdfConvertor
Aspose.Pdf.Facades.PdfConverter pdfConverter = new Aspose.Pdf.Facades.PdfConverter(pdfDoc);
// Set barcode optimization
pdfConverter.RenderingOptions.BarcodeOptimization = true;
// Set page resolution
// 300 dpi is standard resolution
pdfConverter.Resolution = new Aspose.Pdf.Devices.Resolution(300);
// Set all pages to render into images
pdfConverter.StartPage = 1; //starts from page 1
pdfConverter.EndPage = pdfConverter.Document.Pages.Count;
// Render selected pages into the images
pdfConverter.DoConvert();
while (pdfConverter.HasNextImage())
{
// Render current page to memory stream image
MemoryStream ms = new MemoryStream();
pdfConverter.GetNextImage(ms, ImageFormat.Png);
ms.Position = 0;
// Recognize barcodes from the rendered image of the page
BarCodeReader reader = new BarCodeReader(ms);
// Show results
foreach (BarCodeResult result in reader.ReadBarCodes())
{
Console.WriteLine("Codetext found: " + result.CodeText);
Console.WriteLine("Symbology: " + result.CodeType);
Console.WriteLine("-------------------------------");
}
}
我们还可以使用类识别嵌入在 PDF 页面上的条形码图像。它允许从 PDF 中提取图像,然后我们将从提取的图像中读取条形码信息。
我们可以按照以下步骤从提取的图像中读取条形码:
以下代码示例展示了如何使用 C# 从 PDF 文档中提取和读取条形码图像。
// The following code example shows how to convert PDF pages into images with PdfExtractor and read barcodes using C#.
// The path to the document
string file = @"C:\Files\BarCode\sample-PDF-with-Barcodes.pdf";
// Bind a PDF document
Aspose.Pdf.Facades.PdfExtractor pdfExtractor = new Aspose.Pdf.Facades.PdfExtractor();
pdfExtractor.BindPdf(file);
// Set page range for image extraction
pdfExtractor.StartPage = 1;
pdfExtractor.EndPage = 3;
// Extract the images
pdfExtractor.ExtractImage();
// Save images to stream in a loop
while (pdfExtractor.HasNextImage())
{
// Save image to stream
MemoryStream imageStream = new MemoryStream();
pdfExtractor.GetNextImage(imageStream);
imageStream.Position = 0;
// Recognize the barcodes from the image stream above
BarCodeReader reader = new BarCodeReader(imageStream);
foreach (BarCodeResult result in reader.ReadBarCodes())
{
Console.WriteLine("Codetext found: " + result.CodeText);
Console.WriteLine("Symbology: " + result.CodeType);
Console.WriteLine("-------------------------------");
}
}
我们还可以使用类从 PDF 文档中查找和提取条形码图像。它表示图像放置对象的吸收体对象。它执行图像使用搜索,并通过 ImagePlacements 集合提供对搜索结果的访问。此方法有助于识别具有原始分辨率的条码。它可能无法正确识别矢量格式。
我们可以按照以下步骤从 PDF 文档中查找和读取条形码:
以下代码示例展示了如何使用 C# 从 PDF 中查找和读取条形码图像。
// This code example demonstrates how to read a barcode from a PDF document using ImagePlacementAbsorber.
// The path to the document
string file = @"C:\Files\BarCode\sample-PDF-with-Barcodes.pdf";
// Load a PDF document
Aspose.Pdf.Document pdfDoc = new Aspose.Pdf.Document(file);
// Initialize ImagePlacementAbsorber
Aspose.Pdf.ImagePlacementAbsorber imagePlacementAbsorber = new Aspose.Pdf.ImagePlacementAbsorber();
// Process all PDF pages in the document starting from page 1
for (int i = 1; i <= pdfDoc.Pages.Count; ++i)
{
// Visit the page create an image extractor
imagePlacementAbsorber.Visit(pdfDoc.Pages[i]);
// Extract all images from the PDF page
foreach (Aspose.Pdf.ImagePlacement imagePlacement in imagePlacementAbsorber.ImagePlacements)
{
// Convert an image from the PDF page to the stream
MemoryStream ms = new MemoryStream();
imagePlacement.Save(ms, ImageFormat.Png);
ms.Position = 0;
// Recognize barcode from extracted image of the page
BarCodeReader reader = new BarCodeReader(ms);
// Show results
foreach (BarCodeResult result in reader.ReadBarCodes())
{
Console.WriteLine("Codetext found: " + result.CodeText);
Console.WriteLine("Symbology: " + result.CodeType);
Console.WriteLine("-------------------------------");
}
}
}
以上便是如何在 C# 中从 PDF 读取条形码 ,要是您还有其他关于产品方面的问题,欢迎咨询我们,或者加入我们官方技术交流群。
欢迎下载|体验更多Aspose产品
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@ke049m.cn