在 C# 產生 PDF 時加入自訂斷字的方法
在 C# 中產生 PDF 時,自訂斷字有助於改善窄欄、發票、合約和多語報告中的字詞間距過大、字詞溢出及換行不當等問題。PDF 渲染器若未套用正確的斷字模式,兩端對齊的文字可能會出現過大的字詞間距,或在不適當的位置換行。
IronPDF 在將 HTML 渲染為 PDF 時,由 Chromium 引擎處理斷字,而非透過 Word 式的文件物件模型。CSS 的 hyphens: auto 屬性讓渲染器能在適當的音節邊界斷開字詞,IronPDF 會在產生 PDF 時套用此行為。ChromePdfRenderOptions 的 CustomHyphenation 屬性可控制要使用的斷字模式。
斷字模式檔案採用 TeX 格式,可從本機檔案路徑或遠端 URL 載入。您可以針對不同語言和文件版面定義自訂斷字規則,更精確地控制 PDF 中字詞的斷開位置。
本指南說明如何在 C# 中使用 CustomHyphenationDefinitions API,包括如何從本機或遠端載入斷字模式,以及備援行為、限制、錯誤處理和快取機制。
-
1使用 IronPDF 安裝 NuGet Package Manager
-
2複製並運行這段程式碼。
using IronPdf; // Create renderer and assign custom hyphenation patterns from a remote URL var renderer = new ChromePdfRenderer(); renderer.RenderingOptions.CustomHyphenation = new CustomHyphenationDefinitions { PatternSource = "https://raw.githubusercontent.com/hyphenation/tex-hyphen/master/hyph-utf8/tex/generic/hyph-utf8/patterns/txt/hyph-en-us.pat.txt", ExceptionSource = "https://raw.githubusercontent.com/hyphenation/tex-hyphen/master/hyph-utf8/tex/generic/hyph-utf8/patterns/txt/hyph-en-us.hyp.txt" }; // Render HTML with CSS hyphens:auto to trigger word breaking var pdf = renderer.RenderHtmlAsPdf("<div style='text-align:justify; hyphens:auto; width:120px;'>Supercalifragilisticexpialidocious</div>"); pdf.SaveAs("hyphenated.pdf");Imports IronPdf ' Create renderer and assign custom hyphenation patterns from a remote URL Dim renderer = New ChromePdfRenderer() renderer.RenderingOptions.CustomHyphenation = New CustomHyphenationDefinitions With { .PatternSource = "https://raw.githubusercontent.com/hyphenation/tex-hyphen/master/hyph-utf8/tex/generic/hyph-utf8/patterns/txt/hyph-en-us.pat.txt", .ExceptionSource = "https://raw.githubusercontent.com/hyphenation/tex-hyphen/master/hyph-utf8/tex/generic/hyph-utf8/patterns/txt/hyph-en-us.hyp.txt" } ' Render HTML with CSS hyphens:auto to trigger word breaking Dim pdf = renderer.RenderHtmlAsPdf("<div style='text-align:justify; hyphens:auto; width:120px;'>Supercalifragilisticexpialidocious</div>") pdf.SaveAs("hyphenated.pdf")Imports IronPdf ' Create renderer and assign custom hyphenation patterns from a remote URL Dim renderer = New ChromePdfRenderer() renderer.RenderingOptions.CustomHyphenation = New CustomHyphenationDefinitions With { .PatternSource = "https://raw.githubusercontent.com/hyphenation/tex-hyphen/master/hyph-utf8/tex/generic/hyph-utf8/patterns/txt/hyph-en-us.pat.txt", .ExceptionSource = "https://raw.githubusercontent.com/hyphenation/tex-hyphen/master/hyph-utf8/tex/generic/hyph-utf8/patterns/txt/hyph-en-us.hyp.txt" } ' Render HTML with CSS hyphens:auto to trigger word breaking Dim pdf = renderer.RenderHtmlAsPdf("<div style='text-align:justify; hyphens:auto; width:120px;'>Supercalifragilisticexpialidocious</div>") pdf.SaveAs("hyphenated.pdf") -
3部署到您的生產環境進行測試
立即在您的項目中開始使用 IronPDF,並使用 免費試用
基本流程
- 安裝 IronPDF NuGet 套件
- 建立
renderer執行個體 - 將
renderer.RenderingOptions.CustomHyphenation設定為新的CustomHyphenationDefinitions執行個體,並為其PatternSource指定路徑或 URL - 在 HTML 內容的 CSS 中包含
hyphens - 呼叫
renderer.RenderHtmlAsPdf()並儲存結果
自訂斷字在 PDF 渲染時如何運作?
CustomHyphenationDefinitions 類別指定 IronPDF 在渲染時載入斷字規則的來源。Chromium 引擎會讀取這些模式,並在 HTML 元素設有 CSS hyphens 規則時套用。
什麼是 CustomHyphenationDefinitions 類別?
此類別提供兩個屬性:
| 屬性 | 類型 | 必填 | 說明 |
|---|---|---|---|
PatternSource | string | 是 | 斷字模式檔案的路徑或 URL(例如 hyph-en-us.pat.txt) |
ExceptionSource | string | 否 | 斷字例外清單檔案的路徑或 URL(例如 hyph-en-us.hyp.txt) |
模式檔案遵循 GitHub 上的 tex-hyphen 專案維護的 TeX 斷字格式。存放庫為每種語言提供兩個檔案:hyph-{lang}.pat.txt 存放斷字模式,hyph-{lang}.hyp.txt 存放例外清單。引用 GitHub 上的檔案時,必須使用原始內容 URL(以 https://raw.githubusercontent.com/ 開頭)。一般的 GitHub 頁面 URL 會傳回 HTML,而非斷字模式的純文字內容。
自訂斷字如何覆寫內建語言設定?
ChromePdfRenderOptions 的 HyphenationLanguage 屬性使用 PdfHyphenationLanguage 列舉,提供美式英語、英式英語和俄語的內建預設。若同時設定 CustomHyphenation,則自訂斷字的優先順序較高。套用順序如下:
- CustomHyphenation - 如果設定了有效的
PatternSource,就使用自訂模式 - HyphenationLanguage - 如果未設定自訂模式,就套用內建語言預設
- None - 如果兩者皆未設定,就不進行斷字
載入自訂模式失敗時會發生什麼事?
載入模式時若發生錯誤,系統會記錄錯誤,但不會擲回例外狀況。渲染會繼續進行。若已設定 HyphenationLanguage,渲染器會改用該內建預設,否則不進行斷字。
這項設計讓正式環境中的渲染作業能繼續執行。擷取遠端模式檔案時網路逾時、檔案路徑無效、DNS 解析失敗或模式格式錯誤,都不會造成渲染流程中斷。PDF 仍會產生,只是無法套用斷字規則。
這也讓問題較難察覺。若第一次載入時模式檔案有誤或 URL 無法存取,之後使用相同來源值的渲染作業都會受到影響,因為快取也會保留失敗狀態。建議在應用程式啟動或 CI/CD 部署檢查時驗證模式檔案,並確認遠端 URL 可透過網路存取,無須等到渲染時才檢查。
如何從遠端 URL 載入模式檔案?
將 PatternSource 設為遠端 URL,是不將檔案納入專案即可套用自訂斷字的最快方法。下例從 tex-hyphen 存放庫載入美式英語斷字模式,並渲染一段兩端對齊的文字:
using IronPdf;
var renderer = new ChromePdfRenderer();
// Load custom patterns from a remote TeX hyphenation repository
renderer.RenderingOptions.CustomHyphenation = new CustomHyphenationDefinitions
{
PatternSource = "https://raw.githubusercontent.com/hyphenation/tex-hyphen/master/hyph-utf8/tex/generic/hyph-utf8/patterns/txt/hyph-en-us.pat.txt",
ExceptionSource = "https://raw.githubusercontent.com/hyphenation/tex-hyphen/master/hyph-utf8/tex/generic/hyph-utf8/patterns/txt/hyph-en-us.hyp.txt"
};
string html = @"
<html>
<head>
<style>
body { font-family: Arial, sans-serif; }
.narrow-column {
width: 150px;
text-align: justify;
hyphens: auto;
-webkit-hyphens: auto;
border: 1px solid #ccc;
padding: 10px;
}
</style>
</head>
<body>
<div class='narrow-column'>
The extraordinarily sophisticated implementation demonstrates
how hyphenation significantly improves the typographical quality
of justified text in constrained column widths.
</div>
</body>
</html>";
var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("remote-hyphenation.pdf");using IronPdf;
var renderer = new ChromePdfRenderer();
// Load custom patterns from a remote TeX hyphenation repository
renderer.RenderingOptions.CustomHyphenation = new CustomHyphenationDefinitions
{
PatternSource = "https://raw.githubusercontent.com/hyphenation/tex-hyphen/master/hyph-utf8/tex/generic/hyph-utf8/patterns/txt/hyph-en-us.pat.txt",
ExceptionSource = "https://raw.githubusercontent.com/hyphenation/tex-hyphen/master/hyph-utf8/tex/generic/hyph-utf8/patterns/txt/hyph-en-us.hyp.txt"
};
string html = @"
<html>
<head>
<style>
body { font-family: Arial, sans-serif; }
.narrow-column {
width: 150px;
text-align: justify;
hyphens: auto;
-webkit-hyphens: auto;
border: 1px solid #ccc;
padding: 10px;
}
</style>
</head>
<body>
<div class='narrow-column'>
The extraordinarily sophisticated implementation demonstrates
how hyphenation significantly improves the typographical quality
of justified text in constrained column widths.
</div>
</body>
</html>";
var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("remote-hyphenation.pdf");Imports IronPdf
Dim renderer As New ChromePdfRenderer()
' Load custom patterns from a remote TeX hyphenation repository
renderer.RenderingOptions.CustomHyphenation = New CustomHyphenationDefinitions With {
.PatternSource = "https://raw.githubusercontent.com/hyphenation/tex-hyphen/master/hyph-utf8/tex/generic/hyph-utf8/patterns/txt/hyph-en-us.pat.txt",
.ExceptionSource = "https://raw.githubusercontent.com/hyphenation/tex-hyphen/master/hyph-utf8/tex/generic/hyph-utf8/patterns/txt/hyph-en-us.hyp.txt"
}
Dim html As String = "
<html>
<head>
<style>
body { font-family: Arial, sans-serif; }
.narrow-column {
width: 150px;
text-align: justify;
hyphens: auto;
-webkit-hyphens: auto;
border: 1px solid #ccc;
padding: 10px;
}
</style>
</head>
<body>
<div class='narrow-column'>
The extraordinarily sophisticated implementation demonstrates
how hyphenation significantly improves the typographical quality
of justified text in constrained column widths.
</div>
</body>
</html>"
Dim pdf = renderer.RenderHtmlAsPdf(html)
pdf.SaveAs("remote-hyphenation.pdf")Imports IronPdf
Dim renderer As New ChromePdfRenderer()
' Load custom patterns from a remote TeX hyphenation repository
renderer.RenderingOptions.CustomHyphenation = New CustomHyphenationDefinitions With {
.PatternSource = "https://raw.githubusercontent.com/hyphenation/tex-hyphen/master/hyph-utf8/tex/generic/hyph-utf8/patterns/txt/hyph-en-us.pat.txt",
.ExceptionSource = "https://raw.githubusercontent.com/hyphenation/tex-hyphen/master/hyph-utf8/tex/generic/hyph-utf8/patterns/txt/hyph-en-us.hyp.txt"
}
Dim html As String = "
<html>
<head>
<style>
body { font-family: Arial, sans-serif; }
.narrow-column {
width: 150px;
text-align: justify;
hyphens: auto;
-webkit-hyphens: auto;
border: 1px solid #ccc;
padding: 10px;
}
</style>
</head>
<body>
<div class='narrow-column'>
The extraordinarily sophisticated implementation demonstrates
how hyphenation significantly improves the typographical quality
of justified text in constrained column widths.
</div>
</body>
</html>"
Dim pdf = renderer.RenderHtmlAsPdf(html)
pdf.SaveAs("remote-hyphenation.pdf")輸出
渲染後的 PDF 段落採兩端對齊,並在適當的音節邊界斷字。若不使用斷字,相同文字可能會出現過大的字詞間距,或溢出欄位。
為了與 Chromium 相容,請同時使用 hyphens 和 hyphenate-limit-chars 這兩項 CSS 宣告。hyphens 規則最能突顯斷字效果。若目標 HTML 元素未使用這兩項宣告中的任何一項,自訂模式仍會載入,但不會套用。
https://github.com/hyphenation/tex-hyphen/blob/master/... 這樣的標準 GitHub URL 返回一個 HTML 頁面包裝器,這將導致模式驗證失敗。 使用 https://raw.githubusercontent.com/... 格式,或點擊 GitHub 上的“Raw”按鈕以獲取正確的 URL。遠端來源限制是什麼?
| 限制 | 值 |
|---|---|
| 通訊協定 | HTTP 和 HTTPS(建議使用 HTTPS) |
| 允許的內容類型 | text/plain、application/octet-stream |
| 回應大小上限 | 5 MB |
| 請求逾時 | 10 秒 |
| 安全性 | 封鎖對私有或本機 IP 位址(10.x.x.x、192.168.x.x、localhost)的請求,以防止 SSRF 攻擊 |
| 拒絕的內容 | 二進位檔案、含有零值位元組的檔案,以及含有 <script> 標籤的檔案 |
容器和雲端環境(Docker、Azure、AWS)必須能透過對外的 HTTPS 連線存取模式檔案所在的主機,才能從遠端載入檔案。
如何從本機載入模式檔案?
若環境限制對外網路存取,或您希望在建置時一併封裝檔案,PatternSource 也接受本機檔案系統路徑:
hyph-en-us.pat.txt和hyph-en-us.hyp.txt,並將它們放置在程式碼所引用的路徑中。using IronPdf;
var renderer = new ChromePdfRenderer();
// Load English hyphenation patterns from local files
renderer.RenderingOptions.CustomHyphenation = new CustomHyphenationDefinitions
{
PatternSource = @"C:\patterns\hyph-en-us.pat.txt",
ExceptionSource = @"C:\patterns\hyph-en-us.hyp.txt"
};
string html = @"
<html>
<head>
<style>
.invoice-container {
width: 220px;
text-align: justify;
hyphens: auto;
-webkit-hyphens: auto;
font-family: Georgia, serif;
font-size: 11px;
line-height: 1.5;
border: 1px solid #ddd;
padding: 12px;
}
h3 { font-size: 13px; margin-top: 0; }
.terms { color: #555; margin-top: 10px; font-size: 9px; }
</style>
</head>
<body>
<div class='invoice-container'>
<h3>Invoice #20260331</h3>
<p>Nondiscrimination acknowledgement: The undersigned
representative hereby confirms that all pharmaceutical
reimbursement documentation has been independently
verified and cross-referenced against the applicable
regulatory framework established by the appropriate
governmental oversight authority.</p>
<p class='terms'>Notwithstanding any indemnification
provisions, the counterparty's disproportionate
liability shall not exceed the predetermined
recharacterization threshold established under the
intergovernmental cooperation agreement.</p>
</div>
</body>
</html>";
var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("local-hyphenation.pdf");using IronPdf;
var renderer = new ChromePdfRenderer();
// Load English hyphenation patterns from local files
renderer.RenderingOptions.CustomHyphenation = new CustomHyphenationDefinitions
{
PatternSource = @"C:\patterns\hyph-en-us.pat.txt",
ExceptionSource = @"C:\patterns\hyph-en-us.hyp.txt"
};
string html = @"
<html>
<head>
<style>
.invoice-container {
width: 220px;
text-align: justify;
hyphens: auto;
-webkit-hyphens: auto;
font-family: Georgia, serif;
font-size: 11px;
line-height: 1.5;
border: 1px solid #ddd;
padding: 12px;
}
h3 { font-size: 13px; margin-top: 0; }
.terms { color: #555; margin-top: 10px; font-size: 9px; }
</style>
</head>
<body>
<div class='invoice-container'>
<h3>Invoice #20260331</h3>
<p>Nondiscrimination acknowledgement: The undersigned
representative hereby confirms that all pharmaceutical
reimbursement documentation has been independently
verified and cross-referenced against the applicable
regulatory framework established by the appropriate
governmental oversight authority.</p>
<p class='terms'>Notwithstanding any indemnification
provisions, the counterparty's disproportionate
liability shall not exceed the predetermined
recharacterization threshold established under the
intergovernmental cooperation agreement.</p>
</div>
</body>
</html>";
var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("local-hyphenation.pdf");Imports IronPdf
Dim renderer As New ChromePdfRenderer()
' Load English hyphenation patterns from local files
renderer.RenderingOptions.CustomHyphenation = New CustomHyphenationDefinitions With {
.PatternSource = "C:\patterns\hyph-en-us.pat.txt",
.ExceptionSource = "C:\patterns\hyph-en-us.hyp.txt"
}
Dim html As String = "
<html>
<head>
<style>
.invoice-container {
width: 220px;
text-align: justify;
hyphens: auto;
-webkit-hyphens: auto;
font-family: Georgia, serif;
font-size: 11px;
line-height: 1.5;
border: 1px solid #ddd;
padding: 12px;
}
h3 { font-size: 13px; margin-top: 0; }
.terms { color: #555; margin-top: 10px; font-size: 9px; }
</style>
</head>
<body>
<div class='invoice-container'>
<h3>Invoice #20260331</h3>
<p>Nondiscrimination acknowledgement: The undersigned
representative hereby confirms that all pharmaceutical
reimbursement documentation has been independently
verified and cross-referenced against the applicable
regulatory framework established by the appropriate
governmental oversight authority.</p>
<p class='terms'>Notwithstanding any indemnification
provisions, the counterparty's disproportionate
liability shall not exceed the predetermined
recharacterization threshold established under the
intergovernmental cooperation agreement.</p>
</div>
</body>
</html>"
Dim pdf = renderer.RenderHtmlAsPdf(html)
pdf.SaveAs("local-hyphenation.pdf")Imports IronPdf
Dim renderer As New ChromePdfRenderer()
' Load English hyphenation patterns from local files
renderer.RenderingOptions.CustomHyphenation = New CustomHyphenationDefinitions With {
.PatternSource = "C:\patterns\hyph-en-us.pat.txt",
.ExceptionSource = "C:\patterns\hyph-en-us.hyp.txt"
}
Dim html As String = "
<html>
<head>
<style>
.invoice-container {
width: 220px;
text-align: justify;
hyphens: auto;
-webkit-hyphens: auto;
font-family: Georgia, serif;
font-size: 11px;
line-height: 1.5;
border: 1px solid #ddd;
padding: 12px;
}
h3 { font-size: 13px; margin-top: 0; }
.terms { color: #555; margin-top: 10px; font-size: 9px; }
</style>
</head>
<body>
<div class='invoice-container'>
<h3>Invoice #20260331</h3>
<p>Nondiscrimination acknowledgement: The undersigned
representative hereby confirms that all pharmaceutical
reimbursement documentation has been independently
verified and cross-referenced against the applicable
regulatory framework established by the appropriate
governmental oversight authority.</p>
<p class='terms'>Notwithstanding any indemnification
provisions, the counterparty's disproportionate
liability shall not exceed the predetermined
recharacterization threshold established under the
intergovernmental cooperation agreement.</p>
</div>
</body>
</html>"
Dim pdf = renderer.RenderHtmlAsPdf(html)
pdf.SaveAs("local-hyphenation.pdf")輸出
如下所示,原本可能溢出或造成間距過大的長字詞,會自動在音節邊界斷開。引擎只在必要時斷字,能完整排入一行的字詞會保持原樣。
若要改用其他語言,只需變更檔案路徑:
// Switch to French hyphenation — just change the file paths
renderer.RenderingOptions.CustomHyphenation = new CustomHyphenationDefinitions
{
PatternSource = @"C:\patterns\hyph-fr.pat.txt",
ExceptionSource = @"C:\patterns\hyph-fr.hyp.txt"
};// Switch to French hyphenation — just change the file paths
renderer.RenderingOptions.CustomHyphenation = new CustomHyphenationDefinitions
{
PatternSource = @"C:\patterns\hyph-fr.pat.txt",
ExceptionSource = @"C:\patterns\hyph-fr.hyp.txt"
};' Switch to French hyphenation — just change the file paths
renderer.RenderingOptions.CustomHyphenation = New CustomHyphenationDefinitions With {
.PatternSource = "C:\patterns\hyph-fr.pat.txt",
.ExceptionSource = "C:\patterns\hyph-fr.hyp.txt"
}' Switch to French hyphenation — just change the file paths
renderer.RenderingOptions.CustomHyphenation = New CustomHyphenationDefinitions With {
.PatternSource = "C:\patterns\hyph-fr.pat.txt",
.ExceptionSource = "C:\patterns\hyph-fr.hyp.txt"
}因此,CustomHyphenation 特別適用於內建 PdfHyphenationLanguage 列舉尚未支援的語言。此列舉目前僅支援美式英語、英式英語和俄語。
本機檔案有哪些限制?
| 限制 | 值 |
|---|---|
| 允許的副檔名 | .txt、.pat |
| 檔案大小上限 | 5 MB |
| 編碼 | UTF-8 |
| 內容規則 | 只能包含有效的斷字模式,不可包含註解、中繼資料、檔頭、TeX 指令或編碼說明 |
| 拒絕的內容 | 二進位檔案、含有零值位元組的檔案,以及含有 <script> 標籤的檔案 |
快取如何影響批次渲染的效能?
自訂斷字模式首次載入後會快取在記憶體中,並以 PatternSource 和 ExceptionSource 的值作為快取索引鍵。之後若使用相同的來源路徑或 URL,渲染器會重複使用快取中的模式,無須重新下載或讀取檔案。
此行為對於 大量 PDF 渲染 工作流程有兩個實際影響:
效能:首次渲染會產生 I/O 成本(網路請求或磁碟讀取)。後續渲染幾乎沒有載入模式的成本。若以相同的斷字設定批次產生數百份 PDF,這項成本可忽略不計。
失敗狀態也會保留:載入模式時發生錯誤不會擲回例外狀況,渲染器仍會繼續渲染。若首次載入時模式檔案有誤或網路連線失敗,整個批次都會受到影響。後續渲染也不會套用斷字,且不會再發出錯誤訊號。請在應用程式啟動或部署時驗證模式檔案並確認 URL 可存取,無須等到渲染時才檢查。
快取索引鍵:快取索引鍵使用 PatternSource 的完整字串值,若已設定 ExceptionSource,也會納入其值。兩個渲染器執行個體若指向相同的 URL 或檔案路徑,就會共用快取中的模式。即使新 URL 指向同一檔案的不同版本,變更 URL 也會觸發重新載入。
部署至正式環境前,請先驗證檔案內容。模式檔案只能包含有效的斷字模式文字。若含有註解、TeX 指令、編碼宣告或其他非模式內容,整合就會失敗。tex-hyphen 存放庫提供數十種語言的現成模式檔案,僅含有效的斷字模式。
建議使用 HTTPS 存取遠端模式來源。雖然支援 HTTP,但 HTTP 不會為檔案內容提供傳輸層保護。
下一步是什麼?
ChromePdfRenderOptions 的 CustomHyphenation 屬性可直接控制字詞的斷開方式。只要有對應的 TeX 模式檔案,就能支援超出 PdfHyphenationLanguage 三種內建預設以外的語言。模式檔案可從遠端 URL 或本機路徑載入,首次使用後會快取在記憶體中。若載入失敗,渲染器會改用 HyphenationLanguage 設定。系統只會記錄錯誤,不會擲回例外狀況,因此應在部署期間驗證模式檔案,無須等到渲染時才檢查。
有關 IronPDF 的渲染設定,請參閱以下資源:
ChromePdfRenderOptionsAPI 參考列出所有可用的渲染選項PdfHyphenationLanguage列舉列出內建的語言預設- HTML 轉 PDF 渲染教學說明完整的 HTML 渲染流程
- 渲染選項設定方法說明其他 ChromePdfRenderOptions 設定
- IronPDF 功能概覽介紹完整的 PDF 產生與處理功能
免費試用 IronPDF 30 天,在實際專案中測試自訂斷字,或查看授權選項以部署至正式環境。
ChromePdfRendererPatternSourceCustomHyphenationDefinitionsstringhyphens: auto``RenderHtmlAsPdfCustomHyphenationDefinitionshyphens: autoHyphenationLanguageChromePdfRenderOptionshyph-en-us.pat.txt-webkit-hyphens: auto``text-align: justifyPdfHyphenationLanguageChromePdfRenderOptions
常見問題
什麼是C# PDF生成中的自訂斷字?
C# PDF生成中的自訂斷字可控制文字的斷列,改善在狹窄欄位或特定文件布局中的文本換行和間距,使用IronPDF。它在HTML到PDF的渲染過程中利用CSS和TeX斷字模式。
IronPDF如何處理自訂斷字?
IronPDF通過其ChromePdfRenderOptions中的CustomHyphenationDefinitions類處理自訂斷字。這允許從本地檔案或遠程URL加載斷字模式,並在使用Chromium引擎的渲染過程中應用它們。
在IronPDF中使用自訂斷字模式有哪些優勢?
使用IronPDF的自訂斷字模式可精確控制斷字,改善PDF的排版質量。這對多語言文件或具有限制的狹窄布局尤其有益。
IronPDF可以使用遠程URL來獲取斷字模式嗎?
可以,IronPDF可以從遠程URL加載自訂斷字模式,允許動態模式來源,而不需將檔案嵌入到專案中。它需要原始內容URL,例如由GitHub提供的URL。
如果加載自訂斷字模式失敗會發生什麼事?
如果在IronPDF中加載自訂斷字模式失敗,錯誤會被記錄但不會作為例外拋出。IronPDF嘗試回退到任何配置的HyphenationLanguage,或者在沒有斷字的情況下繼續渲染。
快取如何影響IronPDF中的自訂斷字?
快取在首次使用後將加載的斷字模式儲存在記憶體中。這提升了批量操作的性能,但如果最初加載失敗,可能會靜默持續錯誤。建議在渲染前確保模式文件和URL的有效訪問。
如何驗證IronPDF的URL和模式內容?
驗證應在啟動或部署期間進行。驗證URL的可訪問性,並測試模式文件以避免在IronPDF的斷字模式加載過程中發生靜默失敗。
使用IronPDF的斷字功能時,使用遠程URL有何限制?
限制包括使用HTTP/HTTPS協議、最大響應大小為5 MB,以及禁止訪問私有IP以維護安全性。僅允許text/plain和application/octet-stream內容類型。
IronPDF中的自訂斷字可以使用本地文件嗎?
可以,IronPDF支持使用本地文件路徑的斷字模式,允許在沒有網絡訪問的環境下通過CustomHyphenationDefinitions屬性應用自訂斷字規則。
為什麼有人會選擇自訂斷字而非IronPDF內建設置?
自訂斷字為IronPDF內建預設未覆蓋的語言或特定文件需要量身定制斷字規則以優化布局和可讀性提供了靈活性。
Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。