翻譯|使用教程|編輯:李顯亮|2019-11-29 10:30:44.403|閱讀 504 次
概述:MS Word本身不支持直接插入條形碼和二維碼,可以使用條形碼控件Spire.Barcode來創建條碼圖片,然后以圖片的形式添加到Word中。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Spire.Doc for .NET是一個專業的Word .NET庫,設計用于幫助開發人員高效地開發創建、閱讀、編寫、轉換和打印任何來自.NET( C#, VB.NET, ASP.NET)平臺的Word文檔文件的功能。
本系列教程將為大家帶來Spire.Doc for .NET在使用過程中的各類實際操作,本文將講解如何在 Word 中添加條形碼、二維碼。
11月優惠進行時,消費滿額即享折上豪禮,想買Spire.Doc的朋友趕快吧!
推薦閱讀:【想要快速完成文檔格式轉換嗎?Spire系列組件格式轉換完整攻略來啦!】
MS Word本身不支持直接插入條形碼和二維碼,可以安裝條形碼字體,然后在Word中應用該字體來創建條形碼。當然,也可以使用條形碼控件Spire.Barcode來創建條碼圖片,然后以圖片的形式添加到Word中。本文將詳細介紹這兩種方案。
使用條形碼字體創建條形碼
使用條形碼字體創建條形碼時,請確保字體已經正確至電腦中。按照默認路徑安裝后,在目錄C:\Windows\Fonts下能查找到。
//創建Document對象,添加section及段落 Document doc = new Document(); Section section = doc.AddSection(); Paragraph paragraph = section.AddParagraph(); //添加文字“Code 128:” TextRange txtRang = paragraph.AppendText("Code 128:\n"); txtRang.CharacterFormat.FontSize = 15f; //添加條形碼 txtRang = paragraph.AppendText("H63TWX11072"); //條形碼數據 txtRang.CharacterFormat.FontName = "Code 128"; //應用條形碼字體 txtRang.CharacterFormat.FontSize = 60f; //將字體嵌入Word文檔,使條形碼能在未安裝該字體的電腦中正確顯示 doc.EmbedFontsInFile = true; doc.EmbedSystemFonts = true; //保存文檔 doc.SaveToFile("Code128.docx", FileFormat.Docx2013);
使用Spire.Barcode創建條碼(以二維碼為例)圖片,添加圖片到Word文檔
//創建Document對象,添加section及段落 Document doc = new Document(); Section section = doc.AddSection(); Paragraph paragraph = section.AddParagraph(); //添加文字“QR Code:” TextRange txtRang = paragraph.AppendText("QR Code:\n"); txtRang.CharacterFormat.FontSize = 15f; //使用Spire.Barcode的BarcodeSettings和BarcodeGenerator類創建二維碼圖形 Spire.Barcode.BarcodeSettings settings = new BarcodeSettings(); settings.Type = BarCodeType.QRCode; settings.Data = "123456789"; settings.Data2D = "123456789"; settings.X = 2f; settings.LeftMargin = 0; settings.ShowTextOnBottom = true; settings.QRCodeECL = QRCodeECL.Q; settings.QRCodeDataMode = QRCodeDataMode.Numeric; Spire.Barcode.BarCodeGenerator generator = new BarCodeGenerator(settings); Image image = generator.GenerateImage(); //添加二維碼圖形到Word paragraph.AppendPicture(image); //保存文檔 doc.SaveToFile("QRCode.docx", FileFormat.Docx2013);
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn