如何在C# PDF生成中添加自定义断字
在 C# 中生成 PDF 时,自定义断字有助于解决窄栏排版、发票、合同和多语言报告中的间距不均、单词溢出和换行不当等问题。如果 PDF 渲染器未应用正确的断字模式,两端对齐的文本可能出现过大的词间距,或在不合适的位置换行。
IronPDF 在通过 Chromium 引擎将 HTML 渲染为 PDF 时处理断字,不依赖 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设置为具有PatternSource路径或URL的新CustomHyphenationDefinitions - 在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/...返回HTML页面包装,这将导致模式验证失败。 使用https://raw.githubusercontent.com/...格式,或者点击GitHub上的“Raw”按钮以获得正确的URL。远程资源约束是什么?
| 约束 | 值 |
|---|---|
| 协议 | HTTP和HTTPS(推荐使用HTTPS) |
| 允许的内容类型 | text/plain, application/octet-stream |
| 最大响应大小 | 5 MB |
| 请求超时 | 10秒 |
| 安全 | 为防止 SSRF 攻击,系统会阻止发往私有或本地 IP 地址(10.x.x.x、192.168.x.x、localhost)的请求 |
| 拒绝的内容 | 二进制文件、包含空字节的文件、包含<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,这部分开销可以忽略不计。
加载失败会持续影响后续渲染:模式加载失败时不会抛出异常,渲染器会继续生成 PDF,但不会应用断字。如果首次加载遇到无效模式文件或网络故障,问题会持续影响整个批次。后续渲染也不会应用断字,且不会产生新的错误提示。建议在应用程序启动或部署时验证模式文件并确认 URL 可访问。
缓存键:缓存键使用 PatternSource 的原始字符串值,以及已设置的 ExceptionSource 值。指向同一 URL 或文件路径的两个渲染器实例会复用同一份缓存。更改 URL 会触发重新加载,即使新 URL 指向同一文件的另一个版本。
部署到生产环境前,请先验证文件内容。模式文件只能包含有效的断字模式。注释、TeX 指令、编码声明或其他非模式内容都会导致集成失败。tex-hyphen 仓库提供数十种语言的预构建模式文件,可直接使用。
推荐使用HTTPS作为远程模式源。 支持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提供的那些。
如果加载自定义断字模式失败会怎样?
如果在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。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。