如今我们在便利店随意买一样东西,就可以在商品的外包装上看到该商品对应的条码。条形码在我们生活中的应用有多广泛这里就不细说了,本文主要介绍如何使用C#给PDF文档添加一维条形码和二维条形码。
该方案需要用到一个免费PDF组件和一个免费条形码组件,它们分别是Free Spire.PDF和Free Spire.Barcode。在编写代码前,需要在Visual Studio中创建一个C#应用程序,并引用Spire.Pdf.dll和Spire.Barcode.dll到工程中。
添加一维条形码
Free Spire.PDF组件本身支持创建Codabar、Code 11、Code 39、Code128、Code32、Code39及Code 93等一维条形码类型,因此这里仅需要使用到Free Spire.PDF组件。
需要用到的命名空间:
using System.Drawing;
using Spire.Pdf;
using Spire.Pdf.Barcode;
using Spire.Pdf.Graphics;
主体代码:
//创建PdfDocument实例
PdfDocument pdf = new PdfDocument();
PdfPageBase page =pdf.Pages.Add();
//初始化一些变量
float y = 20; //初始化坐标
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, newPointF(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, newPointF(0, y)); //在指定位置绘制条形码
y = code39.Bounds.Bottom + 5;
//保存文档
pdf.SaveToFile("一维条码.pdf");
生成效果:
添加二维条形码
由于Free Spire.PDF组件只支持创建一维条形码,因此要创建二维条形码还需要使用Free Spire.Barcode组件。Free Spire.Barcode组件支持创建和识别多种类型的一维、二维条形码,这些条形码类型可以在BarCodeType枚举中查看。
需要用到的命名空间:
using System.Drawing;
using Spire.Barcode;
using Spire.Pdf;
using Spire.Pdf.Graphics;
主体代码:
//创建PdfDocument实例
PdfDocument pdf = new PdfDocument();
PdfPageBase page =pdf.Pages.Add();
//使用Spire.Barcode的BarcodeSettings和BarcodeGenerator类创建二维码图形
Spire.Barcode.BarcodeSettingssettings = new BarcodeSettings();
settings.Type = BarCodeType.QRCode;
settings.Data = "123456789";
settings.Data2D = "123456789";
settings.X = 2f;
settings.LeftMargin = 1;
settings.ShowTextOnBottom = true;
settings.QRCodeECL = QRCodeECL.Q;
settings.QRCodeDataMode = QRCodeDataMode.Numeric;
Spire.Barcode.BarCodeGeneratorgenerator = 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 = "QRCode:";
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("二维条形码.pdf");
生成效果: