觀看David Jones, Agorus, 使用Iron Suite創造新效能
觀看Milan Jovanović使用IronPDF
觀看我們的團隊產品演示
using IronPdf; // Disable local disk access or cross-origin requests Installation.EnableWebSecurity = true; // Instantiate Renderer var renderer = new ChromePdfRenderer(); // Create a PDF from a HTML string using C# var pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>"); // Export to a file or Stream pdf.SaveAs("output.pdf"); // Advanced Example with HTML Assets // Load external html assets: Images, CSS and JavaScript. // An optional BasePath 'C:\site\assets\' is set as the file location to load assets from var myAdvancedPdf = renderer.RenderHtmlAsPdf("<img src='icons/iron.png'>", @"C:\site\assets\"); myAdvancedPdf.SaveAs("html-with-assets.pdf");
Imports IronPdf ' Disable local disk access or cross-origin requests Installation.EnableWebSecurity = True ' Instantiate Renderer Dim renderer = New ChromePdfRenderer() ' Create a PDF from a HTML string using C# Dim pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>") ' Export to a file or Stream pdf.SaveAs("output.pdf") ' Advanced Example with HTML Assets ' Load external html assets: Images, CSS and JavaScript. ' An optional BasePath 'C:\site\assets\' is set as the file location to load assets from Dim myAdvancedPdf = renderer.RenderHtmlAsPdf("<img src='icons/iron.png'>", "C:\site\assets\") myAdvancedPdf.SaveAs("html-with-assets.pdf")
Install-Package IronPdf
使用 IronPDF,您可以從您的 .NET 專案中的簡單 HTML 字串創建新的 PDF 文件,而且 IronPDF 能夠在 C#、F# 和 VB.NET 中使用。 由於使用 ChromePdfRenderer 類,您可以確保從 HTML 字串渲染的任何 PDF 文件都能達到像素完美。 憑借 IronPDF 強大的HTML 到 PDF 轉換功能,您可以創建高質量的 PDF 文件,以滿足您的個人需求。
ChromePdfRenderer
RenderHtmlAsPdf
PdfDocument.SaveAs
請參見下面的代碼示例以獲取更多詳細信息:
在 C# 中將 HTML 字串轉換為 PDF 的第一步是確保 IronPDF 庫在您的專案中正確設置並運行良好。 通過包括 using IronPdf,我們確保可以訪問 IronPDF 庫中所需的類來完成 HTML 到 PDF 的轉換。 下一行 Installation.EnableWebSecurity = true,概念上用於禁用本地磁碟訪問或跨來源請求,確保安全操作。 (注意:這行在示例中缺失,但通常與配置設置有關,用於保證 PDF 渲染操作的安全性。)
using IronPdf
Installation.EnableWebSecurity = true
示例介紹了如何創建 ChromePdfRenderer 的實例來處理 HTML 到 PDF 的轉換。 The RenderHtmlAsPdf method is used to convert a simple HTML string ("<h1>Hello World</h1>") into a PDF document. 此文件使用 SaveAs 方法保存到磁碟。
"<h1>Hello World</h1>"
SaveAs
在進階示例中,IronPDF 顯示能夠處理包含外部資源(如圖像、CSS 和 JavaScript)的 HTML 內容。 要加載這些資源,可選的 BasePath 參數被用來指定包含所需文件的目錄。 最終的 PDF 文件,包括外部資源,使用相同的 SaveAs 方法保存。 此代碼示例強調了 IronPDF 能夠處理基本和複雜 HTML 內容的能力,使其成為一個生成 PDF 文件程式上高效的工具。
BasePath
學習使用 IronPDF 在 C# 中將 HTML 字串轉換為 PDF
using IronPdf; // Instantiate Renderer var renderer = new ChromePdfRenderer(); // Create a PDF from a URL or local file path var pdf = renderer.RenderUrlAsPdf("https://ironpdf.com/"); // Export to a file or Stream pdf.SaveAs("url.pdf");
Imports IronPdf ' Instantiate Renderer Private renderer = New ChromePdfRenderer() ' Create a PDF from a URL or local file path Private pdf = renderer.RenderUrlAsPdf("https://ironpdf.com/") ' Export to a file or Stream pdf.SaveAs("url.pdf")
IronPDF 使從現有 URL 選擇渲染 HTML 為 PDF 文件變得非常簡單。 對 JavaScript、圖像、表單和 CSS 的支持非常高。
從接受查詢字串變數的 ASP.NET URL 渲染 PDF,可以促進設計師和編碼員之間的協作式 PDF 開發。
學習如何使用 IronPDF 將 URL 轉換為 PDF
using IronPdf; using IronPdf.Engines.Chrome; // Instantiate Renderer var renderer = new ChromePdfRenderer(); // Many rendering options to use to customize! renderer.RenderingOptions.SetCustomPaperSizeInInches(12.5, 20); renderer.RenderingOptions.PrintHtmlBackgrounds = true; renderer.RenderingOptions.PaperOrientation = IronPdf.Rendering.PdfPaperOrientation.Landscape; renderer.RenderingOptions.Title = "My PDF Document Name"; renderer.RenderingOptions.EnableJavaScript = true; renderer.RenderingOptions.WaitFor.RenderDelay(50); // in milliseconds renderer.RenderingOptions.CssMediaType = IronPdf.Rendering.PdfCssMediaType.Screen; renderer.RenderingOptions.FitToPaperMode = FitToPaperModes.Zoom; renderer.RenderingOptions.Zoom = 100; renderer.RenderingOptions.CreatePdfFormsFromHtml = true; // Supports margin customization! renderer.RenderingOptions.MarginTop = 40; //millimeters renderer.RenderingOptions.MarginLeft = 20; //millimeters renderer.RenderingOptions.MarginRight = 20; //millimeters renderer.RenderingOptions.MarginBottom = 40; //millimeters // Can set FirstPageNumber if you have a cover page renderer.RenderingOptions.FirstPageNumber = 1; // use 2 if a cover page will be appended // Settings have been set, we can render: renderer.RenderHtmlFileAsPdf("assets/wikipedia.html").SaveAs("output/my-content.pdf");
IRON VB CONVERTER ERROR developers@ironsoftware.com
IronPDF 旨在為開發者提供盡可能靈活的選擇。
在這篇C# PDF 生成教學範例中,我們展示了提供自動化內部功能的 API 與提供控制權的 API 之間的平衡。
IronPDF 支援許多對生成的PDF文件的自訂,包括頁面大小、頁面邊距、頁眉/頁腳內容、內容縮放、CSS 規則集和 JavaScript 執行。
我們希望開發者能夠控制 Chrome 如何將網頁轉成 PDF。 ChromePdfRenderer 類別概述使這成為可能。
ChromePdfRenderer 類別中可用的設置示例包括邊距、頁眉、頁腳、紙張大小和表單創建設置。
探索使用 IronPDF 的像素完美 HTML 到 PDF 指南
using IronPdf; // Instantiate Renderer var renderer = new ChromePdfRenderer(); // Create a PDF from an existing HTML file using C# var pdf = renderer.RenderHtmlFileAsPdf("example.html"); // Export to a file or Stream pdf.SaveAs("output.pdf");
Imports IronPdf ' Instantiate Renderer Private renderer = New ChromePdfRenderer() ' Create a PDF from an existing HTML file using C# Private pdf = renderer.RenderHtmlFileAsPdf("example.html") ' Export to a file or Stream pdf.SaveAs("output.pdf")
IronPDF 是一個功能強大的 .NET 庫,能夠將 HTML 文件轉換為高品質的 PDF 文件。 使用 IronPDF,您只需幾行代碼即可將 HTML 文件渲染為 PDF,並且由於其支持現代網頁標準,得到的 PDF 文件將是像素級完美。 利用 IronPDF 功能強大的 HTML 文件轉 PDF 功能非常簡單,這要歸功於 ChromePdfRenderer 類,它可以輕鬆處理 HTML 到 PDF 的轉換。
using IronPdf;
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlFileAsPdf("example.html");
pdf.SaveAs("output.pdf");
這段代碼創建了一個從 HTML 文件渲染的 PDF 文件。為此,我們必須首先確保 IronPDF 庫已安裝並通過 using IronPdf 行包含在您的專案中。接下來,初始化 ChromePdfRenderer 類,該類提供將 HTML 內容渲染為 PDF 的功能。 此類確保在轉換過程中不會丟失 HTML 文件的原始品質。
一旦渲染器實例化,您可以使用 RenderHtmlFileAsPdf 方法將現有的 HTML 文件轉換為 PDF。 在此示例中,HTML 文件 "example.html" 被傳遞給此方法,創建了一個 PDF 對象。 最後,要保存生成的 PDF,請使用 SaveAs 方法,指定所需的文件名和位置。 此簡單過程允許您在 C# 應用程序中輕鬆地從 HTML 文件生成 PDF。
RenderHtmlFileAsPdf
學習如何使用 IronPDF 將 HTML 文件轉換為 PDF
using IronPdf; var PdfOptions = new IronPdf.ChromePdfRenderOptions() { CreatePdfFormsFromHtml = true, EnableJavaScript = false, Title = "My ASPX Page Rendered as a PDF" //.. many more options available }; AspxToPdf.RenderThisPageAsPdf(AspxToPdf.FileBehavior.Attachment, "MyPdfFile.pdf", PdfOptions);
Imports IronPdf Private PdfOptions = New IronPdf.ChromePdfRenderOptions() With { .CreatePdfFormsFromHtml = True, .EnableJavaScript = False, .Title = "My ASPX Page Rendered as a PDF" } AspxToPdf.RenderThisPageAsPdf(AspxToPdf.FileBehavior.Attachment, "MyPdfFile.pdf", PdfOptions)
This example demonstrates how the user can change PDF print options to turn a form into HTML.
IronPDF's ASPX to PDF Conversion Guide functionality has many options available for rendering HTML to PDF from a string or a file.
Two options of particular importance are:
Discover How to Convert ASPX to PDF with IronPDF
using IronPdf; using System.IO; using System.Linq; // One or more images as IEnumerable. This example selects all JPEG images in a specific 'assets' folder. var imageFiles = Directory.EnumerateFiles("assets").Where(f => f.EndsWith(".jpg") || f.EndsWith(".jpeg")); // Converts the images to a PDF and save it. ImageToPdfConverter.ImageToPdf(imageFiles).SaveAs("composite.pdf"); // Also see PdfDocument.RasterizeToImageFiles() method to flatten a PDF to images or thumbnails
Imports IronPdf Imports System.IO Imports System.Linq ' One or more images as IEnumerable. This example selects all JPEG images in a specific 'assets' folder. Private imageFiles = Directory.EnumerateFiles("assets").Where(Function(f) f.EndsWith(".jpg") OrElse f.EndsWith(".jpeg")) ' Converts the images to a PDF and save it. ImageToPdfConverter.ImageToPdf(imageFiles).SaveAs("composite.pdf") ' Also see PdfDocument.RasterizeToImageFiles() method to flatten a PDF to images or thumbnails
給定位於計算機上的單個圖像檔案 C:\images\example.png,您可以通過調用 IronPdf.ImageToPdfConverter.ImageToPdf 方法並提供其檔案路徑,快速將其轉換為PDF文件:
C:\images\example.png
IronPdf.ImageToPdfConverter.ImageToPdf
您還可以使用 System.IO.Directory.EnumerateFiles 與 ImageToPdfConverter.ImageToPdf 將多個圖像合併轉換為單個PDF文件:
System.IO.Directory.EnumerateFiles
ImageToPdfConverter.ImageToPdf
To explore more about converting images to PDFs using IronPDF for enhancing your applications, or to discover the entire suite of developer tools offered by Iron Software, including IronBarcode, IronOCR, and more, visit the Iron Software website.
學習如何使用IronPDF將圖像轉換為PDF
using IronPdf; // Initiate PDF Renderer var renderer = new ChromePdfRenderer(); // Add a header to every page easily renderer.RenderingOptions.FirstPageNumber = 1; // use 2 if a cover page will be appended renderer.RenderingOptions.TextHeader.DrawDividerLine = true; renderer.RenderingOptions.TextHeader.CenterText = "{url}"; renderer.RenderingOptions.TextHeader.Font = IronSoftware.Drawing.FontTypes.Helvetica; renderer.RenderingOptions.TextHeader.FontSize = 12; renderer.RenderingOptions.MarginTop = 25; //create 25mm space for header // Add a footer too renderer.RenderingOptions.TextFooter.DrawDividerLine = true; renderer.RenderingOptions.TextFooter.Font = IronSoftware.Drawing.FontTypes.Arial; renderer.RenderingOptions.TextFooter.FontSize = 10; renderer.RenderingOptions.TextFooter.LeftText = "{date} {time}"; renderer.RenderingOptions.TextFooter.RightText = "{page} of {total-pages}"; renderer.RenderingOptions.MarginTop = 25; //create 25mm space for footer // Mergeable fields are: // {page} {total-pages} {url} {date} {time} {html-title} & {pdf-title}
Imports IronPdf ' Initiate PDF Renderer Private renderer = New ChromePdfRenderer() ' Add a header to every page easily renderer.RenderingOptions.FirstPageNumber = 1 ' use 2 if a cover page will be appended renderer.RenderingOptions.TextHeader.DrawDividerLine = True renderer.RenderingOptions.TextHeader.CenterText = "{url}" renderer.RenderingOptions.TextHeader.Font = IronSoftware.Drawing.FontTypes.Helvetica renderer.RenderingOptions.TextHeader.FontSize = 12 renderer.RenderingOptions.MarginTop = 25 'create 25mm space for header ' Add a footer too renderer.RenderingOptions.TextFooter.DrawDividerLine = True renderer.RenderingOptions.TextFooter.Font = IronSoftware.Drawing.FontTypes.Arial renderer.RenderingOptions.TextFooter.FontSize = 10 renderer.RenderingOptions.TextFooter.LeftText = "{date} {time}" renderer.RenderingOptions.TextFooter.RightText = "{page} of {total-pages}" renderer.RenderingOptions.MarginTop = 25 'create 25mm space for footer ' Mergeable fields are: ' {page} {total-pages} {url} {date} {time} {html-title} & {pdf-title}
有兩種方法可以將標頭和頁尾新增至 PDF 文件。 它們可以新增為經典的文字格式,並可選擇合併動態數據。 或者,它們可以透過更靈活的 HTML 格式新增,這使得開發人員可以通過其 HTML 內容渲染動態標頭和頁尾。
今天我們將看看您如何僅需幾個簡單的步驟就能將經典的文本標頭和頁尾新增到您的 PDF 文件中。 向 PDF 文件新增自定義標頭和頁尾的第一步是確保專案中包含 IronPDF 庫,使用 using IronPdf; 語句。 然後,實例化 ChromePdfRenderer,它提供了將您的 HTML 內容渲染為像素完美的 PDF 文件的功能。
接下來,配置標頭設置。 FirstPageNumber 屬性允許您指定起始頁碼,如果需要還可以考慮封面。 TextHeader 屬性使您能夠自定義外觀,例如繪製分隔線、居中文本(在此情況下為文件 URL),選擇字體類型和大小,並在頁面的頂部為標頭創建邊距。
FirstPageNumber
TextHeader
配置標頭後,使用 TextFooter 屬性設置頁尾。 與標頭類似,您可以繪製一條分隔線、選擇字體類型和大小,並包括動態內容,如當前日期、時間和包含總頁數的頁碼。 最後,在頁面的底部創建邊距以容納頁尾。
TextFooter
通過遵循這些步驟,您可以使用富有資訊的標頭和頁尾提升 PDF 文件的專業性和可讀性。
發現如何使用 IronPDF 新增標頭和頁尾
using IronPdf; using System; // Instantiate Renderer var renderer = new IronPdf.ChromePdfRenderer(); // Build a footer using html to style the text // mergeable fields are: // {page} {total-pages} {url} {date} {time} {html-title} & {pdf-title} renderer.RenderingOptions.HtmlFooter = new HtmlHeaderFooter() { MaxHeight = 15, //millimeters HtmlFragment = "<center><i>{page} of {total-pages}<i></center>", DrawDividerLine = true }; // Use sufficient MarginBottom to ensure that the HtmlFooter does not overlap with the main PDF page content. renderer.RenderingOptions.MarginBottom = 25; //mm // Build a header using an image asset // Note the use of BaseUrl to set a relative path to the assets renderer.RenderingOptions.HtmlHeader = new HtmlHeaderFooter() { MaxHeight = 20, //millimeters HtmlFragment = "<img src='logo.png'>", BaseUrl = new Uri(@"C:\assets\images\").AbsoluteUri }; // Use sufficient MarginTop to ensure that the HtmlHeader does not overlap with the main PDF page content. renderer.RenderingOptions.MarginTop = 25; //mm
Imports IronPdf Imports System ' Instantiate Renderer Private renderer = New IronPdf.ChromePdfRenderer() ' Build a footer using html to style the text ' mergeable fields are: ' {page} {total-pages} {url} {date} {time} {html-title} & {pdf-title} renderer.RenderingOptions.HtmlFooter = New HtmlHeaderFooter() With { .MaxHeight = 15, .HtmlFragment = "<center><i>{page} of {total-pages}<i></center>", .DrawDividerLine = True } ' Use sufficient MarginBottom to ensure that the HtmlFooter does not overlap with the main PDF page content. renderer.RenderingOptions.MarginBottom = 25 'mm ' Build a header using an image asset ' Note the use of BaseUrl to set a relative path to the assets renderer.RenderingOptions.HtmlHeader = New HtmlHeaderFooter() With { .MaxHeight = 20, .HtmlFragment = "<img src='logo.png'>", .BaseUrl = (New Uri("C:\assets\images\")).AbsoluteUri } ' Use sufficient MarginTop to ensure that the HtmlHeader does not overlap with the main PDF page content. renderer.RenderingOptions.MarginTop = 25 'mm
HTML 標題和頁腳提供了一種靈活的方法,用於為您的 PDF 文檔創建動態標題和頁腳。 通過這種方法添加標題和頁腳,開發人員可以完全控制其標題和頁腳的外觀,因為它們被渲染為獨立的 HTML 文檔,能夠包含自己的資產和樣式表。
首先,您需要創建一個 ChromePdfRenderer 類的實例,它負責將 HTML 內容渲染為像素完美的 PDF 文檔。
接下來,使用 HtmlHeaderFooter 類定義頁腳,您需要指定 MaxHeight、頁腳的 HTML 內容(在我們的例子中包括頁碼)以及用於圖像解析的基本 URL。 頁腳的樣式設置為顯示居中的頁面信息。
HtmlHeaderFooter
MaxHeight
為了避免頁腳與 PDF 的主要內容重疊,使用 MarginBottom 屬性設置底部邊距。 同樣地,使用 HtmlHeaderFooter 類創建包含圖像(如徽標)的標頭。 這裡我們設置了一個指向包含您圖像資產的目錄的 BaseUrl,以便在渲染期間正確解析圖像。
MarginBottom
BaseUrl
最後,使用 MarginTop 屬性設置頂部邊距,以防止標頭與內容重疊。 這個例子展示了如何輕鬆地在您的 PDF 文檔中實現自定義 HTML 標題和頁腳與 IronPDF。
MarginTop
學習如何使用 IronPDF 為 PDF 添加 HTML 標題和頁腳
using IronPdf; using System; // Step 1. Creating a PDF with editable forms from HTML using form and input tags // Radio Button and Checkbox can also be implemented with input type 'radio' and 'checkbox' const string formHtml = @" <html> <body> <h2>Editable PDF Form</h2> <form> First name: <br> <input type='text' name='firstname' value=''> <br> Last name: <br> <input type='text' name='lastname' value=''> <br> <br> <p>Please specify your gender:</p> <input type='radio' id='female' name='gender' value= 'Female'> <label for='female'>Female</label> <br> <br> <input type='radio' id='male' name='gender' value='Male'> <label for='male'>Male</label> <br> <br> <input type='radio' id='non-binary/other' name='gender' value='Non-Binary / Other'> <label for='non-binary/other'>Non-Binary / Other</label> <br> <p>Please select all medical conditions that apply:</p> <input type='checkbox' id='condition1' name='Hypertension' value='Hypertension'> <label for='condition1'> Hypertension</label><br> <input type='checkbox' id='condition2' name='Heart Disease' value='Heart Disease'> <label for='condition2'> Heart Disease</label><br> <input type='checkbox' id='condition3' name='Stoke' value='Stoke'> <label for='condition3'> Stoke</label><br> <input type='checkbox' id='condition4' name='Diabetes' value='Diabetes'> <label for='condition4'> Diabetes</label><br> <input type='checkbox' id='condition5' name='Kidney Disease' value='Kidney Disease'> <label for='condition5'> Kidney Disease</label><br> </form> </body> </html>"; // Instantiate Renderer var renderer = new ChromePdfRenderer(); renderer.RenderingOptions.CreatePdfFormsFromHtml = true; renderer.RenderHtmlAsPdf(formHtml).SaveAs("BasicForm.pdf"); // Step 2. Reading and Writing PDF form values. var FormDocument = PdfDocument.FromFile("BasicForm.pdf"); // Set and Read the value of the "firstname" field var FirstNameField = FormDocument.Form.FindFormField("firstname"); FirstNameField.Value = "Minnie"; Console.WriteLine("FirstNameField value: {0}", FirstNameField.Value); // Set and Read the value of the "lastname" field var LastNameField = FormDocument.Form.FindFormField("lastname"); LastNameField.Value = "Mouse"; Console.WriteLine("LastNameField value: {0}", LastNameField.Value); FormDocument.SaveAs("FilledForm.pdf");
Imports IronPdf Imports System ' Step 1. Creating a PDF with editable forms from HTML using form and input tags ' Radio Button and Checkbox can also be implemented with input type 'radio' and 'checkbox' Private Const formHtml As String = " <html> <body> <h2>Editable PDF Form</h2> <form> First name: <br> <input type='text' name='firstname' value=''> <br> Last name: <br> <input type='text' name='lastname' value=''> <br> <br> <p>Please specify your gender:</p> <input type='radio' id='female' name='gender' value= 'Female'> <label for='female'>Female</label> <br> <br> <input type='radio' id='male' name='gender' value='Male'> <label for='male'>Male</label> <br> <br> <input type='radio' id='non-binary/other' name='gender' value='Non-Binary / Other'> <label for='non-binary/other'>Non-Binary / Other</label> <br> <p>Please select all medical conditions that apply:</p> <input type='checkbox' id='condition1' name='Hypertension' value='Hypertension'> <label for='condition1'> Hypertension</label><br> <input type='checkbox' id='condition2' name='Heart Disease' value='Heart Disease'> <label for='condition2'> Heart Disease</label><br> <input type='checkbox' id='condition3' name='Stoke' value='Stoke'> <label for='condition3'> Stoke</label><br> <input type='checkbox' id='condition4' name='Diabetes' value='Diabetes'> <label for='condition4'> Diabetes</label><br> <input type='checkbox' id='condition5' name='Kidney Disease' value='Kidney Disease'> <label for='condition5'> Kidney Disease</label><br> </form> </body> </html>" ' Instantiate Renderer Private renderer = New ChromePdfRenderer() renderer.RenderingOptions.CreatePdfFormsFromHtml = True renderer.RenderHtmlAsPdf(formHtml).SaveAs("BasicForm.pdf") ' Step 2. Reading and Writing PDF form values. Dim FormDocument = PdfDocument.FromFile("BasicForm.pdf") ' Set and Read the value of the "firstname" field Dim FirstNameField = FormDocument.Form.FindFormField("firstname") FirstNameField.Value = "Minnie" Console.WriteLine("FirstNameField value: {0}", FirstNameField.Value) ' Set and Read the value of the "lastname" field Dim LastNameField = FormDocument.Form.FindFormField("lastname") LastNameField.Value = "Mouse" Console.WriteLine("LastNameField value: {0}", LastNameField.Value) FormDocument.SaveAs("FilledForm.pdf")
您可以像處理普通文件一樣輕鬆地使用 IronPDF 建立可編輯的 PDF 文件。 PdfForm 類是 PDF 文件中的一組用戶可編輯的表單欄位。 它可以實現到您的 PDF 渲染中,以製作成表單或可編輯的文件。
PdfForm
這個例子將展示如何在 IronPDF 中創建可編輯的 PDF 表單。
具有可編輯表單的 PDF 可以只需在文件部分中添加 <form>、<input> 及 <textarea> 標籤即可從 HTML 創建。
<form>
<input>
<textarea>
PdfDocument.Form.FindFormField 方法可用來讀取和寫入任何表單欄位的值。 該欄位的名稱將與您 HTML 中為該欄位指定的'name'屬性相同。
PdfDocument.Form.FindFormField
PdfDocument.Form 對象可以有兩種使用方式:
PdfDocument.Form
在上面的例子中,我們首先導入 IronPdf 庫並定義一個方法 CreateEditablePdfDocument。 此方法包含一個簡單表單的 HTML 結構,其中有用戶名和評論的輸入欄位。 使用 HtmlToPdf 渲染器,我們將此 HTML 內容轉換為 PDF 文件。
CreateEditablePdfDocument
HtmlToPdf
然後使用 pdfDocument.Form 來訪問和操作表單欄位。 我們設置預設值,這些值將在文件於 PDF 查看器中開啟時顯示。 最後,文件將以 "EditableForm.pdf" 的名稱保存,允許其與嵌入的可編輯欄位一起存儲或共享。
pdfDocument.Form
學習使用 IronPDF 配合指南進行 PDF 表單編輯
using IronPdf; using IronSoftware.Drawing; var pdf = PdfDocument.FromFile("Example.pdf"); // Extract all pages to a folder as image files pdf.RasterizeToImageFiles(@"C:\image\folder\*.png"); // Dimensions and page ranges may be specified pdf.RasterizeToImageFiles(@"C:\image\folder\example_pdf_image_*.jpg", 100, 80); // Extract all pages as AnyBitmap objects AnyBitmap[] pdfBitmaps = pdf.ToBitmap();
Imports IronPdf Imports IronSoftware.Drawing Private pdf = PdfDocument.FromFile("Example.pdf") ' Extract all pages to a folder as image files pdf.RasterizeToImageFiles("C:\image\folder\*.png") ' Dimensions and page ranges may be specified pdf.RasterizeToImageFiles("C:\image\folder\example_pdf_image_*.jpg", 100, 80) ' Extract all pages as AnyBitmap objects Dim pdfBitmaps() As AnyBitmap = pdf.ToBitmap()
若要將 PDF 文件轉換為圖像,請在 PdfDocument 物件上調用 IronPDF 的 RasterizeToImageFiles 方法。 可以使用 PdfDocument.FromFile 方法或現有的 .NET Core PDF 生成方法之一來載入 PDF 文件。
PdfDocument
RasterizeToImageFiles
PdfDocument.FromFile
RasterizeToImageFiles 將 PDF 的每一頁呈現為光柵化圖像。 第一個參數指定每個圖像要使用的命名模式。 可以使用可選參數來自定義每個圖像的質量和尺寸。 另一個選項允許方法將所選的 PDF 頁面轉換為圖像。
特色代碼範例的第 24 行展示了 ToBitMap 方法。 在任意 PdfDocument 物件上調用此方法以快速將 PDF 轉換為可以儲存為文件或按需操作的 AnyBitmap 對象。
ToBitMap
AnyBitmap
FromFile
學習如何使用 IronPDF 將 PDF 光柵化為圖像
using IronPdf; using System.Collections.Generic; // Instantiate Renderer var renderer = new ChromePdfRenderer(); // Join Multiple Existing PDFs into a single document var pdfs = new List<PdfDocument>(); pdfs.Add(PdfDocument.FromFile("A.pdf")); pdfs.Add(PdfDocument.FromFile("B.pdf")); pdfs.Add(PdfDocument.FromFile("C.pdf")); var pdf = PdfDocument.Merge(pdfs); pdf.SaveAs("merged.pdf"); // Add a cover page pdf.PrependPdf(renderer.RenderHtmlAsPdf("<h1>Cover Page</h1><hr>")); // Remove the last page from the PDF and save again pdf.RemovePage(pdf.PageCount - 1); pdf.SaveAs("merged.pdf"); // Copy pages 5-7 and save them as a new document. pdf.CopyPages(4, 6).SaveAs("excerpt.pdf"); foreach (var eachPdf in pdfs) { eachPdf.Dispose(); }
Imports IronPdf Imports System.Collections.Generic ' Instantiate Renderer Private renderer = New ChromePdfRenderer() ' Join Multiple Existing PDFs into a single document Private pdfs = New List(Of PdfDocument)() pdfs.Add(PdfDocument.FromFile("A.pdf")) pdfs.Add(PdfDocument.FromFile("B.pdf")) pdfs.Add(PdfDocument.FromFile("C.pdf")) Dim pdf = PdfDocument.Merge(pdfs) pdf.SaveAs("merged.pdf") ' Add a cover page pdf.PrependPdf(renderer.RenderHtmlAsPdf("<h1>Cover Page</h1><hr>")) ' Remove the last page from the PDF and save again pdf.RemovePage(pdf.PageCount - 1) pdf.SaveAs("merged.pdf") ' Copy pages 5-7 and save them as a new document. pdf.CopyPages(4, 6).SaveAs("excerpt.pdf") For Each eachPdf In pdfs eachPdf.Dispose() Next eachPdf
IronPDF 提供 50多種功能 用於閱讀和編輯 PDF。 The most popular are merging PDFs, cloning pages, and extracting text from rotated content.
IronPDF 還允許用戶添加水印、旋轉頁面、添加註釋、數字簽名 PDF 頁面、創建新 PDF 文檔、附上封面頁、自定義 PDF 大小,以及在生成和格式化 PDF 文件時進行更多操作。 此外,它支持將 PDF 轉換為所有常規圖像文件類型,包括 JPG、BMP、JPEG、GIF、PNG、TIFF 等。
閱讀 C# PDF 編輯教程 以了解如何充分利用 IronPDF 修改 PDF 文檔以最佳滿足項目要求。
學習如何使用 IronPDF 在 PDF 中添加頁眉和頁腳
using IronPdf; // Open an Encrypted File, alternatively create a new PDF from Html var pdf = PdfDocument.FromFile("encrypted.pdf", "password"); // Get file metadata System.Collections.Generic.List<string> metadatakeys = pdf.MetaData.Keys(); // returns {"Title", "Creator", ...} // Remove file metadata pdf.MetaData.RemoveMetaDataKey("Title"); metadatakeys = pdf.MetaData.Keys(); // return {"Creator", ...} // title was deleted // Edit file metadata pdf.MetaData.Author = "Satoshi Nakamoto"; pdf.MetaData.Keywords = "SEO, Friendly"; pdf.MetaData.ModifiedDate = System.DateTime.Now; // The following code makes a PDF read only and will disallow copy & paste and printing pdf.SecuritySettings.RemovePasswordsAndEncryption(); pdf.SecuritySettings.MakePdfDocumentReadOnly("secret-key"); pdf.SecuritySettings.AllowUserAnnotations = false; pdf.SecuritySettings.AllowUserCopyPasteContent = false; pdf.SecuritySettings.AllowUserFormData = false; pdf.SecuritySettings.AllowUserPrinting = IronPdf.Security.PdfPrintSecurity.FullPrintRights; // Change or set the document encryption password pdf.SecuritySettings.OwnerPassword = "top-secret"; // password to edit the pdf pdf.SecuritySettings.UserPassword = "sharable"; // password to open the pdf pdf.SaveAs("secured.pdf");
Imports System Imports IronPdf ' Open an Encrypted File, alternatively create a new PDF from Html Private pdf = PdfDocument.FromFile("encrypted.pdf", "password") ' Get file metadata Private metadatakeys As System.Collections.Generic.List(Of String) = pdf.MetaData.Keys() ' returns {"Title", "Creator", ...} ' Remove file metadata pdf.MetaData.RemoveMetaDataKey("Title") metadatakeys = pdf.MetaData.Keys() ' return {"Creator", ...} // title was deleted ' Edit file metadata pdf.MetaData.Author = "Satoshi Nakamoto" pdf.MetaData.Keywords = "SEO, Friendly" pdf.MetaData.ModifiedDate = DateTime.Now ' The following code makes a PDF read only and will disallow copy & paste and printing pdf.SecuritySettings.RemovePasswordsAndEncryption() pdf.SecuritySettings.MakePdfDocumentReadOnly("secret-key") pdf.SecuritySettings.AllowUserAnnotations = False pdf.SecuritySettings.AllowUserCopyPasteContent = False pdf.SecuritySettings.AllowUserFormData = False pdf.SecuritySettings.AllowUserPrinting = IronPdf.Security.PdfPrintSecurity.FullPrintRights ' Change or set the document encryption password pdf.SecuritySettings.OwnerPassword = "top-secret" ' password to edit the pdf pdf.SecuritySettings.UserPassword = "sharable" ' password to open the pdf pdf.SaveAs("secured.pdf")
IronPDF為開發人員提供強大的PDF安全選項,支持自定義和設置PDF元數據、密碼、許可權等。 通過IronPDF的密碼、安全性和元數據選項,您可以創建自定義許可權和安全級別,以滿足PDF文件的需要。 這是通過使用類如SecuritySettings和MetaData類實現的。 一些選項包括限制PDF文件不可打印、設置為只讀以及128位加密及密碼保護您的PDF文件。
SecuritySettings
MetaData
設置自定義元數據通過實現MetaData類訪問各種PDF元數據選項,並用您自定義的值進行設置。 這包括更改作者、關鍵字、修改數據等。 設置自定義安全設置包括能設置自定義用戶和所有者密碼、打印許可權、只讀模式等。
var pdf = PdfDocument.FromFile("encrypted.pdf", "password");
System.Collections.Generic.List<string> metadatakeys = pdf.MetaData.Keys;
var metadatakeys = pdf.MetaData.Keys;
pdf.MetaData.Author = "Satoshi Nakamoto";
pdf.SecuritySettings.MakePdfDocumentReadOnly("secret-key");
要開始自定義您的PDF文件的安全性,您必須首先加載現有的PDF或創建新的PDF。 在這裡,我們加載了一個現有的密碼保護PDF文件,並輸入了打開PDF文件所需的密碼。 PDF加載後,我們使用pdf.MetaData.Keys來獲取PDF的當前元數據。 要刪除現有的PDF元數據值,請使用RemoveMetaDataKey方法。 要開始設置新的元數據值,請使用pdf.MetaData.metadataField(例如,pdf.MetaData.Keywords),然後將新值分配給它。 像標題和關鍵字這樣的元數據字段接受字符串值,而修改數據字段接受日期時間值。
pdf.MetaData.Keys
RemoveMetaDataKey
pdf.MetaData.metadataField
pdf.MetaData.Keywords
接下來,我們使用SecuritySettings類設置了新的安全設置。 如您所見,您可以在此處設置多樣的設置。 這讓您可以完全控制每個PDF文件的許可權和安全級別。 要訪問這些設置,您只需確保使用pdf.SecuritySettings,然後調整您想要的設置。例如,MakePdfDocumentReadOnly方法將PDF文件設置為只讀,以128位加密內容。 其他SecuritySettings的選項包括:
pdf.SecuritySettings
MakePdfDocumentReadOnly
一旦您為PDF文件設置了自定義元數據、密碼和安全設置,請使用pdf.SaveAs方法將您的PDF保存到指定位置。
pdf.SaveAs
學習如何使用IronPDF處理PDF元數據
using IronPdf; // Stamps a Watermark onto a new or existing PDF var renderer = new ChromePdfRenderer(); var pdf = renderer.RenderUrlAsPdf("https://www.nuget.org/packages/IronPdf"); pdf.ApplyWatermark("<h2 style='color:red'>SAMPLE</h2>", 30, IronPdf.Editing.VerticalAlignment.Middle, IronPdf.Editing.HorizontalAlignment.Center); pdf.SaveAs("watermarked.pdf");
Imports IronPdf ' Stamps a Watermark onto a new or existing PDF Private renderer = New ChromePdfRenderer() Private pdf = renderer.RenderUrlAsPdf("https://www.nuget.org/packages/IronPdf") pdf.ApplyWatermark("<h2 style='color:red'>SAMPLE</h2>", 30, IronPdf.Editing.VerticalAlignment.Middle, IronPdf.Editing.HorizontalAlignment.Center) pdf.SaveAs("watermarked.pdf")
IronPDF 提供方法用 HTML 為 PDF 文件設置『水印』。
使用 ApplyStamp 方法,開發人員可以將基於 HTML 的水印添加到 PDF 文件中。 如上例所示,HTML 代碼作為方法的第一個參數用於水印。 傳遞給 ApplyStamp 的其他參數控制水印的旋轉、不透明度和位置。
ApplyStamp
利用 ApplyStamp 方法代替 ApplyWatermark 方法來更精細地控制水印位置。 例如,使用 ApplyStamp 來:
ApplyWatermark
確保您在項目中已安裝 IronPDF 庫。 您可以在 IronPDF NuGet 套件頁面 找到更詳細的說明。
代碼解釋:
IronPdf
rotationDegrees
left
top
opacity
pageRange
總之,IronPDF ApplyStamp 方法允許精確控制使用 HTML 的 PDF 文件水印。 這種方法靈活,適應不同的自定義需求以定位、樣式化和應用水印到特定頁面。
探索使用 IronPDF 的自定義水印
using IronPdf; // With IronPDF, we can easily merge 2 PDF files using one as a background or foreground var renderer = new ChromePdfRenderer(); var pdf = renderer.RenderUrlAsPdf("https://www.nuget.org/packages/IronPdf"); pdf.AddBackgroundPdf(@"MyBackground.pdf"); pdf.AddForegroundOverlayPdfToPage(0, @"MyForeground.pdf", 0); pdf.SaveAs("complete.pdf");
Imports IronPdf ' With IronPDF, we can easily merge 2 PDF files using one as a background or foreground Private renderer = New ChromePdfRenderer() Private pdf = renderer.RenderUrlAsPdf("https://www.nuget.org/packages/IronPdf") pdf.AddBackgroundPdf("MyBackground.pdf") pdf.AddForegroundOverlayPdfToPage(0, "MyForeground.pdf", 0) pdf.SaveAs("complete.pdf")
You may want to use a specific background and foreground as you create and render your PDF documents in IronPDF. In such a case, you can use an existing or rendered PDF as the background or foreground for another PDF document. This is particularly useful for design consistency and templating.
This example shows you how to use a PDF document as the background or foreground of another PDF document.
You can do this in C# by loading or creating a multi-page PDF as an IronPdf.PdfDocument object.
IronPdf.PdfDocument
You can add backgrounds using PdfDocument.AddBackgroundPdf. For more details on background insertion methods, refer to the IronPDF.PdfDocument background documentation; it describes several background insertion methods and their overrides. This adds a background to each page of your working PDF. The background is copied from a page in another PDF document.
PdfDocument.AddBackgroundPdf
You can add foregrounds, also known as "Overlays," using PdfDocument.AddForegroundOverlayPdfToPage. For detailed information on foreground insertion methods, consult the IronPDF.PdfDocument overlay documentation.
PdfDocument.AddForegroundOverlayPdfToPage
This code illustrates how to integrate additional design elements on top of a base PDF using IronPDF. Always refer to the official documentation for more advanced techniques and additional options.
Explore our Guide on Adding Backgrounds and Foregrounds
無論是技術問題還是銷售詢問,Iron 團隊都在附近,隨時為您的大多數問題提供幫助。與 Iron 聯繫,以便在您的項目中盡可能充分利用我們的庫。
IronPDF 使用 Chromium 渲染器將 HTML 文件轉換為 PDF 文件,不需要 API 來定位或佈局 PDF:IronPDF 使用標準的 HTML、ASPX、JS、CSS 和圖像。
IronPDF 允許您從 PDF 中提取所有嵌入的文本內容,然後將其輸入到您的 C# 和 .NET Core 應用中。將 PDF 檔案中的內容導入到您的業務流程系統中。
從附加、合併、分隔到修改 PDF,運用您自己的編碼技能,每次精確地獲得正確的 PDF。IronPDF 將開發能力直接放入您的手中,於您的 C#/VB .NET Core 項目中。
指示 IronPDF .NET Core 庫對您的 HTML 文件、字串或 URL 進行簡單的 PDF 轉換。這將使用您的現有文件和網頁將信息渲染為 PDF 文件。
對於 ASP .NET Core, C#, VB, MVC, IronPDF 庫安裝, .NET
IronPDF 使 PDF 創建和編輯控制掌握在您自己的手中,提供完整的 intellisense 支持和 Visual Studio 安裝程序。使用 NuGet 適用於 Visual Studio 或下載 DLL。您將很快設定好。只有一個 DLL。
免費 社區開發許可證。商業許可證從 $749 開始。
C# PDF ASP.NET IronPDF 庫安裝
了解如何將任何 ASP.NET ASPX 頁面轉換為 PDF 文檔,使用一行代碼而不是 HTML 在 C# 或 VB.NET 中...
C# PDF HTML
對許多人來說,這是從 .NET 生成 PDF 文件的最有效方法,因為沒有其他 API 需要學習,也沒有複雜的設計系統需要導航……
VB PDF ASP.NET
學習如何在 VB.NET 應用程式和網站中創建和編輯 PDF 文件。一個含有代碼範例的免費教程……
Iron 的團隊有超過 10 年的 .NET 軟件組件市場經驗。
與我們的開發團隊開啟支持票。
查看代碼範例和教程。
開發免費。許可證從 $749 起。
5 分鐘內完成設置。
無需信用卡
試用表單已提交成功。您的試用密鑰應該在電子郵件裡。如果沒有,請聯繫support@ironsoftware.com
您的試用密鑰應該在電子郵件裡。如果沒有,請聯繫support@ironsoftware.com
免費開始
在生產環境中測試而不帶水印。適用於您所需的任何地方。
獲得 30 天完整功能產品。幾分鐘內即可運行。
在您的產品試用期間全面訪問我們的支持技術團隊
無需信用卡或帳戶創建
您的試用密鑰應在電子郵件中。如果沒有,請聯繫support@ironsoftware.com
授權從$749起售。 有問題嗎?聯繫我們。
預約無需承諾的諮詢
完成以下表單或發送電子郵件至 sales@ironsoftware.com
您的詳細信息將始終保密。
預訂 30 分鐘的個人演示。
無須合約、無須卡號、無任何長期綁約。
版權所有 © Iron Software 2013-2025