條碼的應用已深入生活和作業的方方面面,在處理條碼時,常需要和各種檔案格式相結合,當需要在檔案中插入、編輯或者洗掉條碼時,可借助于一些專業的類別庫工具來實作,本文,以操作PDF檔案為例,介紹如何在編輯表格時,向單元格中插入條形碼,
【類別庫引入及代碼思路】
本次功能測驗中,使用 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 = https://www.cnblogs.com/Yesi/p/"產品編號"; row1.Cells[1].Value = https://www.cnblogs.com/Yesi/p/"條碼"; row2.Cells[0].Value = https://www.cnblogs.com/Yesi/p/"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 =https://www.cnblogs.com/Yesi/p/ contentList; //繪制表格到頁面指定位置 grid.Draw(page, new PointF(0, 40)); //保存PDF檔案 pdf.SaveToFile("AddBarcodeToTable.pdf",FileFormat.PDF); System.Diagnostics.Process.Start("AddBarcodeToTable.pdf"); } } }
vb.net
Imports Spire.Pdf Imports Spire.Pdf.Barcode Imports Spire.Pdf.Graphics Imports Spire.Pdf.Grid Imports System.Drawing Namespace AddBarcodeToTable Class Program Private Shared Sub Main(args As String()) '創建PDF檔案 Dim pdf As New PdfDocument() Dim page As PdfPageBase = pdf.Pages.Add() '創建PdfGrid類的表格物件 Dim grid As 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列到表格 Dim row1 As PdfGridRow = grid.Rows.Add() Dim row2 As PdfGridRow = grid.Rows.Add() grid.Columns.Add(2) '設定列寬 For Each column As PdfGridColumn In grid.Columns column.Width = 150F Next '在單元格中寫入資料 row1.Cells(0).Value = https://www.cnblogs.com/Yesi/p/"產品編號" row1.Cells(1).Value = https://www.cnblogs.com/Yesi/p/"條碼" row2.Cells(0).Value = https://www.cnblogs.com/Yesi/p/"B0215" '創建條碼 Dim barcode1 As New PdfCodabarBarcode("00:12-3456/7890") barcode1.BarcodeToTextGapHeight = 1F barcode1.EnableCheckDigit = True barcode1.ShowCheckDigit = True barcode1.TextDisplayLocation = TextLocation.Bottom barcode1.TextColor = Color.Blue '將條碼保存為圖片到指定路徑 Dim image As Image = barcode1.ToImage() image.Save("F:/VS2017Project/DrawTable_PDF/AddBarcodeToTable/bin/Debug/BarcodeImage.png") '將條碼圖片添加到表格單元格 Dim imgpath As String = "F:/VS2017Project/DrawTable_PDF/AddBarcodeToTable/bin/Debug/BarcodeImage.png" Dim contentList As New PdfGridCellContentList() Dim content As New PdfGridCellContent() Dim imageSize As New SizeF(120, 80) content.ImageSize = imageSize content.Image = PdfImage.FromFile(imgpath) contentList.List.Add(content) row2.Cells(1).Value =https://www.cnblogs.com/Yesi/p/ contentList '繪制表格到頁面指定位置 grid.Draw(page, New PointF(0, 40)) '保存PDF檔案 pdf.SaveToFile("AddBarcodeToTable.pdf", FileFormat.PDF) System.Diagnostics.Process.Start("AddBarcodeToTable.pdf") End Sub End Class End Namespace
檔案效果:

—END—
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/485503.html
標籤:C#
