IronPDF 简化了 .NET C# 中 PDF 到 HTML 的转换,提供了内部处理复杂转换过程的方法。 无论是构建文档管理系统、创建基于网络的 PDF 阅读器,还是使 PDF 内容可被搜索引擎搜索,IronPDF 的转换功能都能提供可靠的解决方案。
如何将基本 PDF 转换为 HTML?
ToHtmlString 方法允许分析现有 PDF 文档中的 HTML 元素。 它可作为调试或 PDF 对比的工具。 SaveAsHtml 方法直接将 PDF 文档保存为 HTML 文件。 这两种方法都能根据具体需求提供灵活性。
PDF 到 HTML 的转换过程保留了 PDF 文档的视觉布局,同时为网络应用程序创建了 HTML 输出。 当您需要在网络浏览器中显示 PDF 内容而不需要用户下载 PDF 文件或安装阅读器插件时,这将有所帮助。
注意:原始 PDF 中的所有交互式表单字段在生成的 HTML 文档中将不再可用。
对于使用 IronPDF 表单的开发人员,转换过程会将表单字段渲染为静态内容。 为保持表单功能,可考虑使用 IronPdf 的 表单编辑功能在转换前提取表单数据。
如何实现转换代码?
:path=/static-assets/pdf/content-code-examples/how-to/pdf-to-html.cs
using IronPdf;
using System;
PdfDocument pdf = PdfDocument.FromFile("sample.pdf");
// Convert PDF to HTML string
string html = pdf.ToHtmlString();
Console.WriteLine(html);
// Convert PDF to HTML file
pdf.SaveAsHtml("myHtml.html");
$vbLabelText
$csharpLabel
代码演示了将 PDF 转换为 HTML 的两种主要方法。 ToHtmlString 方法适用于需要以编程方式处理 HTML 内容的情况,而 SaveAsHtml 方法则直接生成文件。 对于多个 PDF 文件,请使用类似技术批量处理。
输出的 HTML 看起来像什么?
使用 SaveAsHtml 方法生成的完整 HTML 输出已输入到以下网站中。
如何配置高级 PDF 至 HTML 选项?
ToHtmlString 和 SaveAsHtml 方法都通过 HtmlFormatOptions 类提供配置选项。 该配置系统可自定义生成的 HTML 输出的外观和行为。 可用的属性包括
- BackgroundColor:设置 HTML 输出背景颜色
- PdfPageMargin:以像素为单位设置页面边距
以下属性适用于 ToHtmlString 和 SaveAsHtml 方法中的"title"参数。 他们在不修改原始 PDF 标题的情况下,在内容开头添加了一个新标题:
- H1Color:设置标题颜色
- H1FontSize:以像素为单位设置标题字体大小
- H1TextAlignment:设置标题对齐方式(左对齐、居中对齐或右对齐)
对于使用自定义纸张大小或特定页面方向的开发人员,这些配置选项可确保 HTML 输出保持预期的视觉结构。
有哪些配置选项?
:path=/static-assets/pdf/content-code-examples/how-to/pdf-to-html-advanced-settings.cs
using IronPdf;
using IronSoftware.Drawing;
using System;
PdfDocument pdf = PdfDocument.FromFile("sample.pdf");
// PDF to HTML configuration options
HtmlFormatOptions htmlformat = new HtmlFormatOptions();
htmlformat.BackgroundColor = Color.White;
htmlformat.PdfPageMargin = 10;
htmlformat.H1Color = Color.Blue;
htmlformat.H1FontSize = 25;
htmlformat.H1TextAlignment = TextAlignment.Center;
// Convert PDF to HTML string
string html = pdf.ToHtmlString();
Console.WriteLine(html);
// Convert PDF to HTML file
pdf.SaveAsHtml("myHtmlConfigured.html", true, "Hello World", htmlFormatOptions: htmlformat);
$vbLabelText
$csharpLabel
本示例展示了如何使用自定义样式创建精美的 HTML 输出。 配置选项与 IronPdf 的 渲染引擎配合使用,可生成保持视觉保真度的高质量 HTML。
配置输出有何不同?
使用 SaveAsHtml 方法生成的完整 HTML 输出已输入到以下网站中。
为什么 HTML 输出使用 SVG 标记?
这些方法可生成带有内联 CSS 的 HTML 字符串。 输出的 HTML 使用 SVG 标记代替标准 HTML 标记。 尽管存在这种差异,但它仍能生成有效的 HTML,并能在网络浏览器中正确渲染。 使用 RenderHtmlAsPdf 方法渲染 PDF 文档时,该方法返回的 HTML 字符串可能与输入的 HTML 字符串不同。
基于 SVG 的方法可确保准确呈现复杂的 PDF 布局,包括精确定位、字体和图形。 这种方法对于包含 图像、图表或难以用标准 HTML 元素复制的复杂格式的 PDF 文件非常有效。
附加代码示例:批量将 PDF 转换为 HTML.
关于将多个 PDF 转换为 HTML,这里有一个处理整个目录 PDF 文件的示例:
using IronPdf;
using System.IO;
public class BatchPdfToHtmlConverter
{
public static void ConvertPdfDirectory(string inputDirectory, string outputDirectory)
{
// Ensure output directory exists
Directory.CreateDirectory(outputDirectory);
// Configure HTML output settings once for consistency
HtmlFormatOptions formatOptions = new HtmlFormatOptions
{
BackgroundColor = Color.WhiteSmoke,
PdfPageMargin = 15,
H1FontSize = 28,
H1TextAlignment = TextAlignment.Left
};
// Process all PDF files in the directory
string[] pdfFiles = Directory.GetFiles(inputDirectory, "*.pdf");
foreach (string pdfPath in pdfFiles)
{
try
{
// Load PDF document
PdfDocument pdf = PdfDocument.FromFile(pdfPath);
// Generate output filename
string fileName = Path.GetFileNameWithoutExtension(pdfPath);
string htmlPath = Path.Combine(outputDirectory, $"{fileName}.html");
// Convert and save as HTML with consistent formatting
pdf.SaveAsHtml(htmlPath, true, fileName, htmlFormatOptions: formatOptions);
Console.WriteLine($"Converted: {fileName}.pdf → {fileName}.html");
}
catch (Exception ex)
{
Console.WriteLine($"Error converting {pdfPath}: {ex.Message}");
}
}
}
}
using IronPdf;
using System.IO;
public class BatchPdfToHtmlConverter
{
public static void ConvertPdfDirectory(string inputDirectory, string outputDirectory)
{
// Ensure output directory exists
Directory.CreateDirectory(outputDirectory);
// Configure HTML output settings once for consistency
HtmlFormatOptions formatOptions = new HtmlFormatOptions
{
BackgroundColor = Color.WhiteSmoke,
PdfPageMargin = 15,
H1FontSize = 28,
H1TextAlignment = TextAlignment.Left
};
// Process all PDF files in the directory
string[] pdfFiles = Directory.GetFiles(inputDirectory, "*.pdf");
foreach (string pdfPath in pdfFiles)
{
try
{
// Load PDF document
PdfDocument pdf = PdfDocument.FromFile(pdfPath);
// Generate output filename
string fileName = Path.GetFileNameWithoutExtension(pdfPath);
string htmlPath = Path.Combine(outputDirectory, $"{fileName}.html");
// Convert and save as HTML with consistent formatting
pdf.SaveAsHtml(htmlPath, true, fileName, htmlFormatOptions: formatOptions);
Console.WriteLine($"Converted: {fileName}.pdf → {fileName}.html");
}
catch (Exception ex)
{
Console.WriteLine($"Error converting {pdfPath}: {ex.Message}");
}
}
}
}
$vbLabelText
$csharpLabel
该批量转换示例适用于内容管理系统、数字档案或需要在网络上访问大量 PDF 内容的应用程序。 有关以编程方式处理 PDF 的更多信息,请浏览我们的 教程部分。