在實際環境中測試
在生產環境中測試無浮水印。
在任何需要的地方都能運作。
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")
<p>使用 IronPDF,您可以在 .NET 專案中從簡單的 HTML 字串創建新的 PDF 文件,並且 IronPDF 可以在 C#、F# 和 VB.NET 中使用。 由於使用了 <code>ChromePdfRenderer</code> 類,您可以確保從 HTML 字串渲染的任何 PDF 文件將會呈現出來。<a href="/how-to/pixel-perfect-html-to-pdf/" target="__blank">像素完美</a>. 使用IronPDF的強大功能<a href="/tutorials/html-to-pdf/" target="__blank">HTML 轉換為 PDF</a>功能可創建符合您個人需求的高品質 PDF 文件。</p> <div class="examples__featured-snippet"> <h2>將 HTML 字串轉換為 PDF 的四個步驟</h2> <ol> <li>匯入 IronPDF 程式庫。</li> <li>初始化一個新的 <code>ChromePdfRenderer</code> 物件。</li> <li>使用 <code>ChromePdfRenderer.RenderHtmlAsPdf</code> 方法。</li> <li>使用保存 PDF <code>PdfDocument.SaveAs</code>.</li> </ol> </div> <p>請參閱下面的代碼範例以了解更多詳細信息:</p> <pre class='naked-code'><code class="language-cs">using IronPdf; var renderer = new ChromePdfRenderer(); var pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>"); var myAdvancedPdf = renderer.RenderHtmlAsPdf("<img src='icons/iron.png'>", @"C:\site\assets\"); pdf.SaveAs("output.pdf");</code></pre> <div class="code-content code-content-inner"> <div class="code_window" > <div class="language-selection__content-page-wrapper"> </div> <div class="code_window_content"> <div class="code-window__action-buttons-wrapper code-window__action-buttons-wrapper--content-page"> <button title="點擊複製" class=" code-window__action-button code-window__action-button--copy copy-clipboard " data-copy-text="點擊複製" data-copied-text="已複製到剪貼板" data-clipboard-id="code-explorer" data-placement="bottom" > <i class="fa-kit fa-copy-example"></i> </button> <button title="全螢幕模式" class=" code-window__action-button code-window__action-button--full-screen js-full-screen-code-example-modal " > <i class="fas fa-expand"></i> </button> <button title="退出全螢幕" class=" code-window__action-button code-window__action-button--exit-full-screen js-exit-full-screen-code-example-modal " > <i class="fas fa-compress"></i> </button> </div> <pre class="prettyprint linenums lang-cs"><code>using IronPdf; var renderer = new ChromePdfRenderer(); var pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>"); var myAdvancedPdf = renderer.RenderHtmlAsPdf("<img src='icons/iron.png'>", @"C:\site\assets\"); pdf.SaveAs("output.pdf");</code></pre> <pre class="prettyprint linenums lang-vb"><code>Imports IronPdf Private renderer = New ChromePdfRenderer() Private pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>") Private myAdvancedPdf = renderer.RenderHtmlAsPdf("<img src='icons/iron.png'>", "C:\site\assets\") pdf.SaveAs("output.pdf")</code></pre> </div> <div class="code_window_bottom"> <span class="language_selection"> <span class="ls-span">VB </span> <span> <label class="switch"> <input type="checkbox" checked="checked"> <span class="slider round"></span> </label> </span> <span class="ls-span">C#</span> </span> </div> </div> </div> <p>將 HTML 字串轉換為 C# 中的 PDF 的第一步是確保在專案中正確設置並運行 IronPDF 庫。 透過包含使用 IronPdf,我們確保可以訪問所需的 IronPDF 程式庫中的類,以進行 HTML 到 PDF 的轉換。 完成此操作後,下一行 <code>Installation.EnableWebSecurity = true</code> 用於禁用本地磁碟存取或跨域請求,確保操作安全。</p> <p>下一行會建立一個新的 ChromePdfRenderer 實例,該實例將負責將 HTML 轉換為 PDF。 在基本範例中,使用 RenderHtmlAsPdf 方法將簡單的 HTML 字串轉換為 PDF("<h1>你好,世界</h1>")<code>到 PDF 文件,並使用</code>SaveAs` 方法將其保存到磁碟。</p> <p>在進階方法中,我們展示了IronPDF如何處理包含外部資源(如圖片、CSS和JavaScript)的HTML內容。 要加載這些資產,使用可選的 BasePath 參數來指定包含所需文件的目錄。 包含外部資產的生成 PDF 使用與基本範例中相同的 <code>SaveAs</code> 方法保存。 此程式碼範例突顯了 IronPDF 處理簡單和複雜 HTML 內容的能力,使其成為以程式方式生成 PDF 的高效工具。 <a href="/how-to/html-string-to-pdf/" class="code_content__related-link__doc-cta-link">如需更多示例,請查看使用IronPDF與C#的操作指南。</a></p>
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")
<p>IronPDF 讓從現有 URL 渲染 HTML 為 PDF 文件變得非常簡單。 對 JavaScript、圖片、表單和 CSS 的支持非常全面。</p> <p>從接受查詢字串變數的 ASP.NET 網址渲染 PDF 可以促進設計師和編碼人員之間的協作,使 PDF 開發更加順利。</p> <p>請提供您要翻譯的內容。</p> <div class="examples__featured-snippet"> <h2>將 URL 轉換為 PDF 的 C# 步驟</h2> <ol> <li>下載 <a href="/" target="_blank" rel="nofollow noopener noreferrer">IronPDF URL 轉 PDF 轉換庫</a></li> <li>使用 NuGet 安裝該庫以測試其功能。</li> <li>使用 IronPDF 從接受查詢字串變數的 ASP.NET URL 生成 PDF。</li> <li>使用 IronPDF 直接從 URL 建立 PDF 文件。</li> <li>輕鬆查看和驗證您的 PDF 輸出文件。</li> </ol> </div>
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
<p>IronPDF 致力於為開發者提供最大的靈活性。</p> <p>In this<a href="/blog/using-ironpdf/csharp-generate-pdf-tutorial/" target="_blank" rel="nofollow noopener noreferrer">C# PDF 生成教學範例</a>,在此,我們展示了提供自動化內部功能的 API 與提供可讓您控制的 API 之間的平衡。</p> <p>IronPDF支持生成PDF文件的多種自定義選項,包括:頁面大小、頁面邊距、頁首/頁尾內容、內容縮放、CSS規則集和JavaScript執行。</p> <hr class="separator"> <p>我们希望开发者能够控制 Chrome 如何将网页转换为 PDF。 這<a href="/object-reference/api/IronPdf.ChromePdfRenderOptions.html" target="_blank" rel="nofollow noopener noreferrer"><code>ChromePdfRenderer</code> 類別概述</a>使這成為可能。</p> <p><code>ChromePDFRenderOptions</code> 類別上可用的設置示例包括邊距、頁眉、頁腳、紙張大小和表單創建的設置。</p>
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")
<p>IronPDF 是一個功能強大的 .NET 函式庫,能夠將 HTML 文件轉換成高品質的 PDF 文件。 使用 IronPDF,您只需幾行代碼即可將 HTML 檔案轉換成 PDF,並且由於其對現代 Web 標準的支持,生成的 PDF 檔會以像素完美的效果呈現。 利用IronPDF強大的HTML文件轉PDF功能變得很簡單,這要歸功於它使用了<code>ChromePdfRenderer</code>類,這使得HTML到PDF的轉換變得輕而易舉。</p> <div class="examples__featured-snippet"> <h2>使用 IronPDF 將 HTML 文件轉換為 PDF 的步驟</h2> <ol> <li>安裝 C# IronPDF 庫以將 HTML 轉換為 PDF。</li> <li><code>使用 IronPdf;</code></li> <li><code>var renderer = new ChromePdfRenderer();</code></li> <li><code>var pdf = renderer.RenderHtmlFileAsPdf("example.html");</code></li> <li><code>pdf.SaveAs("output.pdf");</code></li> </ol> </div> <p>此程式碼從 HTML 文件生成一個新的 PDF 文件。要實現這一點,我們必須首先確保 IronPDF 庫已安裝並通過 <code>using IronPdf</code> 行包含在您的專案中。接下來,初始化 ChromePdfRenderer 類,該類提供將 HTML 內容渲染為 PDF 的功能,確保在轉換過程中不丟失 HTML 文件的原始品質。</p> <p>一旦渲染器被實例化,您可以使用 <code>RenderHtmlAsPdf</code> 方法將現有的 HTML 文件轉換為 PDF。 在這個範例中,HTML 檔案 "example.html" 被傳遞給方法,創建一個 PDF 物件。 最後,使用 <code>SaveAs</code> 方法來儲存生成的 PDF,指定所需的檔案名稱和位置。 此簡單過程使您能夠在 C# 應用程式中輕鬆將 HTML 檔案生成 PDF。 <a href="/how-to/html-file-to-pdf/" class="code_content__related-link__doc-cta-link">點擊這裡查看使用指南,包括範例、範例代碼和文件。</a></p>
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)
<p>此示例展示了使用者如何更改 PDF 打印選項以將表單轉換為 HTML。</p> <p>IronPDF 的<a href="/how-to/aspx-to-pdf/" target="_blank" rel="nofollow noopener noreferrer">ASPX 到 PDF 轉換指南</a>功能具有多種選項,可將 HTML 從字符串或文件轉換為 PDF。</p> <p>特別重要的兩個選項是:</p> <ul> <li>允許開發者在轉換過程中指定是否應將HTML表單渲染為互動式PDF表單。</li> <li>允許開發者指定PDF應該「在瀏覽器中」顯示,還是作為文件下載。</li> </ul>
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
<p>使用 <code>IronPdf.ImageToPdfConverter</code> 類別從一個或多個圖像文件構建 PDF。</p> <h2 id="anchor-36-49c-num-36-49pdf">如何在C#中將圖像轉換為PDF</h2> <p>給定位於電腦 <code>C:\images\example.png</code> 上的單一圖片,我們可以通過調用 <code>IronPdf.ImageToPdfConverter.ImageToPdf</code> 方法及其文件路徑,迅速將其轉換成 PDF 文件。</p> <pre class='naked-code'><code class="language-cs">IronPdf.ImageToPdfConverter.ImageToPdf(@"C:\images\example.png").SaveAs("example.pdf");</code></pre> <div class="code-content code-content-inner"> <div class="code_window" > <div class="language-selection__content-page-wrapper"> </div> <div class="code_window_content"> <div class="code-window__action-buttons-wrapper code-window__action-buttons-wrapper--content-page"> <button title="點擊複製" class=" code-window__action-button code-window__action-button--copy copy-clipboard " data-copy-text="點擊複製" data-copied-text="已複製到剪貼板" data-clipboard-id="code-explorer" data-placement="bottom" > <i class="fa-kit fa-copy-example"></i> </button> <button title="全螢幕模式" class=" code-window__action-button code-window__action-button--full-screen js-full-screen-code-example-modal " > <i class="fas fa-expand"></i> </button> <button title="退出全螢幕" class=" code-window__action-button code-window__action-button--exit-full-screen js-exit-full-screen-code-example-modal " > <i class="fas fa-compress"></i> </button> </div> <pre class="prettyprint linenums lang-cs"><code>IronPdf.ImageToPdfConverter.ImageToPdf(@"C:\images\example.png").SaveAs("example.pdf");</code></pre> <pre class="prettyprint linenums lang-vb"><code>IronPdf.ImageToPdfConverter.ImageToPdf("C:\images\example.png").SaveAs("example.pdf")</code></pre> </div> <div class="code_window_bottom"> <span class="language_selection"> <span class="ls-span">VB </span> <span> <label class="switch"> <input type="checkbox" checked="checked"> <span class="slider round"></span> </label> </span> <span class="ls-span">C#</span> </span> </div> </div> </div> <h2 id="anchor-36-49pdf36-49">將多個圖像合併成一個PDF文件</h2> <p>我們也可以使用 <code>System.IO.Directory.EnumerateFiles</code> 以及 <code>ImageToPdfConverter.ImageToPdf</code> 將圖片批量轉換成一個 PDF 文件。</p> <pre class='naked-code'><code class="language-cs">string sourceDirectory = "D:\web\assets"; string destinationFile = "JpgToPDF.pdf"; var imageFiles = Directory.EnumerateFiles(sourceDirectory, "*.jpg"); ImageToPdfConverter.ImageToPdf(imageFiles).SaveAs(destinationFile);</code></pre> <div class="code-content code-content-inner"> <div class="code_window" > <div class="language-selection__content-page-wrapper"> </div> <div class="code_window_content"> <div class="code-window__action-buttons-wrapper code-window__action-buttons-wrapper--content-page"> <button title="點擊複製" class=" code-window__action-button code-window__action-button--copy copy-clipboard " data-copy-text="點擊複製" data-copied-text="已複製到剪貼板" data-clipboard-id="code-explorer" data-placement="bottom" > <i class="fa-kit fa-copy-example"></i> </button> <button title="全螢幕模式" class=" code-window__action-button code-window__action-button--full-screen js-full-screen-code-example-modal " > <i class="fas fa-expand"></i> </button> <button title="退出全螢幕" class=" code-window__action-button code-window__action-button--exit-full-screen js-exit-full-screen-code-example-modal " > <i class="fas fa-compress"></i> </button> </div> <pre class="prettyprint linenums lang-cs"><code>string sourceDirectory = "D:\web\assets"; string destinationFile = "JpgToPDF.pdf"; var imageFiles = Directory.EnumerateFiles(sourceDirectory, "*.jpg"); ImageToPdfConverter.ImageToPdf(imageFiles).SaveAs(destinationFile);</code></pre> <pre class="prettyprint linenums lang-vb"><code>Dim sourceDirectory As String = "D:\web" & ChrW(7) & "ssets" Dim destinationFile As String = "JpgToPDF.pdf" Dim imageFiles = Directory.EnumerateFiles(sourceDirectory, "*.jpg") ImageToPdfConverter.ImageToPdf(imageFiles).SaveAs(destinationFile)</code></pre> </div> <div class="code_window_bottom"> <span class="language_selection"> <span class="ls-span">VB </span> <span> <label class="switch"> <input type="checkbox" checked="checked"> <span class="slider round"></span> </label> </span> <span class="ls-span">C#</span> </span> </div> </div> </div> <p>探索更多有關內容<a href="/how-to/image-to-pdf/" target="_blank" rel="nofollow noopener noreferrer">使用 IronPDF 將圖像轉換為 PDF</a>以增強您的應用程式,或造訪<a href="https://ironsoftware.com" target="__blank">Iron Software 網站</a>探索由Iron Software提供的整套開發者工具,包括IronBarcode、IronOCR等。</p>
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")
<div class="alert alert-info iron-variant-1" role="alert"> 您的企業在每年的 PDF 安全和合規訂閱上花費過多。 考慮 <a href="https://ironsoftware.com/enterprise/securedoc/">IronSecureDoc,一個全面的 PDF 安全解決方案</a>,提供用於管理數位簽名、編輯、加密和保護等SaaS服務的解決方案,且僅需一次性付款。 <a href="https://ironsoftware.com/enterprise/securedoc/docs/">探索 IronSecureDoc 文件</a> </div> <p>您可以像編輯普通文件一樣輕鬆地使用IronPDF創建可編輯的PDF文件。 <code>PdfForm</code> 類別是 PDF 文檔中一系列用戶可編輯的表單字段。 它可以整合到您的 PDF 渲染器中,使其成為表單或可編輯文件。</p> <p>此示例展示如何在IronPDF中創建可編輯的PDF表格。</p> <p>PDF有可編輯表單可由HTML簡單添加<code><form></code>, <code><input></code>, 和 <code><textarea>將</code><p>` 標籤添加到文件部分。</p> <p><code>PdfDocument.Form.FindFormField</code> 可以用來讀寫任何表單欄位的值。 該欄位的名稱將與您的HTML中該欄位的'name'屬性相同。</p> <p><code>PdfDocument.Form</code> 物件可以有兩種使用方式。</p> <ul> <li>首先是填充表單欄位的預設值,必須在Adobe Reader中聚焦才能顯示此值。</li> <li>第二個功能是讀取使用者以任何語言填寫的PDF表格中的數據。</li> </ul>
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()
<p>使用 IronPDF 將 PDF 轉換成您偏好的檔案類型、影像尺寸和 DPI 品質的圖片。</p> <p>要將 PDF 文檔轉換成圖像,請在 <code>PdfDocument</code> 物件上調用 IronPDF 的 <code>RasterizeToImageFiles</code> 方法。 PDF文件可以使用<code>PdfDocument.FromFile</code>方法或其他可用方法加載。<a href="/tutorials/dotnet-core-pdf-generating/" target="_blank" rel="nofollow noopener noreferrer">.NET Core 的 PDF 生成方法</a>.</p> <hr /> <p><div class="main-article__video-wrapper js-article-video-modal-wrapper"><iframe class="" loading="lazy" src="https://www.youtube.com/embed/1MdE_nGu0UM?start=0" title="YouTube Video Player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe></div></p> <hr /> <p><code>RasterizeToImageFiles</code> 將 PDF 的每一頁渲染為光柵圖像。 第一個參數指定每個影像使用的命名模式。 選用參數可用於自訂每個影像的品質和尺寸。 另一個選項允許該方法將選定的 PDF 頁面轉換為圖像。</p> <p>第24行的代碼示例演示了<code>ToBitMap</code>方法。 調用此方法於任何 <code>PdfDocument</code> 物件上,以快速將 PDF 轉換成可以保存到文件或根據需要操作的 <code>AnyBitmap</code> 物件。</p> <hr /> <div class="hsg-featured-snippet"> <h2>如何在 C# 中將 PDF 轉換為圖像</h2> <ol> <li><a class="js-modal-open" data-modal-id="trial-license-after-download" href="https://nuget.org/packages/IronPdf/" target="_blank" rel="nofollow noopener noreferrer">安裝 PDF 到圖片 C# 函式庫</a></li> <li>匯入現有 PDF 檔案 透過 <code>從文件</code> 方法</li> <li>使用將PDF轉換為圖片 <code>光柵化為圖像檔案</code> 方法</li> <li>指定儲存目錄和圖片大小</li> <li>檢查輸出圖片</li> </ol> </div>
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
<p>IronPDF 提供<a href="/features/" target="_blank" rel="nofollow noopener noreferrer">50 多項功能</a>用於閱讀和編輯PDF文件。 最受歡迎的是<a href="/how-to/merge-or-split-pdfs/" target="_blank" rel="nofollow noopener noreferrer">合併PDFs</a>, <a href="/examples/copy-pdf-page-to-another-pdf-file/" target="_blank" rel="nofollow noopener noreferrer">克隆頁面</a>,和<a href="/how-to/rotating-text/" target="_blank" rel="nofollow noopener noreferrer">從旋轉內容中提取文本</a>.</p> <p>IronPDF還允許用戶在生成和格式化PDF文件時添加水印、旋轉頁面、添加註釋、數位簽署PDF頁面、創建新的PDF文件、附加封面頁、自訂PDF尺寸等等。 此外,它支持將PDF轉換成所有常見的圖像文件類型,包括JPG、BMP、JPEG、GIF、PNG、TIFF等。</p> <p>讀取<a href="/tutorials/csharp-edit-pdf-complete-tutorial/" target="_blank" rel="nofollow noopener noreferrer">C# PDF 編輯教程</a>了解如何充分利用IronPDF來修改PDF文件,以最佳符合項目需求。</p> <hr /> <div class="hsg-featured-snippet"> <h2>如何在 C# 中編輯 PDF 文件</h2> <ol> <li><a class="js-modal-open" data-modal-id="trial-license-after-download" href="https://nuget.org/packages/IronPdf/" target="_blank" rel="nofollow noopener noreferrer">安裝可以編輯 PDF 檔案的 C# 庫</a></li> <li>利用 `FromFile` 方法匯入多個 PDF 文件</li> <li>使用直觀的 API 在 C# 中訪問和修改 PDF 文件</li> <li>使用 C# 將更新的版本另存為新的 PDF 文件</li> <li>查看新編輯的PDF文件</li> </ol> </div>
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")
<p>IronPDF為開發者提供強大的PDF安全選項,支持自訂與設置PDF的元數據、密碼、許可權等。 使用 IronPDF 的密碼、安全性和中繼資料選項,您可以針對您的 PDF 文件需求創建自訂許可與安全等級。 這是通過使用如 <code>SecuritySettings</code> 和 <code>MetaData</code> 類別等類別來完成的。 一些選項包括將 PDF 文件設置為不可列印、設置為唯讀、使用 128 位加密以及對 PDF 文件進行密碼保護。</p> <p>設置自定義元數據的工作原理是通過實施 MetaData 類來訪問各種 PDF 元數據選項,並使用自定義值設置它們。 這包括更改作者、關鍵字、修改數據等。 設定自訂安全性設置包括能夠設置自訂用戶和擁有者密碼、列印權限、只讀模式等。</p> <div class="examples__featured-snippet"> <h2>設定 PDF 密碼、元數據和安全性的 5 個步驟</h2> <ol> <li><code>var pdf = PdfDocument.FromFile("encrypted.pdf", "password");</code></li> <li><code>System.Collections.Generic.List<string> metadatakeys = pdf.MetaData.Keys();</code></li> <li><code>var metadatakeys = pdf.MetaData.Keys();</code></li> <li><code>pdf.MetaData.Author = "Satoshi Nakamoto";</code></li> <li><code>pdf.SecuritySettings.MakePdfDocumentReadOnly("secret-key");</code></li> </ol> </div> <p>若要開始自定義 PDF 文件的安全性,你必須先加載現有的 PDF 或創建新的 PDF。 在這裡,我們已加載了一個現有的受密碼保護的 PDF 文件,並輸入了開啟 PDF 文件所需的密碼。 一旦載入 PDF,我們接著使用 <code>pdf.MetaData.Keys</code>()使用 <code>;</code> 來獲取 PDF 的當前元數據。 要移除現有的 PDF 元數據值,請使用 <code>RemoveMetaDataKey</code> 方法。 要開始設定新的元數據值,使用 pdf.MetaData.metadatafield(例如 <code>pdf.MetaData.Keywords</code>),然後只需將新值賦予給它。 元數據欄位如標題和關鍵字採用字串值,而修改日期欄位則採用日期時間值。</p> <p>接下來,我們使用 SecuritySettings 類別設置了新的安全設定。 如您所見,您可以在此設置多種設定。 這讓您能完全控制您處理的每個 PDF 文件的權限和安全等級。 要訪問這些設置,您只需確保使用 <code>pdf.SecuritySettings</code>,然後選擇您想要調整的設置。例如,<code>MakePdfDocumentReadOnly</code> 屬性將 PDF 文檔設置為只讀,以 128 位加密內容。 SecuritySettings 的其他選項包括:</p> <ul> <li><strong>AllowUserAnnotations:</strong> 控制用戶是否可以註釋 PDF。</li> <li><strong>AllowUserPrinting:</strong> 控制文件的打印權限。</li> <li><strong>AllowUserFormData:</strong> 設置是否允許用戶填寫表單的權限。</li> <li><strong>OwnerPassword:</strong> 設定 PDF 的擁有者密碼,用於禁用或啟用其他安全設置。</li> <li> <p><strong>UserPassword:</strong> 設定 PDF 的使用者密碼,以便在開啟或列印文件時輸入。</p> <p>設定好自訂的中繼資料、密碼和安全性設定後,使用 <code>pdf.SaveAs</code> 方法將您的 PDF 存儲到指定位置。 <a href="/how-to/metadata/" class="code_content__related-link__doc-cta-link">點擊此處查看操作指南,包括範例、示例代碼和文件。</a></p> </li> </ul>
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(@"C:\Path\To\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("C:\Path\To\Watermarked.pdf")
<p>IronPDF 提供使用 HTML 對 PDF 文件進行「水印」的方法。</p> <p>使用 <code>ApplyStamp</code> 方法,開發者可以在 PDF 文件中添加基於 HTML 的浮水印。如上例所示,浮水印的 HTML 代碼作為方法的第一個參數。 對 <code>ApplyStamp</code> 的額外參數可控制浮水印的旋轉、透明度和位置。</p> <p>使用 <code>ApplyStamp</code> 方法替代 <code>ApplyWatermark</code> 方法以獲得更細緻的浮水印放置控制。 例如,使用 <code>ApplyStamp</code> 來:</p> <ul> <li>向PDF添加文字、圖像或HTML水印</li> <li>將相同的浮水印應用於 PDF 的每個頁面</li> <li>對特定PDF頁面應用不同的浮水印</li> <li>調整浮水印在頁面副本前或後的位置</li> <li> <p>更精確地調整浮水印的不透明度、旋轉和對齊方式</p> <p>請提供您要翻譯的內容。</p> <div class="hsg-featured-snippet"> <h2>如何在 C# 中將水印添加到 PDF 文件中</h2> <ol> <li><a class="js-modal-open" data-modal-id="trial-license-after-download" href="https://nuget.org/packages/IronPdf/" target="_blank" rel="nofollow noopener noreferrer">從NuGet下載並安裝IronPDF庫。</a></li> <li>創建新的 <code>PdfDocument</code> 或使用現有的 <code>PdfDocument</code> 檔案。</li> <li>呼叫 <code>應用水印</code> 添加水印到 PDF 的方法。</li> <li>通過調用導出 PDF 文件 <code>另存為</code>.</li> </ol> </div> </li> </ul>
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(@"C:\Path\To\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("C:\Path\To\Complete.pdf")
<p>在使用IronPDF創建和渲染PDF文件時,您可能會想要使用特定的背景和前景。 在這種情況下,您可以使用現有的或已渲染的PDF作為另一個PDF文件的背景或前景。 這對於設計一致性和模板化特別有用。</p> <p>此範例將展示如何將一份PDF文件作為另一份PDF文件的背景或前景使用。</p> <p>您可以在 C# 中通過加載或創建多頁 PDF 作為 <code>IronPdf.PdfDocument</code> 對象來實現。</p> <p>您可以使用 <code>PdfDocument.AddBackgroundPdf</code> 添加背景。 有關背景插入方法的更多詳細資訊,請參閱<a href="/object-reference/api/IronPdf.PdfDocument.html#IronPdf_PdfDocument_AddBackgroundPdf_IronPdf_PdfDocument_System_Int32_" target="_blank" rel="nofollow noopener noreferrer">IronPDF.PdfDocument 背景檔介紹文檔</a>; 它描述了幾種背景插入方法及其覆寫。 這在您正在處理的PDF的每個頁面上增加了背景。 背景是從另一個PDF文件的頁面複製的。</p> <p>您可以使用 <code>PdfDocument.AddForegroundOverlayPdfToPage</code> 添加前景,亦稱為「覆蓋層」。 有關前景插入方法的詳細資訊,請參閱<a href="/object-reference/api/IronPdf.PdfDocument.html" target="_blank" rel="nofollow noopener noreferrer">IronPDF.PdfDocument 疊加文件</a>.</p>
IronPDF使用Chromium渲染器將HTML檔案轉換為PDF文件,適用於 .NET Chromium。不需要API來定位或佈局PDF:IronPDF使用標準的HTML、ASPX、JS、CSS和圖像。
查看教程IronPDF 允許您從 PDF 中提取所有嵌入的文本內容,並將其傳遞到您的 C# 和 .NET Core 應用程序。將 PDF 檔案中的內容導入到您的業務流程系統中。
完整 API 參考資料從附加、合併、分割和修改 PDF,利用您自己的編碼技巧,每次都精確獲得正確的 PDF。IronPDF 將開發功能專門交給您,放在您的 C#/VB .NET Core 項目中。
閱讀我們的文件IronPDF 將 PDF 創建和編輯控制放在你手中,快速且具有完整的智能感知支持和 Visual Studio 安裝程序。使用 NuGet for Visual Studio 或下載 DLL。你只需片刻即可完成設置。只需一個 DLL。
PM > Install-Package IronPdf 下載DLLC# PDF ASP.NET ASPX
了解如何使用 C# 或 VB.NET 的一行程式碼將任何 ASP.NET ASPX 頁面轉換為 PDF 文件,而不是 HTML…
查看 Jacob 的 ASPX-to-PDF 範例C# PDF HTML
對於許多人來說,這是從 .NET 生成 PDF 文件的最有效方法,因為不需要學習額外的 API 或導航複雜的設計系統…
查看 Jean 的 HTML-To-PDF 範例VB PDF ASP.NET
學習如何在 VB.NET 應用程序和網站中創建和編輯 PDF 文件。免費教程附帶代碼示例……
查看 Veronica 的 VB.NET PDF 教程Iron 團隊在 .NET 軟體元件市場有超過 10 年的經驗。