行業

如何使用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套件

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文件的壓縮文件,您的目標是將這些HTML文件轉換為PDF格式,然後進行後續的OCR(光學字元識別)分析。 要有效且高效地完成此任務,最優解決方案就是利用Iron Suite的三個基本程式庫的功能。 讓我們簡要地了解每一個程式庫。

IronZIP - C#壓縮檔案.NET程式庫

簡化文件壓縮和提取,IronZIP使在C#應用中無縫處理壓縮档案。 其直觀的API允許輕鬆從我們的壓縮档案中提取HTML文件。

Use Csharp Convert Html To Pdf Ocr Documents For Legal Industry 2 related to IronZIP - C#壓縮檔案.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 Forms應用)。

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

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

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

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

    接下來,選擇.NET Framework並點擊建立。

  5. 點擊"建立"以建立專案。

安裝程式庫

接下來,將三個基本程式庫——IronZIP、IronPDF和IronOCR——安裝到您的專案中。 您可以通過從Iron Software的網站下載相應的套件或使用Visual Studio中的NuGet套件管理器輕鬆完成此操作。

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

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

  • 在您的Visual StudioC# ConsoleApp中,右擊您的專案在解決方案資源管理器中選擇"管理NuGet套件方案"

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中的程式庫,每個程式庫提供專為我們任務量身定制的獨特功能。 將使用以下壓縮檔案,其中包含多個HTML網頁文件,以便提取HTML文件,然後將HTML文件轉換為PDF文件格式以進行OCR分析:

ZIP archive website.zip containing three HTML files

上圖中顯示的Zip文件包含一個簡單網站的3個HTML網頁。HTML頁面包含會被傳遞給PDF轉換方法的簡單HTML程式碼,用於將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從壓縮档案中提取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,我們可以輕鬆生成高品質的PDF,直接在我們的C#程式碼中。

// 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頁面的內容在單獨的PDF頁面上,這要歸功於IronPDF強大的Chromium HTML轉換引擎。

除此之外,您還可以使用IronPrint for .NET - The C# Printing Library來列印生成的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. 2的價錢買9個:

使用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有問題?專門的工程師團隊每周五天全天候提供技術支援,處理您可能遇到的任何問題。 放心,幫助總是隨時待命。

6. 退款保証:

Iron Suite提供30天退款保証。 如果出於任何原因您對購買不完全滿意,只需要在30天內告知Iron團隊,他們便會退還您的付款,無需任何問題。

7. 開始免費試用:

準備好親身體驗Iron Suite的威力和多功能性了嗎? 立即開始您的免費試用期,並發現這套全面的.NET文件工具如何能夠簡化您的開發工作流程並為您的專案開啟全新的可能性。

結論

總之,Iron Suite為.NET開發者提供了一個全面的工具包,用於簡化生成PDF和將HTML文件轉換為可搜索PDF文件的法律自動化任務。 藉助IronZIPIronPDFIronOCR的強大功能,開發者可以自動化並優化其工序,從而最終提高法律文件處理的效率和準確性。 有了Iron Suite在手,自動化的可能性是無限的。

針對HTML到PDF轉換任務,我們使用了Iron Suite的三個基本程式庫:IronZIPIronPDF,和IronOCRIronPrint如果需要列印功能,也可以成為此任務的有力競爭者。 如果單獨購買,這四個程式庫的總成本為$2,998 * 4 = $2,996。

然而,購買Iron Suite可以讓您不僅獲得三或四個,而是九個強大程式庫的許可,只需付出僅相當於兩個獨立產品的價格。 這是一個不可思議的價值主張,為您提供一個全面的工具包,滿足您所有的.NET開發需求。 僅需$2,998,Iron Suite提供了超高價值,讓您在節省時間和金錢的同時配備一系列工具來簡化您的開發工作流程。