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)