使用 GroupDocs 高亮 PDF 並轉換為 HTML
在 PDF 中對文字進行高亮並將結果轉換為具樣式的 HTML 頁面,是法律審查、電子學習與數位出版的常見需求。在本教學中,您將學習如何使用 GroupDocs.Redaction for .NET 來 高亮 PDF 檔案,並產生可嵌入網站入口或學習管理系統的高亮 HTML 輸出。指南將逐步說明環境設定、路徑初始化、HTML 頁面產生以及資源 URL 處理——全部以可直接執行的 C# 程式碼示例呈現。
快速解答
- 什麼函式庫負責高亮? GroupDocs.Redaction for .NET.
- 支援哪些 .NET 版本? .NET Framework 4.6+、.NET Core 3.1+、.NET 5/6/7.
- 生產環境是否需要授權? 是——商業授權可移除試用限制。
- 能處理大型 PDF(數百頁)嗎? 能,API 會串流頁面,對 500 頁檔案的記憶體使用低於 200 MB。
- HTML 輸出是否具互動性? 產生的 HTML 為靜態但完整樣式;您可自行加入 JavaScript 以實現互動。
什麼是 PDF 文字高亮?
PDF 文字高亮是指在選取的字元後方繪製彩色覆蓋層的視覺標記,使其在檢視文件時更為突出。GroupDocs.Redaction 直接將此覆蓋層寫入 PDF 的內容串流,保留原始版面配置,同時在匯出的 HTML 中呈現高亮效果。
為何使用 GroupDocs.Redaction for .NET?
GroupDocs.Redaction 支援 70 多種輸入與輸出格式,可處理高達 500 頁 的 PDF 而無需將整個檔案載入記憶體,並提供 單次通過 API 同時執行遮蔽與高亮。這些可量化的能力使其成為企業級文件流程的可靠選擇。
前置條件
- 開發環境: Visual Studio 2022(或更新版本)搭配 .NET Core 3.1 / .NET 6 專案。
- NuGet 套件:
GroupDocs.Redaction(最新穩定版)。 - 基礎知識: C# 語法、檔案系統路徑與 HTML 基礎。
如何設定 GroupDocs.Redaction for .NET?
要安裝此函式庫,請選擇以下三種支援的方法之一。.NET CLI 指令會將套件加入專案檔,Package Manager Console 透過 NuGet 整合,UI 則提供圖形化的瀏覽與安裝方式。三種方式皆會引用相同的 GroupDocs.Redaction 程式集,讓您立即開始撰寫程式碼。
Using .NET CLI:
dotnet add package GroupDocs.Redaction
Using Package Manager Console:
Install-Package GroupDocs.Redaction
Using NuGet Package Manager UI: Search for “GroupDocs.Redaction” and click Install.
安裝完成後,於 C# 檔案頂部加入 using 指令:
using GroupDocs.Redaction;
Feature_InitializeIndexedFileInfo 類別如何運作?
Feature_InitializeIndexedFileInfo 是一個協助建立並儲存檢視器快取與來源 PDF 所需路徑的工具類別。
此類別負責準備檢視器與 HTML 產生器依賴的檔案系統位置。它會為暫存檔建立專屬快取資料夾,從來源 PDF 產生資料夾名稱,並儲存原始文件的絕對路徑。這些屬性以唯讀成員方式公開,供後續處理使用。
// ```csharp
using GroupDocs.Redaction;
// Initialize the Redactor
Redactor redactor = new Redactor("your-file-path.pdf");
如何產生 HTML 頁面檔案路徑?
Feature_GenerateHtmlPageFilePath 會根據頁碼為每個 HTML 頁面產生可預測的檔名。
此類別使用簡單的 p{pageNumber}.html 格式建立唯一識別每個渲染頁面的檔名,接著將該名稱與先前建立的快取資料夾路徑結合,產生可存放 HTML 的完整檔案系統位置。此可預測的命名方式可避免在處理多頁 PDF 時產生衝突。
// ```csharp
using System.IO;
namespace GroupDocs.Search.Examples.CSharp.HighlightInHtml
{
internal class Feature_InitializeIndexedFileInfo
{
private readonly string _viewerCacheFolderPath;
private readonly string _filePath;
private readonly string _fileFolderName;
private readonly string _fileCacheFolderPath;
public Feature_InitializeIndexedFileInfo(string viewerCacheFolderPath, string filePath)
{
// Store the provided cache folder path.
_viewerCacheFolderPath = viewerCacheFolderPath;
// Store the file path.
_filePath = filePath;
// Extract and modify the file name to create a folder name.
var fileName = Path.GetFileName(_filePath);
_fileFolderName = fileName.Replace(".", "_");
// Combine paths for cache directory.
_fileCacheFolderPath = Path.Combine(viewerCacheFolderPath, _fileFolderName);
}
public string ViewerCacheFolderPath => _viewerCacheFolderPath;
public string FilePath => _filePath;
public string FileFolderName => _fileFolderName;
public string FileCacheFolderPath => _fileCacheFolderPath;
}
}
如何建立 HTML 頁面資源檔案路徑與 URL?
Feature_GenerateHtmlPageResourceFilePathAndUrl 同時建立頁面資源的實體檔案路徑與對應的 Web URL。
圖片、字型或 CSS 等資源需要磁碟上的位置與瀏覽器可請求的 URL。此類別接受頁碼與資源名稱,回傳一個元組,內含快取資料夾內的絕對檔案系統路徑以及可由 Web 伺服器映射的虛擬 URL。使用此方式可確保產生的頁面之間資源參照保持一致。
// ```csharp
using System.IO;
namespace GroupDocs.Search.Examples.CSharp.HighlightInHtml
{
internal class Feature_GenerateHtmlPageFilePath
{
private readonly string _fileCacheFolderPath;
public Feature_GenerateHtmlPageFilePath(string fileCacheFolderPath)
{
// Initialize with cache folder path.
_fileCacheFolderPath = fileCacheFolderPath;
}
public string GetHtmlPageFilePath(int pageNumber)
{
// Create a page-specific HTML file name.
string pageFileName = $"p{pageNumber}.html";
// Combine paths for the full file location.
string pageFilePath = Path.Combine(_fileCacheFolderPath, pageFileName);
return pageFilePath;
}
}
}
實務應用
- 法律文件審查: 高亮條款、匯出為 HTML,讓律師在瀏覽器中進行評論。
- 電子學習內容: 將帶註解的講義 PDF 轉換為具可搜尋高亮的互動式網頁。
- 數位出版: 製作適合上網的雜誌版本,利用高亮摘錄吸引讀者注意。
上述情境皆受惠於 GroupDocs.Redaction 所提供的 高效能串流 能力,讓您每天處理上千份文件。
常見問題與解決方案
| 問題 | 原因 | 解決方案 |
|---|---|---|
| 高亮未在 HTML 中顯示 | 產生的頁面缺少 CSS 類別 | 確保已引用檢視器的 highlight.css,或手動嵌入樣式區塊。 |
| 大型 PDF 發生記憶體不足錯誤 | 使用 Document.Load 未啟用串流 | 使用 RedactorOptions 並將 EnableStreaming = true。 |
| 資源 URL 回傳 404 | 基礎 URL 設定錯誤 | 將 RedactionViewerOptions.BaseUrl 設為靜態檔案資料夾的根目錄。 |
常見問答
問:我可以一次在單一 PDF 中高亮多個區段嗎?
答:可以。將 RedactionRegion 物件集合傳入 Redactor.Apply,每個區域都會在同一次操作中被高亮。
問:API 是否支援關鍵字高亮?
答:支援。使用 Redactor.Search 找出所有關鍵字出現位置,然後對取得的區域套用高亮遮蔽。
問:產生的 HTML 是否具互動性(例如點擊導覽)?
答:預設輸出為靜態,但您可在產生後注入 JavaScript 以加入導覽、提示或自訂點擊處理程序。
問:如何變更高亮顏色?
答:在匯出的 HTML 中修改 CSS 類別 .redaction-highlight,或在套用前於 RedactionOptions 設定 HighlightColor 屬性。
問:這能處理大於 1 GB 的 PDF 嗎?
答:可以,只要啟用串流並配置足夠的暫存磁碟空間;API 永不會將整份文件載入記憶體。
結論
您現在已掌握完整且可投入生產的工作流程,使用 GroupDocs.Redaction for .NET 來 高亮 PDF 檔案並將其轉換為帶高亮的 HTML 頁面。透過初始化索引檔案資訊、產生可預測的 HTML 路徑以及處理資源 URL,您可將此解決方案整合至任何基於 .NET 的文件管理系統、法律審查平台或電子學習平台。
最後更新: 2026-08-20
測試環境: GroupDocs.Redaction 23.12 for .NET
作者: GroupDocs
using System.IO;
namespace GroupDocs.Search.Examples.CSharp.HighlightInHtml
{
internal class Feature_GenerateHtmlPageResourceFilePathAndUrl
{
private readonly string _fileCacheFolderPath;
public Feature_GenerateHtmlPageResourceFilePathAndUrl(string fileCacheFolderPath)
{
// Initialize with the cache folder path.
_fileCacheFolderPath = fileCacheFolderPath;
}
public string GetHtmlPageResourceFilePath(int pageNumber, string resourceFileName)
{
// Construct a unique name for resources.
string resFileName = GetResourceFileName(pageNumber, resourceFileName);
// Combine paths to form full resource path.
string resFilePath = Path.Combine(_fileCacheFolderPath, resFileName);
return resFilePath;
}
public string GetHtmlPageResourceUrl(int pageNumber, string resourceFileName)
{
// Generate a URL-like string for the resource.
return GetResourceFileName(pageNumber, resourceFileName);
}
private static string GetResourceFileName(int pageNumber, string resourceFileName)
{
// Construct resource names based on page number and file name.
var resFileName = $"p{pageNumber}_{resourceFileName}";
return resFileName;
}
}
}