行業

如何使用C#將HTML轉換為PDF和法律行業的OCR文件

在當今快速變化的數位環境中,自動化在提升效率與準確性方面扮演著關鍵角色。 在法律自動化中,一項常見的任務是將多個HTML檔案轉換成一個可搜尋的PDF文件。

在本指南中,我們將探索如何使用Iron Suite,這是一套專為.NET環境量身打造的強大開發工具組,來簡化這一過程。

Iron Suite:為.NET開發者準備的綜合工具包

Iron SoftwareIron Suite是一套全面的.NET軟體組件集合,旨在簡化開發流程並提升使用各種.NET框架開發者的生產力。 Iron Software作為.NET軟體工具的領先提供者,提供Iron Suite作為開發者尋求高質量組件的單一解決方案,適用於文件操作到條碼生成等多種任務。

Iron Suite for .NET:適合您辦公室的.NET Suite

Iron Suite的核心是為應對軟體開發專案中常見挑戰而量身訂製的豐富程式庫和工具集合。 以下是Iron Suite的一些關鍵組件概覽:

  • IronPDF:用於創建、編輯和管理PDF文件,包括將HTML轉換為PDF。
  • IronWord:可以創建和編輯DOC和DOCX檔案。
  • IronXL:允許直接處理Excel檔案,實現讀取、編輯和創建,不需要Microsoft Office或Excel Interop。
  • IronOCR:提供光學字元識別功能,從超過125種語言的圖像中提取文字。
  • IronBarcode:提供讀寫各種條碼格式的能力,包括QR碼。
  • IronQR:專注於生成、讀取和設計QR碼。
  • IronZIP:可以壓縮和解壓縮ZIP格式的檔案。
  • IronPrint:提供管理列印任務並直接從您的.NET程式碼與印表機互動的功能。
  • IronWebScraper:專為高效抓取網站的結構化資料而設計。

使用Iron Suite進行HTML到PDF轉換和OCR分析

考慮這樣一個場景,您需要處理包含HTML文件的zip檔案,您的目標是將這些HTML文件轉換為PDF格式,以進行後續的OCR(光學字元識別)分析。 為了有效和高效地完成此任務,利用Iron Suite的三個基本程式庫的功能是最佳解決方案。 讓我們簡要介紹一下各個程式庫。

IronZIP - C# Zip Archive .NET程式庫

簡化檔案壓縮和提取,IronZIP可以在C#應用程式中無縫處理zip檔案。 其直觀的API允許輕鬆從zip檔案中提取HTML文件。

Use Csharp Convert Html To Pdf Ocr Documents For Legal Industry 2 related to IronZIP - C# Zip Archive .NET程式庫

IronZIP的一些關鍵功能:

  • 壓縮和解壓縮:支援多種檔案格式,如ZIP、ZIPX、RAR、7Z等。
  • 加密和解密:使用傳統密碼或更強的AES加密(默認為256位)來保護您的檔案。
  • 流操作:高效處理大型檔案,而無需將整個檔案載入記憶體。
  • 自解壓檔案建立:創建可執行檔案,無需IronZIP即可提取。

IronPDF - C# PDF程式庫

作為一個強大的PDF渲染和操控程式庫,IronPDF可以直接在C#程式碼中創建和操作PDF文件。 具有如HTML到PDF轉換等功能,這對於我們將HTML文件轉換為PDF格式的任務非常完美。

IronPDF for .NET:C#的PDF程式庫

IronPDF DLL的一些關鍵特性:

  • HTML到PDF轉換:將HTML字串內容或HTML文件,包括CSS和JavaScript,轉換成高質量的PDF文件。
  • PDF創建和操控:從零創建新的PDF文件,合併現有PDF文件或提取頁面。
  • 頁面定制:控制頁面佈局、邊距、頁眉、頁腳和浮水印。
  • 表單處理:從HTML來源捕獲表單數據並填寫PDF表單。

IronOCR - C# OCR程式庫

作為文字提取的重要工具,IronOCR專長於從多種來源(包括掃描文檔和PDF文件)中提取文本。 其多功能性使其成為從我們生成的PDF文件中提取可搜尋文本的理想選擇。

IronOCR for .NET:C#的OCR程式庫

IronOCR的一些關鍵特性:

  • 光學字元識別:從掃描文檔、影像和PDF中以高準確度提取文本。
  • 多語言支援:能夠識別超過125種語言的文本。
  • 佈局分析:最大限度地保持提取文本的原始格式。
  • 自訂準確度:通過不同的配置來調整OCR準確度。

創建Visual Studio專案

