在实际环境中测试
在生产中测试无水印。
随时随地为您服务。
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允许开发人员在C#、F# 和 VB.NET 中为.NET Core 和 .NET Framework 轻松创建PDF文档。</p> <p>在这个例子中,我们展示了可以从任何 HTML 呈现 PDF 文档。 这使我们能够创建与现有网站的品牌形象紧密匹配的PDF文件。</p> <p>您可以选择类似上述的简单 HTML,也可以加入 CSS、图片和 JavaScript。</p> <p>This<a href="/tutorials/html-to-pdf/" target="_blank" rel="nofollow noopener noreferrer">HTML 转换为 PDF 教程</a>还允许将PDF设计任务委托给网页设计师,而不是后端开发人员。</p> <p>IronPDF 使用<a href="/how-to/pixel-perfect-html-to-pdf/" target="_blank" rel="nofollow noopener noreferrer">像素完美 Chrome 渲染引擎指南</a>将支持 CSS3 和 JavaScript 的 HTML5 转换为 PDF 文档。 其形式可以是字符串、外部文件或外部 URL,所有这些都可以使用 IronPDF 轻松呈现为 PDF。</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 URL 渲染 PDF 可以促进 PDF 开发的顺利进行,是设计人员和编码人员之间的一项协作努力。</p> <hr /> <h2 id="anchor-36-49-c-36-49-url-36-49-pdf-36-49">用 C# 将 URL 转换为 PDF 的步骤</h2> <ol> <li> <p>下载<a href="/" target="_blank" rel="nofollow noopener noreferrer">IronPDF URL 到 PDF 转换库</a></p> </li> <li> <p>使用 NuGet 安装库以测试其功能</p> </li> <li> <p>使用 IronPDF for .NET 从接受查询字符串变量的 ASP.NET URL 渲染 PDF 文件</p> </li> <li> <p>使用 IronPDF 直接从 URL 创建 PDF 文档</p> </li> <li>轻松查看和验证 PDF 输出文档</li> </ol>
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">ChromePdfRenderer`类概述</a>这一切都有赖于我们的翻译团队。</p> <p><code>ChromePDFRenderOptions</code> 类中可用的设置示例包括边距、页眉、页脚、纸张大小和表单创建的设置。</p>
using IronPdf;
private void Form1_Load(object sender, EventArgs e)
{
//Changes the ASPX output into a pdf instead of HTML
IronPdf.AspxToPdf.RenderThisPageAsPdf();
}
Imports IronPdf
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
'Changes the ASPX output into a pdf instead of HTML
IronPdf.AspxToPdf.RenderThisPageAsPdf()
End Sub
<p>使用IronPDF库,通过在<strong><code>Form_Load</code></strong>事件中添加一行代码,ASP.NET网页可以被渲染成PDF而不是HTML。</p> <p>此示例展示了IronPDF如何能够生成复杂的、数据驱动的PDF文件,这些文件首先以HTML的形式进行设计和测试,以简化流程。</p> <p>IronPDF 的<a href="/how-to/aspx-to-pdf/" target="_blank" rel="nofollow noopener noreferrer">ASPX 到 PDF 的转换</a>此功能允许您在ASPX页面中调用一个方法,它会返回一个PDF而不是HTML。</p> <p>您可以对 PDF 进行编码,使其既可以在浏览器中显示,也可以作为文件下载。</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最简单的方法之一是让它渲染一个HTML文件。</p> <p>IronPDF 可以渲染存储在计算机上的任何 HTML 文件。</p> <p>In this<a href="/tutorials/html-to-pdf/" target="_blank" rel="nofollow noopener noreferrer">HTML 转 PDF 教程</a>此外,我们还将使用 "file://"协议来显示所有相关资产(如 CSS、图像和 JavaScript),就像使用 "file://"协议打开文件一样。</p> <p>这种方法的优点是允许开发人员在开发过程中在浏览器中测试HTML内容。 他们可以特别测试在呈现中的保真度。 我们推荐使用Chrome,因为IronPDF的渲染引擎是基于这款网络浏览器的。</p> <p>如果在Chrome中显示正常,那么它将会<a href="/how-to/pixel-perfect-html-to-pdf/" target="_blank" rel="nofollow noopener noreferrer">将 HTML 像素完美地渲染为 PDF</a>在 IronPDF 中也是如此。</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>使用 "IronPdf.ImageToPdfConverter "类从一个或多个图像文件生成 PDF。</p> <h2 id="anchor-36-49c-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 软件网站</a>了解 Iron Software 提供的整个开发者工具套件,包括 IronBarcode、IronOCR 等。</p>
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>用于阅读和编辑PDFs。 最受欢迎的是<a href="/how-to/merge-or-split-pdfs/" target="_blank" rel="nofollow noopener noreferrer">合并 PDF</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>使用 C# 中的直观 API 访问和修改 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>可以应用细粒度元数据和安全设置。 现在包括将 PDF 文档限制为不可打印、只读和加密的功能。 支持对 PDF 文档进行 128 位加密、解密和密码保护。</p>
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>将文本、图片或HTML水印添加到PDFs</li> <li>将相同的水印应用到PDF的每一页上。</li> <li>将不同的水印应用于特定的PDF页面</li> <li>调整水印在页面副本前后的位置</li> <li> <p>更精确地调整水印的不透明度、旋转和对齐方式</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">从 NuGet 下载并安装 IronPDF 库。</a></li> <li>创建一个新的 <code>PdfDocument</code> 或使用现有的 <code>PdfDocument</code> 锉刀</li> <li>致电 <code>应用水印</code> 方法为 PDF 添加水印。</li> <li>通过调用 <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>您可以通过将多页 PDF 加载或创建为 <code>IronPdf.PdfDocument</code> 对象,在 C# 中实现这一操作。</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>
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/">PDF 安全综合解决方案 IronSecureDoc</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带可编辑表单的文件可以通过添加<code><form></code>, <code><input>和</code><textarea>将`标签添加到文档部分。</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> 方法。 可以使用 <code>PdfDocument.FromFile</code> 方法或其他可用方法加载 PDF 文档。<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>RasterizeToImageFiles "可将 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 IronPdf.Signing;
// Cryptographically sign an existing PDF in 1 line of code!
new IronPdf.Signing.PdfSignature("Iron.p12", "123456").SignPdfFile("any.pdf");
/***** Advanced example for more control *****/
// Step 1. Create a PDF
var renderer = new ChromePdfRenderer();
var doc = renderer.RenderHtmlAsPdf("<h1>Testing 2048 bit digital security</h1>");
// Step 2. Create a Signature.
// You may create a .pfx or .p12 PDF signing certificate using Adobe Acrobat Reader.
// Read: https://helpx.adobe.com/acrobat/using/digital-ids.html
var signature = new IronPdf.Signing.PdfSignature("Iron.pfx", "123456")
{
// Step 3. Optional signing options and a handwritten signature graphic
SigningContact = "support@ironsoftware.com",
SigningLocation = "Chicago, USA",
SigningReason = "To show how to sign a PDF"
};
//Step 3. Sign the PDF with the PdfSignature. Multiple signing certificates may be used
doc.Sign(signature);
//Step 4. The PDF is not signed until saved to file, steam or byte array.
doc.SaveAs("signed.pdf");
Imports IronPdf
Imports IronPdf.Signing
' Cryptographically sign an existing PDF in 1 line of code!
Call (New IronPdf.Signing.PdfSignature("Iron.p12", "123456")).SignPdfFile("any.pdf")
'''*** Advanced example for more control ****
' Step 1. Create a PDF
Dim renderer = New ChromePdfRenderer()
Dim doc = renderer.RenderHtmlAsPdf("<h1>Testing 2048 bit digital security</h1>")
' Step 2. Create a Signature.
' You may create a .pfx or .p12 PDF signing certificate using Adobe Acrobat Reader.
' Read: https://helpx.adobe.com/acrobat/using/digital-ids.html
Dim signature = New IronPdf.Signing.PdfSignature("Iron.pfx", "123456") With {
.SigningContact = "support@ironsoftware.com",
.SigningLocation = "Chicago, USA",
.SigningReason = "To show how to sign a PDF"
}
'Step 3. Sign the PDF with the PdfSignature. Multiple signing certificates may be used
doc.Sign(signature)
'Step 4. The PDF is not signed until saved to file, steam or byte array.
doc.SaveAs("signed.pdf")
<div class="alert alert-info iron-variant-1" role="alert"> 您的企业在PDF安全性和合规性的年度订阅上花费过多。 考虑 <a href="https://ironsoftware.com/enterprise/securedoc/">IronSecureDoc</a>,提供了管理SaaS服务的解决方案,如数字签名、编辑、加密和保护,且仅需一次性付款。 <a href="https://ironsoftware.com/enterprise/securedoc/docs/">探索 IronSecureDoc 文档</a>. </div> <p>IronPDF 支持使用 .pfx 和 .p12 X509Certificate2 数字证书对新的或现有的 PDF 文件进行数字签名。</p> <p>PDF 一经签署,在证书失效的情况下不得修改。 这确保了保真度。</p> <p>要使用 Adobe Reader 免费生成签名证书,请阅读<a href="https://helpx.adobe.com/acrobat/using/digital-ids.html" target="_blank" rel="nofollow noopener noreferrer">Adobe 数字 ID 指南</a>.</p> <p>除加密签名外,还可使用 IronPDF 手写签名图像或公司印章图像进行签名。</p> <p>您可以从以下链接下载演示使用 IronPDF 进行 PDF 数字签名的示例项目<a href="https://ironpdf.com/downloads/digitally-sign-pdf.zip" target="_blank" rel="nofollow noopener noreferrer">IronPDF 数字签名 PDF 示例项目</a>.</p>
Our .NET PDF Library solution is a dream for developers, especially software engineers who use C#. You can easily create a core pdf library for .NET.
IronPDF 使用 .NET Chromium 引擎将 HTML 页面渲染为 PDF 文件。无需使用复杂的 API 来定位或设计 PDF。IronPDF 支持标准的网页文档 HTML、ASPX、JS、CSS 和图像。
It enables you to create a .NET PDF library using HTML5, CSS, JavaScript, and also images. You can edit, stamp, and add headers and footers to a PDF effortlessly. It also makes it easy to read PDF text and extract images!
我们从未见过如此精确的HTML转PDF转换器!我们的行业领先PDF库拥有众多功能和一个渲染引擎,可以在Chrome/Webkit中实现无缝嵌入和嵌入性。无需安装。
创建
无需Adobe Acrobat编辑现有PDF文档
操作现有的PDF文档
从多种格式转换
导出
保存
安全
打印
创建、合并、拆分、编辑和操作PDF文件,无论何时、无论您希望以何种方式,都易如反掌。利用您的C#开发技能,充分利用IronPDF不断扩展的功能列表。
要开始使用IronPDF进行项目工作,请下载免费的NuGet软件包安装程序或直接下载DLL。然后,您可以继续创建PDF文档、编辑和操作现有文件格式,或无须Adobe Acrobat即可导出到任何格式。
我们的支持范围从免费的全方位教程到24/7全天候实时支持。
开始使用 IronPDFIronPDF让您可以使用主流HTML文档格式,并在ASP.NET web应用程序中将其转换为PDF。 您可以应用多种设置,包括设置文件行为和名称、添加页眉和页脚、更改打印选项、添加分页符、结合异步和多线程等。
同样,您可以将C# MVC HTML转换为PDF用于ASP .NET应用程序,打印MVC视图以返回PDF文件格式,支持HTML、CSS、JavaScript和图像。
此外,在ASP .NET C#应用程序和网站中创建PDF文档并将当前HTML页面转换为PDF(C# html到pdf转换器)。使用丰富的HTML作为PDF内容,并利用IronPDF的生成功能进行编辑和操作。
使用IronPDF,分辨率问题已成为过去。IronPDF生成的PDF文档与Google Chrome网络浏览器中的PDF功能完全像素相同。
适用于 .NET, C#, VB, 模型-视图-控制器 (MVC), ASPX, ASP.NET, .NET Core
几分钟内开始使用好处显而易见!使用IronPDF,你可以更加轻松地完成更多任务。我们的产品非常适合需要制作、管理和编辑PDF库的任何人,包括房地产、出版、金融和企业等行业的企业。我们的解决方案价格也非常有竞争力。
Ready to see what IronPDF can do for your projects and business? Try it out now
C# PDF HTML
C# PDF .NET ASPX
VB.NET PDF ASP.NET
IronPDF是始终作为领先的.NET PDF库支持的