PDF处理控件pdf功能演示:在 C# 中从 PDF 读取条形码

1、C# API 从 PDF 读取条形码

我们将按照两步程序从 PDF 文档中读取条形码。首先,我们将使用Aspose.PDF for .NET API 加载 PDF 文档,然后将其页面渲染为光栅图像。之后,我们将使用Aspose.BarCode for .NET API 从渲染图像中读取条形码。

请下载 API 的 DLL或使用NuGet安装它。

PM> Install-Package Aspose.BarCodePM> Install-Package Aspose.PDF
2、使用 C# 从 PDF 读取条形码

Aspose.PDF API的Document类代表一个 PDF 文档。API的ConvertToPNGMemoryStream()函数将 PDF 页面呈现为 PNG 内存流。Aspose.BarCode API的BarCodeReader类使我们能够执行ReadBarCodes操作来检测条形码。BarCodeResult类存储检测到的条码信息,例如条码类型、代码文本、区域和其他参数。

我们可以按照以下步骤读取嵌入在 PDF 文档任何页面上的条形码图像:

  1. 首先,使用Document类加载 PDF 文档。
  2. 接下来,遍历所有页面并将它们呈现到内存流中。
  3. 然后,使用流对象创建BarCodeReader类的实例。
  4. 之后,调用ReadBarCodes()方法获取BarCodeResult对象。
  5. 最后,显示条码信息。

以下代码示例展示了如何使用 C# 从 PDF 文档中读取条形码

// This code example demonstrates how to read a barcode from a PDF document using C#.// The path to the documentstring file = @"C:FilesBarCodesample-PDF-with-Barcodes.pdf";// Load a PDF documentAspose.Pdf.Document pdfDoc = new Aspose.Pdf.Document(file);// Proceed all PDF pages starting from page 1for (int i = 1; i <= pdfDoc.Pages.Count; ++i){// Render PDF page to the streamMemoryStream ms = pdfDoc.Pages[i].ConvertToPNGMemoryStream();ms.Position = 0;// Recognize barcodes from the rendered image of the pageBarCodeReader reader = new BarCodeReader(ms);// Show resultsforeach (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 文档。

3、使用 C# 将 PDF 转换为图像并读取条形码

我们可以通过使用PdfConverter类将 PDF 页面转换为图像来从 PDF 文档中读取条形码。它允许将PDF文件的每一页转换为图像,然后我们将从转换后的图像中读取条形码信息。

我们可以按照以下步骤从转换后的 PDF 页面中读取条形码:

  1. 首先,使用Document类加载 PDF 文档。
  2. 接下来,创建PdfConverter类的实例。
  3. (可选)设置呈现选项,例如BarcodeOptimization
  4. 然后,设置图像分辨率。
  5. 接下来,指定StartPageEndPage以将一系列页面呈现为图像。
  6. 然后,调用DoConvert()方法将所选页面呈现为图像。
  7. 接下来,保存图像以循环播放。
  8. 然后,使用流对象创建BarCodeReader类的实例。
  9. 之后,调用ReadBarCodes()方法获取BarCodeResult对象。
  10. 最后,显示条码信息。

以下代码示例展示了如何使用 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 documentstring file = @"C:FilesBarCodesample-PDF-with-Barcodes.pdf";// Load a PDF documentAspose.Pdf.Document pdfDoc = new Aspose.Pdf.Document(file);// Initialize a PdfConvertorAspose.Pdf.Facades.PdfConverter pdfConverter = new Aspose.Pdf.Facades.PdfConverter(pdfDoc);// Set barcode optimizationpdfConverter.RenderingOptions.BarcodeOptimization = true;// Set page resolution// 300 dpi is standard resolutionpdfConverter.Resolution = new Aspose.Pdf.Devices.Resolution(300);// Set all pages to render into imagespdfConverter.StartPage = 1; //starts from page 1pdfConverter.EndPage = pdfConverter.Document.Pages.Count;// Render selected pages into the imagespdfConverter.DoConvert();while (pdfConverter.HasNextImage()){// Render current page to memory stream imageMemoryStream ms = new MemoryStream();pdfConverter.GetNextImage(ms, ImageFormat.Png);ms.Position = 0;// Recognize barcodes from the rendered image of the pageBarCodeReader reader = new BarCodeReader(ms);// Show resultsforeach (BarCodeResult result in reader.ReadBarCodes()){Console.WriteLine("Codetext found: " + result.CodeText);Console.WriteLine("Symbology: " + result.CodeType);Console.WriteLine("-------------------------------");}}
4、在 C# 中使用 PngDevice 从 PDF 读取条形码

这是另一种类似于前一种的方法。唯一不同的是,在这个方法中,我们将使用 API 的PngDevice类将 PDF 文档的页面转换为图像。它允许将 PDF 文档的页面转换为 PNG 图像。

我们可以按照以下步骤将转换后的 PDF 页面中的条形码读取为 PNG 图像:

  1. 首先,使用Document类加载 PDF 文档。
  2. 接下来,创建PngDevice类的实例。
  3. 然后,遍历所有页面并调用Process()方法以渲染到流中。
  4. 接下来,使用流对象创建BarCodeReader类的实例。
  5. 之后,调用ReadBarCodes()方法获取BarCodeResult对象。
  6. 最后,显示条码信息。

