C#/VB.NET 實現(xiàn)在PDF表格中添加條形碼
條碼的應(yīng)用已深入生活和工作的方方面面。在處理條碼時,常需要和各種文檔格式相結(jié)合。當(dāng)需要在文檔中插入、編輯或者刪除條碼時,可借助于一些專業(yè)的類庫工具來實現(xiàn)。本文,以操作PDF文件為例,介紹如何在編輯表格時,向單元格中插入條形碼。
類庫引入及代碼思路
本次功能測試中,使用 Free Spire.PDF for .NET。
實現(xiàn)功能的大致思路:生成條形碼,將條形碼保存為圖片,然后在PDF中的表格單元格中插入條碼圖片。
Spire.PDF for .NET 中的Spire.Pdf.Barcode namespace提供了多種Barcode類型,用于滿足創(chuàng)建不同類型barcode的需求,如圖:
Spire.Pdf.dll文件的引入方法如下:
方法1
在程序中引入Spire.Pdf.dll文件;將 Free Spire.PDF for .NET 下載到本地,解壓,安裝。安裝完成后,找到安裝路徑下BIN文件夾中的Spire.Pdf.dll。然后在Visual Studio中打開“解決方案資源管理器”,鼠標(biāo)右鍵點擊“引用”,“添加引用”,將本地路徑BIN文件夾下的dll文件添加引用至程序。
方法2
通過 NuGet 安裝??赏ㄟ^以下2種方法安裝:
1.可以在Visual Studio中打開“解決方案資源管理器”,鼠標(biāo)右鍵點擊“引用”,“管理NuGet包”,然后搜索“ Free Spire.PDF”,點擊“安裝”。等待程序安裝完成。
2.將以下內(nèi)容復(fù)制到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) { //創(chuàng)建PDF文檔 PdfDocument pdf = new PdfDocument(); PdfPageBase page = pdf.Pages.Add(); //創(chuàng)建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); //設(shè)置列寬 foreach (PdfGridColumn column in grid.Columns) { column.Width = 150f; } //在單元格中寫入數(shù)據(jù) row1.Cells[0].Value = "產(chǎn)品編號"; row1.Cells[1].Value = "條碼"; row2.Cells[0].Value = "B0215"; //創(chuàng)建條碼 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"); } } }
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()) '創(chuàng)建PDF文檔 Dim pdf As New PdfDocument() Dim page As PdfPageBase = pdf.Pages.Add() '創(chuàng)建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) '設(shè)置列寬 For Each column As PdfGridColumn In grid.Columns column.Width = 150F Next '在單元格中寫入數(shù)據(jù) row1.Cells(0).Value = "產(chǎn)品編號" row1.Cells(1).Value = "條碼" row2.Cells(0).Value = "B0215" '創(chuàng)建條碼 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 = 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
文檔效果:
以上就是C#/VB.NET 實現(xiàn)在PDF表格中添加條形碼 的詳細(xì)內(nèi)容,更多關(guān)于C# PDF添加條形碼 的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
.NET實現(xiàn)父窗體關(guān)閉而不影響子窗體的方法
這篇文章主要介紹了.NET實現(xiàn)父窗體關(guān)閉而不影響子窗體的方法,很實用的功能,需要的朋友可以參考下2014-08-08C#中實現(xiàn)輸入漢字獲取其拼音(漢字轉(zhuǎn)拼音)的2種方法
這篇文章主要介紹了C#中實現(xiàn)輸入漢字獲取其拼音(漢字轉(zhuǎn)拼音)的2種方法,本文分別給出了使用微軟語言包、手動編碼實現(xiàn)兩種實現(xiàn)方式,需要的朋友可以參考下2015-01-01C# 操作PostgreSQL 數(shù)據(jù)庫的示例代碼
本篇文章主要介紹了C# 操作PostgreSQL 數(shù)據(jù)庫的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11C#語法糖(Csharp Syntactic sugar)大匯總
首先需要聲明的是“語法糖”這個詞絕非貶義詞,它可以給我?guī)矸奖悖且环N便捷的寫法,編譯器會幫我們做轉(zhuǎn)換;而且可以提高開發(fā)編碼的效率,在性能上也不會帶來損失。這讓java開發(fā)人員羨慕不已,呵呵。2010-06-06