C# 在PDF中创建条码、二维码条形码技术是一种广泛应用于商业、邮政、图书管理、仓储、工业生产过程控制、交通运输、包装、配送等领域的自动识别技术。
它是由一组规则排列的条、空及其对应字符组成,用以表示一定信息的标识。
同时,技术的创新也使二维码技术得以发展应用。
因此,本篇文章将对如何在PDF文档中创建条码和二维码进行讲述。
注:Spire.PDF本身支持创建以下几种类型的条码,包括Codabar、Code 11、Code 32、Code 39、Code128、Code 93等,而创建其他类型的条码或者二维码时,需要使用Spire.Barcode。
下面在PDF中创建二维码的示例中,就需要同时使用到Sprie.Pdf.dll和Spire.Barcode.dll。
PS: 这里可以使用Free Spire.Office for .NET,下载安装后,dll文件可在安装路径下的Bin文件夹下获取。
也可以通过Nuget网站下载安装。
1.生成条码Dll引用:Spire.Pdf.dllC#using System.Drawing;using Spire.Pdf;using Spire.Pdf.Graphics;using Spire.Pdf.Barcode;namespace DrawPdfBarcode{class Program{static void Main(string[] args){//实例化一个PdfDocument类PdfDocument doc = new PdfDocument();//设置PDF Margin,并添加sectionPdfUnitConvertor unitCvtr = new PdfUnitConvertor();PdfMargins margin = new PdfMargins();margin.Top = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);margin.Bottom = margin.Top;margin.Left = unitCvtr.ConvertUnits(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);margin.Right = margin.Left;PdfSection section = doc.Sections.Add();section.PageSettings.Margins = margin;section.PageSettings.Size = PdfPageSize.A4;// 添加一页,设置文字字体及格式PdfPageBase page = section.Pages.Add();float y = 10;PdfBrush brush1 = PdfBrushes.Black;PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 12f, FontStyle.Bold), true);RectangleF rctg = new RectangleF(new PointF(0, 0), page.Canvas.ClientSize);PdfLinearGradientBrush brush2= new PdfLinearGradientBrush(rctg, Color.Navy, Color.OrangeRed, PdfLinearGradientMode.Vertical);//绘入条码PdfTextWidget text = new PdfTextWidget();text.Font = font1;text.Text = "Codabar:";PdfLayoutResult result = text.Draw(page, 0, y);page = result.Page;y = result.Bounds.Bottom + 2;PdfCodabarBarcode barcode1 = new PdfCodabarBarcode("00:12-3456/7890");barcode1.BarcodeToTextGapHeight = 1f;barcode1.EnableCheckDigit = true;barcode1.ShowCheckDigit = true;barcode1.TextDisplayLocation = TextLocation.Bottom;barcode1.TextColor = Color.Blue;barcode1.Draw(page, new PointF(0, y));y = barcode1.Bounds.Bottom + 5;//绘入条码 Code11text.Text = "Code11:";result = text.Draw(page, 0, y);page = result.Page;y = result.Bounds.Bottom + 2;PdfCode11Barcode barcode2 = new PdfCode11Barcode("123-4567890");barcode2.BarcodeToTextGapHeight = 1f;barcode2.TextDisplayLocation = TextLocation.Bottom;barcode2.TextColor = Color.Blue;barcode2.Draw(page, new PointF(0, y));y = barcode2.Bounds.Bottom + 5;//绘入条码 Code128-Atext.Text = "Code128-A:";result = text.Draw(page, 0, y);page = result.Page;y = result.Bounds.Bottom + 2;PdfCode128ABarcode barcode3 = new PdfCode128ABarcode("HELLO 00-123"); barcode3.BarcodeToTextGapHeight = 1f;barcode3.TextDisplayLocation = TextLocation.Bottom;barcode3.TextColor = Color.Blue;barcode3.Draw(page, new PointF(0, y));y = barcode3.Bounds.Bottom + 5;//绘入条码 Code128-Btext.Text = "Code128-B:";result = text.Draw(page, 0, y);page = result.Page;y = result.Bounds.Bottom + 2;PdfCode128BBarcode barcode4 = new PdfCode128BBarcode("Hello 00-123"); barcode4.BarcodeToTextGapHeight = 1f;barcode4.TextDisplayLocation = TextLocation.Bottom;barcode4.TextColor = Color.Blue;barcode4.Draw(page, new PointF(0, y));y = barcode4.Bounds.Bottom + 5;//绘入条码Code32text.Text = "Code32:";result = text.Draw(page, 0, y);page = result.Page;y = result.Bounds.Bottom + 2;PdfCode32Barcode barcode5 = new PdfCode32Barcode("16273849");barcode5.BarcodeToTextGapHeight = 1f;barcode5.TextDisplayLocation = TextLocation.Bottom;barcode5.TextColor = Color.Blue;barcode5.Draw(page, new PointF(0, y));y = barcode5.Bounds.Bottom + 5;page = section.Pages.Add();y = 10;//绘入条码 Code39text.Text = "Code39:";result = text.Draw(page, 0, y);page = result.Page;y = result.Bounds.Bottom + 2;PdfCode39Barcode barcode6 = new PdfCode39Barcode("16-273849"); barcode6.BarcodeToTextGapHeight = 1f;barcode6.TextDisplayLocation = TextLocation.Bottom;barcode6.TextColor = Color.Blue;barcode6.Draw(page, new PointF(0, y));y = barcode6.Bounds.Bottom + 5;//绘入条码 Code39-Etext.Text = "Code39-E:";result = text.Draw(page, 0, y);page = result.Page;y = result.Bounds.Bottom + 2;PdfCode39ExtendedBarcode barcode7 = newPdfCode39ExtendedBarcode("16-273849");barcode7.BarcodeToTextGapHeight = 1f;barcode7.TextDisplayLocation = TextLocation.Bottom;barcode7.TextColor = Color.Blue;barcode7.Draw(page, new PointF(0, y));y = barcode7.Bounds.Bottom + 5;//绘入条码 Code93text.Text = "Code93:";result = text.Draw(page, 0, y);page = result.Page;y = result.Bounds.Bottom + 2;PdfCode93Barcode barcode8 = new PdfCode93Barcode("16-273849"); barcode8.BarcodeToTextGapHeight = 1f;barcode8.TextDisplayLocation = TextLocation.Bottom;barcode8.TextColor = Color.Blue;barcode8.QuietZone.Bottom = 5;barcode8.Draw(page, new PointF(0, y));y = barcode8.Bounds.Bottom + 5;//绘入条码 Code93-Etext.Text = "Code93-E:";result = text.Draw(page, 0, y);page = result.Page;y = result.Bounds.Bottom + 2;PdfCode93ExtendedBarcode barcode9 = newPdfCode93ExtendedBarcode("16-273849");barcode9.BarcodeToTextGapHeight = 1f;barcode9.TextDisplayLocation = TextLocation.Bottom; barcode9.TextColor = Color.Blue;barcode9.Draw(page, new PointF(0, y));y = barcode9.Bounds.Bottom + 5;//保存文档doc.SaveToFile("Barcode.pdf");doc.Close();}}}项目运行结果(可在该项目文件下bin>Debug下查看生成的文档)2.生成二维码引用Spire.PDF.dll和Spire.PDF.Barcode.dllC#using Spire.Pdf;using Spire.Barcode;using Spire.Pdf.Graphics;using System.Drawing;namespace Barcode2{class Program{static void Main(string[] args){//初始化PdfDocument和PdfPageBase类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 = 2;settings.ShowTextOnBottom = true;settings.QRCodeECL = QRCodeECL.Q;settings.QRCodeDataMode = QRCodeDataMode.Numeric;Spire.Barcode.BarCodeGenerator generator = new BarCodeGenerator(settings);Image image = generator.GenerateImage();//绘制文本“QR Code:”到PDFfloat 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;//绘制二维码图形到PDFPdfImage pdfImage = PdfImage.FromImage(image);page.Canvas.DrawImage(pdfImage, 0, y);//保存文档pdf.SaveToFile("QRCode.pdf");}}}项目运行结果:以上内容是在PDF 中创建条码、二维码的方法介绍,代码内容供参考。