如何使用IronPDF在C#中將PDF轉換為HTML
IronPDF使用SaveAsHtml方法在C#中通過一行程式碼實現PDF轉HTML轉換,使PDF更加適合網頁,提升可存取性、SEO和網頁整合。 IronPDF程式庫提供了一個強大的解決方案,能夠在保持視覺結構和佈局的情況下將PDF內容轉換為HTML格式。
將PDF轉換為HTML帶來以下好處:
- 增強網頁可訪性
- 不同裝置的響應式設計
- 提高搜尋引擎最佳化
- 無縫的網頁整合
- 通過網頁工具輕鬆編輯內容
- 跨平台相容性
- 支持動態元素
此轉換過程在重新利用PDF內容於網路平台或當您需要從PDF中提取文字和圖像以進行進一步處理時非常有用。
IronPDF在.NET C#中簡化了PDF轉HTML的轉換,提供了內部處理複雜轉換過程的方法。 無論是構建文件管理系統、建立基於網頁的PDF查看器,還是讓PDF內容可被搜尋引擎搜索,IronPDF的轉換能力都提供了可靠的解決方案。
快速入門:使用IronPDF即時將PDF轉換為HTML使用IronPDF通過一行程式碼將PDF文件轉換為HTML文件。 此範例演示如何使用IronPDF的SaveAsHtml方法進行快速的PDF轉HTML轉換。
-
1Install IronPDF with NuGet Package Manager
-
2複製並運行這段程式碼片段。
IronPdf.PdfDocument.FromFile("example.pdf").SaveAsHtml("output.html");C# -
3部署以在您的實時環境中測試
今天就開始在您的專案中使用IronPDF,透過免費試用
最小工作流程 (5步)
- Download the
IronPdfLibrary for .NET - 使用
FromFile方法匯入現有的PDF文件 - 使用HtmlFormatOptions類配置輸出HTML
- 使用
ToHtmlString方法將PDF轉換為HTML字串 - 使用
SaveAsHtml方法匯出HTML文件
如何將基本的PDF轉換為HTML?
ToHtmlString方法允許分析現有PDF文件中的HTML元素。 它作為除錯或PDF比較的工具。 SaveAsHtml方法直接將PDF文件保存為HTML文件。 這兩種方法根據特定需求提供了靈活性。
PDF轉HTML轉換過程保持了PDF文件的視覺佈局,同時為網頁應用建立HTML輸出。 這在您需要在網頁瀏覽器中顯示PDF內容而不需要使用者下載PDF文件或安裝閱讀器插件時非常有用。
對於正在處理PDF表單的開發者來說,轉換過程將表單欄位渲染為靜態內容。 為了保持表單功能,請考慮使用IronPDF的表單編輯功能在轉換之前提取表單資料。
範例PDF的樣子如何?
如何實現轉換程式碼?
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");Imports IronPdf
Imports System
Dim pdf As PdfDocument = PdfDocument.FromFile("sample.pdf")
' Convert PDF to HTML string
Dim html As String = pdf.ToHtmlString()
Console.WriteLine(html)
' Convert PDF to HTML file
pdf.SaveAsHtml("myHtml.html")程式碼展示了兩種主要的PDF轉HTML轉換方法。 SaveAsHtml方法直接生成文件。 對於多個PDF,可以使用類似技術進行批量處理。
輸出的HTML的樣子如何?
從SaveAsHtml方法生成的整個輸出HTML已被輸入到下面的網站中。
如何配置高級PDF轉HTML選項?
HtmlFormatOptions類提供配置選項。 這個配置系統自定義了生成的HTML輸出的外觀和行為。 可用的屬性包括:
BackgroundColor: 設定HTML輸出背景顏色PdfPageMargin: 設定頁面邊距(像素)
下面的屬性適用於SaveAsHtml方法中的'title'參數。 它們在內容的開頭新增新標題而不修改原始PDF標題:
H1Color: 設定標題顏色H1FontSize: 設定標題字體大小(像素)H1TextAlignment: 設定標題對齊(左、居中或右)
對於需要自訂紙張尺寸或特定頁面方向的開發者來說,這些配置選項確保HTML輸出保持預期的視覺結構。
可用的配置選項有哪些?
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);Imports IronPdf
Imports IronSoftware.Drawing
Imports System
Dim pdf As PdfDocument = PdfDocument.FromFile("sample.pdf")
' PDF to HTML configuration options
Dim htmlformat As 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
Dim html As String = pdf.ToHtmlString()
Console.WriteLine(html)
' Convert PDF to HTML file
pdf.SaveAsHtml("myHtmlConfigured.html", True, "Hello World", htmlFormatOptions:=htmlformat)此範例顯示如何使用自訂樣式建立精緻的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}");
}
}
}
}Imports IronPdf
Imports System.IO
Public Class BatchPdfToHtmlConverter
Public Shared Sub ConvertPdfDirectory(inputDirectory As String, outputDirectory As String)
' Ensure output directory exists
Directory.CreateDirectory(outputDirectory)
' Configure HTML output settings once for consistency
Dim formatOptions As New HtmlFormatOptions With {
.BackgroundColor = Color.WhiteSmoke,
.PdfPageMargin = 15,
.H1FontSize = 28,
.H1TextAlignment = TextAlignment.Left
}
' Process all PDF files in the directory
Dim pdfFiles As String() = Directory.GetFiles(inputDirectory, "*.pdf")
For Each pdfPath As String In pdfFiles
Try
' Load PDF document
Dim pdf As PdfDocument = PdfDocument.FromFile(pdfPath)
' Generate output filename
Dim fileName As String = Path.GetFileNameWithoutExtension(pdfPath)
Dim htmlPath As String = 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 ex As Exception
Console.WriteLine($"Error converting {pdfPath}: {ex.Message}")
End Try
Next
End Sub
End Class此批次轉換範例適用於內容管理系統、數位檔案館,或需要將大量PDF內容放置至網上的應用程式。 有關如何以程式方式處理PDF的更多資訊,請瀏覽教程部分。
常見問題
如何在C#中將PDF文件轉換為HTML?
使用IronPDF,您可以僅用一行程式碼在C#中將PDF轉換為HTML:IronPdf.PdfDocument.FromFile("example.pdf").SaveAsHtml("output.html")。此方法在內部處理複雜的轉換過程,同時保持PDF文件的視覺結構和佈局。
將PDF轉換為HTML的主要好處是什麼?
IronPDF的PDF轉HTML轉換提供了多項好處,包括增強的網頁無障礙性、適用於不同裝置的響應式設計、改善SEO、無縫的網頁整合、透過網頁工具輕鬆編輯內容、跨平台相容性和對動態元素的支援。
有哪些方法可用於PDF轉HTML轉換?
IronPDF提供兩種主要方法進行PDF轉HTML轉換:ToHtmlString方法允許分析HTML元素並以字串形式返回HTML,SaveAsHtml方法直接將PDF文件保存為HTML文件。這兩種方法都保留了PDF文件的視覺佈局。
在將PDF轉換為HTML後,互動式表單字段會正常工作嗎?
不,使用IronPDF的PDF轉HTML轉換時,原始PDF中的所有互動式表單字段在生成的HTML文件中將不再具備功能。表單字段被呈現為靜態內容。為了保持表單功能,您應在轉換前使用IronPDF的表單編輯功能來提取表單資料。
在從PDF轉換時,我可以自定義HTML輸出嗎?
是的,IronPDF允許您使用HtmlFormatOptions類來配置輸出HTML。這使您可以控制HTML轉換過程的各個方面,以確保輸出的內容符合您的特定需求。
Why does IronPDF use SVG tags in HTML output?
IronPDF uses SVG tags to ensure accurate representation of complex PDF layouts, maintaining precise positioning, fonts, and graphics which standard HTML might not replicate effectively.
Is batch processing available for converting multiple PDFs to HTML with IronPDF?
Yes, IronPDF supports batch conversion of multiple PDF files to HTML, facilitating bulk processing in digital archives and content management systems.
How can I ensure consistent styling across multiple HTML files generated from PDFs?
IronPDF allows you to set a consistent HtmlFormatOptions configuration, ensuring uniform styling across multiple converted HTML files.
Does IronPDF provide a way to analyze HTML elements extracted from a PDF?
Yes, using the ToHtmlString method, developers can analyze HTML content extracted from PDF documents, useful for debugging or element comparison.
What is an example of a minimal workflow for converting PDF to HTML with IronPDF?
A minimal workflow involves downloading the IronPDF library, loading a PDF using FromFile, and converting it with the SaveAsHtml method, typically incorporating steps for configuration and formatting.

Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。