
如何在C#(逐步指南)中從URL下載PDF
1.0 簡介
Adobe 的可攜式文件格式 (PDF) 廣泛用於文件查看和交換。 開發人員經常需要建立 PDF 以滿足客戶需求,現代程式庫簡化了此過程。 在為專案選擇程式庫時,重要的是考慮構建、閱讀和轉換能力等功能。
2.0 IronPDF 功能
IronPDF 是一個多功能的程式庫,用於建立、閱讀和編輯 PDF 文件,具備使用 Chrome 引擎將 HTML 轉換為 PDF 的能力。它支持廣泛的網頁組件,可以用於 ASP.NET Web 應用程式及傳統的 Windows 應用。 此程式庫允許使用 HTML5、JavaScript、CSS 和圖像建立具有視覺吸引力的 PDF,並包括強大的 HTML 到 PDF 轉換器。
- IronPDF 程式庫支持各種輸入以生成 PDF,包括 影像檔案到 PDF 轉換、HTML5 到 PDF 轉換、ASPX 到 PDF 轉換,以及 Razor/MVC 檢視轉換。
- 該程式庫具有建立互動式 PDF 文件的工具,編輯和發送互動式表單、分割 PDF 文件、提取文字和圖像、文字搜索以及 將 PDF 頁面光柵化為圖像。
- 該程式庫允許使用 URL 作為 PDF 文件的基礎,支持使用者代理、代理、Cookie、HTTP 標頭和表單變數進行 HTML 登錄表單後的身份驗證。
- IronPDF 通過提供使用者名和密碼,允許存取受密碼保護的 PDF 文件。
- 該程式庫支持閱讀和編輯現有的 PDF 文件。
- 使用者可以新增文字、圖形、組織書籤、水印、自訂頁首和頁尾到 PDF 文件中,並從 PDF 文件中提取圖像。
- IronPDF 允許在新的或現有的 PDF 文件中分割和合併頁面,並且可以從文字中生成 PDF 物件,而無需 Adobe Acrobat Reader。
- 支持 CSS 媒體文件和 CSS 文件轉換為 PDF。
- 使用者可以 新增可填寫的 PDF 表單或使用 IronPDF 完成現有表單。
- IronPDF 還可以 將 PDF 頁面轉換為各種圖像格式,如 JPEG、PNG 等。
3.0 從 URL 建立 PDF 文件
利用 IronPDF 的內建 Chrome 瀏覽器和 API 程式庫,從網頁生成 PDF 文件變得輕而易舉。 只需提供 URL 並使用 IronPDF API 程式庫將其轉換為 PDF 文件即可。 僅需幾行程式碼即可快速完成文件轉換:
// Create a new instance of the ChromePdfRenderer class
var renderer = new IronPdf.ChromePdfRenderer();
// Convert the specified URL to a PDF
var pdf = renderer.RenderUrlAsPdf("https://www.google.co.in/");
// Save the PDF to the specified path
pdf.SaveAs("result.pdf");' Create a new instance of the ChromePdfRenderer class
Dim renderer = New IronPdf.ChromePdfRenderer()
' Convert the specified URL to a PDF
Dim pdf = renderer.RenderUrlAsPdf("https://www.google.co.in/")
' Save the PDF to the specified path
pdf.SaveAs("result.pdf")RenderUrlAsPdf 方法可以用以快速將 URL 轉換為 PDF 文件。 只需提供 URL 和希望的儲存位置,IronPDF 將根據上述資訊生成 PDF 文件。 僅需幾行程式碼便能輕鬆將網頁轉換為 PDF 文件。
PDF Converter .NET (開發者教程),圖1:從 Google URL 生成的 PDF 文件 從 Google URL 生成的 PDF 文件
4.0 從 HTML 字串建立 PDF
using IronPDF API 程式庫可以快速將 HTML 字串轉換為 PDF 文件。 以下程式碼片段可以將 HTML 字串轉換為文件,並將任何 HTML 標籤翻譯為 PDF 文件。
// Create a new instance of the ChromePdfRenderer class
var renderer = new IronPdf.ChromePdfRenderer();
// Convert the specified HTML string to a PDF
var pdf = renderer.RenderHtmlAsPdf("<p>Hello world!!</p>");
// Save the PDF to the specified path
pdf.SaveAs("result.pdf");' Create a new instance of the ChromePdfRenderer class
Dim renderer = New IronPdf.ChromePdfRenderer()
' Convert the specified HTML string to a PDF
Dim pdf = renderer.RenderHtmlAsPdf("<p>Hello world!!</p>")
' Save the PDF to the specified path
pdf.SaveAs("result.pdf")此程式碼片段演示了如何使用 RenderHtmlAsPdf 函式將 HTML 文字轉換為 PDF。 該函式接受所需的 HTML 程式碼來將其轉換為字串。 然後可以快速且輕鬆地使用 SaveAs 功能保存文件,並完成過程。
PDF Converter .NET (開發者教程),圖2:從 HTML 字串生成的 PDF 文件 從 HTML 字串生成的 PDF 文件
5.0 從 HTML 文件建立 PDF 文件
IronPDF API 程式庫允許快速將 HTML 文件轉換為 PDF 文件。 使用以下範例程式碼可以將任何 HTML 標籤轉換為 PDF 文件。
// Create a new instance of the ChromePdfRenderer class
var renderer = new IronPdf.ChromePdfRenderer();
// Convert the specified HTML file to a PDF
var pdf = renderer.RenderHtmlFileAsPdf("test.html");
// Save the PDF to the specified path
pdf.SaveAs("result.pdf");' Create a new instance of the ChromePdfRenderer class
Dim renderer = New IronPdf.ChromePdfRenderer()
' Convert the specified HTML file to a PDF
Dim pdf = renderer.RenderHtmlFileAsPdf("test.html")
' Save the PDF to the specified path
pdf.SaveAs("result.pdf")HTML 文字如下所示:
<p style="color:red">Hello world</p>
PDF Converter .NET (開發者教程),圖3:從 HTML 文件生成的 PDF 文件 從 HTML 文件生成的 PDF 文件
6.0 從富文字文件建立 PDF 文件
IronPDF API 程式庫還允許從富文字文件 (RTF) 快速建立 PDF。 用於將 RTFs 轉換為 PDFs 的範例程式碼可用於將任意數量的 RTFs 轉換為單個 PDF 文件。程式碼如下所示。
// Create a new instance of the ChromePdfRenderer class
var renderer = new IronPdf.ChromePdfRenderer();
// Convert the specified RTF file to a PDF
var pdf = renderer.RenderRtfFileAsPdf("test.rtf");
// Save the PDF to the specified path
pdf.SaveAs("result.pdf");' Create a new instance of the ChromePdfRenderer class
Dim renderer = New IronPdf.ChromePdfRenderer()
' Convert the specified RTF file to a PDF
Dim pdf = renderer.RenderRtfFileAsPdf("test.rtf")
' Save the PDF to the specified path
pdf.SaveAs("result.pdf")PDF Converter .NET (開發者教程),圖4:從 RTF 文件生成的 PDF 文件 從 RTF 文件生成的 PDF 文件
在上圖中,左側顯示了源文件,而右側顯示了轉換後的 PDF 文件。 另外,我們也可以使用下面提供的程式碼利用 RTF 字串建立 PDF。
// Create a new instance of the ChromePdfRenderer class
var renderer = new IronPdf.ChromePdfRenderer();
// Convert the specified RTF string to a PDF
var pdf = renderer.RenderRtfStringAsPdf("{\\rtf1...}");
// Save the PDF to the specified path
pdf.SaveAs("result.pdf");' Create a new instance of the ChromePdfRenderer class
Dim renderer = New IronPdf.ChromePdfRenderer()
' Convert the specified RTF string to a PDF
Dim pdf = renderer.RenderRtfStringAsPdf("{\rtf1...}")
' Save the PDF to the specified path
pdf.SaveAs("result.pdf")7.0 從 Markdown 文件建立 PDF 文件
IronPDF API 模塊使從 markdown 文件快速生成 PDF 成為可能。 可以使用下面顯示的轉換範例程式碼將任意數量的 Markdown 文件轉換為 PDF 文件。
// Create a new instance of the ChromePdfRenderer class
var renderer = new IronPdf.ChromePdfRenderer();
// Convert the specified Markdown file to a PDF
var pdf = renderer.RenderMarkdownFileAsPdf("Markdown.md");
// Save the PDF to the specified path
pdf.SaveAs("Markdown_result.pdf");' Create a new instance of the ChromePdfRenderer class
Dim renderer = New IronPdf.ChromePdfRenderer()
' Convert the specified Markdown file to a PDF
Dim pdf = renderer.RenderMarkdownFileAsPdf("Markdown.md")
' Save the PDF to the specified path
pdf.SaveAs("Markdown_result.pdf")PDF Converter .NET (開發者教程),圖5:從 Markdown 文件生成的 PDF 文件 從 Markdown 文件生成的 PDF 文件
如上圖所示,源文件在左側,轉換後的 PDF 文件在右側。 下面提供的程式碼允許將 MD 字串轉換為 PDF。 如需更多有關使用 IronPDF 進行 HTML 轉換的資訊,請存取此 HTML 到 PDF 轉換教程。
// Create a new instance of the ChromePdfRenderer class
var renderer = new IronPdf.ChromePdfRenderer();
// Convert the specified Markdown string to a PDF
var pdf = renderer.RenderMarkdownStringAsPdf("# Hello world\n\nHello world");
// Save the PDF to the specified path
pdf.SaveAs("Markdown_result.pdf");Imports Microsoft.VisualBasic
' Create a new instance of the ChromePdfRenderer class
Dim renderer = New IronPdf.ChromePdfRenderer()
' Convert the specified Markdown string to a PDF
Dim pdf = renderer.RenderMarkdownStringAsPdf("# Hello world" & vbLf & vbLf & "Hello world")
' Save the PDF to the specified path
pdf.SaveAs("Markdown_result.pdf")如欲瞭解更多有關如何使用 IronPDF API 程式庫,請參閱開發者文件 IronPDF 文件資源。
8.0 結論
IronPDF 程式庫為開發提供免費授權,根據開發者的需求,有多種授權可供購買以用於生產環境中。 Lite 套件起始價格為 $999 且無持續費用。 授權包括永久授權、30天退款保證、一年產品支援和升級,以及 SaaS 和 OEM 再分發的可能性。 他們是一次性購買,可以用於開發、暫存和生產。 IronPDF 還提供其他限時免費授權和再分發保護的免費授權。 如需完整的 IronPDF 價格和授權資訊總覽,請存取 IronPDF 授權資訊頁面。

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


