翻譯|使用教程|編輯:胡濤|2022-09-22 11:05:24.860|閱讀 369 次
概述:本文將為你介紹如何在 C# 中從 PDF 讀取條形碼,歡迎查閱~
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Aspose.PDF 是一款高級PDF處理API,可以在跨平臺應用程序中輕松生成,修改,轉換,呈現,保護和打印文檔。無需使用Adobe Acrobat。此外,API提供壓縮選項,表創建和處理,圖形和圖像功能,廣泛的超鏈接功能,圖章和水印任務,擴展的安全控件和自定義字體處理。本文將為你介紹如何在 C# 中從 PDF 讀取條形碼
我們可以生成條形碼并將其添加到PDF文檔中,如我之前的帖子中所述。在某些情況下,我們可能需要以編程方式檢測和讀取嵌入到 PDF 文檔中的條形碼。它有助于解碼 PDF 文檔(如發票、收據或報告)中的條形碼和二維碼形式的嵌入信息。在本文中,我們將學習如何使用 C# 從 PDF 文檔中讀取條形碼。
我們將按照兩步程序從 PDF 文檔中讀取條形碼。首先,我們將使用Aspose.PDF for .NET API 加載 PDF 文檔,然后將其頁面渲染為光柵圖像。之后,我們將使用Aspose.BarCode for .NET API 從渲染圖像中讀取條形碼。
請下載 API 的 DLL或使用NuGet安裝它。
PM> Install-Package Aspose.BarCode PM> Install-Package Aspose.PDF
Aspose.PDF API的類代表一個 PDF 文檔。API的函數將 PDF 頁面呈現為 PNG 內存流。Aspose.BarCode API的類使我們能夠執行操作來檢測條形碼。BarCodeResult類存儲檢測的條碼信息,例如條碼類型、代碼文本、區域和其他參數。
我們可以按照以下步驟讀取嵌入在 PDF 文檔任何頁面上的條形碼圖像:
以下代碼示例展示了如何使用 C# 從 PDF 文檔中讀取條形碼。
// This code example demonstrates how to read a barcode from a PDF document using C#. // The path to the document string file = @"C:\Files\BarCode\sample-PDF-with-Barcodes.pdf"; // Load a PDF document Aspose.Pdf.Document pdfDoc = new Aspose.Pdf.Document(file); // Proceed all PDF pages starting from page 1 for (int i = 1; i <= pdfDoc.Pages.Count; ++i) { // Render PDF page to the stream MemoryStream ms = pdfDoc.Pages[i].ConvertToPNGMemoryStream(); ms.Position = 0; // Recognize barcodes from the rendered image of the page BarCodeReader reader = new BarCodeReader(ms); // Show results foreach (BarCodeResult result in reader.ReadBarCodes()) { Console.WriteLine("Codetext found: " + result.CodeText); Console.WriteLine("Symbology: " + result.CodeType); Console.WriteLine("-------------------------------"); } }
Codetext found: Aspose.Barcode Pdf417 Example
Symbology: Pdf417
-------------------------------
Codetext found: Aspose.Barcode QR Example
Symbology: QR
-------------------------------
Codetext found: Aspose.Barcode DataMatrix Example
Symbology: DataMatrix
-------------------------------
請下載本博文中使用的帶有條形碼的輸入 PDF 文檔。
類將 PDF 頁面轉換為圖像來從 PDF 文檔中讀取條形碼。它允許將PDF文件的每一頁轉換為圖像,然后我們將從轉換后的圖像中讀取條形碼信息。
我們可以按照以下步驟從轉換后的 PDF 頁面中讀取條形碼:
以下代碼示例展示了如何使用 C# 將 PDF 頁面轉換為圖像并讀取條形碼。
// The following code example shows how to convert PDF pages into images with PDF Convertor and read barcodes using C#. // The path to the document string file = @"C:\Files\BarCode\sample-PDF-with-Barcodes.pdf"; // Load a PDF document Aspose.Pdf.Document pdfDoc = new Aspose.Pdf.Document(file); // Initialize a PdfConvertor Aspose.Pdf.Facades.PdfConverter pdfConverter = new Aspose.Pdf.Facades.PdfConverter(pdfDoc); // Set barcode optimization pdfConverter.RenderingOptions.BarcodeOptimization = true; // Set page resolution // 300 dpi is standard resolution pdfConverter.Resolution = new Aspose.Pdf.Devices.Resolution(300); // Set all pages to render into images pdfConverter.StartPage = 1; //starts from page 1 pdfConverter.EndPage = pdfConverter.Document.Pages.Count; // Render selected pages into the images pdfConverter.DoConvert(); while (pdfConverter.HasNextImage()) { // Render current page to memory stream image MemoryStream ms = new MemoryStream(); pdfConverter.GetNextImage(ms, ImageFormat.Png); ms.Position = 0; // Recognize barcodes from the rendered image of the page BarCodeReader reader = new BarCodeReader(ms); // Show results foreach (BarCodeResult result in reader.ReadBarCodes()) { Console.WriteLine("Codetext found: " + result.CodeText); Console.WriteLine("Symbology: " + result.CodeType); Console.WriteLine("-------------------------------"); } }
這是另一種類似于前一種的方法。唯一不同的是,在這個方法中,我們將使用 API 的類將 PDF 文檔的頁面轉換為圖像。它允許將 PDF 文檔的頁面轉換為 PNG 圖像。
我們可以按照以下步驟將轉換后的 PDF 頁面中的條形碼讀取為 PNG 圖像:
以下代碼示例展示了如何將 PDF 頁面轉換為 PNG 圖像并使用 C# 讀取條形碼。
// The following code example shows how to convert PDF pages into images with PDF Convertor and read barcodes using C#. // The path to the document string file = @"C:\Files\BarCode\sample-PDF-with-Barcodes.pdf"; // Load a PDF document Aspose.Pdf.Document pdfDoc = new Aspose.Pdf.Document(file); // Initialize a PdfConvertor Aspose.Pdf.Facades.PdfConverter pdfConverter = new Aspose.Pdf.Facades.PdfConverter(pdfDoc); // Set barcode optimization pdfConverter.RenderingOptions.BarcodeOptimization = true; // Set page resolution // 300 dpi is standard resolution pdfConverter.Resolution = new Aspose.Pdf.Devices.Resolution(300); // Set all pages to render into images pdfConverter.StartPage = 1; //starts from page 1 pdfConverter.EndPage = pdfConverter.Document.Pages.Count; // Render selected pages into the images pdfConverter.DoConvert(); while (pdfConverter.HasNextImage()) { // Render current page to memory stream image MemoryStream ms = new MemoryStream(); pdfConverter.GetNextImage(ms, ImageFormat.Png); ms.Position = 0; // Recognize barcodes from the rendered image of the page BarCodeReader reader = new BarCodeReader(ms); // Show results foreach (BarCodeResult result in reader.ReadBarCodes()) { Console.WriteLine("Codetext found: " + result.CodeText); Console.WriteLine("Symbology: " + result.CodeType); Console.WriteLine("-------------------------------"); } }
我們還可以使用類識別嵌入在 PDF 頁面上的條形碼圖像。它允許從 PDF 中提取圖像,然后我們將從提取的圖像中讀取條形碼信息。
我們可以按照以下步驟從提取的圖像中讀取條形碼:
以下代碼示例展示了如何使用 C# 從 PDF 文檔中提取和讀取條形碼圖像。
// The following code example shows how to convert PDF pages into images with PdfExtractor and read barcodes using C#. // The path to the document string file = @"C:\Files\BarCode\sample-PDF-with-Barcodes.pdf"; // Bind a PDF document Aspose.Pdf.Facades.PdfExtractor pdfExtractor = new Aspose.Pdf.Facades.PdfExtractor(); pdfExtractor.BindPdf(file); // Set page range for image extraction pdfExtractor.StartPage = 1; pdfExtractor.EndPage = 3; // Extract the images pdfExtractor.ExtractImage(); // Save images to stream in a loop while (pdfExtractor.HasNextImage()) { // Save image to stream MemoryStream imageStream = new MemoryStream(); pdfExtractor.GetNextImage(imageStream); imageStream.Position = 0; // Recognize the barcodes from the image stream above BarCodeReader reader = new BarCodeReader(imageStream); foreach (BarCodeResult result in reader.ReadBarCodes()) { Console.WriteLine("Codetext found: " + result.CodeText); Console.WriteLine("Symbology: " + result.CodeType); Console.WriteLine("-------------------------------"); } }
我們還可以使用類從 PDF 文檔中查找和提取條形碼圖像。它表示圖像放置對象的吸收體對象。它執行圖像使用搜索,并通過 ImagePlacements 集合提供對搜索結果的訪問。此方法有助于識別具有原始分辨率的條碼。它可能無法正確識別矢量格式。
我們可以按照以下步驟從 PDF 文檔中查找和讀取條形碼:
以下代碼示例展示了如何使用 C# 從 PDF 中查找和讀取條形碼圖像。
// This code example demonstrates how to read a barcode from a PDF document using ImagePlacementAbsorber. // The path to the document string file = @"C:\Files\BarCode\sample-PDF-with-Barcodes.pdf"; // Load a PDF document Aspose.Pdf.Document pdfDoc = new Aspose.Pdf.Document(file); // Initialize ImagePlacementAbsorber Aspose.Pdf.ImagePlacementAbsorber imagePlacementAbsorber = new Aspose.Pdf.ImagePlacementAbsorber(); // Process all PDF pages in the document starting from page 1 for (int i = 1; i <= pdfDoc.Pages.Count; ++i) { // Visit the page create an image extractor imagePlacementAbsorber.Visit(pdfDoc.Pages[i]); // Extract all images from the PDF page foreach (Aspose.Pdf.ImagePlacement imagePlacement in imagePlacementAbsorber.ImagePlacements) { // Convert an image from the PDF page to the stream MemoryStream ms = new MemoryStream(); imagePlacement.Save(ms, ImageFormat.Png); ms.Position = 0; // Recognize barcode from extracted image of the page BarCodeReader reader = new BarCodeReader(ms); // Show results foreach (BarCodeResult result in reader.ReadBarCodes()) { Console.WriteLine("Codetext found: " + result.CodeText); Console.WriteLine("Symbology: " + result.CodeType); Console.WriteLine("-------------------------------"); } } }
以上便是如何在 C# 中從 PDF 讀取條形碼 ,要是您還有其他關于產品方面的問題,歡迎咨詢我們,或者加入我們官方技術交流群。
歡迎下載|體驗更多Aspose產品
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn