MS Word本身不支持直接插入条形码和二维码,您可以安装条形码字体,然后在Word中应用该字体来创建条形码。当然,您也可以使用我们的条形码控件Spire.Barcode来创建条码图片,然后以图片的形式添加到Word中。本文将详细介绍这两种方案。
使用条形码字体创建条形码
使用条形码字体创建条形码时,请确保字体已经正确至电脑中。按照默认路径安装后,在目录C:\Windows\Fonts下能查找到。
C#
//创建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);
VB.NET
'创建Document对象,添加section及段落
Dim doc As New Document()
Dim section As Section = doc.AddSection()
Dim paragraph As Paragraph = section.AddParagraph()
'添加文字“Code 128:”
Dim txtRang As TextRange = paragraph.AppendText("Code 128:" & vbLf)
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文档
C#
//创建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);
VB.NET
'创建Document对象,添加section及段落
Dim doc As New Document()
Dim section As Section = doc.AddSection()
Dim paragraph As Paragraph = section.AddParagraph()
'添加文字“QR Code:”
Dim txtRang As TextRange = paragraph.AppendText("QR Code:" & vbLf)
txtRang.CharacterFormat.FontSize = 15F
'使用Spire.Barcode的BarcodeSettings和BarcodeGenerator类创建二维码图形
Dim settings As Spire.Barcode.BarcodeSettings = 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
Dim generator As Spire.Barcode.BarCodeGenerator = New BarCodeGenerator(settings)
Dim image As Image = generator.GenerateImage()
'添加二维码图形到Word
paragraph.AppendPicture(image)
'保存文档
doc.SaveToFile("QRCode.docx", FileFormat.Docx2013)