您好,登錄后才能下訂單哦!
這篇文章主要介紹“C#/VB.NET 在Word中怎么添加條碼/二維碼”,在日常操作中,相信很多人在C#/VB.NET 在Word中怎么添加條碼/二維碼問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”C#/VB.NET 在Word中怎么添加條碼/二維碼”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
本文介紹如何通過C# 和VB.NET代碼實現(xiàn)在Word文檔中添加條碼和二維碼。代碼中將分為在Word正文段落中、頁眉頁腳中等情況來添加。
使用工具:
Free Spire.Office for .NET (免費版)
工具簡介:
這是Spire所有.NET平臺下免費產(chǎn)品的集合包,包含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) { //調(diào)用方法 ApplyKey(string key) 移除水印文字 Spire.Barcode.BarcodeSettings.ApplyKey("在這里輸入去除水印字樣的序列號"); //創(chuàng)建Document對象,加載Word文檔 Document doc = new Document(); doc.LoadFromFile("test.docx"); //獲取第2節(jié) Section section = doc.Sections[1]; //使用Spire.Barcode的BarcodeSettings和BarcodeGenerator類創(chuàng)建條碼并保存為圖片 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圖片,可執(zhí)行此步驟代碼 //添加條碼到正文段落 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 = "掃碼識真?zhèn)危?quot;; 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()) '調(diào)用方法 ApplyKey(string key) 移除水印文字 Spire.Barcode.BarcodeSettings.ApplyKey("在這里輸入去除水印字樣的序列號") '創(chuàng)建Document對象,加載Word文檔 Dim doc As New Document() doc.LoadFromFile("test.docx") '獲取第2節(jié) Dim section As Section = doc.Sections(1) '使用Spire.Barcode的BarcodeSettings和BarcodeGenerator類創(chuàng)建條碼并保存為圖片 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圖片,可執(zhí)行此步驟代碼 '添加條碼到正文段落 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 = "掃碼識真?zhèn)危?quot; 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) { //調(diào)用方法 ApplyKey(string key) 移除水印文字 Spire.Barcode.BarcodeSettings.ApplyKey("在這里輸入去除水印字樣的序列號"); //創(chuàng)建Document對象,加載Word文檔 Document doc = new Document(); doc.LoadFromFile("test.docx"); //獲取第2節(jié) Section section = doc.Sections[1]; //使用Spire.Barcode的BarcodeSettings和BarcodeGenerator類創(chuàng)建二維碼并保存為圖片 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);//如果需要保存生成的二維碼圖片,可執(zhí)行此步驟代碼 //添加二維碼到正文段落 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()) '調(diào)用方法 ApplyKey(string key) 移除水印文字 Spire.Barcode.BarcodeSettings.ApplyKey("在這里輸入去除水印字樣的序列號") '創(chuàng)建Document對象,加載Word文檔 Dim doc As New Document() doc.LoadFromFile("test.docx") '獲取第2節(jié) Dim section As Section = doc.Sections(1) '使用Spire.Barcode的BarcodeSettings和BarcodeGenerator類創(chuàng)建二維碼并保存為圖片 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);//如果需要保存生成的二維碼圖片,可執(zhí)行此步驟代碼 '添加二維碼到正文段落 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
二維碼添加效果:
到此,關(guān)于“C#/VB.NET 在Word中怎么添加條碼/二維碼”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。