以下代码示例展示了如何将 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 documentstring file = @"C:FilesBarCodesample-PDF-with-Barcodes.pdf";// Load a PDF documentAspose.Pdf.Document pdfDoc = new Aspose.Pdf.Document(file);// Initialize a PdfConvertorAspose.Pdf.Facades.PdfConverter pdfConverter = new Aspose.Pdf.Facades.PdfConverter(pdfDoc);// Set barcode optimizationpdfConverter.RenderingOptions.BarcodeOptimization = true;// Set page resolution// 300 dpi is standard resolutionpdfConverter.Resolution = new Aspose.Pdf.Devices.Resolution(300);// Set all pages to render into imagespdfConverter.StartPage = 1; //starts from page 1pdfConverter.EndPage = pdfConverter.Document.Pages.Count;// Render selected pages into the imagespdfConverter.DoConvert();while (pdfConverter.HasNextImage()){// Render current page to memory stream imageMemoryStream ms = new MemoryStream();pdfConverter.GetNextImage(ms, ImageFormat.Png);ms.Position = 0;// Recognize barcodes from the rendered image of the pageBarCodeReader reader = new BarCodeReader(ms);// Show resultsforeach (BarCodeResult result in reader.ReadBarCodes()){Console.WriteLine("Codetext found: " + result.CodeText);Console.WriteLine("Symbology: " + result.CodeType);Console.WriteLine("-------------------------------");}}
5、使用 C# 从 PDF 中提取和读取条形码

我们还可以使用PdfExtractor类识别嵌入在 PDF 页面上的条形码图像。它允许从 PDF 中提取图像,然后我们将从提取的图像中读取条形码信息。

我们可以按照以下步骤从提取的图像中读取条形码:

  1. 首先,创建PdfExtractor类的实例。
  2. 接下来,使用BindPdf()方法绑定输入的 PDF 文档。
  3. 然后,设置图像提取的页面范围。
  4. 接下来,调用ExtractImage()方法来提取图像。
  5. 然后,保存图像以循环播放。
  6. 接下来,使用流对象创建BarCodeReader类的实例。
  7. 之后,调用ReadBarCodes()方法获取BarCodeResult对象。
  8. 最后,显示条码信息。

以下代码示例展示了如何使用 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 documentstring file = @"C:FilesBarCodesample-PDF-with-Barcodes.pdf";// Bind a PDF documentAspose.Pdf.Facades.PdfExtractor pdfExtractor = new Aspose.Pdf.Facades.PdfExtractor();pdfExtractor.BindPdf(file);// Set page range for image extractionpdfExtractor.StartPage = 1;pdfExtractor.EndPage = 3;// Extract the imagespdfExtractor.ExtractImage();// Save images to stream in a loopwhile (pdfExtractor.HasNextImage()){// Save image to streamMemoryStream imageStream = new MemoryStream();pdfExtractor.GetNextImage(imageStream);imageStream.Position = 0;// Recognize the barcodes from the image stream aboveBarCodeReader reader = new BarCodeReader(imageStream);foreach (BarCodeResult result in reader.ReadBarCodes()){Console.WriteLine("Codetext found: " + result.CodeText);Console.WriteLine("Symbology: " + result.CodeType);Console.WriteLine("-------------------------------");}}

6、使用 C# 从 PDF 中查找和读取条形码图像

我们还可以使用ImagePlacementAbsorber类从 PDF 文档中查找和提取条形码图像。它表示图像放置对象的吸收体对象。它执行图像使用搜索,并通过 ImagePlacements 集合提供对搜索结果的访问。此方法有助于识别具有原始分辨率的条码。它可能无法正确识别矢量格式。

我们可以按照以下步骤从 PDF 文档中查找和读取条形码:

  1. 首先,使用Document类加载 PDF 文档。
  2. 接下来,创建ImagePlacementAbsorber类的实例。
  3. 然后,在循环中为每个页面调用Visit()方法。
  4. 接下来,遍历ImagePlacements集合中找到的所有图像。
  5. 然后,将图像保存到内存流中。
  6. 接下来,使用流对象创建BarCodeReader类的实例。
  7. 之后,调用ReadBarCodes()方法获取BarCodeResult对象。
  8. 最后,显示条码信息。

以下代码示例展示了如何使用 C# 从 PDF 中查找和读取条形码图像

// This code example demonstrates how to read a barcode from a PDF document using ImagePlacementAbsorber.// The path to the documentstring file = @"C:FilesBarCodesample-PDF-with-Barcodes.pdf";// Load a PDF documentAspose.Pdf.Document pdfDoc = new Aspose.Pdf.Document(file);// Initialize ImagePlacementAbsorberAspose.Pdf.ImagePlacementAbsorber imagePlacementAbsorber = new Aspose.Pdf.ImagePlacementAbsorber();// Process all PDF pages in the document starting from page 1for (int i = 1; i <= pdfDoc.Pages.Count; ++i){// Visit the page create an image extractorimagePlacementAbsorber.Visit(pdfDoc.Pages[i]);// Extract all images from the PDF pageforeach (Aspose.Pdf.ImagePlacement imagePlacement in imagePlacementAbsorber.ImagePlacements){// Convert an image from the PDF page to the streamMemoryStream ms = new MemoryStream();imagePlacement.Save(ms, ImageFormat.Png);ms.Position = 0;// Recognize barcode from extracted image of the pageBarCodeReader reader = new BarCodeReader(ms);// Show resultsforeach (BarCodeResult result in reader.ReadBarCodes()){Console.WriteLine("Codetext found: " + result.CodeText);Console.WriteLine("Symbology: " + result.CodeType);Console.WriteLine("-------------------------------");}}}

以上便是如何在 C# 中从 PDF 读取条形码,要是您还有其他关于产品方面的问题,欢迎咨询我们,或者加入我们官方技术交流群。


欢迎下载|体验更多Aspose产品

获取更多信息请咨询在线客服 或 加入Aspose技术交流群(
标签:

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2022年8月19日
下一篇 2022年8月19日

相关推荐

发表回复

登录后才能评论