首先,為我們的法律自動化任務創建一個新的Visual Studio專案。 確保您已設置好.NET開發所需的環境。 按以下步驟在Visual Studio中創建一個專案:

  1. 開啟Visual Studio並點擊"創建一個新專案"選項。

    Open Visual Studio IDE and click on Create a new project option.

  2. 根據需求選擇合適的專案模板(如控制台應用程式,Windows窗體應用程式)。

    For the new project, select a Console App in C#.

  3. 指定專案名稱和位置,然後點擊"下一步"。

    通過指定專案名稱、位置和解決方案名稱來設定專案。

  4. 從附加資訊中選擇最新的.NET Framework。

    接著選擇.NET框架並點擊創建。

  5. 點擊"創建"來創建專案。

安裝程式庫

接下來,將三個基本程式庫:IronZIP、IronPDF和IronOCR安裝到您的專案中。 您可以輕鬆地從Iron Software的網站上下載相應的軟體包,或在Visual Studio中使用NuGet套件管理器。

使用NuGet套件管理器安裝到解決方案

要將程式庫整合到您的.NET專案中:

  • 在您的Visual StudioC# ConsoleApp中,右鍵點擊Solution Explorer中的專案,選擇"管理NuGet Packages for Solution"

IronZIP

  1. 在NuGet套件管理器窗口中搜尋"IronZIP"。

    Install IronZIP using the Manage NuGet Package for Solution by searching IronZIP in the search bar of NuGet Package Manager, then select the project and click on the Install button.

  2. 從搜尋結果中選擇"IronZIP"並點擊"安裝"按鈕。

  3. NuGet將自動下載並將必要的相依性添加到您的專案中。

IronPDF

  1. 在NuGet套件管理器窗口中搜尋"IronPDF"。

    Install IronPDF using the Manage NuGet Package for Solution by searching IronPDF in the search bar of NuGet Package Manager, then select the project and click on the Install button.

  2. 從搜尋結果中選擇"IronPDF"並點擊"安裝"按鈕。

IronOCR

  1. 在NuGet套件管理器窗口中搜尋"IronOCR"。

    Install IronOCR using the Manage NuGet Package for Solution by searching IronOCR in the search bar of NuGet Package Manager, then select the project and click on the Install button.

  2. 從搜尋結果中選擇"IronOCR"並點擊"安裝"按鈕。

將HTML文件轉換為PDF文件的步驟

現在,要輕鬆將HTML轉換為PDF並隨後進行OCR,我們設置了Iron Suite的程式庫,每個程式庫都提供了針對我們任務的獨特功能。 將使用以下zip檔案提取多個HTML網頁檔案,然後將HTML檔案轉換為PDF文件格式以進行OCR分析:

ZIP archive website.zip containing three HTML files

上面截圖中顯示的Zip檔案包含從一個簡單網站中的3個HTML網頁。這些HTML頁面包含簡單的HTML代碼,將傳遞給PDF轉換方法以將HTML文件轉換為單個PDF文檔。 然後將應用OCR分析生成的PDF文檔中的HTML元素,並在控制台窗口中打印所有HTML內容。

1. 包含Iron Suite程式庫(.NET命名空間)

要在我們的C#專案中利用Iron Suite提供的功能,我們需要引用每個程式庫的相應命名空間。 以下是如何在我們的Program.cs文件中包含它們:

using IronZip;
using IronPdf;
using IronOcr;
using IronZip;
using IronPdf;
using IronOcr;
Imports IronZip
Imports IronPdf
Imports IronOcr
$vbLabelText   $csharpLabel

2. 使用IronZIP從Zip中提取HTML文件

首先,我們將利用IronZIP的直觀API從zip檔案中提取HTML文件。通過簡單的方法調用,我們可以高效地提取必要的文件以進行轉換過程。

// Extract all contents of the ZIP file to a specified directory
IronZipArchive.ExtractArchiveToDirectory("assets/website.zip", "extracted");
// Extract all contents of the ZIP file to a specified directory
IronZipArchive.ExtractArchiveToDirectory("assets/website.zip", "extracted");
' Extract all contents of the ZIP file to a specified directory
IronZipArchive.ExtractArchiveToDirectory("assets/website.zip", "extracted")
$vbLabelText   $csharpLabel

要充分利用IronZIP程式庫,請訪問此文件頁面。

輸出 - 提取的HTML文件

Output: HTML files extracted from the archive website.zip using IronZIP.

3. 使用IronPDF將HTML轉換為PDF

