與其他組件比較

IronOCR與PDFTRON OCR的比較

發佈 2022年9月5日
分享:

OCR 代表「光學字符識別」。這是一個將紙質文件或圖像轉換成可讀文本的過程。有多種方法可以做到這一點,例如通過掃描或通過鍵盤手動輸入。這樣做是為了將任何掃描文件、PDF 文件或手寫文件轉換為其原始文本格式。在刑事案件中,當文件因損壞過於嚴重而無法手動轉錄時,這一過程已被證明是無價的,因為它們可以被掃描並由 OCR 軟件解釋。

隨著技術的進步和數字工具的普及,OCR還在其他領域得到了應用,例如在 Google Docs 等應用中的文件轉換,還有在學術界和商業世界中實現了廣泛應用。主要有兩種類型的 OCR:「靜態」和「動態」。最常見的是靜態 OCR,其中整個文件一次性被掃描。另一方面,動態 OCR 則一次掃描一行,並且可以處理更複雜的佈局,例如表格數據。

本文將討論兩個最流行的 OCR 和 PDF 應用程序及文檔庫之間的比較。它們是:

  • PDFTron OCR
  • IronOCR

1.0 介紹

1.1 PDFTron OCR 介紹和功能

要在 PDFTron SDK 中使用 OCR,我們需要安裝一個單獨的 OCR 模組附加工具。這有助於 SDK 從文件中檢測文本。它可以使文本可選和可搜索。PDFTron SDK 支援多達100種語言。PDFTron OCR 引擎由 Tesseract 的開源 LSTM 神經網路提供支持。PDFTron OCR 支援多種影像格式的文本檢測。僅含有光柵圖像的 PDF 文件也可以進行 OCR,輸出的質量取決於輸入圖像。最適合 OCR 的圖像是具有 300 DPI 分辨率的灰度圖像。

PDFTron OCR 的功能

  • 使印刷文件中的圖像可以搜索。
  • 將簡單的 PDF 轉換為可搜索的 PDF。
  • 從商業文件中檢測重要信息。
  • 使圖書掃描更加簡便。
  • 從車輛圖片中檢測車輛號碼。
  • 幫助視障用戶。
  • 使用 OCR 從文件中輕鬆進行多文檔數據錄入。
  • 將名片信息輕鬆轉移到聯絡數據列表中。

1.2 IronOCR — 介紹和功能

Iron Software 為軟體工程師提供 IronOCR for .NET,以從照片和 PDF 中讀取文字內容在 .NET 應用程式和網站中。該軟體有助於掃描照片中的文字和條碼,支援多種全球語言,並以純文字或結構化數據輸出。Iron Software 的 OCR 庫可以應用於 Web、控制台、MVC 和許多 .NET 桌面應用程式。在商業部署的情況下,購買許可證後會直接獲得開發團隊的協助。

  • IronOCR 使用最新的 Tesseract 5 引擎,可以從任何 PDF 格式中讀取文字、QR 碼和條碼。通過這個庫將 OCR 添加到桌面、Web 應用程式和控制台中,確保快速集成。
  • IronOCR 支援127種國際語言。它還支援自定義語言和詞彙表。
  • IronOCR 可以讀取超過20種條碼和 QR 碼格式。
  • 支援多頁 GIF 和 TIFF 圖像格式。
  • 修正品質較差的掃描和圖像。
  • 支援多執行緒。它可以同時執行一個或多個流程。
  • IronOCR 可以提供頁面、段落、行、單詞、字元等結構化數據輸出。
  • IronOCR 支援 Windows、Linux、macOS 等操作系統。

2. 在 Visual Studio 中建立一個新專案

打開 Visual Studio 並找到檔案選單。選擇新專案,然後選擇主控台應用程式。我們將在本文中使用主控台應用程式來生成 PDF 文件。

在相應的文字框中輸入專案名稱並選擇檔案路徑。然後,點擊建立按鈕。此外,選擇所需的.NET Framework。

Visual Studio 專案現在將為選定的應用程式生成結構。

Visual Studio 現在將生成專案的結構。如果您選擇了 Windows、控制台和 Web 應用程式,program.cs 檔案將會打開,這樣您可以輸入代碼並構建/運行您的應用程式。

下一步,我們需要添加庫來測試代碼。

3.0 安裝

3.1 安裝 PDFtron OCR

PDFTron OCR 的安裝是手動完成的,可以從給定的網址以 zip 檔案下載。 連結. 解壓縮並將其配置到專案中。該指南將幫助您使用免費集成的 PDFTron SDK 試用版,在 Windows 上運行的 .NET Framework 應用程式中執行 PDFTron 示例。免費試用版包括來自解決方案工程師的支援和無限試用。

先決條件

Visual Studio: 確保您的安裝包含 .NET 桌面開發.NET Framework 4.5.1+ 開發工具 工作負載。本指南將使用 Visual Studio 2017 和 PDFTron's C# .NET PDF Library for Windows。使用此連結下載該庫。 .NET PDF SDK 下載.

初始設置

從 .zip 文件中解壓縮文件夾。在本指南中使用 PDFNET_BASE 來選擇您解壓縮文件夾的路徑。

PDFNET_BASE = path/to/extraction/folder/PDFNetDotNet4/
PDFNET_BASE = path/to/extraction/folder/PDFNetDotNet4/
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'PDFNET_BASE = path/@to/extraction/folder/PDFNetDotNet4/
VB   C#

執行範例

導航到解壓縮內容的位置。找到並進入 Samples 資料夾 (PDFNET_BASE/範例). 這個文件夾包含許多 PDFTron SDK 支援功能的示例代碼。

  1. 在 Visual Studio 中打開 Samples_20XX.sln。選擇適合您 Visual Studio 安裝的版本。

  2. 選擇示例代碼並 將其設為啟動項目 解決方案。

  3. 執行專案。

集成到您的應用程序

這個被稱為“PDFTron Hello World”應用程序。如果您能夠打開、保存和關閉 PDF 文件,那麼集成其餘的 PDFTron SDK 是非常簡單的。

  1. 在 Visual Studio 中,使用您偏好的語言創建一個 .NET Framework 控制台應用程序項目。您可以在 Visual C# 或 Visual Basic 類別下找到它們。

  2. 進入您的項目文件夾。默認情況下,路徑應類似於:C:/Users/User_Name/source/repos/myApp

  3. 從 PDFNET_BASE 到您的項目文件夾 (此資料夾將包含你的 .csproj 或 .vbproj 檔案),複製 Lib 資料夾。

  4. 在右側找到「方案總管」。右鍵單擊「引用」並選擇「加入參考」。這會打開「參考管理員」對話框。

  5. 在對話框底部選擇「瀏覽」。定位到複製的 Lib 資料夾中,將 PDFNetLoader.dll 添加到引用中。

  6. 另外,從 x86 資料夾中添加適當版本的 PDFNet.dll 作為另一個引用。 (path/to/your/project/folder/Lib/PDFNet/x86/PDFNet.dll)這將確保該應用程式能夠在32-bit和64-bit操作系統上運行。

  7. 點擊PDFNet.dll。確保將其Copy Local屬性設置為False。

3.2 安裝 IronOCR

IronOCR 庫可以通過四種方式安裝。

這些方式是:

  • 使用 Visual Studio。
  • 使用 Visual Studio 命令列界面。
  • 從 NuGet 網站直接下載。
  • 從 IronPDF 網站直接下載。

3.2.1 使用 Visual Studio

Visual Studio 軟體提供 NuGet 套件管理器選項,允許將套件直接安裝到解決方案中。截圖展示了如何打開 NuGet 套件管理器。

這將提供一個搜索框,以顯示來自NuGet網站的套件列表。在套件管理器中,我們需要搜索關鍵字"IronOCR",如下圖所示:

