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");
// NuGet: Install-Package PDFsharp-MigraDoc-GDI
using MigraDoc.DocumentObjectModel;
using MigraDoc.Rendering;
using System.Diagnostics;
class Program
{
static void Main()
{
//MigraDocdoesn't support HTML directly
// Must manually create document structure
Document document = new Document();
Section section = document.AddSection();
Paragraph paragraph = section.AddParagraph();
paragraph.AddFormattedText("Hello World", TextFormat.Bold);
paragraph.Format.Font.Size = 16;
PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer();
pdfRenderer.Document = document;
pdfRenderer.RenderDocument();
pdfRenderer.PdfDocument.Save("output.pdf");
}
}
// NuGet: Install-Package PDFsharp-MigraDoc-GDI
using MigraDoc.DocumentObjectModel;
using MigraDoc.Rendering;
using System.Diagnostics;
class Program
{
static void Main()
{
//MigraDocdoesn't support HTML directly
// Must manually create document structure
Document document = new Document();
Section section = document.AddSection();
Paragraph paragraph = section.AddParagraph();
paragraph.AddFormattedText("Hello World", TextFormat.Bold);
paragraph.Format.Font.Size = 16;
PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer();
pdfRenderer.Document = document;
pdfRenderer.RenderDocument();
pdfRenderer.PdfDocument.Save("output.pdf");
}
}
Imports MigraDoc.DocumentObjectModel
Imports MigraDoc.Rendering
Imports System.Diagnostics
Class Program
Shared Sub Main()
'MigraDoc doesn't support HTML directly
' Must manually create document structure
Dim document As New Document()
Dim section As Section = document.AddSection()
Dim paragraph As Paragraph = section.AddParagraph()
paragraph.AddFormattedText("Hello World", TextFormat.Bold)
paragraph.Format.Font.Size = 16
Dim pdfRenderer As New PdfDocumentRenderer()
pdfRenderer.Document = document
pdfRenderer.RenderDocument()
pdfRenderer.PdfDocument.Save("output.pdf")
End Sub
End Class
$vbLabelText $csharpLabel
After (IronPDF):
// NuGet: Install-Package IronPdf
using IronPdf;
class Program
{
static void Main()
{
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>");
pdf.SaveAs("output.pdf");
}
}
// NuGet: Install-Package IronPdf
using IronPdf;
class Program
{
static void Main()
{
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>");
pdf.SaveAs("output.pdf");
}
}
Imports IronPdf
Class Program
Shared Sub Main()
Dim renderer = New ChromePdfRenderer()
Dim pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>")
pdf.SaveAs("output.pdf")
End Sub
End Class
IronPDF 只需 3 行即可实现相同的效果:创建呈现器、呈现 HTML 并保存。 HTML <h1> 标签自然提供粗体、大标题样式。 有关其他渲染选项,请参阅 HTML to PDF 文档。
示例 2:创建表格
之前 (MigraDoc):
// NuGet: Install-Package PDFsharp-MigraDoc-GDI
using MigraDoc.DocumentObjectModel;
using MigraDoc.DocumentObjectModel.Tables;
using MigraDoc.Rendering;
class Program
{
static void Main()
{
Document document = new Document();
Section section = document.AddSection();
Table table = section.AddTable();
table.Borders.Width = 0.75;
Column column1 = table.AddColumn("3cm");
Column column2 = table.AddColumn("3cm");
Row row1 = table.AddRow();
row1.Cells[0].AddParagraph("Name");
row1.Cells[1].AddParagraph("Age");
Row row2 = table.AddRow();
row2.Cells[0].AddParagraph("John");
row2.Cells[1].AddParagraph("30");
PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer();
pdfRenderer.Document = document;
pdfRenderer.RenderDocument();
pdfRenderer.PdfDocument.Save("table.pdf");
}
}
// NuGet: Install-Package PDFsharp-MigraDoc-GDI
using MigraDoc.DocumentObjectModel;
using MigraDoc.DocumentObjectModel.Tables;
using MigraDoc.Rendering;
class Program
{
static void Main()
{
Document document = new Document();
Section section = document.AddSection();
Table table = section.AddTable();
table.Borders.Width = 0.75;
Column column1 = table.AddColumn("3cm");
Column column2 = table.AddColumn("3cm");
Row row1 = table.AddRow();
row1.Cells[0].AddParagraph("Name");
row1.Cells[1].AddParagraph("Age");
Row row2 = table.AddRow();
row2.Cells[0].AddParagraph("John");
row2.Cells[1].AddParagraph("30");
PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer();
pdfRenderer.Document = document;
pdfRenderer.RenderDocument();
pdfRenderer.PdfDocument.Save("table.pdf");
}
}
Imports MigraDoc.DocumentObjectModel
Imports MigraDoc.DocumentObjectModel.Tables
Imports MigraDoc.Rendering
Class Program
Shared Sub Main()
Dim document As New Document()
Dim section As Section = document.AddSection()
Dim table As Table = section.AddTable()
table.Borders.Width = 0.75
Dim column1 As Column = table.AddColumn("3cm")
Dim column2 As Column = table.AddColumn("3cm")
Dim row1 As Row = table.AddRow()
row1.Cells(0).AddParagraph("Name")
row1.Cells(1).AddParagraph("Age")
Dim row2 As Row = table.AddRow()
row2.Cells(0).AddParagraph("John")
row2.Cells(1).AddParagraph("30")
Dim pdfRenderer As New PdfDocumentRenderer()
pdfRenderer.Document = document
pdfRenderer.RenderDocument()
pdfRenderer.PdfDocument.Save("table.pdf")
End Sub
End Class
$vbLabelText $csharpLabel
After (IronPDF):
// NuGet: Install-Package IronPdf
using IronPdf;
class Program
{
static void Main()
{
var renderer = new ChromePdfRenderer();
string htmlTable = @"
<table border='1'>
<tr><th>Name</th><th>Age</th></tr>
<tr><td>John</td><td>30</td></tr>
</table>";
var pdf = renderer.RenderHtmlAsPdf(htmlTable);
pdf.SaveAs("table.pdf");
}
}
// NuGet: Install-Package IronPdf
using IronPdf;
class Program
{
static void Main()
{
var renderer = new ChromePdfRenderer();
string htmlTable = @"
<table border='1'>
<tr><th>Name</th><th>Age</th></tr>
<tr><td>John</td><td>30</td></tr>
</table>";
var pdf = renderer.RenderHtmlAsPdf(htmlTable);
pdf.SaveAs("table.pdf");
}
}
Imports IronPdf
Class Program
Shared Sub Main()
Dim renderer = New ChromePdfRenderer()
Dim htmlTable As String = "
<table border='1'>
<tr><th>Name</th><th>Age</th></tr>
<tr><td>John</td><td>30</td></tr>
</table>"
Dim pdf = renderer.RenderHtmlAsPdf(htmlTable)
pdf.SaveAs("table.pdf")
End Sub
End Class
IronPDF 使用任何网络开发人员都知道的标准 HTML 表格语法。 border='1' 属性提供边框,
元素创建头部单元格,
元素创建数据单元格。 可以添加 CSS 以实现斑马线、悬停效果或响应式布局等高级样式。 了解有关在 PDF 中创建表格的更多信息。
示例 3:带页码的页眉和页脚
之前 (MigraDoc):
// NuGet: Install-Package PDFsharp-MigraDoc-GDI
using MigraDoc.DocumentObjectModel;
using MigraDoc.Rendering;
class Program
{
static void Main()
{
Document document = new Document();
Section section = document.AddSection();
// Add header
Paragraph headerPara = section.Headers.Primary.AddParagraph();
headerPara.AddText("Document Header");
headerPara.Format.Font.Size = 12;
headerPara.Format.Alignment = ParagraphAlignment.Center;
// Add footer
Paragraph footerPara = section.Footers.Primary.AddParagraph();
footerPara.AddText("Page ");
footerPara.AddPageField();
footerPara.Format.Alignment = ParagraphAlignment.Center;
// Add content
section.AddParagraph("Main content of the document");
PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer();
pdfRenderer.Document = document;
pdfRenderer.RenderDocument();
pdfRenderer.PdfDocument.Save("header-footer.pdf");
}
}
// NuGet: Install-Package PDFsharp-MigraDoc-GDI
using MigraDoc.DocumentObjectModel;
using MigraDoc.Rendering;
class Program
{
static void Main()
{
Document document = new Document();
Section section = document.AddSection();
// Add header
Paragraph headerPara = section.Headers.Primary.AddParagraph();
headerPara.AddText("Document Header");
headerPara.Format.Font.Size = 12;
headerPara.Format.Alignment = ParagraphAlignment.Center;
// Add footer
Paragraph footerPara = section.Footers.Primary.AddParagraph();
footerPara.AddText("Page ");
footerPara.AddPageField();
footerPara.Format.Alignment = ParagraphAlignment.Center;
// Add content
section.AddParagraph("Main content of the document");
PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer();
pdfRenderer.Document = document;
pdfRenderer.RenderDocument();
pdfRenderer.PdfDocument.Save("header-footer.pdf");
}
}
Imports MigraDoc.DocumentObjectModel
Imports MigraDoc.Rendering
Class Program
Shared Sub Main()
Dim document As New Document()
Dim section As Section = document.AddSection()
' Add header
Dim headerPara As Paragraph = section.Headers.Primary.AddParagraph()
headerPara.AddText("Document Header")
headerPara.Format.Font.Size = 12
headerPara.Format.Alignment = ParagraphAlignment.Center
' Add footer
Dim footerPara As Paragraph = section.Footers.Primary.AddParagraph()
footerPara.AddText("Page ")
footerPara.AddPageField()
footerPara.Format.Alignment = ParagraphAlignment.Center
' Add content
section.AddParagraph("Main content of the document")
Dim pdfRenderer As New PdfDocumentRenderer()
pdfRenderer.Document = document
pdfRenderer.RenderDocument()
pdfRenderer.PdfDocument.Save("header-footer.pdf")
End Sub
End Class
$vbLabelText $csharpLabel
After (IronPDF):
// NuGet: Install-Package IronPdf
using IronPdf;
class Program
{
static void Main()
{
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
var renderer = new ChromePdfRenderer();
renderer.RenderingOptions.TextHeader = new TextHeaderFooter
{
CenterText = "Document Header"
};
renderer.RenderingOptions.TextFooter = new TextHeaderFooter
{
CenterText = "Page {page}"
};
var pdf = renderer.RenderHtmlAsPdf("<h1>Main content of the document</h1>");
pdf.SaveAs("header-footer.pdf");
}
}
// NuGet: Install-Package IronPdf
using IronPdf;
class Program
{
static void Main()
{
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
var renderer = new ChromePdfRenderer();
renderer.RenderingOptions.TextHeader = new TextHeaderFooter
{
CenterText = "Document Header"
};
renderer.RenderingOptions.TextFooter = new TextHeaderFooter
{
CenterText = "Page {page}"
};
var pdf = renderer.RenderHtmlAsPdf("<h1>Main content of the document</h1>");
pdf.SaveAs("header-footer.pdf");
}
}
Imports IronPdf
Class Program
Shared Sub Main()
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY"
Dim renderer As New ChromePdfRenderer()
renderer.RenderingOptions.TextHeader = New TextHeaderFooter With {
.CenterText = "Document Header"
}
renderer.RenderingOptions.TextFooter = New TextHeaderFooter With {
.CenterText = "Page {page}"
}
Dim pdf = renderer.RenderHtmlAsPdf("<h1>Main content of the document</h1>")
pdf.SaveAs("header-footer.pdf")
End Sub
End Class
//MigraDocfield methods:
footerPara.AddPageField(); // Current page
footerPara.AddNumPagesField(); // Total pages
//IronPDFplaceholders:
"Page {page} of {total-pages}"
//MigraDocfield methods:
footerPara.AddPageField(); // Current page
footerPara.AddNumPagesField(); // Total pages
//IronPDFplaceholders:
"Page {page} of {total-pages}"
' MigraDoc field methods:
footerPara.AddPageField() ' Current page
footerPara.AddNumPagesField() ' Total pages
' IronPDF placeholders:
"Page {page} of {total-pages}"
//MigraDocpattern (DELETE):
PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer();
pdfRenderer.Document = document;
pdfRenderer.RenderDocument();
pdfRenderer.PdfDocument.Save("output.pdf");
//IronPDFpattern:
var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("output.pdf");
//MigraDocpattern (DELETE):
PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer();
pdfRenderer.Document = document;
pdfRenderer.RenderDocument();
pdfRenderer.PdfDocument.Save("output.pdf");
//IronPDFpattern:
var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("output.pdf");
' MigraDocpattern (DELETE):
Dim pdfRenderer As New PdfDocumentRenderer()
pdfRenderer.Document = document
pdfRenderer.RenderDocument()
pdfRenderer.PdfDocument.Save("output.pdf")
' IronPDFpattern:
Dim pdf = renderer.RenderHtmlAsPdf(html)
pdf.SaveAs("output.pdf")
$vbLabelText $csharpLabel
故障排除
问题 1:未找到文档/章节
问题:Document 和 Section 类在IronPDF中不存在。
解决方案:替换为 HTML 结构:
// MigraDoc
Document document = new Document();
Section section = document.AddSection();
// IronPDF
string html = "<html><body>...</body></html>";
var pdf = renderer.RenderHtmlAsPdf(html);
// MigraDoc
Document document = new Document();
Section section = document.AddSection();
// IronPDF
string html = "<html><body>...</body></html>";
var pdf = renderer.RenderHtmlAsPdf(html);
Imports MigraDoc.DocumentObjectModel
Imports IronPdf
Dim document As New Document()
Dim section As Section = document.AddSection()
Dim html As String = "<html><body>...</body></html>"
Dim pdf = renderer.RenderHtmlAsPdf(html)