从 DOCX 文档中扫描条形码是从事自动化任务、数据提取或文档处理工作流的开发人员的必备功能。使用解析器库,您可以轻松地使用 C# 从 DOCX 扫描条形码并无缝提取条形码数据。本文将逐步指导您如何实现条形码提取。解析器库简化了从 DOCX 文件中提取各种类型内容(例如文本、元数据、图像和条形码)的过程。使用它,您可以高效地使用 C# 从 DOCX 中提取条形码,而无需额外的第三方工具。以下步骤显示如何从 DOCX 文件中扫描条形码。
使用 C# 从 DOCX 扫描条形码的步骤
- 首先设置您的开发工作区并合并 GroupDocs.Parser for .NET 库,以便从 DOCX 文件中提取条形码
- 在初始化期间提供 DOCX 文件的路径,创建 Parser 的实例
- 在 Parser 实例上调用 GetBarcodes 方法以获取一组 PageBarcodeArea 对象
- 最后,遍历该集合以检索和利用条形码值
借助解析器库,该过程具有高度的通用性,并支持在各种操作系统(包括 Windows、macOS 和 Linux)上使用 DOCX C# 提取条形码。.NET 框架确保了一致的开发环境,无论在哪个平台上都可以轻松实现。设置所需的库并配置文件路径后,将代码集成到您的项目中将变得简单而高效。下面的代码示例展示了如何从 DOCX 读取条形码。
使用 C# 从 DOCX 扫描条形码的代码
using GroupDocs.Parser; | |
using GroupDocs.Parser.Data; | |
using System; | |
using System.Collections.Generic; | |
namespace ScanBarcodefromDOCXusingCSharp | |
{ | |
internal class Program | |
{ | |
static void Main(string[] args) | |
{ | |
// Set License to avoid the limitations of Parser library | |
License lic = new License(); | |
lic.SetLicense(@"GroupDocs.Parser.lic"); | |
// Create an instance of Parser class | |
using (Parser parser = new Parser("input.docx")) | |
{ | |
// Check if the file supports barcodes extraction | |
if (!parser.Features.Barcodes) | |
{ | |
Console.WriteLine("The file doesn't support barcodes extraction."); | |
return; | |
} | |
// Scan barcodes from the DOCX | |
IEnumerable<PageBarcodeArea> barcodes = parser.GetBarcodes(); | |
// Iterate over barcodes | |
foreach (PageBarcodeArea barcode in barcodes) | |
{ | |
// Print the page index | |
Console.WriteLine("Page: " + (barcode.Page.Index + 1)); | |
// Print the barcode value | |
Console.WriteLine("Value: " + barcode.Value); | |
} | |
} | |
} | |
} | |
} |
总之,C# 从 DOCX 读取条形码 是一个高效且直接的过程,可显著增强文档自动化和数据管理工作流程。通过利用 Parser 库,开发人员可以轻松地从各种平台的 DOCX 文件中提取条形码,从而简化库存管理、内容处理和自动报告等任务。只需进行最少的设置和集成,将条形码扫描功能整合到您的应用程序中不仅可以提高性能,还可以增加处理文档内结构化数据的宝贵功能。
之前,我们分享了如何使用 C# 从 PDF 扫描条形码的指南。如需更详细的说明,请务必查看我们的完整教程,了解如何 使用 C# 从 PDF 扫描条形码。