其次,我們將使用IronPDF的強大功能將所有提取的HTML文件輕易轉換成單一的PDF文檔。 透過其直截了當的API,我們可以輕鬆在C#代碼中生成高質量的PDF文件。

// Create a ChromePdfRenderer object for rendering HTML to PDF (consider using IronHtmlRenderer for better HTML to PDF conversion)
ChromePdfRenderer renderer = new ChromePdfRenderer();

// Initialize an empty list to store the individual PDF documents
List<PdfDocument> pdfs = new List<PdfDocument>();

// Get all HTML files with the .html extension from the "extracted" folder
var htmlFiles = Directory.EnumerateFiles("extracted", "*.html");

// Loop through each HTML file
foreach (var htmlFile in htmlFiles)
{
    // Render the current HTML file as a PDF document using the ChromePdfRenderer
    var pdf = renderer.RenderHtmlFileAsPdf(htmlFile);

    // Add the generated PDF document to the list
    pdfs.Add(pdf);
}

// Merge all the individual PDF documents in the list into a single PDF document
var document = PdfDocument.Merge(pdfs);

// Save the merged PDF document as "HtmlToPDF.pdf"
document.SaveAs("HtmlToPDF.pdf");
// Create a ChromePdfRenderer object for rendering HTML to PDF (consider using IronHtmlRenderer for better HTML to PDF conversion)
ChromePdfRenderer renderer = new ChromePdfRenderer();

// Initialize an empty list to store the individual PDF documents
List<PdfDocument> pdfs = new List<PdfDocument>();

// Get all HTML files with the .html extension from the "extracted" folder
var htmlFiles = Directory.EnumerateFiles("extracted", "*.html");

// Loop through each HTML file
foreach (var htmlFile in htmlFiles)
{
    // Render the current HTML file as a PDF document using the ChromePdfRenderer
    var pdf = renderer.RenderHtmlFileAsPdf(htmlFile);

    // Add the generated PDF document to the list
    pdfs.Add(pdf);
}

// Merge all the individual PDF documents in the list into a single PDF document
var document = PdfDocument.Merge(pdfs);

// Save the merged PDF document as "HtmlToPDF.pdf"
document.SaveAs("HtmlToPDF.pdf");
' Create a ChromePdfRenderer object for rendering HTML to PDF (consider using IronHtmlRenderer for better HTML to PDF conversion)
Dim renderer As New ChromePdfRenderer()

' Initialize an empty list to store the individual PDF documents
Dim pdfs As New List(Of PdfDocument)()

' Get all HTML files with the .html extension from the "extracted" folder
Dim htmlFiles = Directory.EnumerateFiles("extracted", "*.html")

' Loop through each HTML file
For Each htmlFile In htmlFiles
	' Render the current HTML file as a PDF document using the ChromePdfRenderer
	Dim pdf = renderer.RenderHtmlFileAsPdf(htmlFile)

	' Add the generated PDF document to the list
	pdfs.Add(pdf)
Next htmlFile

' Merge all the individual PDF documents in the list into a single PDF document
Dim document = PdfDocument.Merge(pdfs)

' Save the merged PDF document as "HtmlToPDF.pdf"
document.SaveAs("HtmlToPDF.pdf")
$vbLabelText   $csharpLabel

IronPDF不僅提供從HTML文件、HTML表單、HTML字符串或URL的PDF轉換,還提供從其他格式到PDF的轉換。 有關更詳細的信息和即用型樣本代碼片段,請訪問此文件代碼示例頁面。

輸出 - PDF

PDF生成輸出:將提取的HTML文件轉換為PDF文件,然後使用IronPDF將轉換的PDF文件合併成一個單一的PDF文件。

輸出的PDF清晰地在單獨的一頁上顯示每個HTML頁面內容,使用IronPDF強大的Chromium HTML轉換引擎。

除了此以外,您還可以使用IronPrint for .NET - C#列印程式庫來列印生成的PDF文件。IronPrint能有效地將PDF或影像傳送到預設的印表機進行列印。

有關如何使用IronPrint列印文檔的更多信息,請訪問此文件頁面。

4. 使用IronOCR提取文本

最後,使用IronOCR從生成的PDF文件中提取可搜尋的文本。 通過利用IronOCR的先進文本提取功能,我們可以確保提取的文本是準確的並準備進一步處理。

以下代碼片段取自IronPDF生成的PDF文件,並成功執行OCR以進行進一步分析:

// Create an IronTesseract object for Optical Character Recognition (OCR)
var ocrTesseract = new IronTesseract();

// Create an OcrInput object to specify the input for OCR processing
using var ocrInput = new OcrInput();

