如何使用 GroupDocs.Editor 從文件中提取 CSS
在本教學中,您將學習 如何提取 CSS,使用 GroupDocs.Editor .NET API 從各種文件格式中。我們將逐步說明所需的設定,展示您需要的完整程式碼,並解釋每一步,讓您能自信地從 Word、HTML 或其他支援的檔案中提取外部樣式表內容。此功能在建構內容管理系統、執行樣式稽核或在網頁應用程式中重新使用文件主題時非常重要。
快速解答
- What does “extract css from document” mean? 這表示從受支援的檔案中取得嵌入的外部樣式表字串,以便您閱讀或修改它們。
- Which library provides this feature? GroupDocs.Editor for .NET.
- Do I need a license? 可使用免費試用版;商業授權在正式環境中是必需的。
- What .NET versions are supported? 支援 .NET Framework 4.6.1+、.NET Core 3.1+、.NET 5/6+。
- How long does the implementation take? 基本提取通常在 10 分鐘以內完成。
如何從文件中提取 CSS?
使用 Editor 類別載入目標檔案,呼叫 Edit 取得 EditableDocument,然後使用 GetCssContent 方法擷取所有樣式表字串。整個流程僅需三個 API 呼叫,即可支援 DOCX、HTML、PPTX 以及 GroupDocs.Editor 支援的其他格式。
什麼是從文件中提取 CSS?
GetCssContent 作業會回傳文件所引用的原始 CSS,無論樣式是透過 HTML 中的 <link> 標籤連結,或是儲存在 DOCX 套件中的嵌入式樣式部件。這讓您能檢視、轉換或在原始檔案之外重新使用樣式邏輯。
為何在此任務中使用 GroupDocs.Editor?
GroupDocs.Editor 支援 30 多種輸入與輸出格式,且可在不將整個文件載入記憶體的情況下處理高達 500 MB 的檔案,對於一般 100 頁的文件,提取時間可低於 2 秒。API 會回傳乾淨的 IList<string> 樣式表內容,省去手動 XML 解析或 HTML 抓取的需求。
前置條件
在開始之前,請確保您已具備:
- .NET Framework 4.6.1 或更新版本(或受支援的 .NET Core/5/6 執行環境)。
- Visual Studio 2017 或更新版本。
- GroupDocs.Editor for .NET – 從 GroupDocs.Editor 下載頁面 下載。
- 具備 C# 程式設計的基本知識。
匯入命名空間
Editor、LoadOptions 與 EditableDocument 類別位於 GroupDocs.Editor 命名空間。請在檔案頂部匯入它們,以便編譯器能解析這些型別。
using System;
using System.Collections.Generic;
using GroupDocs.Editor.Options;
步驟 1:初始化編輯器
Editor 是所有文件操作的入口點。它會載入來源檔案並準備相應的格式特定選項。
using (Editor editor = new Editor("Your Sample Document", delegate { return new WordProcessingLoadOptions(); }))
{
// Proceed to the next steps
}
步驟 2:以可編輯模式開啟文件
呼叫 Edit 會將來源檔案轉換為 EditableDocument。此物件提供 GetCssContent 方法以提取樣式表。
using (EditableDocument document = editor.Edit(new WordProcessingEditOptions()))
{
// Proceed to the next steps
}
步驟 3:提取 CSS 內容
GetCssContent 會掃描文件中所有連結或嵌入的樣式表,並以字串集合的形式回傳。
List<string> stylesheets = document.GetCssContent();
步驟 4:輸出 CSS 內容
遍歷回傳的集合,列印計數並顯示每個樣式表。此驗證步驟可確保提取成功,並讓您看到原始 CSS。
Console.WriteLine("There are {0} stylesheets in the input document", stylesheets.Count);
foreach (string css in stylesheets)
{
Console.WriteLine(css);
}
常見問題與技巧
- No stylesheets returned? 請確認來源檔案確實包含外部 CSS(例如,帶有連結樣式表的 DOCX)。
- Encoding problems – 若輸出出現亂碼,請確認文件的原始編碼受到編輯器支援。
- Large documents – 對於非常大的檔案,請在背景執行緒中處理文件,以保持 UI 響應並避免阻塞主執行緒。
常見問答
Q: GroupDocs.Editor for .NET 是什麼?
A: GroupDocs.Editor for .NET 是一個文件編輯 API,讓開發人員能以程式方式編輯、轉換及從各種檔案格式中提取內容。
Q: 如何開始使用 GroupDocs.Editor for .NET?
A: 從 GroupDocs.Editor 下載頁面 下載函式庫,將 NuGet 套件加入您的專案,然後依照上述步驟操作。
Q: 可以免費使用 GroupDocs.Editor 嗎?
A: 可以,您可從 GroupDocs 免費試用頁面 取得免費試用版。正式部署時需購買付費授權。
Q: GroupDocs.Editor 支援哪些檔案格式?
A: 它支援 DOCX、XLSX、PPTX、PDF、HTML 等多種格式。完整列表請參閱 文件說明。
Q: 如何取得 GroupDocs.Editor 的支援?
A: 請前往 GroupDocs 支援論壇 提問,您將獲得社群與 GroupDocs 工程師的協助。
最後更新: 2026-08-31
測試環境: GroupDocs.Editor for .NET (latest release)
作者: GroupDocs