条形码技术广泛应用于商业零售、运输、物流、电子商务等领域。文本将为大家介绍如何使用spire.pdf和spire.barcode在pdf中添加条形码、二维码。
目前,spire.pdf本身仅支持创建codabar、code 11、code 39、code128、code 93等一维条形码类型,如果需要添加其他类型的条形码及二维码,则需要使用spire.barcode来进行创建。
使用spire.pdf自带的类创建条形码
c#
//创建pdfdocument实例
pdfdocument pdf = new pdfdocument();
pdfpagebase page = pdf.pages.add();
//初始化一些变量
float y = 20; //初始化y坐标
pdftruetypefont font = new pdftruetypefont(new font("arial", 12f, fontstyle.bold), true); //定义font
//绘制文本“codabar:”到pdf
pdftextwidget text = new pdftextwidget();
text.font = font;
text.text = "codabar:";
pdflayoutresult result = text.draw(page, 0, y); //在指定位置绘制文本
y = result.bounds.bottom 2;
//绘制codabar条形码到pdf
pdfcodabarbarcode codabar = new pdfcodabarbarcode("00:12-3456/7890"); //初始化pdfcodabarbarcode类的实例
codabar.barcodetotextgapheight = 1f;
codabar.enablecheckdigit = true;
codabar.showcheckdigit = true;
codabar.textdisplaylocation = textlocation.bottom;
codabar.textcolor = color.green;
codabar.draw(page, new pointf(0, y)); //在指定位置绘制条形码
y = codabar.bounds.bottom 5;
//绘制文本“code39:”到pdf
text.text = "code39:";
result = text.draw(page, 0, y); //在指定位置绘制文本
page = result.page;
y = result.bounds.bottom 2;
//绘制code 39条形码到pdf
pdfcode39barcode code39 = new pdfcode39barcode("16-273849"); //初始化pdfcode39barcode类的实例
codabar.barcodetotextgapheight = 1f;
code39.barcodetotextgapheight = 1f;
code39.textdisplaylocation = textlocation.bottom;
code39.textcolor = color.blue;
code39.draw(page, new pointf(0, y)); //在指定位置绘制条形码
y = code39.bounds.bottom 5;
//保存文档
pdf.savetofile("barcode.pdf");
vb.net
'创建pdfdocument实例
dim pdf as new pdfdocument()
dim page as pdfpagebase = pdf.pages.add()
'初始化一些变量
dim y as single = 20
'初始化y坐标
dim font as new pdftruetypefont(new font("arial", 12f, fontstyle.bold), true)
'定义font
'绘制文本“codabar:”到pdf
dim text as new pdftextwidget()
text.font = font
text.text = "codabar:"
dim result as pdflayoutresult = text.draw(page, 0, y)
'在指定位置绘制文本
y = result.bounds.bottom 2
'绘制codabar条形码到pdf
dim codabar as new pdfcodabarbarcode("00:12-3456/7890")
'初始化pdfcodabarbarcode类的实例
codabar.barcodetotextgapheight = 1f
codabar.enablecheckdigit = true
codabar.showcheckdigit = true
codabar.textdisplaylocation = textlocation.bottom
codabar.textcolor = color.green
codabar.draw(page, new pointf(0, y))
'在指定位置绘制条形码
y = codabar.bounds.bottom 5
'绘制文本“code39:”到pdf
text.text = "code39:"
result = text.draw(page, 0, y)
'在指定位置绘制文本
page = result.page
y = result.bounds.bottom 2
'绘制code 39条形码到pdf
dim code39 as new pdfcode39barcode("16-273849")
'初始化pdfcode39barcode类的实例
codabar.barcodetotextgapheight = 1f
code39.barcodetotextgapheight = 1f
code39.textdisplaylocation = textlocation.bottom
code39.textcolor = color.blue
code39.draw(page, new pointf(0, y))
'在指定位置绘制条形码
y = code39.bounds.bottom 5
'保存文档
pdf.savetofile("barcode.pdf")
使用spire.barcode创建二维码,添加到pdf
以下代码需同时使用spire.barcode和spire.pdf,请下载并添加spire.barcode.dll和spire.pdf.dll至程序中。
c#
//创建pdfdocument实例
pdfdocument pdf = new pdfdocument();
pdfpagebase page = pdf.pages.add();
//使用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();
//绘制文本“qr code:”到pdf
float y = 20;
pdftruetypefont font = new pdftruetypefont(new font("arial", 12f, fontstyle.bold), true);
pdftextwidget text = new pdftextwidget();
text.font = font;
text.text = "qr code:";
pdflayoutresult result = text.draw(page, 0, y);
y = result.bounds.bottom 2;
//绘制二维码图形到pdf
pdfimage pdfimage = pdfimage.fromimage(image);
page.canvas.drawimage(pdfimage, 0, y);
//保存文档
pdf.savetofile("qrcode.pdf");
vb.net
'创建pdfdocument实例
dim pdf as new pdfdocument()
dim page as pdfpagebase = pdf.pages.add()
'使用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()
'绘制文本“qr code:”到pdf
dim y as single = 20
dim font as new pdftruetypefont(new font("arial", 12f, fontstyle.bold), true)
dim text as new pdftextwidget()
text.font = font
text.text = "qr code:"
dim result as pdflayoutresult = text.draw(page, 0, y)
y = result.bounds.bottom 2
'绘制二维码图形到pdf
dim pdfimage__1 as pdfimage = pdfimage.fromimage(image)
page.canvas.drawimage(pdfimage__1, 0, y)
'保存文档
pdf.savetofile("qrcode.pdf")