本文介绍如何通过C# 和VB.NET代码实现在Word文档中添加条码和二维码。代码中将分为在Word正文段落中、页眉页脚中等情况来添加。
使用工具:
Free Spire.Office for .NET (免费版)
工具简介:
这是Spire所有.NET平台下免费产品的集合包,包含Spire.Barcode.dll、Spire.DataExport.dll、Spire.Pdf.dll、Spire.Doc.dll、Spire.DocViewer.Forms.dll 、Spire.PdfViewer.Forms.dll 、Spire.Presentation.dll 、Spire.XLS.dll等dll可用于操作Word/PDF/Excel/PPT等文件。
本文添加条码、二维码需要在VS程序中添加引用 Spire.Doc.dll 和 Spire.Barcode.dll 这两个dll文件。
dll添加引用效果:
注意:
1. 需要下载安装到本地指定路径,dll文件可在安装路径下的Bin文件夹下获取。
2. 生成的条码、二维码会有水印字样的文字,可通过此方法去除。
代码示例
1. 添加条码到Word
C#
using Spire.Doc; using Spire.Barcode; using System.Drawing; using System.IO; using Spire.Doc.Documents; namespace AddBarcode { class Program { static void Main(string[] args) { //调用方法 ApplyKey(string key) 移除水印文字 Spire.Barcode.BarcodeSettings.ApplyKey("在这里输入去除水印字样的序列号"); //创建Document对象,加载Word文档 Document doc = new Document(); doc.LoadFromFile("test.docx"); //获取第2节 Section section = doc.Sections[1]; //使用Spire.Barcode的BarcodeSettings和BarcodeGenerator类创建条码并保存为图片 BarcodeSettings settings = new BarcodeSettings(); settings.Type = BarCodeType.Code128; settings.Data = "123456789"; settings.Data2D = "123456789"; settings.ShowText = false; settings.BarHeight = 4; settings.X = 0.3f; settings.HasBorder = true; settings.BorderWidth = 0.5f; settings.BorderColor = Color.AliceBlue; settings.BackColor = Color.Wheat; BarCodeGenerator barCodeGenerator = new BarCodeGenerator(settings); Image Image = barCodeGenerator.GenerateImage(); //Image.Save("Barcode.png", System.Drawing.Imaging.ImageFormat.Png);//如果需要保存生成的Barcode图片,可执行此步骤代码 //添加条码到正文段落 Paragraph paragraph = section.AddParagraph(); paragraph.Text = "收货码:"; paragraph.AppendPicture(Image); paragraph.Format.HorizontalAlignment = HorizontalAlignment.Right; //添加条码图片到Word页脚 HeaderFooter footer = section.HeadersFooters.Footer; Paragraph footerpara = footer.AddParagraph(); footerpara.Text = "扫码识真伪:"; footerpara.AppendPicture(Image); footerpara.Format.HorizontalAlignment = HorizontalAlignment.Left; //保存文档 doc.SaveToFile("BarCodeToWord.docx", FileFormat.Docx2013); System.Diagnostics.Process.Start("BarCodeToWord.docx"); } } }
VB.NET
Imports Spire.Doc Imports Spire.Barcode Imports System.Drawing Imports System.IO Imports Spire.Doc.Documents Namespace AddBarcode Class Program Private Shared Sub Main(args As String()) '调用方法 ApplyKey(string key) 移除水印文字 Spire.Barcode.BarcodeSettings.ApplyKey("在这里输入去除水印字样的序列号") '创建Document对象,加载Word文档 Dim doc As New Document() doc.LoadFromFile("test.docx") '获取第2节 Dim section As Section = doc.Sections(1) '使用Spire.Barcode的BarcodeSettings和BarcodeGenerator类创建条码并保存为图片 Dim settings As New BarcodeSettings() settings.Type = BarCodeType.Code128 settings.Data = "123456789" settings.Data2D = "123456789" settings.ShowText = False settings.BarHeight = 4 settings.X = 0.3F settings.HasBorder = True settings.BorderWidth = 0.5F settings.BorderColor = Color.AliceBlue settings.BackColor = Color.Wheat Dim barCodeGenerator As New BarCodeGenerator(settings) Dim Image As Image = barCodeGenerator.GenerateImage() 'Image.Save("Barcode.png", System.Drawing.Imaging.ImageFormat.Png);//如果需要保存生成的Barcode图片,可执行此步骤代码 '添加条码到正文段落 Dim paragraph As Paragraph = section.AddParagraph() paragraph.Text = "收货码:" paragraph.AppendPicture(Image) paragraph.Format.HorizontalAlignment = HorizontalAlignment.Right '添加条码图片到Word页脚 Dim footer As HeaderFooter = section.HeadersFooters.Footer Dim footerpara As Paragraph = footer.AddParagraph() footerpara.Text = "扫码识真伪:" footerpara.AppendPicture(Image) footerpara.Format.HorizontalAlignment = HorizontalAlignment.Left '保存文档 doc.SaveToFile("BarCodeToWord.docx", FileFormat.Docx2013) System.Diagnostics.Process.Start("BarCodeToWord.docx") End Sub End Class End Namespace
条码添加效果:
2. 添加二维码到Word
C#
using Spire.Doc; using Spire.Barcode; using System.Drawing; using System.IO; using Spire.Doc.Documents; using System; namespace AddQRCode { class Program { static void Main(string[] args) { //调用方法 ApplyKey(string key) 移除水印文字 Spire.Barcode.BarcodeSettings.ApplyKey("在这里输入去除水印字样的序列号"); //创建Document对象,加载Word文档 Document doc = new Document(); doc.LoadFromFile("test.docx"); //获取第2节 Section section = doc.Sections[1]; //使用Spire.Barcode的BarcodeSettings和BarcodeGenerator类创建二维码并保存为图片 BarcodeSettings settings = new BarcodeSettings(); settings.Type = BarCodeType.QRCode; settings.ImageWidth = 50; settings.ImageHeight = 50; settings.Data = "123456"; settings.Data2D = "123456"; settings.X =0.7f; settings.LeftMargin = 1; settings.ShowTextOnBottom = true; settings.QRCodeECL = QRCodeECL.Q; settings.QRCodeDataMode = QRCodeDataMode.Numeric; BarCodeGenerator generator = new BarCodeGenerator(settings); Image image = generator.GenerateImage(); //image.Save("QRCode.png", System.Drawing.Imaging.ImageFormat.Png);//如果需要保存生成的二维码图片,可执行此步骤代码 //添加二维码到正文段落 Paragraph paragraph = section.AddParagraph(); paragraph.AppendPicture(image); paragraph.Format.HorizontalAlignment = HorizontalAlignment.Right; //添加二维码图片到Word页眉 HeaderFooter header = section.HeadersFooters.Header; //HeaderFooter footer = section.HeadersFooters.Footer;//获取页脚 Paragraph headerpara = header.AddParagraph(); headerpara.AppendPicture(image); headerpara.Format.HorizontalAlignment = HorizontalAlignment.Center; //保存文档 doc.SaveToFile("QRCodeToHeader.docx", FileFormat.Docx2013); System.Diagnostics.Process.Start("QRCodeToHeader.docx"); } } }
VB.NET
Imports Spire.Doc Imports Spire.Barcode Imports System.Drawing Imports System.IO Imports Spire.Doc.Documents Namespace AddQRCode Class Program Private Shared Sub Main(args As String()) '调用方法 ApplyKey(string key) 移除水印文字 Spire.Barcode.BarcodeSettings.ApplyKey("在这里输入去除水印字样的序列号") '创建Document对象,加载Word文档 Dim doc As New Document() doc.LoadFromFile("test.docx") '获取第2节 Dim section As Section = doc.Sections(1) '使用Spire.Barcode的BarcodeSettings和BarcodeGenerator类创建二维码并保存为图片 Dim settings As New BarcodeSettings() settings.Type = BarCodeType.QRCode settings.ImageWidth = 50 settings.ImageHeight = 50 settings.Data = "123456" settings.Data2D = "123456" settings.X = 0.7F settings.LeftMargin = 1 settings.ShowTextOnBottom = True settings.QRCodeECL = QRCodeECL.Q settings.QRCodeDataMode = QRCodeDataMode.Numeric Dim generator As New BarCodeGenerator(settings) Dim image As Image = generator.GenerateImage() 'image.Save("QRCode.png", System.Drawing.Imaging.ImageFormat.Png);//如果需要保存生成的二维码图片,可执行此步骤代码 '添加二维码到正文段落 Dim paragraph As Paragraph = section.AddParagraph() paragraph.AppendPicture(image) paragraph.Format.HorizontalAlignment = HorizontalAlignment.Right '添加二维码图片到Word页眉 Dim header As HeaderFooter = section.HeadersFooters.Header 'HeaderFooter footer = section.HeadersFooters.Footer;//获取页脚 Dim headerpara As Paragraph = header.AddParagraph() headerpara.AppendPicture(image) headerpara.Format.HorizontalAlignment = HorizontalAlignment.Center '保存文档 doc.SaveToFile("QRCodeToHeader.docx", FileFormat.Docx2013) System.Diagnostics.Process.Start("QRCodeToHeader.docx") End Sub End Class End Namespace
二维码添加效果:
以上就是C#/VB.NET 在Word中添加条码、二维码的示例代码的详细内容,更多关于C#/VB.NET 在Word中添加条码、二维码的资料请关注其它相关文章!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
- 古典吉他《鲁特琴-BachInspirations巴赫灵感》ThibautGarcia[24Bit/96kHz]
- 刘纬武《睡眠音乐家 缓解疼痛 赫兹白噪音》[320K/MP3][217.2MB]
- 刘纬武《睡眠音乐家 缓解疼痛 赫兹白噪音》[FLAC/分轨][870.69MB]
- 群星1998 《宝丽金最佳电视主题曲》香港首版[WAV+CUE][1.2G]
- 群星1990《群星会·金曲重现》新加坡版[WAV+CUE][1.1G]
- 张惠妹2003《勇敢》[WAV+CUE][1.1G]
- 群星1995《摇滚中国乐势力》首版引进版[WAV+CUE][983M]
- 陈思安《32首酒廊情调》2CD新雅(国际)影碟[WAV+CUE]
- 齐豫潘越云《回声》K2HD[正版原抓WAV+CUE]
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]