在当今的数字世界中,二维码越来越多地用于各种应用,包括营销、支付和数据共享。如果您正在处理 DOCX 文件并需要使用 C# 从 DOCX 扫描二维码,则可以使用 GroupDocs.Parser 库轻松实现。在本文中,我们将指导您完成此过程以及示例代码。本文讨论的库是一个强大的工具,用于解析和提取多种文档格式(包括 DOCX)中的数据。它提供了广泛的功能,可简化从文档中提取条形码(包括二维码)的过程,而无需任何其他软件。在实现此功能之前,请确保您的计算机上安装了 Visual Studio 并为 C# 开发设置了 .NET 环境。以下关键步骤解释了如何在 C# 中从 DOCX 中提取二维码的过程。
使用 C# 从 DOCX 扫描二维码的步骤
- 设置您的开发环境并将 GroupDocs.Parser for .NET 库添加到您的项目中,以便从 DOCX 扫描二维码
- 通过将 DOCX 文件的路径作为其构造函数的参数来创建 Parser 对象
- 使用 Parser.GetBarcodes 方法检索 PageBarcodeArea 集合以获取QR”类型的条形码
- 最后,循环遍历过滤后的条形码集合,以访问并提取 DOCX 文件中包含的二维码数据
Parser 库提供了一种高度通用的解决方案,有助于在多个操作系统(包括 Windows、macOS 和 Linux)上从 DOCX C# 中提取二维码。通过利用 .NET,开发人员可以维护一致的开发环境,无论选择哪个平台,都可以简化实施。一旦安装了必要的库并配置了适当的文件路径,将代码集成到项目中就变得简单而高效。以下代码示例演示了如何有效地从 DOCX 文件中读取二维码。
使用 C# 从 DOCX 扫描二维码的代码
using GroupDocs.Parser; | |
using GroupDocs.Parser.Data; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace ScanQRCodefromDOCXusingCSharp | |
{ | |
internal class Program | |
{ | |
static void Main(string[] args) | |
{ | |
// Apply the license to remove the restrictions imposed | |
// by the Parser library | |
License lic = new License(); | |
lic.SetLicense(@"GroupDocs.Parser.lic"); | |
// Instantiate an object of the Parser class to enable access to its | |
// methods and properties for processing or manipulating data | |
using (Parser parser = new Parser("input.docx")) | |
{ | |
// Verify if the file is compatible for QR extraction | |
if (!parser.Features.Barcodes) | |
{ | |
Console.WriteLine("The file doesn't support QR extraction."); | |
return; | |
} | |
// Scan and extract only the barcodes of type "QR" from your file | |
IEnumerable<PageBarcodeArea> qrcodes = parser.GetBarcodes() | |
.Where(i => i.CodeTypeName == "QR"); | |
// Iterate over QR codes | |
foreach (PageBarcodeArea qrcode in qrcodes) | |
{ | |
// Print the page index | |
Console.WriteLine("Page: " + (qrcode.Page.Index + 1)); | |
// Print the barcode value | |
Console.WriteLine("Value: " + qrcode.Value); | |
} | |
} | |
} | |
} | |
} |
借助解析器库,C# 从 DOCX 文档读取二维码既高效又易于跨各种平台实现。该库的强大功能使开发人员能够专注于核心应用程序逻辑,同时确保无缝提取条形码。通过利用此过程,您可以显著增强应用程序处理 DOCX 文件和提取二维码中编码的有价值数据的能力。无论您是构建数据自动化解决方案还是文档管理解决方案,此库都是可靠的,可在任何平台上扫描二维码。
之前,我们提供了使用 C# 从 PDF 扫描二维码的指南。如需全面解释,请不要错过我们关于如何 使用 C# 从 PDF 扫描二维码 的完整教程。