從上圖可以看到相關搜索結果列表。要將套件安裝到解決方案中,我們必須選擇所需的選項。

3.2.2 使用 Visual Studio 命令列

  • 在 Visual Studio 中:前往 工具-> NuGet 套件管理員 -> 套件管理員主控台
  • 在套件管理員主控台標籤中輸入以下程式碼片段。
Install-Package IronOCR

該套件將直接安裝到當前專案中,然後即可使用。

### 3.2.3 從 NuGet 網站直接下載

第三種方法,我們可以直接從網站下載 NuGet 套件
* 瀏覽到這個 [鏈接](https://www.nuget.org/packages/Ironocr).
* 從右側菜單中,確保選擇下載套件選項。
* 雙擊下載的套件。它會自動安裝。
* 接下來,重新加載解決方案並開始在項目中使用它。

### 3.2.4 從 IronOCR 網站直接下載

您可以通過點擊此處直接從網站下載最新的套件 [連結](https://ironsoftware.com/csharp/ocr/packages/IronOcr.zip)按照提供的說明,在下載最新的套件後將套件添加到項目中。
* 右擊方案視窗中的項目。
* 選擇 "添加參考" 項以瀏覽下載的參考位置。
* 然後,點擊 "確定" 以添加參考。

### 4.0 OCR 圖像

IronOCR 和 **PDFtron OCR** 都擁有將圖像轉換為可搜索文本的 OCR 技術。

### 4.1 使用 **PDFTron**

將 PDF 轉換為 DOCX、DOC、HTML、SVG、TIFF、PNG、JPEG、XPS、EPUB、TXT 及其他多種格式。

```cs
PDFDoc doc = new PDFDoc
```(檔案名稱)轉換 PDF 文件為 SVG

Convert.ToSvg(doc, output_filename + ".svg")// 將 PDF 文件轉換為 XPS

Convert.ToXps(檔案名稱, 輸出檔案名稱 + ".xps")將內容翻譯成中文(繁體):

// 將 PDF 文檔轉換為多頁 TIFF

Convert.TiffOutputOptions tiff_options = new Convert.TiffOutputOptions ```();

tiff_options.SetDPI(200);

tiff_options.SetDither(真);

tiff_options.SetMono(真);

Convert.ToTiff(檔案名稱, output_filename + ".tiff", tiff_options);

// 將 PDF 轉換為 XOD

Convert.ToXod(檔案名稱, 輸出檔案名稱 + ".xod");

// 將 PDF 轉換為 HTML

Convert.ToHtml(檔案名, output_filename + ".html");

請提供內容以進行翻譯。

4.2 使用 IronOCR

var Ocr = new IronTesseract(); // nothing to configure
Ocr.Language = OcrLanguage.EnglishBest;
Ocr.Configuration.TesseractVersion = TesseractVersion.Tesseract5;
using (var Input = new OcrInput())
{
    Input.AddImage(@"3.png");
    var Result = Ocr.Read(Input);
    Console.WriteLine(Result.Text);
    Console.ReadKey();
}
var Ocr = new IronTesseract(); // nothing to configure
Ocr.Language = OcrLanguage.EnglishBest;
Ocr.Configuration.TesseractVersion = TesseractVersion.Tesseract5;
using (var Input = new OcrInput())
{
    Input.AddImage(@"3.png");
    var Result = Ocr.Read(Input);
    Console.WriteLine(Result.Text);
    Console.ReadKey();
}
Dim Ocr = New IronTesseract() ' nothing to configure
Ocr.Language = OcrLanguage.EnglishBest
Ocr.Configuration.TesseractVersion = TesseractVersion.Tesseract5
Using Input = New OcrInput()
	Input.AddImage("3.png")
	Dim Result = Ocr.Read(Input)
	Console.WriteLine(Result.Text)
	Console.ReadKey()
End Using
VB   C#

如上所示,這是使用 Tesseract 5 API 將圖像文件轉換為文字的過程。上述程式碼行用於創建 Iron Tesseract 物件。此外,為了確保我們可以添加一個或多個圖片文件,我們創建了一個 OcrInput 物件,該物件將需要可用的圖片路徑。在 Iron Tesseract 物件中,可以使用 "Read" 函數通過解析圖片文件來獲取圖像,並將生成的結果提取到 OCR 結果中。它能夠從照片中提取文字並將其轉換為字串。

Tesseract 可以使用 "AddMultiFrameTiff" 方法來添加多幀圖像。在此過程中,圖像中的每一幀都被讀取並由 Tesseract 庫視為一個獨立的頁面。圖像的每一幀都會被讀取,然後才繼續到下一幀,直到每一幀都成功掃描完畢。此方法僅支持 TIFF 圖像格式。

數據成功轉換為可編輯文本顯示在上圖中,這是IronOCR精確度的結果。

5.0 OCR PDF 文件

IronOCR 和 PDFTron OCR 將 PDF 文件轉換成可編輯的文本。PDFTron OCR 提供了一系列選項給使用者,例如儲存頁面、編輯圖像、識別頁面等。此外,它還可以提供儲存選項,如文件、文本、HTML 格式等。IronOCR 也允許我們將轉換後的 OCR 文件儲存為 HTML、文本、PDF 等。

5.1 使用 PDFTron OCR

完整的範例程式碼顯示瞭如何使用 PDFTron OCR 進行 PDF、XPS、EMF、SVG、TIFF、PNG、JPEG 和其他圖像格式之間的直接高品質轉換。

// Copyright (c) 2001-2021 by PDFTron Systems Inc. All Rights Reserved.

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using pdftron;
using pdftron.Common;
using pdftron.Filters;
using pdftron.SDF;
using pdftron.PDF;

// The following code showcases the conversion of documents to formats // such as SVG, PDF, EMF, or XPS.

namespace ConvertTestCS
{
    class Testfile
    {
        public string inputFile, outputFile;
        public bool requiresWindowsPlatform;
        public Testfile(string inFile, string outFile, bool requiresWindowsPlatform_)
        {
            inputFile = inFile;
            outputFile = outFile;
            requiresWindowsPlatform = requiresWindowsPlatform_;
        }
    };

    class Class1
    {
        private static pdftron.PDFNetLoader pdfNetLoader = pdftron.PDFNetLoader.Instance();
        static Class1() {}

        // Relative path to the folder containing test files.
        const string inputPath = "../../../../TestFiles/";
        const string outputPath = "../../../../TestFiles/Output/";

        static bool ConvertSpecificFormats()
        {
            bool err = false;
            try
            {
                using (PDFDoc pdfdoc = new PDFDoc())
                {
                    Console.WriteLine("Converting from XPS");
                    pdftron.PDF.Convert.FromXps(pdfdoc, inputPath + "simple-xps.xps");
                    pdfdoc.Save(outputPath + "xps2pdf v2.pdf", SDFDoc.SaveOptions.e_remove_unused);
                    Console.WriteLine("Saved xps2pdf v2.pdf");
                }
            }
            catch (PDFNetException e)
            {
                Console.WriteLine(e.Message);
                err = true;
            }

    /////////////////////////////////////////////
    if (Environment.OSVersion.Platform == PlatformID.Win32NT) {
                try
                {
                    using (PDFDoc pdfdoc = new PDFDoc())
                    {
                        Console.WriteLine("Converting from EMF");
                        pdftron.PDF.Convert.FromEmf(pdfdoc, inputPath + "simple-emf.emf");
                        pdfdoc.Save(outputPath + "emf2pdf v2.pdf", SDFDoc.SaveOptions.e_remove_unused);
                        Console.WriteLine("Saved emf2pdf v2.pdf");
                    }
                }
                catch (PDFNetException e)
                {
                    Console.WriteLine(e.Message);
                    err = true;
                }
            }

        ///////////////////////////////////////////
            try
            {
                using (PDFDoc pdfdoc = new PDFDoc())
                {
                    // Add a dictionary
                    ObjSet set = new ObjSet();
                    Obj options = set.CreateDict();

                    // Put options
                    options.PutNumber("FontSize", 15);
                    options.PutBool("UseSourceCodeFormatting", true);
                    options.PutNumber("PageWidth", 12);
                    options.PutNumber("PageHeight", 6);

                    // Convert from .txt file
                    Console.WriteLine("Converting from txt");
                    pdftron.PDF.Convert.FromText(pdfdoc, inputPath + "simple-text.txt", options);
                    pdfdoc.Save(outputPath + "simple-text.pdf", SDFDoc.SaveOptions.e_remove_unused);
                    Console.WriteLine("Saved simple-text.pdf");
                }
            }
            catch (PDFNetException e)
            {
                Console.WriteLine(e.Message);
                err = true;
            }

    ///////////////////////////////////////////
            try
            {
                using (PDFDoc pdfdoc = new PDFDoc(inputPath + "newsletter.pdf"))
                {
                    // Convert PDF document to SVG
                    Console.WriteLine("Converting pdfdoc to SVG");
                    pdftron.PDF.Convert.ToSvg(pdfdoc, outputPath + "pdf2svg v2.svg");
                    Console.WriteLine("Saved pdf2svg v2.svg");
                }
            }
            catch (PDFNetException e)
            {
                Console.WriteLine(e.Message);
                err = true;
            }

    /////////////////////////////////////////////////
            try
            {
                // Convert PNG image to XPS
                Console.WriteLine("Converting PNG to XPS");
                pdftron.PDF.Convert.ToXps(inputPath + "butterfly.png", outputPath + "butterfly.xps");
                Console.WriteLine("Saved butterfly.xps");
            }
            catch (PDFNetException e)
            {
                Console.WriteLine(e.Message);
                err = true;
            }
    ///////////////////////////////////////////
            if (Environment.OSVersion.Platform == PlatformID.Win32NT)
            {
                try
                {
                    // Convert MSWord document to XPS
                    Console.WriteLine("Converting DOCX to XPS");
                    pdftron.PDF.Convert.ToXps(inputPath + "simple-word_2007.docx", outputPath + "simple-word_2007.xps");
                    Console.WriteLine("Saved simple-word_2007.xps");
                }
                catch (PDFNetException e)
                {
                    Console.WriteLine(e.Message);
                    err = true;
                }
            }
            ////////////////////////////////////////////////////////////////////
            try
            {
                // Convert PDF document to XPS
                Console.WriteLine("Converting PDF to XPS");
                pdftron.PDF.Convert.ToXps(inputPath + "newsletter.pdf", outputPath + "newsletter.xps");
                Console.WriteLine("Saved newsletter.xps");
            }
            catch (PDFNetException e)
            {
                Console.WriteLine(e.Message);
                err = true;
            }

            //////////////////////////////////////////////////////////////////////
            try
            {
                // Convert PDF document to HTML
                Console.WriteLine("Converting PDF to HTML");
                pdftron.PDF.Convert.ToHtml(inputPath + "newsletter.pdf", outputPath + "newsletter");
                Console.WriteLine("Saved newsletter as HTML");
            }
            catch (PDFNetException e)
            {
                Console.WriteLine(e.Message);
                err = true;
            }

            //////////////////////////////////////////////////////////////////////
            try
            {
                // Convert PDF document to EPUB
                Console.WriteLine("Converting PDF to EPUB");
                pdftron.PDF.Convert.ToEpub(inputPath + "newsletter.pdf", outputPath + "newsletter.epub");
                Console.WriteLine("Saved newsletter.epub");
            }
            catch (PDFNetException e)
            {
                Console.WriteLine(e.Message);
                err = true;
            }

            /////////////////////////////////////////////////////////////////////
            try
            {
                // Convert PDF document to multipage TIFF
                Console.WriteLine("Converting PDF to multipage TIFF");
                pdftron.PDF.Convert.TiffOutputOptions tiff_options = new pdftron.PDF.Convert.TiffOutputOptions();
                tiff_options.SetDPI(200);
                tiff_options.SetDither(true);
                tiff_options.SetMono(true);
                pdftron.PDF.Convert.ToTiff(inputPath + "newsletter.pdf", outputPath + "newsletter.tiff", tiff_options);
                Console.WriteLine("Saved newsletter.tiff");
            }
            catch (PDFNetException e)
            {
                Console.WriteLine(e.Message);
                err = true;
            }

            return err;
        }

        static Boolean ConvertToPdfFromFile()
        {
            System.Collections.ArrayList testfiles = new System.Collections.ArrayList();
            testfiles.Add(new ConvertTestCS.Testfile("simple-word_2007.docx", "docx2pdf.pdf", false));
            testfiles.Add(new ConvertTestCS.Testfile("simple-powerpoint_2007.pptx", "pptx2pdf.pdf", false));
            testfiles.Add(new ConvertTestCS.Testfile("simple-excel_2007.xlsx", "xlsx2pdf.pdf", false));
            testfiles.Add(new ConvertTestCS.Testfile("simple-publisher.pub", "pub2pdf.pdf", true));
            testfiles.Add(new ConvertTestCS.Testfile("simple-text.txt", "txt2pdf.pdf", false));
            testfiles.Add(new ConvertTestCS.Testfile("simple-rtf.rtf", "rtf2pdf.pdf", true));
            testfiles.Add(new ConvertTestCS.Testfile("butterfly.png", "png2pdf.pdf", false));
            testfiles.Add(new ConvertTestCS.Testfile("simple-emf.emf", "emf2pdf.pdf", true));
            testfiles.Add(new ConvertTestCS.Testfile("simple-xps.xps", "xps2pdf.pdf", false));
            // testfiles.Add(new ConvertTestCS.Testfile("simple-webpage.mht", "mht2pdf.pdf", true));
            testfiles.Add(new ConvertTestCS.Testfile("simple-webpage.html", "html2pdf.pdf", true));

            bool err = false;
            if (Environment.OSVersion.Platform == PlatformID.Win32NT)
            {
                try
                {
                    if (pdftron.PDF.Convert.Printer.IsInstalled("PDFTron PDFNet"))
                    {
                        pdftron.PDF.Convert.Printer.SetPrinterName("PDFTron PDFNet");
                    }
                    else if (!pdftron.PDF.Convert.Printer.IsInstalled())
                    {
                        try
                        {
                            Console.WriteLine("Installing printer (requires Windows platform and administrator)");
                            pdftron.PDF.Convert.Printer.Install();
                            Console.WriteLine("Installed printer " + pdftron.PDF.Convert.Printer.GetPrinterName());
                            // the function ConvertToXpsFromFile may require the printer so leave it installed
                            // uninstallPrinterWhenDone = true;
                        }
                        catch (PDFNetException e)
                        {
                            Console.WriteLine("ERROR: Unable to install printer.");
                            Console.WriteLine(e.Message);
                            err = true;
                        }
                        catch
                        {
                            Console.WriteLine("ERROR: Unable to install printer. Make sure that the package's bitness matches your operating system's bitness and that you are running with administrator privileges.");
                        }
                    }
                }
                catch (PDFNetException e)
                {
                    Console.WriteLine("ERROR: Unable to install printer.");
                    Console.WriteLine(e.Message);
                    err = true;
                }
            }
            foreach (Testfile file in testfiles)
            {
                if ( Environment.OSVersion.Platform != PlatformID.Win32NT)
                {
                    if (file.requiresWindowsPlatform)
                    {
                        continue;
                    }
                }
                try
                {
                    using (pdftron.PDF.PDFDoc pdfdoc = new PDFDoc())
                    {

                        if (pdftron.PDF.Convert.RequiresPrinter(inputPath + file.inputFile))
                        {
                            Console.WriteLine("Using PDFNet printer to convert file " + file.inputFile);
                        }
                        pdftron.PDF.Convert.ToPdf(pdfdoc, inputPath + file.inputFile);
                        pdfdoc.Save(outputPath + file.outputFile, SDFDoc.SaveOptions.e_linearized);
                        Console.WriteLine("Converted file: " + file.inputFile);
                        Console.WriteLine("to: " + file.outputFile);
                    }
                }
                catch (PDFNetException e)
                {
                    Console.WriteLine("ERROR: on input file " + file.inputFile);
                    Console.WriteLine(e.Message);
                    err = true;
                }
            }

            return err;
        }
        [STAThread]
        static void Main(string [] args)
        {
            PDFNet.Initialize(PDFTronLicense.Key);
            bool err = false;

            err = ConvertToPdfFromFile();
            if (err)
            {
                Console.WriteLine("ConvertFile failed");
            }
            else
            {
                Console.WriteLine("ConvertFile succeeded");
            }

            err = ConvertSpecificFormats();
            if (err)
            {
                Console.WriteLine("ConvertSpecificFormats failed");
            }
            else
            {
                Console.WriteLine("ConvertSpecificFormats succeeded");
            }

            if (Environment.OSVersion.Platform == PlatformID.Win32NT)
            {
                if (pdftron.PDF.Convert.Printer.IsInstalled())
                {
                    try
                    {
                        Console.WriteLine("Uninstalling printer (requires Windows platform and administrator)");
                        pdftron.PDF.Convert.Printer.Uninstall();
                        Console.WriteLine("Uninstalled Printer " + pdftron.PDF.Convert.Printer.GetPrinterName());
                    }
                    catch
                    {
                        Console.WriteLine("Unable to uninstall printer");
                    }
                }
            }
            PDFNet.Terminate();
            Console.WriteLine("Done.");
        }
    }
}
// Copyright (c) 2001-2021 by PDFTron Systems Inc. All Rights Reserved.

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using pdftron;
using pdftron.Common;
using pdftron.Filters;
using pdftron.SDF;
using pdftron.PDF;

// The following code showcases the conversion of documents to formats // such as SVG, PDF, EMF, or XPS.

namespace ConvertTestCS
{
    class Testfile
    {
        public string inputFile, outputFile;
        public bool requiresWindowsPlatform;
        public Testfile(string inFile, string outFile, bool requiresWindowsPlatform_)
        {
            inputFile = inFile;
            outputFile = outFile;
            requiresWindowsPlatform = requiresWindowsPlatform_;
        }
    };

    class Class1
    {
        private static pdftron.PDFNetLoader pdfNetLoader = pdftron.PDFNetLoader.Instance();
        static Class1() {}

        // Relative path to the folder containing test files.
        const string inputPath = "../../../../TestFiles/";
        const string outputPath = "../../../../TestFiles/Output/";

        static bool ConvertSpecificFormats()
        {
            bool err = false;
            try
            {
                using (PDFDoc pdfdoc = new PDFDoc())
                {
                    Console.WriteLine("Converting from XPS");
                    pdftron.PDF.Convert.FromXps(pdfdoc, inputPath + "simple-xps.xps");
                    pdfdoc.Save(outputPath + "xps2pdf v2.pdf", SDFDoc.SaveOptions.e_remove_unused);
                    Console.WriteLine("Saved xps2pdf v2.pdf");
                }
            }
            catch (PDFNetException e)
            {
                Console.WriteLine(e.Message);
                err = true;
            }

    /////////////////////////////////////////////
    if (Environment.OSVersion.Platform == PlatformID.Win32NT) {
                try
                {
                    using (PDFDoc pdfdoc = new PDFDoc())
                    {
                        Console.WriteLine("Converting from EMF");
                        pdftron.PDF.Convert.FromEmf(pdfdoc, inputPath + "simple-emf.emf");
                        pdfdoc.Save(outputPath + "emf2pdf v2.pdf", SDFDoc.SaveOptions.e_remove_unused);
                        Console.WriteLine("Saved emf2pdf v2.pdf");
                    }
                }
                catch (PDFNetException e)
                {
                    Console.WriteLine(e.Message);
                    err = true;
                }
            }

        ///////////////////////////////////////////
            try
            {
                using (PDFDoc pdfdoc = new PDFDoc())
                {
                    // Add a dictionary
                    ObjSet set = new ObjSet();
                    Obj options = set.CreateDict();

                    // Put options
                    options.PutNumber("FontSize", 15);
                    options.PutBool("UseSourceCodeFormatting", true);
                    options.PutNumber("PageWidth", 12);
                    options.PutNumber("PageHeight", 6);

                    // Convert from .txt file
                    Console.WriteLine("Converting from txt");
                    pdftron.PDF.Convert.FromText(pdfdoc, inputPath + "simple-text.txt", options);
                    pdfdoc.Save(outputPath + "simple-text.pdf", SDFDoc.SaveOptions.e_remove_unused);
                    Console.WriteLine("Saved simple-text.pdf");
                }
            }
            catch (PDFNetException e)
            {
                Console.WriteLine(e.Message);
                err = true;
            }

    ///////////////////////////////////////////
            try
            {
                using (PDFDoc pdfdoc = new PDFDoc(inputPath + "newsletter.pdf"))
                {
                    // Convert PDF document to SVG
                    Console.WriteLine("Converting pdfdoc to SVG");
                    pdftron.PDF.Convert.ToSvg(pdfdoc, outputPath + "pdf2svg v2.svg");
                    Console.WriteLine("Saved pdf2svg v2.svg");
                }
            }
            catch (PDFNetException e)
            {
                Console.WriteLine(e.Message);
                err = true;
            }

    /////////////////////////////////////////////////
            try
            {
                // Convert PNG image to XPS
                Console.WriteLine("Converting PNG to XPS");
                pdftron.PDF.Convert.ToXps(inputPath + "butterfly.png", outputPath + "butterfly.xps");
                Console.WriteLine("Saved butterfly.xps");
            }
            catch (PDFNetException e)
            {
                Console.WriteLine(e.Message);
                err = true;
            }
    ///////////////////////////////////////////
            if (Environment.OSVersion.Platform == PlatformID.Win32NT)
            {
                try
                {
                    // Convert MSWord document to XPS
                    Console.WriteLine("Converting DOCX to XPS");
                    pdftron.PDF.Convert.ToXps(inputPath + "simple-word_2007.docx", outputPath + "simple-word_2007.xps");
                    Console.WriteLine("Saved simple-word_2007.xps");
                }
                catch (PDFNetException e)
                {
                    Console.WriteLine(e.Message);
                    err = true;
                }
            }
            ////////////////////////////////////////////////////////////////////
            try
            {
                // Convert PDF document to XPS
                Console.WriteLine("Converting PDF to XPS");
                pdftron.PDF.Convert.ToXps(inputPath + "newsletter.pdf", outputPath + "newsletter.xps");
                Console.WriteLine("Saved newsletter.xps");
            }
            catch (PDFNetException e)
            {
                Console.WriteLine(e.Message);
                err = true;
            }

            //////////////////////////////////////////////////////////////////////
            try
            {
                // Convert PDF document to HTML
                Console.WriteLine("Converting PDF to HTML");
                pdftron.PDF.Convert.ToHtml(inputPath + "newsletter.pdf", outputPath + "newsletter");
                Console.WriteLine("Saved newsletter as HTML");
            }
            catch (PDFNetException e)
            {
                Console.WriteLine(e.Message);
                err = true;
            }

            //////////////////////////////////////////////////////////////////////
            try
            {
                // Convert PDF document to EPUB
                Console.WriteLine("Converting PDF to EPUB");
                pdftron.PDF.Convert.ToEpub(inputPath + "newsletter.pdf", outputPath + "newsletter.epub");
                Console.WriteLine("Saved newsletter.epub");
            }
            catch (PDFNetException e)
            {
                Console.WriteLine(e.Message);
                err = true;
            }

            /////////////////////////////////////////////////////////////////////
            try
            {
                // Convert PDF document to multipage TIFF
                Console.WriteLine("Converting PDF to multipage TIFF");
                pdftron.PDF.Convert.TiffOutputOptions tiff_options = new pdftron.PDF.Convert.TiffOutputOptions();
                tiff_options.SetDPI(200);
                tiff_options.SetDither(true);
                tiff_options.SetMono(true);
                pdftron.PDF.Convert.ToTiff(inputPath + "newsletter.pdf", outputPath + "newsletter.tiff", tiff_options);
                Console.WriteLine("Saved newsletter.tiff");
            }
            catch (PDFNetException e)
            {
                Console.WriteLine(e.Message);
                err = true;
            }

            return err;
        }

        static Boolean ConvertToPdfFromFile()
        {
            System.Collections.ArrayList testfiles = new System.Collections.ArrayList();
            testfiles.Add(new ConvertTestCS.Testfile("simple-word_2007.docx", "docx2pdf.pdf", false));
            testfiles.Add(new ConvertTestCS.Testfile("simple-powerpoint_2007.pptx", "pptx2pdf.pdf", false));
            testfiles.Add(new ConvertTestCS.Testfile("simple-excel_2007.xlsx", "xlsx2pdf.pdf", false));
            testfiles.Add(new ConvertTestCS.Testfile("simple-publisher.pub", "pub2pdf.pdf", true));
            testfiles.Add(new ConvertTestCS.Testfile("simple-text.txt", "txt2pdf.pdf", false));
            testfiles.Add(new ConvertTestCS.Testfile("simple-rtf.rtf", "rtf2pdf.pdf", true));
            testfiles.Add(new ConvertTestCS.Testfile("butterfly.png", "png2pdf.pdf", false));
            testfiles.Add(new ConvertTestCS.Testfile("simple-emf.emf", "emf2pdf.pdf", true));
            testfiles.Add(new ConvertTestCS.Testfile("simple-xps.xps", "xps2pdf.pdf", false));
            // testfiles.Add(new ConvertTestCS.Testfile("simple-webpage.mht", "mht2pdf.pdf", true));
            testfiles.Add(new ConvertTestCS.Testfile("simple-webpage.html", "html2pdf.pdf", true));

            bool err = false;
            if (Environment.OSVersion.Platform == PlatformID.Win32NT)
            {
                try
                {
                    if (pdftron.PDF.Convert.Printer.IsInstalled("PDFTron PDFNet"))
                    {
                        pdftron.PDF.Convert.Printer.SetPrinterName("PDFTron PDFNet");
                    }
                    else if (!pdftron.PDF.Convert.Printer.IsInstalled())
                    {
                        try
                        {
                            Console.WriteLine("Installing printer (requires Windows platform and administrator)");
                            pdftron.PDF.Convert.Printer.Install();
                            Console.WriteLine("Installed printer " + pdftron.PDF.Convert.Printer.GetPrinterName());
                            // the function ConvertToXpsFromFile may require the printer so leave it installed
                            // uninstallPrinterWhenDone = true;
                        }
                        catch (PDFNetException e)
                        {
                            Console.WriteLine("ERROR: Unable to install printer.");
                            Console.WriteLine(e.Message);
                            err = true;
                        }
                        catch
                        {
                            Console.WriteLine("ERROR: Unable to install printer. Make sure that the package's bitness matches your operating system's bitness and that you are running with administrator privileges.");
                        }
                    }
                }
                catch (PDFNetException e)
                {
                    Console.WriteLine("ERROR: Unable to install printer.");
                    Console.WriteLine(e.Message);
                    err = true;
                }
            }
            foreach (Testfile file in testfiles)
            {
                if ( Environment.OSVersion.Platform != PlatformID.Win32NT)
                {
                    if (file.requiresWindowsPlatform)
                    {
                        continue;
                    }
                }
                try
                {
                    using (pdftron.PDF.PDFDoc pdfdoc = new PDFDoc())
                    {

                        if (pdftron.PDF.Convert.RequiresPrinter(inputPath + file.inputFile))
                        {
                            Console.WriteLine("Using PDFNet printer to convert file " + file.inputFile);
                        }
                        pdftron.PDF.Convert.ToPdf(pdfdoc, inputPath + file.inputFile);
                        pdfdoc.Save(outputPath + file.outputFile, SDFDoc.SaveOptions.e_linearized);
                        Console.WriteLine("Converted file: " + file.inputFile);
                        Console.WriteLine("to: " + file.outputFile);
                    }
                }
                catch (PDFNetException e)
                {
                    Console.WriteLine("ERROR: on input file " + file.inputFile);
                    Console.WriteLine(e.Message);
                    err = true;
                }
            }

            return err;
        }
        [STAThread]
        static void Main(string [] args)
        {
            PDFNet.Initialize(PDFTronLicense.Key);
            bool err = false;

            err = ConvertToPdfFromFile();
            if (err)
            {
                Console.WriteLine("ConvertFile failed");
            }
            else
            {
                Console.WriteLine("ConvertFile succeeded");
            }

            err = ConvertSpecificFormats();
            if (err)
            {
                Console.WriteLine("ConvertSpecificFormats failed");
            }
            else
            {
                Console.WriteLine("ConvertSpecificFormats succeeded");
            }

            if (Environment.OSVersion.Platform == PlatformID.Win32NT)
            {
                if (pdftron.PDF.Convert.Printer.IsInstalled())
                {
                    try
                    {
                        Console.WriteLine("Uninstalling printer (requires Windows platform and administrator)");
                        pdftron.PDF.Convert.Printer.Uninstall();
                        Console.WriteLine("Uninstalled Printer " + pdftron.PDF.Convert.Printer.GetPrinterName());
                    }
                    catch
                    {
                        Console.WriteLine("Unable to uninstall printer");
                    }
                }
            }
            PDFNet.Terminate();
            Console.WriteLine("Done.");
        }
    }
}
' Copyright (c) 2001-2021 by PDFTron Systems Inc. All Rights Reserved.

Imports System
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports pdftron
Imports pdftron.Common
Imports pdftron.Filters
Imports pdftron.SDF
Imports pdftron.PDF

' The following code showcases the conversion of documents to formats // such as SVG, PDF, EMF, or XPS.

Namespace ConvertTestCS
	Friend Class Testfile
		Public inputFile, outputFile As String
		Public requiresWindowsPlatform As Boolean
		Public Sub New(ByVal inFile As String, ByVal outFile As String, ByVal requiresWindowsPlatform_ As Boolean)
			inputFile = inFile
			outputFile = outFile
			requiresWindowsPlatform = requiresWindowsPlatform_
		End Sub
	End Class

	Friend Class Class1
		Private Shared pdfNetLoader As pdftron.PDFNetLoader = pdftron.PDFNetLoader.Instance()
		Shared Sub New()
		End Sub

		' Relative path to the folder containing test files.
		Private Const inputPath As String = "../../../../TestFiles/"
		Private Const outputPath As String = "../../../../TestFiles/Output/"

		Private Shared Function ConvertSpecificFormats() As Boolean
			Dim err As Boolean = False
			Try
				Using pdfdoc As New PDFDoc()
					Console.WriteLine("Converting from XPS")
					pdftron.PDF.Convert.FromXps(pdfdoc, inputPath & "simple-xps.xps")
					pdfdoc.Save(outputPath & "xps2pdf v2.pdf", SDFDoc.SaveOptions.e_remove_unused)
					Console.WriteLine("Saved xps2pdf v2.pdf")
				End Using
			Catch e As PDFNetException
				Console.WriteLine(e.Message)
				err = True
			End Try

	'///////////////////////////////////////////
	If Environment.OSVersion.Platform = PlatformID.Win32NT Then
				Try
					Using pdfdoc As New PDFDoc()
						Console.WriteLine("Converting from EMF")
						pdftron.PDF.Convert.FromEmf(pdfdoc, inputPath & "simple-emf.emf")
						pdfdoc.Save(outputPath & "emf2pdf v2.pdf", SDFDoc.SaveOptions.e_remove_unused)
						Console.WriteLine("Saved emf2pdf v2.pdf")
					End Using
				Catch e As PDFNetException
					Console.WriteLine(e.Message)
					err = True
				End Try
	End If

		'/////////////////////////////////////////
			Try
				Using pdfdoc As New PDFDoc()
					' Add a dictionary
					Dim [set] As New ObjSet()
					Dim options As Obj = [set].CreateDict()

					' Put options
					options.PutNumber("FontSize", 15)
					options.PutBool("UseSourceCodeFormatting", True)
					options.PutNumber("PageWidth", 12)
					options.PutNumber("PageHeight", 6)

					' Convert from .txt file
					Console.WriteLine("Converting from txt")
					pdftron.PDF.Convert.FromText(pdfdoc, inputPath & "simple-text.txt", options)
					pdfdoc.Save(outputPath & "simple-text.pdf", SDFDoc.SaveOptions.e_remove_unused)
					Console.WriteLine("Saved simple-text.pdf")
				End Using
			Catch e As PDFNetException
				Console.WriteLine(e.Message)
				err = True
			End Try

	'/////////////////////////////////////////
			Try
				Using pdfdoc As New PDFDoc(inputPath & "newsletter.pdf")
					' Convert PDF document to SVG
					Console.WriteLine("Converting pdfdoc to SVG")
					pdftron.PDF.Convert.ToSvg(pdfdoc, outputPath & "pdf2svg v2.svg")
					Console.WriteLine("Saved pdf2svg v2.svg")
				End Using
			Catch e As PDFNetException
				Console.WriteLine(e.Message)
				err = True
			End Try

	'///////////////////////////////////////////////
			Try
				' Convert PNG image to XPS
				Console.WriteLine("Converting PNG to XPS")
				pdftron.PDF.Convert.ToXps(inputPath & "butterfly.png", outputPath & "butterfly.xps")
				Console.WriteLine("Saved butterfly.xps")
			Catch e As PDFNetException
				Console.WriteLine(e.Message)
				err = True
			End Try
	'/////////////////////////////////////////
			If Environment.OSVersion.Platform = PlatformID.Win32NT Then
				Try
					' Convert MSWord document to XPS
					Console.WriteLine("Converting DOCX to XPS")
					pdftron.PDF.Convert.ToXps(inputPath & "simple-word_2007.docx", outputPath & "simple-word_2007.xps")
					Console.WriteLine("Saved simple-word_2007.xps")
				Catch e As PDFNetException
					Console.WriteLine(e.Message)
					err = True
				End Try
			End If
			'//////////////////////////////////////////////////////////////////
			Try
				' Convert PDF document to XPS
				Console.WriteLine("Converting PDF to XPS")
				pdftron.PDF.Convert.ToXps(inputPath & "newsletter.pdf", outputPath & "newsletter.xps")
				Console.WriteLine("Saved newsletter.xps")
			Catch e As PDFNetException
				Console.WriteLine(e.Message)
				err = True
			End Try

			'////////////////////////////////////////////////////////////////////
			Try
				' Convert PDF document to HTML
				Console.WriteLine("Converting PDF to HTML")
				pdftron.PDF.Convert.ToHtml(inputPath & "newsletter.pdf", outputPath & "newsletter")
				Console.WriteLine("Saved newsletter as HTML")
			Catch e As PDFNetException
				Console.WriteLine(e.Message)
				err = True
			End Try

			'////////////////////////////////////////////////////////////////////
			Try
				' Convert PDF document to EPUB
				Console.WriteLine("Converting PDF to EPUB")
				pdftron.PDF.Convert.ToEpub(inputPath & "newsletter.pdf", outputPath & "newsletter.epub")
				Console.WriteLine("Saved newsletter.epub")
			Catch e As PDFNetException
				Console.WriteLine(e.Message)
				err = True
			End Try

			'///////////////////////////////////////////////////////////////////
			Try
				' Convert PDF document to multipage TIFF
				Console.WriteLine("Converting PDF to multipage TIFF")
				Dim tiff_options As New pdftron.PDF.Convert.TiffOutputOptions()
				tiff_options.SetDPI(200)
				tiff_options.SetDither(True)
				tiff_options.SetMono(True)
				pdftron.PDF.Convert.ToTiff(inputPath & "newsletter.pdf", outputPath & "newsletter.tiff", tiff_options)
				Console.WriteLine("Saved newsletter.tiff")
			Catch e As PDFNetException
				Console.WriteLine(e.Message)
				err = True
			End Try

			Return err
		End Function

		Private Shared Function ConvertToPdfFromFile() As Boolean
			Dim testfiles As New System.Collections.ArrayList()
			testfiles.Add(New ConvertTestCS.Testfile("simple-word_2007.docx", "docx2pdf.pdf", False))
			testfiles.Add(New ConvertTestCS.Testfile("simple-powerpoint_2007.pptx", "pptx2pdf.pdf", False))
			testfiles.Add(New ConvertTestCS.Testfile("simple-excel_2007.xlsx", "xlsx2pdf.pdf", False))
			testfiles.Add(New ConvertTestCS.Testfile("simple-publisher.pub", "pub2pdf.pdf", True))
			testfiles.Add(New ConvertTestCS.Testfile("simple-text.txt", "txt2pdf.pdf", False))
			testfiles.Add(New ConvertTestCS.Testfile("simple-rtf.rtf", "rtf2pdf.pdf", True))
			testfiles.Add(New ConvertTestCS.Testfile("butterfly.png", "png2pdf.pdf", False))
			testfiles.Add(New ConvertTestCS.Testfile("simple-emf.emf", "emf2pdf.pdf", True))
			testfiles.Add(New ConvertTestCS.Testfile("simple-xps.xps", "xps2pdf.pdf", False))
			' testfiles.Add(new ConvertTestCS.Testfile("simple-webpage.mht", "mht2pdf.pdf", true));
			testfiles.Add(New ConvertTestCS.Testfile("simple-webpage.html", "html2pdf.pdf", True))

			Dim err As Boolean = False
			If Environment.OSVersion.Platform = PlatformID.Win32NT Then
				Try
					If pdftron.PDF.Convert.Printer.IsInstalled("PDFTron PDFNet") Then
						pdftron.PDF.Convert.Printer.SetPrinterName("PDFTron PDFNet")
					ElseIf Not pdftron.PDF.Convert.Printer.IsInstalled() Then
						Try
							Console.WriteLine("Installing printer (requires Windows platform and administrator)")
							pdftron.PDF.Convert.Printer.Install()
							Console.WriteLine("Installed printer " & pdftron.PDF.Convert.Printer.GetPrinterName())
							' the function ConvertToXpsFromFile may require the printer so leave it installed
							' uninstallPrinterWhenDone = true;
						Catch e As PDFNetException
							Console.WriteLine("ERROR: Unable to install printer.")
							Console.WriteLine(e.Message)
							err = True
						Catch
							Console.WriteLine("ERROR: Unable to install printer. Make sure that the package's bitness matches your operating system's bitness and that you are running with administrator privileges.")
						End Try
					End If
				Catch e As PDFNetException
					Console.WriteLine("ERROR: Unable to install printer.")
					Console.WriteLine(e.Message)
					err = True
				End Try
			End If
			For Each file As Testfile In testfiles
				If Environment.OSVersion.Platform <> PlatformID.Win32NT Then
					If file.requiresWindowsPlatform Then
						Continue For
					End If
				End If
				Try
					Using pdfdoc As pdftron.PDF.PDFDoc = New PDFDoc()

						If pdftron.PDF.Convert.RequiresPrinter(inputPath & file.inputFile) Then
							Console.WriteLine("Using PDFNet printer to convert file " & file.inputFile)
						End If
						pdftron.PDF.Convert.ToPdf(pdfdoc, inputPath & file.inputFile)
						pdfdoc.Save(outputPath & file.outputFile, SDFDoc.SaveOptions.e_linearized)
						Console.WriteLine("Converted file: " & file.inputFile)
						Console.WriteLine("to: " & file.outputFile)
					End Using
				Catch e As PDFNetException
					Console.WriteLine("ERROR: on input file " & file.inputFile)
					Console.WriteLine(e.Message)
					err = True
				End Try
			Next file

			Return err
		End Function
		<STAThread>
		Shared Sub Main(ByVal args() As String)
			PDFNet.Initialize(PDFTronLicense.Key)
			Dim err As Boolean = False

			err = ConvertToPdfFromFile()
			If err Then
				Console.WriteLine("ConvertFile failed")
			Else
				Console.WriteLine("ConvertFile succeeded")
			End If

			err = ConvertSpecificFormats()
			If err Then
				Console.WriteLine("ConvertSpecificFormats failed")
			Else
				Console.WriteLine("ConvertSpecificFormats succeeded")
			End If

			If Environment.OSVersion.Platform = PlatformID.Win32NT Then
				If pdftron.PDF.Convert.Printer.IsInstalled() Then
					Try
						Console.WriteLine("Uninstalling printer (requires Windows platform and administrator)")
						pdftron.PDF.Convert.Printer.Uninstall()
						Console.WriteLine("Uninstalled Printer " & pdftron.PDF.Convert.Printer.GetPrinterName())
					Catch
						Console.WriteLine("Unable to uninstall printer")
					End Try
				End If
			End If
			PDFNet.Terminate()
			Console.WriteLine("Done.")
		End Sub
	End Class
End Namespace
VB   C#

5.2 使用 IronOCR

PDF 檔案的管理可以使用 OCRInput 函數來完成。文件中的每一頁都將由 Iron Tesseract 類讀取。然後,將從這些頁面中提取文字。另一個名為 "AddPDF" 的函數將允許我們打開受保護的文件,並確保我們可以將 PDF 添加到我們的文件列表中。 (如果受到保護的密碼)要打開受密碼保護的 PDF 文件,請使用以下代碼片段:

var Ocr = new IronTesseract(); // nothing to configure
using (var Input = new OcrInput())
{
    Input.AddPdf("example.pdf", "password");
    var Result = Ocr.Read(Input);
    Console.WriteLine(Result.Text);
}
var Ocr = new IronTesseract(); // nothing to configure
using (var Input = new OcrInput())
{
    Input.AddPdf("example.pdf", "password");
    var Result = Ocr.Read(Input);
    Console.WriteLine(Result.Text);
}
Dim Ocr = New IronTesseract() ' nothing to configure
Using Input = New OcrInput()
	Input.AddPdf("example.pdf", "password")
	Dim Result = Ocr.Read(Input)
	Console.WriteLine(Result.Text)
End Using
VB   C#

從 PDF 文件的某一頁讀取和提取內容可以通過使用 "AddPdfPage" 函數來實現。只需指定我們要提取文本的具體頁碼。"AddPdfPage" 將允許您提取您指定的多個頁面的文本。IEnumerable將允許您高效地指定多個頁面。您還必須包括文件的位置和擴展名。以下代碼段演示了這一點:

IEnumerable<int> numbers = new List<int> {2,8,10 };
 var Ocr = new IronTesseract();
using (var Input = new OcrInput())
{
    //single page
    Input.AddPdfPage("example.pdf",10);
    //Multiple page
    Input.AddPdfPages("example.pdf", numbers);
    var Result = Ocr.Read(Input);
    Console.WriteLine(Result.Text);
    Result.SaveAsTextFile("ocrtext.txt");
}
IEnumerable<int> numbers = new List<int> {2,8,10 };
 var Ocr = new IronTesseract();
using (var Input = new OcrInput())
{
    //single page
    Input.AddPdfPage("example.pdf",10);
    //Multiple page
    Input.AddPdfPages("example.pdf", numbers);
    var Result = Ocr.Read(Input);
    Console.WriteLine(Result.Text);
    Result.SaveAsTextFile("ocrtext.txt");
}
Dim numbers As IEnumerable(Of Integer) = New List(Of Integer) From {2, 8, 10}
 Dim Ocr = New IronTesseract()
Using Input = New OcrInput()
	'single page
	Input.AddPdfPage("example.pdf",10)
	'Multiple page
	Input.AddPdfPages("example.pdf", numbers)
	Dim Result = Ocr.Read(Input)
	Console.WriteLine(Result.Text)
	Result.SaveAsTextFile("ocrtext.txt")
End Using
VB   C#

使用 SaveAsTextFile 函數將結果直接存儲為文本文件格式,這樣您可以將文件直接下載到輸出目錄路徑。要將文件保存為 HTML 格式,請使用 SaveAsHocrFile。

6.1 使用 PDFTron

我們可以使用 PDFTron SDK 從 PDF 文件中提取圖像,以及它們的位置資訊和 DPI。除了將 PDF 圖像轉換成位圖外,您還可以使用 elements.GetImageData 直接提取未壓縮/壓縮的圖像數據() (如 PDF 資料提取程式碼範例中所述)了解更多關於我們的C# PDF庫以及PDF解析和內容提取庫。

6.2 使用 IronOCR

IronOCR 擁有驚人的多項功能,能夠讓您直接從掃描的文件中讀取 QR 碼和條碼。以下代碼片段展示了如何從給定的圖像或文件中掃描條碼。

var Ocr = new IronTesseract(); // nothing to configure
Ocr.Language = OcrLanguage.EnglishBest;
Ocr.Configuration.ReadBarCodes = true;
Ocr.Configuration.TesseractVersion = TesseractVersion.Tesseract5;
using (var Input = new OcrInput())
{
    Input.AddImage("barcode.gif");
    var Result = Ocr.Read(Input);

    foreach (var Barcode in Result.Barcodes)
    {
        Console.WriteLine(Barcode.Value);
    }
}
var Ocr = new IronTesseract(); // nothing to configure
Ocr.Language = OcrLanguage.EnglishBest;
Ocr.Configuration.ReadBarCodes = true;
Ocr.Configuration.TesseractVersion = TesseractVersion.Tesseract5;
using (var Input = new OcrInput())
{
    Input.AddImage("barcode.gif");
    var Result = Ocr.Read(Input);

    foreach (var Barcode in Result.Barcodes)
    {
        Console.WriteLine(Barcode.Value);
    }
}
Dim Ocr = New IronTesseract() ' nothing to configure
Ocr.Language = OcrLanguage.EnglishBest
Ocr.Configuration.ReadBarCodes = True
Ocr.Configuration.TesseractVersion = TesseractVersion.Tesseract5
Using Input = New OcrInput()
	Input.AddImage("barcode.gif")
	Dim Result = Ocr.Read(Input)

	For Each Barcode In Result.Barcodes
		Console.WriteLine(Barcode.Value)
	Next Barcode
End Using
VB   C#

以上是幫助從給定的圖片或PDF文件中讀取條形碼的代碼。在單個圖片或頁面中可以同時讀取多個條形碼。IronOCR有一種獨特的方法來讀取條形碼,即Ocr.Configuration.ReadBarCodes。

掃描輸入後,數據將存儲在一個稱為OCRResult的對象中。在OCRResult中有一個名為Barcodes的屬性,其中將包含所有可用的條形碼數據列表。我們可以利用for-each循環獲取每個與條形碼詳情相關的個別數據。在單個過程中完成兩個操作——掃描和讀取條形碼的值。

還支持多線程選項,可以同時完成多個OCR過程。此外,IronOCR還可以從指定的區域識別精確的區域。

var Ocr = new IronTesseract();
using (var Input = new OcrInput())
{
    var ContentArea = new System.Drawing.Rectangle() { X = 215, Y = 1250, Height = 280, Width = 1335 };
    Input.Add("document.png", ContentArea);
    var Result = Ocr.Read(Input);
    Console.WriteLine(Result.Text);
}
var Ocr = new IronTesseract();
using (var Input = new OcrInput())
{
    var ContentArea = new System.Drawing.Rectangle() { X = 215, Y = 1250, Height = 280, Width = 1335 };
    Input.Add("document.png", ContentArea);
    var Result = Ocr.Read(Input);
    Console.WriteLine(Result.Text);
}
Dim Ocr = New IronTesseract()
Using Input = New OcrInput()
	Dim ContentArea = New System.Drawing.Rectangle() With {
		.X = 215,
		.Y = 1250,
		.Height = 280,
		.Width = 1335
	}
	Input.Add("document.png", ContentArea)
	Dim Result = Ocr.Read(Input)
	Console.WriteLine(Result.Text)
End Using
VB   C#

上面的代碼片段展示了如何對特定區域進行光學字符識別(OCR)。您只需指定 PDF/圖像中的矩形區域,IronOCR 的 Tesseract 引擎將幫助識別文字。

IronOCR和PDFtron OCR授權模型和定價

IronOCR 授權模式和定價

30 天退款保証: 一旦購買了授權,您將獲得 30 天的退款保証。在 30 天內,如果您希望退回產品,將會收到退款。

簡便的整合: IronOCR 與任何專案和環境的整合非常簡單,只需透過加入單行程式碼即可完成整合。 NuGet 套件。另一方面,另一種整合環境的方法是直接從網絡下載。

永久授權: 每個購買的授權都不需要續訂。

免費支持和產品更新: 每個授權都將直接來自產品背後的團隊支持,並附帶一年的免費產品更新。您可以隨時購買延長產品更新。

即時授權: 一旦收到付款,註冊的授權密鑰將立即發送。

所有授權都是永久的,並適用於開發、測試和生產環境。

Lite 授權

  • 1 位開發人員
  • 1 個地點
  • 1 個專案
  • 永久授權

此方案允許組織中的一位軟體開發人員在一個地點使用 Iron Software。Iron Software 可以在單一內部網應用程式、網路應用程式或桌面軟體中使用。嚴禁在組織外部或代理/客戶關係中共享授權,因為這些授權是不可轉讓的。此授權類型,與所有其他授權類型相同,中明確排除在沒有 OEM 再分配和未購買額外覆蓋範圍下作為 SaaS 使用 Iron Software 未經明確授權下授予的所有權利。

價格: 每年起價 $749。

專業授權

  • 10 位開發人員
  • 10 個地點
  • 10 個項目
  • 永久授權

此授權允許一個組織中的預定數量的軟體開發人員在多個地點(最多十個)使用 Iron Software。Iron Software 可以用於任意數量的網站、內聯網應用程序或桌面軟體應用程序。授權是不可轉讓的,並且不能在組織外部或代理/客戶關係外共享。此授權類型與所有其他授權類型一樣,明確排除未在協議中明確授予的所有權利,包括 OEM 重新分發和在未購買額外涵蓋範圍的情況下將 Iron Software 用作 SaaS。此授權最多可合併單一項目達 10 個。

價格: 每年從 $999 起。

無限授權

  • 無限開發人員
  • 無限地點
  • 無限項目
  • 永續授權

這允許組織內的無限數量的軟體開發人員在無限數量的地點使用 Iron Software。Iron Software 可以應用在任意數量的內聯網應用程式、桌面軟體應用程式或網站上。授權是不可轉讓的,並且不能在組織或代理/客戶關係之外共享。這種許可類型,像所有其他許可類型一樣,明確排除了在協議中未授予的所有權利,包括 OEM 重新分發和在未購買額外覆蓋的情況下利用 Iron Software 作為 SaaS。

價格: 每年起價 $2,999。

免權利金重新分發 — 這允許您將 Iron Software 作為幾個不同包裝的商業產品的一部分進行分發。 (無需支付版稅) 根據基本許可證所涵蓋的項目數量。這將允許 Iron Software 部署在 SaaS 軟體服務中,這是基於基本許可證所涵蓋的項目數量。

價格: 從每年 $1,599 起。

PDFTron授權模式和定價

PDFTron 套件 (自訂授權)

  • 自訂授權的價格各不相同——獲取符合您特定預算的報價。
  • 部署PDFTron的強大文件查看和編輯技術,用於網頁、移動和桌面平台上的渲染和文件處理
  • 準備好集成、OEM再分發以及擁有大量文件或特殊需求的組織
  • 多域名定價和有利的多年折扣
  • 支持離線和沙箱操作
  • 自訂、全面的合同條款
  • 咨詢和培訓服務

PDFTron自訂授權經過精心調整,以匹配您的應用程序和業務需求。定價取決於您的功能範圍。

IronOCRLite授權是一個未定義的套件,包括一位開發人員,一年的支援,費用大約是$749。IronOCR專業授權包括10名開發人員套件和一年支援,費用為$999,而PDFTron套件尚未定價。要購買套件,您需要聯絡支援中心以獲取報價。

IronOCRLite專業套件包括OEM或SaaS服務,並提供五年的支援選項。Lite版本包括一位開發人員套件,五年支援,並提供SaaS和OEM服務,費用為$2,897,並可選擇自定義支援選項。IronOCR專業版本包括10名開發人員套件,五年支援,SaaS和OEM服務,費用為$3,397。PDFTron的10名開發人員套件包括一年支援,SaaS和OEM服務,尚未定價。

7.0 結論

IronOCR 在 .NET Framework 上提供易於使用的 Tesseract,支援多種方式處理照片及 PDF 文件。它還提供多種設置來提升 Tesseract OCR 庫的性能。支援多種語言,且能在一次操作中處理多種語言。訪問他們的網站以了解更多關於 Tesseract OCR 的資訊。

PDFTron 是一款使用不同引擎來識別圖片和 PDF 文件的軟體應用程式。它同樣提供多種設置來改進 OCR 過程的性能以及選擇多種語言的選項。PDFTron 對於頁面轉換的使用有局限,也在不同操作系統上有不同的價格。

IronOCR 是一款具競爭力的軟體產品,其準確性可以高於競爭品牌。同類產品有時無法識別低品質圖像,導致未知字元。另一方面,IronOCR 不僅提供精確的結果,還能識別條碼數據並從圖像中讀取條碼的價值。

IronOCR 套件在所有平台上提供具競爭力的許可和支援,價格單一。相比之下,PDFtrons OCR 產品都是專門定製的,價格通常較高。兩者的價格有所不同,其中 IronOCR 起價為 $749,而由於 PDFTron 的定製選項,其起價未定。總的來說,IronOCR 提供了更多的功能,價格更低。

那麼,還等什麼呢?免費試用對所有人開放。獲得 許可證 在此立即開始!

< 上一頁
Tesseract 替代方案(2022 更新)
下一個 >
IronOCR 與 Tesseract.NET 的比較

準備開始了嗎? 版本: 2024.10 剛剛發布

免費 NuGet 下載 總下載次數: 2,433,305 查看許可證 >