Create Barcode in Word Documents using C#

条码是数据的图形代表,通常由平行线、点或直角组成,编码基本信息. 它们在各个行业广泛使用,以嵌入产品细节、跟踪库存和管理产品流动。 在许多场景中,您可能需要 添加一个动态条代码到Word文档使用C#多克斯博士. 在本文中,我们将探讨如何在Word文档中创建条形码,使用C#与Aspose。

内容表

C# API 在 Word 文档中创建条形码

要在 ASP.NET MVC 中使用 C# 从 Word 文档中阅读条形码数据,我们将采用两步方法。 ASPOSE.Words 为 .NET API 创建或加载 Word 文档. 接下来,我们将使用 Aspose.BarCode 为 .NET 火焰 - The 文件 在 Aspose.Words 中的类,您可以创建新的 Word 文件或打开现有文件。 保存( ) 方法方便保存文档。 文档编辑 课堂提供构建文件的方法,包括 主持人( ) 插入图像的方法。

Aspose.BarCode for .NET API 支持多种 条形码类型要创建条形码,它提供 Barcode 发电机 班级,需要 编码类型 并将文本编码为参数. 创建的条码可以通过 保存( ) 此分類上一篇: API 包含 BarCodeImage格式 列表指定保存格式,以及 编辑 BarCodeReader 课程可用于从图像中阅读条形码。

你也可以 下载 DLLs 通过 API 或安装它们 诺基亚:

PM> Install-Package Aspose.BarCode
PM> Install-Package Aspose.Words

创建和添加字符代码到 Word 文档在 C# {# 创建和添加字符代码到 Word 文档到 CSharp}

要在 Word 文档中使用 C# .NET 创建条形码,请遵循以下步骤:

  • 创建一个例子的 **** 类与 標籤 * 標籤 编码文本作为论点。
  • 启动一个记忆流对象。
  • 呼叫 **** 方法,以保存条码图像到记忆流。
  • 创建一个新的例子 **** 类。
  • 与文档对象开始一个 **** 例子。
  • 使用 **** 方法,使用记忆流输入条码图像。
  • 最后,呼叫 **** 方法,指定输出 DOCX 文件路径。

下面的代码示例 如何有效地在Word文档中创建条形码,使用C# .NET:

// This code example demonstrates how to Generate and Add a barcode image to Word document.
// The path to the documents directory.
string dataDir = @"C:\Files\BarCode\";
// Instantiate linear barcode object, Set the Code text and symbology type for the barcode
BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.Code39Standard, "1234567890");
// Creating memory stream and save barcode image to memory stream
Stream ms = new MemoryStream();
generator.Save(ms, BarCodeImageFormat.Bmp);
// Create a new Word document
Aspose.Words.Document doc = new Aspose.Words.Document();
// Create builder for document object
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert the barCode image into the document
builder.InsertImage(ms);
// Save the word document
doc.Save(dataDir + "Sample_out.docx");
Generate and add Barcode to a new Word document in C.

Generate and add Barcode to a new Word document in C#.

将条码添加到现有 Word 文档中的 C# {# 添加条码到现有 Word 文档到 CSharp}

要使用 C# 添加到现有 Word 文档的条形码,请遵循以下步骤:

  • 即时启动 **** 类,提供 標籤 * 標籤 文本要编码。
  • 创建一个记忆流对象。
  • 将条码图像存储到记忆流中使用 保存( ).
  • 加载现有 Word 文档与 **** 类。
  • 与文档对象开始一个 **** 例子。
  • 使用 **** 方法和内存流输入条码图像。
  • 使用所需的 DOCX 文件路径拨打 **** 方法。

下面的代码示例 如何在Word文档中创建条形码,使用C# .NET:

// This code example demonstrates how to Generate and Add a barcode image to an existing Word document.
// The path to the documents directory.
string dataDir = @"C:\Files\BarCode\";
// Instantiate linear barcode object, Set the Code text and symbology type for the barcode
BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.Codabar, "1234567890");
// Creating memory stream and save barcode image to memory stream
Stream ms = new MemoryStream();
generator.Save(ms, BarCodeImageFormat.Bmp);
// Create a new Word document
Document doc = new Document(dataDir + "sample.docx");
// Create builder for document object
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert the barCode image into the document
builder.InsertImage(ms,
RelativeHorizontalPosition.Margin,
0,
RelativeVerticalPosition.Margin,
400,
200,
100,
WrapType.Square);
// Save the word document
doc.Save(dataDir + "Sample_out.docx");
Add Barcode to Existing Word Document in C#.

Add Barcode to Existing Word Document in C#.

将 QR 代码添加到 Word 文档中使用 C# {# 添加 QR 代码到 Word 文档使用 CSharp}

要将 QR 代码图像添加到 Word 文档中,请遵循以下步骤:

  • 创建一个 类的例子,并将 设置为 或 .
  • 按照上述步骤保存并输入图像。

下面的例子描述了如何使用 C# .NET 将 QR 代码添加到 Word 文档中:

// This code example demonstrates how to Generate and Add QR code to an existing Word document.
// The path to the documents directory.
string dataDir = @"C:\Files\BarCode\";
// Instantiate linear barcode object, Set the Code text and symbology type for the barcode
BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.QR, "1234567");
// Creating memory stream and save barcode image to memory stream
Stream ms = new MemoryStream();
generator.Save(ms, BarCodeImageFormat.Bmp);
// Create a new Word document
Document doc = new Document(dataDir + "sample.docx");
// Create builder for document object
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert the barCode image into the document
builder.InsertImage(ms,
RelativeHorizontalPosition.Margin,
400,
RelativeVerticalPosition.Margin,
-50,
100,
100,
WrapType.Square);
// Save the word document
doc.Save(dataDir + "Sample_qr_out.docx");
Add QR Code to Word Document using C#.

Add QR Code to Word Document using C#.

从 Word 文档中阅读条形码使用 C#

使用 C# 从 Word 文档中阅读条形码数据:

  • 加载现有 Word 文档与 **** 类。
  • 接入 **** 的 形状 使用 ****方法的类型。
  • 走过形状并检查形状是否是一个图像。
  • 将图像保存到流中。
  • 使用图像流和 **** 参数创建一个 *** * 示例。
  • 打电话 **** 方法获取 ****.
  • 显示条码信息。

下面的代码示例 如何从Word文档中阅读条形码数据,使用Aspose C#:

// This code example demonstrates how to read barcode from a Word document.
// The path to the documents directory.
string dataDir = @"C:\Files\BarCode\";
// Load the word document
Document wordDocument = new Document(dataDir + "sample_out.docx");
// Get all the shapes
NodeCollection shapes = wordDocument.GetChildNodes(NodeType.Shape, true);
// Loop through all the shapes
foreach (Shape shape in shapes)
{
// Check if it has an image
if (shape.HasImage)
{
// Save the image in memory stream
MemoryStream imgStream = new MemoryStream();
shape.ImageData.Save(imgStream);
// Recognize the barcode from the image stream above
using (BarCodeReader reader = new BarCodeReader(new Bitmap(imgStream), DecodeType.Code39Standard))
{
foreach (BarCodeResult result in reader.ReadBarCodes())
{
Console.WriteLine("Codetext found: " + result.CodeText + ", Symbology: " + result.CodeType);
}
}
}
}
Codetext found: 1234567890, Symbology: Code39Standard

获得免费许可证

你可以 获得免费的临时许可证 试图图书馆,没有评估限制。

结论

在这篇文章中,我们涵盖了:

  • 如何以 编程方式创建一个 Word 文档。
  • 步骤 创建条形码图像并将其添加到 Word 文档
  • 如何创建一个QR代码并将其插入一个Word文档。
  • 技巧在 C# 中的 Word 文档中 阅读条形码图像。

有关详细信息,请参阅 Aspose.BarCode for .NET API 人们在说什么如果你有任何疑问,请自由地找到 论坛.

更多信息

为了提高功能,您还可以找到有用的探索如何在 ASP.NET C#** 中动态生成条形码的网页应用程序。C# .NET 中的 ** Barcode Application 促进集成,并提供编码和处理的简单方法. 此包括使用 ** C # 代码以有效地阅读字符码数据** 或用于自定义需求的 ** 字体图像发明器在 C_** 。

More in this category