// Complete example for saving PDF to diskusing IronPdf;// Initialize the Chrome PDF renderervar renderer = new ChromePdfRenderer();// Create HTML content with stylingstring htmlContent = @"<html><head> <style> body { font-family: Arial, sans-serif; margin: 40px; } h1 { color: #333; } .content { line-height: 1.6; } </style></head><body> <h1>Invoice #12345</h1> <div class='content'> <p>Date: " + DateTime.Now.ToString("yyyy-MM-dd") + @"</p> <p>Thank you for your business!</p> </div></body></html>";// Render HTML to PDFPdfDocument pdf = renderer.RenderHtmlAsPdf(htmlContent);// Save to disk with standard methodpdf.SaveAs("invoice_12345.pdf");// Save with password protection for sensitive documentspdf.Password = "secure123";pdf.SaveAs("protected_invoice_12345.pdf");
// Complete example for saving PDF to disk
using IronPdf;
// Initialize the Chrome PDF renderer
var renderer = new ChromePdfRenderer();
// Create HTML content with styling
string htmlContent = @"
<html>
<head>
<style>
body { font-family: Arial, sans-serif; margin: 40px; }
h1 { color: #333; }
.content { line-height: 1.6; }
</style>
</head>
<body>
<h1>Invoice #12345</h1>
<div class='content'>
<p>Date: " + DateTime.Now.ToString("yyyy-MM-dd") + @"</p>
<p>Thank you for your business!</p>
</div>
</body>
</html>";
// Render HTML to PDF
PdfDocument pdf = renderer.RenderHtmlAsPdf(htmlContent);
// Save to disk with standard method
pdf.SaveAs("invoice_12345.pdf");
// Save with password protection for sensitive documents
pdf.Password = "secure123";
pdf.SaveAs("protected_invoice_12345.pdf");
IRONVBCONVERTERERROR developers@ironsoftware.com
IRON VB CONVERTER ERROR developers@ironsoftware.com
// Example: Save PDF to MemoryStreamusing IronPdf;using System.IO;var renderer = new ChromePdfRenderer();// Render HTML contentPdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Monthly Report</h1><p>Sales figures...</p>");// Get the PDF as a MemoryStreamMemoryStream stream = pdf.Stream;// Example: Upload to cloud storage or database// UploadToCloudStorage(stream);// Example: Email as attachment without saving to disk// EmailService.SendWithAttachment(stream, "report.pdf");// Remember to dispose of the stream when donestream.Dispose();
// Example: Save PDF to MemoryStream
using IronPdf;
using System.IO;
var renderer = new ChromePdfRenderer();
// Render HTML content
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Monthly Report</h1><p>Sales figures...</p>");
// Get the PDF as a MemoryStream
MemoryStream stream = pdf.Stream;
// Example: Upload to cloud storage or database
// UploadToCloudStorage(stream);
// Example: Email as attachment without saving to disk
// EmailService.SendWithAttachment(stream, "report.pdf");
// Remember to dispose of the stream when done
stream.Dispose();
ImportsIronPdfImportsSystem.IO' Example: Save PDF to MemoryStreamDim renderer As New ChromePdfRenderer()' Render HTML contentDim pdf AsPdfDocument = renderer.RenderHtmlAsPdf("<h1>Monthly Report</h1><p>Sales figures...</p>")' Get the PDF as a MemoryStreamDim stream AsMemoryStream = pdf.Stream' Example: Upload to cloud storage or database' UploadToCloudStorage(stream)' Example: Email as attachment without saving to disk' EmailService.SendWithAttachment(stream, "report.pdf")' Remember to dispose of the stream when donestream.Dispose()
Imports IronPdf
Imports System.IO
' Example: Save PDF to MemoryStream
Dim renderer As New ChromePdfRenderer()
' Render HTML content
Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Monthly Report</h1><p>Sales figures...</p>")
' Get the PDF as a MemoryStream
Dim stream As MemoryStream = pdf.Stream
' Example: Upload to cloud storage or database
' UploadToCloudStorage(stream)
' Example: Email as attachment without saving to disk
' EmailService.SendWithAttachment(stream, "report.pdf")
' Remember to dispose of the stream when done
stream.Dispose()
輸出
如何儲存為二進位資料
byte[]返回。 位元組陣列適用於資料庫欄位、快取條目和接受原始位元組而不是流的API。
// Example: Convert PDF to binary datausing IronPdf;var renderer = new ChromePdfRenderer();// Configure rendering options for better qualityrenderer.RenderingOptions = new ChromePdfRenderOptions(){MarginTop = 20,MarginBottom = 20,MarginLeft = 10,MarginRight = 10,PaperSize = IronPdf.Rendering.PdfPaperSize.A4};// Render content to PDFPdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Contract Document</h1>");// Get binary databyte[] binaryData = pdf.BinaryData;// Example: Store in database// database.StorePdfDocument(documentId, binaryData);// Example: Send via API// apiClient.UploadDocument(binaryData);
// Example: Convert PDF to binary data
using IronPdf;
var renderer = new ChromePdfRenderer();
// Configure rendering options for better quality
renderer.RenderingOptions = new ChromePdfRenderOptions()
{
MarginTop = 20,
MarginBottom = 20,
MarginLeft = 10,
MarginRight = 10,
PaperSize = IronPdf.Rendering.PdfPaperSize.A4
};
// Render content to PDF
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Contract Document</h1>");
// Get binary data
byte[] binaryData = pdf.BinaryData;
// Example: Store in database
// database.StorePdfDocument(documentId, binaryData);
// Example: Send via API
// apiClient.UploadDocument(binaryData);
ImportsIronPdf' Example: Convert PDF to binary dataDim renderer As New ChromePdfRenderer()' Configure rendering options for better qualityrenderer.RenderingOptions = New ChromePdfRenderOptions() With { .MarginTop = 20, .MarginBottom = 20, .MarginLeft = 10, .MarginRight = 10, .PaperSize = IronPdf.Rendering.PdfPaperSize.A4}' Render content to PDFDim pdf AsPdfDocument = renderer.RenderHtmlAsPdf("<h1>Contract Document</h1>")' Get binary dataDim binaryData AsByte() = pdf.BinaryData' Example: Store in database' database.StorePdfDocument(documentId, binaryData)' Example: Send via API' apiClient.UploadDocument(binaryData)
Imports IronPdf
' Example: Convert PDF to binary data
Dim renderer As New ChromePdfRenderer()
' Configure rendering options for better quality
renderer.RenderingOptions = New ChromePdfRenderOptions() With {
.MarginTop = 20,
.MarginBottom = 20,
.MarginLeft = 10,
.MarginRight = 10,
.PaperSize = IronPdf.Rendering.PdfPaperSize.A4
}
' Render content to PDF
Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Contract Document</h1>")
' Get binary data
Dim binaryData As Byte() = pdf.BinaryData
' Example: Store in database
' database.StorePdfDocument(documentId, binaryData)
' Example: Send via API
' apiClient.UploadDocument(binaryData)
// MVC controller methods for PDF exportpublic IActionResultDownloadInvoice(int invoiceId){ // Generate your HTML content string htmlContent = GenerateInvoiceHtml(invoiceId); // Render the PDF with IronPDF var renderer = new ChromePdfRenderer(); PdfDocument pdf = renderer.RenderHtmlAsPdf(htmlContent); // Take the PDF stream and rewind it MemoryStream stream = pdf.Stream; stream.Position = 0; // Returning a FileStreamResult prompts a download in the browser return new FileStreamResult(stream, "application/pdf") {FileDownloadName = $"invoice_{invoiceId}.pdf" };}public IActionResultViewInvoice(int invoiceId){ var renderer = new ChromePdfRenderer(); PdfDocument pdf = renderer.RenderHtmlAsPdf(GenerateInvoiceHtml(invoiceId)); // Returning BinaryData with no filename displays the PDF inline returnFile(pdf.BinaryData, "application/pdf");}
// MVC controller methods for PDF export
public IActionResult DownloadInvoice(int invoiceId)
{
// Generate your HTML content
string htmlContent = GenerateInvoiceHtml(invoiceId);
// Render the PDF with IronPDF
var renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf(htmlContent);
// Take the PDF stream and rewind it
MemoryStream stream = pdf.Stream;
stream.Position = 0;
// Returning a FileStreamResult prompts a download in the browser
return new FileStreamResult(stream, "application/pdf")
{
FileDownloadName = $"invoice_{invoiceId}.pdf"
};
}
public IActionResult ViewInvoice(int invoiceId)
{
var renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf(GenerateInvoiceHtml(invoiceId));
// Returning BinaryData with no filename displays the PDF inline
return File(pdf.BinaryData, "application/pdf");
}
// ASP.NET WebForms PDF exportprotected voidExportButton_Click(object sender, EventArgs e){ var renderer = new ChromePdfRenderer(); // Configure rendering options renderer.RenderingOptions = new ChromePdfRenderOptions() {PaperSize = IronPdf.Rendering.PdfPaperSize.Letter,PrintHtmlBackgrounds = true,CreatePdfFormsFromHtml = true }; // Render from custom HTML PdfDocumentMyPdfDocument = renderer.RenderHtmlAsPdf(GetReportHtml()); // Retrieve the PDF bytes byte[] Binary = MyPdfDocument.BinaryData; // Write the bytes to the response as a downloadResponse.Clear();Response.ContentType = "application/octet-stream";Response.AddHeader("Content-Disposition", "attachment; filename=report_" + DateTime.Now.ToString("yyyyMMdd") + ".pdf");Context.Response.OutputStream.Write(Binary, 0, Binary.Length);Response.Flush();Response.End();}
// ASP.NET WebForms PDF export
protected void ExportButton_Click(object sender, EventArgs e)
{
var renderer = new ChromePdfRenderer();
// Configure rendering options
renderer.RenderingOptions = new ChromePdfRenderOptions()
{
PaperSize = IronPdf.Rendering.PdfPaperSize.Letter,
PrintHtmlBackgrounds = true,
CreatePdfFormsFromHtml = true
};
// Render from custom HTML
PdfDocument MyPdfDocument = renderer.RenderHtmlAsPdf(GetReportHtml());
// Retrieve the PDF bytes
byte[] Binary = MyPdfDocument.BinaryData;
// Write the bytes to the response as a download
Response.Clear();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition",
"attachment; filename=report_" + DateTime.Now.ToString("yyyyMMdd") + ".pdf");
Context.Response.OutputStream.Write(Binary, 0, Binary.Length);
Response.Flush();
Response.End();
}
using IronPdf;var renderer = new ChromePdfRenderer();// Render the document you want to archivePdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Archived Document</h1>");// Save as a PDF/A-3b file for long-term archiving.// PdfAVersions controls the conformance level (PdfA1b, PdfA2b, PdfA3b, PdfA4, and others).pdf.SaveAsPdfA("archive-pdfa.pdf", IronPdf.PdfAVersions.PdfA3b);
using IronPdf;
var renderer = new ChromePdfRenderer();
// Render the document you want to archive
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Archived Document</h1>");
// Save as a PDF/A-3b file for long-term archiving.
// PdfAVersions controls the conformance level (PdfA1b, PdfA2b, PdfA3b, PdfA4, and others).
pdf.SaveAsPdfA("archive-pdfa.pdf", IronPdf.PdfAVersions.PdfA3b);
using IronPdf;var renderer = new ChromePdfRenderer();// Render content that should be tagged for assistive technologyPdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Accessible Document</h1><p>Tagged for screen readers.</p>");// Save as a PDF/UA-1 file. The last argument sets the document's primary// language, which screen readers use to choose the correct voice.pdf.SaveAsPdfUA("accessible-pdfua.pdf", IronPdf.PdfUAVersions.PdfUA1, IronPdf.NaturalLanguages.English_UnitedKingdom);
using IronPdf;
var renderer = new ChromePdfRenderer();
// Render content that should be tagged for assistive technology
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Accessible Document</h1><p>Tagged for screen readers.</p>");
// Save as a PDF/UA-1 file. The last argument sets the document's primary
// language, which screen readers use to choose the correct voice.
pdf.SaveAsPdfUA("accessible-pdfua.pdf", IronPdf.PdfUAVersions.PdfUA1, IronPdf.NaturalLanguages.English_UnitedKingdom);
using IronPdf;using IronPdf.Rendering;var renderer = new ChromePdfRenderer();// Create and save the original revision of the documentPdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Versioned Document</h1>");pdf.SaveAs("revision-base.pdf");// Re-open with change tracking enabled so the next save appends a revision// instead of rewriting the file. This preserves earlier signed revisions.PdfDocument loaded = PdfDocument.FromFile("revision-base.pdf", null, null, ChangeTrackingModes.EnableChangeTracking);// Write an incremental revision on top of the existing bytesloaded.SaveAsRevision("revision-v2.pdf");
using IronPdf;
using IronPdf.Rendering;
var renderer = new ChromePdfRenderer();
// Create and save the original revision of the document
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Versioned Document</h1>");
pdf.SaveAs("revision-base.pdf");
// Re-open with change tracking enabled so the next save appends a revision
// instead of rewriting the file. This preserves earlier signed revisions.
PdfDocument loaded = PdfDocument.FromFile("revision-base.pdf", null, null, ChangeTrackingModes.EnableChangeTracking);
// Write an incremental revision on top of the existing bytes
loaded.SaveAsRevision("revision-v2.pdf");
using IronPdf;using System.Threading.Tasks;var renderer = new ChromePdfRenderer();// Render off the calling thread so a web request or UI stays responsivePdfDocument pdf = await renderer.RenderHtmlAsPdfAsync("<h1>Async Generated PDF</h1>");// SaveAs writes the finished document to disk once the render completespdf.SaveAs("async-render.pdf");
using IronPdf;
using System.Threading.Tasks;
var renderer = new ChromePdfRenderer();
// Render off the calling thread so a web request or UI stays responsive
PdfDocument pdf = await renderer.RenderHtmlAsPdfAsync("<h1>Async Generated PDF</h1>");
// SaveAs writes the finished document to disk once the render completes
pdf.SaveAs("async-render.pdf");
可以,IronPDF 允許您將 PDF 作為二進位資料直接提供給網頁瀏覽器。您可以使用 BinaryData 屬性獲取 PDF 作為字節陣列,並通過您的網頁應用程式的回應流提供,無需臨時檔案儲存。
用一句話轉換並儲存 HTML 為 PDF 的最簡單方法是什麼?
IronPDF 提供單行解決方案:new IronPdf.ChromePdfRenderer().RenderHtmlAsPdf("Your HTML").SaveAs("output.pdf")。這會建立一個渲染器,將 HTML 轉換為 PDF,並在一個語句中將其儲存到磁碟。
Can I password-protect a PDF using IronPDF?
Yes, IronPDF allows you to set a password on a `PdfDocument` via the `Password` property before saving it. This ensures that a PDF cannot be opened without the correct password.
What should I do if I need an accessible PDF conforming to PDF/UA standards?
You can create a PDF that conforms to PDF/UA standards using IronPDF by calling the `SaveAsPdfUA` method. This ensures that the document includes tags to aid navigation by screen readers.
How can I keep different versions of a PDF document?
IronPDF supports incremental saves with the `SaveAsRevision` method, which appends changes to an existing PDF file while preserving previous revisions, ideal for maintaining version history.
Why should I consider exporting PDFs using the PDF/A format?
Exporting PDFs in PDF/A format ensures that the document is self-contained and suitable for long-term archiving, as it includes all necessary components like fonts and color data to ensure fidelity over time.