// Load the PDF document for text extraction
ocrInput.LoadPdf("HtmlToPDF.pdf");

// Perform OCR on the loaded PDF using the IronTesseract engine
var ocrResult = ocrTesseract.Read(ocrInput);

// Print the extracted text to the console
Console.WriteLine(ocrResult.Text);
// Create an IronTesseract object for Optical Character Recognition (OCR)
var ocrTesseract = new IronTesseract();

// Create an OcrInput object to specify the input for OCR processing
using var ocrInput = new OcrInput();

// Load the PDF document for text extraction
ocrInput.LoadPdf("HtmlToPDF.pdf");

// Perform OCR on the loaded PDF using the IronTesseract engine
var ocrResult = ocrTesseract.Read(ocrInput);

// Print the extracted text to the console
Console.WriteLine(ocrResult.Text);
' Create an IronTesseract object for Optical Character Recognition (OCR)
Dim ocrTesseract = New IronTesseract()

' Create an OcrInput object to specify the input for OCR processing
Dim ocrInput As New OcrInput()

' Load the PDF document for text extraction
ocrInput.LoadPdf("HtmlToPDF.pdf")

' Perform OCR on the loaded PDF using the IronTesseract engine
Dim ocrResult = ocrTesseract.Read(ocrInput)

' Print the extracted text to the console
Console.WriteLine(ocrResult.Text)
$vbLabelText   $csharpLabel

有關更詳細的文本分析,請訪問這個現成的代碼示例頁面這裡

輸出 - PDF文本

控制台輸出:使用IronOCR從PDF文件中提取文本

輸出的結果不言而喻:快速準確、且無誤,證明了IronOCR的高效性。

為何選擇Iron Suite?

Iron Suite作為市場領先的.NET套件脫穎而出,專為您的辦公文件提供多個令人信服的理由,其優越之處無與倫比。

1. 9種產品的價格相當於2種產品:

通過Iron Suite,您可以以兩個單獨產品的價格購得全部九款Iron Software產品。 這個令人難以置信的價值主張確保您擁有一個全面的工具包,而不會超出預算。

Iron Suite授權信息。

2. 跨平台兼容性:

Iron Suite設計為能在多個平台上無縫運作,包括Windows、macOS、Linux、Docker、Azure和AWS。 無論您是在為桌面、網頁還是雲環境開發應用程序,Iron Suite都能滿足您的需求。

3. 快速設置:

從您下載Iron Suite到在生產環境中部署,您可以在五分鐘內啟動並運行。 簡單明瞭的安裝過程和直觀的API令開發人員可以在最少的時間設置下開始利用該套件的功能。

4. 詳盡的文件:

告別猜測和反覆試錯。 Iron Suite附帶每個組件的詳細文檔和示例,確保開發人員能夠掌握清晰的指導和資源,提高生產力。

5. 24/5技術支援:

需要幫助或對使用Iron Suite有疑問?專業工程師團隊隨時準備為您提供5天×24小時的技術支援,解決您遇到的任何問題。 您可以放心,幫助隨時隨地只需一條訊息。

6. 退款保證:

Iron Suite提供30天退款保證。 如果因任何原因您對您的購買感到不完全滿意,只需在30天內告知Iron團隊,他們將無條件退款給您。

7. 開始您的免費試用:

想親自體驗Iron Suite的強大和多功能性嗎? 立即開始您的免費試用,發現這套.NET文件工具綜合套件如何簡化您的開發流程並開創專案的新可能。

結論

總之,Iron Suite為.NET開發人員提供了一套全面的工具包,用於簡化法律自動化任務,如生成PDF文件和將HTML文件轉換為可搜尋的PDF文件。 通過利用IronZIPIronPDFIronOCR的力量,開發人員可以自動化並優化其工作流程,最終提高法律文件處理的效率和準確性。 有了Iron Suite在手,無限的自動化可能性正等待您探索。

對於HTML到PDF的轉換任務,我們使用了Iron Suite的三個基礎程式庫:IronZIPIronPDFIronOCRIronPrint如果需要列印功能,這也是一個潛在的選擇。 如果單獨購買這四個程式庫,總花費約為$2,998 * 4 = $2,996。

不過,購買Iron Suite,不僅獲得三個或者四個,而是以僅相當於兩個單獨產品的價格獲得九個強大的程式庫。 這是一個令人難以置信的價值主張,提供您滿足所有.NET開發需求的全面工具。 僅需$1,498,Iron Suite提供了出色的價值,節省您時間和金錢,同時為您配備廣泛的工具來簡化您的開發工作流程。