本次功能测试中,使用 Free Spire.PDF for .NET。
实现功能的大致思路:生成条形码,将条形码保存为图片,然后在PDF中的表格单元格中插入条码图片。
Spire.PDF for .NET 中的Spire.Pdf.Barcode namespace提供了多种Barcode类型,用于满足创建不同类型barcode的需求,如图:
Spire.Pdf.dll文件的引入方法如下:
方法1
在程序中引入Spire.Pdf.dll文件;将 Free Spire.PDF for .NET 下载到本地,解压,安装。安装完成后,找到安装路径下BIN文件夹中的Spire.Pdf.dll。然后在Visual Studio中打开“解决方案资源管理器”,鼠标右键点击“引用”,“添加引用”,将本地路径BIN文件夹下的dll文件添加引用至程序。
方法2
通过 NuGet 安装。可以通过以下2种方法安装:
1.可以在Visual Studio中打开“解决方案资源管理器”,鼠标右键点击“引用”,“管理NuGet包”,然后搜索“ Free Spire.PDF”,点击“安装”。等待程序安装完成。
2.将以下内容复制到PM控制台安装。
Install-Package FreeSpire.PDF -Version 8.2.0
C#
using Spire.Pdf;
using Spire.Pdf.Barcode;
using Spire.Pdf.Graphics;
using Spire.Pdf.Grid;
using System.Drawing;
namespace AddBarcodeToTable
{
class Program
{
static void Main(string[] args)
{
//创建PDF文档
PdfDocument pdf = new PdfDocument();
PdfPageBase page = pdf.Pages.Add();
//创建PdfGrid类的表格对象
PdfGrid grid = new PdfGrid();
grid.Style.CellPadding = new PdfPaddings(1, 1, 1, 1);
grid.Style.Font = new PdfTrueTypeFont(new Font("Arial Unicode MS", 9f), true);
//添加2行2列到表格
PdfGridRow row1 = grid.Rows.Add();
PdfGridRow row2 = grid.Rows.Add();
grid.Columns.Add(2);
//设置列宽
foreach (PdfGridColumn column in grid.Columns)
{
column.Width = 150f;
}
//在单元格中写入数据
row1.Cells[0].Value = "产品编号";
row1.Cells[1].Value = "条码";
row2.Cells[0].Value = "B0215";
//创建条码
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;
//将条码保存为图片到指定路径
Image image =barcode1.ToImage();
image.Save(@"F:/VS2017Project/DrawTable_PDF/AddBarcodeToTable/bin/Debug/BarcodeImage.png");
//将条码图片添加到表格单元格
string imgpath = "F:/VS2017Project/DrawTable_PDF/AddBarcodeToTable/bin/Debug/BarcodeImage.png";
PdfGridCellContentList contentList = new PdfGridCellContentList();
PdfGridCellContent content = new PdfGridCellContent();
SizeF imageSize = new SizeF(120, 80);
content.ImageSize = imageSize;
content.Image = PdfImage.FromFile(imgpath);
contentList.List.Add(content);
row2.Cells[1].Value = contentList;
//绘制表格到页面指定位置
grid.Draw(page, new PointF(0, 40));
//保存PDF文档
pdf.SaveToFile("AddBarcodeToTable.pdf",FileFormat.PDF);
System.Diagnostics.Process.Start("AddBarcodeToTable.pdf");
}
}
}
文档效果:
—END—
留言与评论(共有 0 条评论) “” |