IronPDF 快速入門指南
安裝 IronPDF,只需五分鐘即可建立您的第一個 PDF 檔案。憑藉其簡潔易用的 API,您可以將 HTML、DOCX、圖像等多種格式的檔案轉換為像素級完美的 PDF 檔案。
1. 先決條件
- .NET Framework 4.6.2+ 或 .NET Core 3.1+ 或 .NET 5+
- Visual C++ Redistributable (適用於 Windows)。
2. 安裝 IronPDF
標準版 IronPDF 安裝程式在本地運行。 如果您想在 Docker/微服務上進行部署,請查看遠端引擎模式指南。
-
1Install IronPDF with NuGet Package Manager
PM > Install-Package IronPdf
-
2Copy and run this code snippet.
IronPdf.ChromePdfRenderer .StaticRenderHtmlAsPdf("<p>Hello World</p>") .SaveAs("pixelperfect.pdf");C# -
3Deploy to test on your live environment
Start using IronPDF in your project today with a free trial
- 在解決方案資源管理器中,以滑鼠右鍵按一下"引用"。
- 選擇'管理NuGet package' > 'Browse' > 搜尋IronPDF
PM > Install-Package IronPdf
- 下載IronPDF DLL 套件 將適用於您作業系統的 ZIP 檔案解壓縮到解決方案目錄中的某個位置。
- 在 Visual Studio 解決方案資源管理器中,以滑鼠右鍵按一下"依賴項"。
- "新增項目引用" > 選擇"瀏覽"以包含從 zip 檔案中提取的所有 DLL 檔案。
平台專屬指南
3. 應用許可證密鑰
當您購買IronPDF 授權或選擇開始 30 天試用期時,許可證金鑰將會傳送到您的電子郵件。 複製密鑰並將其新增到應用程式的開頭。
IronPdf.License.LicenseKey = "YOUR-IRONPDF-LICENSE-KEY";Imports IronPdf
IronPdf.License.LicenseKey = "YOUR-IRONPDF-LICENSE-KEY"4. 建立你的第一個PDF
將此程式碼新增至您的 .cs 檔案頂部。
using IronPdf;Imports IronPdf建立PDF物件的最簡單方法只是使用寬度和高度。 這個PdfDocument建構子建立了一個空白PDF,準備好進行自訂。
using IronPdf;
PdfDocument pdf = new PdfDocument(270, 270);
pdf.SaveAs("blankPage.pdf");Imports IronPdf
Dim pdf As New PdfDocument(270, 270)
pdf.SaveAs("blankPage.pdf")使用ChromePdfRenderer.RenderHtmlAsPdf方法,您可以使用內嵌的Chromium引擎將任何HTML(包括HTML5)轉換為PDF。
using IronPdf;
IronPdf.ChromePdfRenderer
.StaticRenderHtmlAsPdf("<p>Hello World</p>")
.SaveAs("string-to-pdf.pdf");
使用DocxToPdfRenderer類別將Word文件轉換為PDF,您可以將DOCX文件直接呈現為可自定義的PDF,以便無縫整合到.NET應用中。
using IronPdf;
DocxToPdfRenderer renderer = new DocxToPdfRenderer();
PdfDocument pdf = renderer.RenderDocxAsPdf("Modern-chronological-resume.docx");
pdf.SaveAs("pdfFromDocx.pdf");Imports IronPdf
Private renderer As New DocxToPdfRenderer()
Private pdf As PdfDocument = renderer.RenderDocxAsPdf("Modern-chronological-resume.docx")
pdf.SaveAs("pdfFromDocx.pdf")5. 更多進階範例
除了簡單的 PDF 建立和轉換功能外,IronPDF 還提供更強大的 PDF 自訂選項。
通過實例化TextHeaderFooter建立文字頁眉或頁腳,新增您的文字並將其附加到PDF上。
using IronPdf;
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1>");
// Create text header
TextHeaderFooter textHeader = new TextHeaderFooter
{
CenterText = "This is the header!",
};
// Create text footer
TextHeaderFooter textFooter = new TextHeaderFooter
{
CenterText = "This is the footer!",
};
// Add text header and footer to the PDF
pdf.AddTextHeaders(textHeader);
pdf.AddTextFooters(textFooter);
pdf.SaveAs("addTextHeaderFooter.pdf");Imports IronPdf
Dim renderer As New ChromePdfRenderer()
Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1>")
' Create text header
Dim textHeader As New TextHeaderFooter With {
.CenterText = "This is the header!"
}
' Create text footer
Dim textFooter As New TextHeaderFooter With {
.CenterText = "This is the footer!"
}
' Add text header and footer to the PDF
pdf.AddTextHeaders(textHeader)
pdf.AddTextFooters(textFooter)
pdf.SaveAs("addTextHeaderFooter.pdf")使用RedactTextOnAllPages輕鬆編輯文字,以刪除整個文件中的短語。
using IronPdf;
PdfDocument pdf = PdfDocument.FromFile("novel.pdf");
// Redact 'Alaric' phrase from all pages
pdf.RedactTextOnAllPages("Alaric");
pdf.SaveAs("redacted.pdf");Imports IronPdf
Private pdf As PdfDocument = PdfDocument.FromFile("novel.pdf")
' Redact 'Alaric' phrase from all pages
pdf.RedactTextOnAllPages("Alaric")
pdf.SaveAs("redacted.pdf")使用C#中的Merge方法合併兩個PDF文件。 在任何ReplaceTextOnAllPages替換舊文字為新文字。
using IronPdf;
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>.NET6</h1>");
string oldText = ".NET6";
string newText = ".NET7";
// Replace text on all pages
pdf.ReplaceTextOnAllPages(oldText, newText);
pdf.SaveAs("replaceText.pdf");Imports IronPdf
Private renderer As New ChromePdfRenderer()
Private pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>.NET6</h1>")
Private oldText As String = ".NET6"
Private newText As String = ".NET7"
' Replace text on all pages
pdf.ReplaceTextOnAllPages(oldText, newText)
pdf.SaveAs("replaceText.pdf")快速故障排除
| 問題 | 解決方案 |
|---|---|
| 缺少 Visual C++ 運行時 | 安裝 Visual C++ 可再發行元件套件-Chrome 引擎需要 x86 和 x64 版本。 |
| 許可證未識別 | 使用IronPdf.License.IsLicensed屬性進行驗證。確保在執行任何 IronPDF 操作之前套用許可證。 |
| 慢速首次渲染 | 啟動時呼叫IronPdf.Installation.Initialize()以預初始化渲染引擎 |
| Linux/Docker 依賴項 | 設定Installation.LinuxAndDockerDependenciesAutoConfig = true以自動安裝依賴項 |
後續步驟
Frequently Asked Questions
我如何設置IronPdfEngine以進行遠端PDF生成?
要設置IronPdfEngine進行遠端PDF生成,從NuGet安裝IronPdf.Slim包並使用IronPdfConnectionConfiguration類配置連接設置。此設置允許您將應用程式遠端連接到IronPdfEngine實例。
使用IronPdfEngine和我的應用程式的主要好處是什麼?
將IronPdfEngine與您的應用程式一起使用允許遠端執行PDF任務,有助於避免平臺特定的相容性問題,尤其是在舊系統和移動平臺上。它還消除了執行過程中對.NET運行時的需求。
為什麼我可能選擇使用IronPdfEngine而不是本地PDF庫?
您可能會選擇使用IronPdfEngine來遠端執行性能密集的PDF功能,降低不同操作系統的相容性問題,並通過使用Chrome相同的渲染器來轉換HTML為PDF來提高性能。
IronPdfEngine是否支援水平擴展?
不,IronPdfEngine目前不支援水平擴展,這意味著它不能跨多個實例載入平衡,因為PDF檔二進制檔在伺服器記憶體中處理的方式。
IronPdfEngine能否運行在不同的操作系統上?
IronPdfEngine設計為運行在使用Docker容器的Linux系統上。然而,二進制檔是平臺特定的,因此您需要確保使用正確版本的操作系統。
如果使用IronPdfEngine時我的PDF輸出不同,我該怎麼辦?
由於不同操作系統的行為,PDF輸出可能略有不同。為了最小化差異,請確保您使用正確的Docker影像,並檢查可能影響渲染的任何OS特定設置。
如何確保我的應用程式正在使用正確版本的IronPdfEngine?
為了確保相容性,每個版本的IronPDF需要對應的IronPdfEngine版本。請確保同時更新兩個組件以避免跨版本問題。
在Windows上使用IronPdfEngine有什麼限制?
在Windows上使用IronPdfEngine時,您需要Docker的Linux容器,並且必須確保伺服器端口可達。二進制檔是平臺特定的,並需要切換到Linux容器。
如何配置IronPDF以連接到遠端IronPdfEngine伺服器?
要配置IronPDF用於遠端伺服器,使用Installation.ConnectToIronPdfHost,結合IronPdf.GrpcLayer.IronPdfConnectionConfiguration.RemoteServer方法,指定伺服器的IP和端口詳情。
使用IronPdfEngine時應使用哪個包以減小應用程式大小?
您應該使用來自NuGet的IronPdf.Slim包,因為它僅包含運行IronPDF與IronPdfEngine所需的組件,因此可以減小應用程式大小。

Curtis Chau holds a Bachelor’s degree in Computer Science (Carleton University) and specializes in front-end development with expertise in Node.js, TypeScript, JavaScript, and React. Passionate about crafting intuitive and aesthetically pleasing user interfaces, Curtis enjoys working with modern frameworks and creating well-structured, visually appealing manuals.