产品比较

IronPDF 与 PDFSharpCore 的比较

发布 2024年三月6日
分享:

创建和管理 PDF 是软件开发中的常见需求。开发人员通常需要能轻松生成、编辑和处理 PDF 的库,这些库可以作为 NuGet 包提供。 IronPDFPDFSharpCore 是.NET 语言生态系统中两个重要的库,它们都能实现这一目的。本文将针对 Visual Studio 用户深入介绍这两种库,重点介绍它们的特点、区别以及如何在 .NET 项目中使用。

IronPDF 和 PDFSharpCore 简介

IronPDF是一个全面的.NET库,专为开发人员轻松创建、编辑和转换PDF文档而设计。它为创建、编辑和转换此类文档提供了广泛的功能。IronPDF 支持 .NET Core 和框架,因此适用于各种应用程序,包括 Web 和桌面应用程序。

PDFSharpCore 库是原始 PDFsharp 库的部分移植。它以 .NET Core 和 MigraDoc 基础为目标,专注于创建和处理 PDF 文档,而无需依赖 Windows 库。这使得它适用于跨平台项目,可在 Linux、MacOS 和 Windows 上运行。

安装 IronPDF 和 PDFSharpCore

安装 IronPDF

要开始在项目中使用 IronPDF,可以通过 NuGet 包管理器轻松安装。请按照以下步骤安装 IronPDF:

1.在 Visual Studio 中打开项目。

2.导航至 工具 > NuGet 包管理器 > 管理解决方案的 NuGet 包

3.在 NuGet 管理器中搜索IronPdf

4.选择您的项目,然后点击安装,将 IronPDF 添加到您的项目中。

IronPDF 与 PDFSharpCore 的比较:图 1 - IronPDF

或者,你也可以使用软件包管理器控制台,用以下命令安装 IronPDF:

Install-Package IronPdf

安装 PDFSharpCore

要使用 NuGet 安装 PDFSharpCore,请遵循以下说明:

1.确保已打开 Visual Studio 项目。

2.转到工具 > NuGet 包管理器 > 管理解决方案的 NuGet 包

3.在 NuGet 包管理器中,搜索 PDFSharpCore.

4.选择您的项目,然后点击安装,以集成 PDFSharpCore。

IronPDF 与 PDFSharpCore 的比较:图 2 - PDFSharpCore

对于喜欢使用软件包管理器控制台的开发人员,可以使用此命令安装 PDFSharpCore:

Install-Package PdfSharpCore

创建 PDF 文件:IronPDF vs. PDFSharpCore

IronPDF:轻松创建和操作

IronPDF 简化了开发人员生成新 PDF 文档的过程。它提供了一种直接从 HTML 内容或网页创建 PDF 的直观方法,简化了将网页内容转换为便携式文档格式文件的过程。

HTML 字符串到 PDF

IronPDF 允许 根据 HTML 字符串创建 PDF 文档 使用其ChromePdfRenderer类。当您需要从 HTML 模板或应用程序中生成的内容动态生成 PDF 时,该功能尤其有用。下面介绍如何使用 IronPDF 将 HTML 字符串转换为 PDF 文档:

using IronPdf;
class Program
{
    static void Main(string [] args)
    {
        License.LicenseKey = "License-Key";
        // Create a new instance of IronPdf.ChromePdfRenderer
        var Renderer = new ChromePdfRenderer();
        // HTML content with meaningful content
        string htmlContent = @"
                <h1>Report on Sales Performance</h1>
                <p>This report analyzes the sales performance for the first quarter of 2024.</p>
                <table>
                    <thead>
                        <tr>
                            <th>Month</th>
                            <th>Sales Revenue</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td>January</td>
                            <td>$50,000</td>
                        </tr>
                        <tr>
                            <td>February</td>
                            <td>$55,000</td>
                        </tr>
                        <tr>
                            <td>March</td>
                            <td>$60,000</td>
                        </tr>
                    </tbody>
                </table>
                <p>This report highlights the positive growth in sales revenue over the quarter.</p>";
        // Render HTML content as PDF
        var PDF = Renderer.RenderHtmlAsPdf(htmlContent);
        // Save the PDF as example.pdf
        PDF.SaveAs("example.pdf");
        Console.WriteLine("PDF saved successfully.");
    }
}
using IronPdf;
class Program
{
    static void Main(string [] args)
    {
        License.LicenseKey = "License-Key";
        // Create a new instance of IronPdf.ChromePdfRenderer
        var Renderer = new ChromePdfRenderer();
        // HTML content with meaningful content
        string htmlContent = @"
                <h1>Report on Sales Performance</h1>
                <p>This report analyzes the sales performance for the first quarter of 2024.</p>
                <table>
                    <thead>
                        <tr>
                            <th>Month</th>
                            <th>Sales Revenue</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td>January</td>
                            <td>$50,000</td>
                        </tr>
                        <tr>
                            <td>February</td>
                            <td>$55,000</td>
                        </tr>
                        <tr>
                            <td>March</td>
                            <td>$60,000</td>
                        </tr>
                    </tbody>
                </table>
                <p>This report highlights the positive growth in sales revenue over the quarter.</p>";
        // Render HTML content as PDF
        var PDF = Renderer.RenderHtmlAsPdf(htmlContent);
        // Save the PDF as example.pdf
        PDF.SaveAs("example.pdf");
        Console.WriteLine("PDF saved successfully.");
    }
}
Imports IronPdf
Friend Class Program
	Shared Sub Main(ByVal args() As String)
		License.LicenseKey = "License-Key"
		' Create a new instance of IronPdf.ChromePdfRenderer
		Dim Renderer = New ChromePdfRenderer()
		' HTML content with meaningful content
		Dim htmlContent As String = "
                <h1>Report on Sales Performance</h1>
                <p>This report analyzes the sales performance for the first quarter of 2024.</p>
                <table>
                    <thead>
                        <tr>
                            <th>Month</th>
                            <th>Sales Revenue</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td>January</td>
                            <td>$50,000</td>
                        </tr>
                        <tr>
                            <td>February</td>
                            <td>$55,000</td>
                        </tr>
                        <tr>
                            <td>March</td>
                            <td>$60,000</td>
                        </tr>
                    </tbody>
                </table>
                <p>This report highlights the positive growth in sales revenue over the quarter.</p>"
		' Render HTML content as PDF
		Dim PDF = Renderer.RenderHtmlAsPdf(htmlContent)
		' Save the PDF as example.pdf
		PDF.SaveAs("example.pdf")
		Console.WriteLine("PDF saved successfully.")
	End Sub
End Class
VB   C#

IronPDF 与 PDFSharpCore 的比较:图 3 - PDF 报告发送输出

HTML 文件转 PDF

IronPDF 还支持将整个 HTML 文件转换为 PDF 文档。这对于将存储为文件的静态 HTML 页面或模板转换为 PDF 格式尤其有用。下面是一个如何将 HTML 文件转换为 PDF 文档的代码示例:

using IronPdf;
class Program
{
    void Main(string [] args)
    {
        License.LicenseKey = "License-Key";
        // Create a new instance of IronPdf.ChromePdfRenderer
        var Renderer = new ChromePdfRenderer();
        // Render HTML content as PDF
        var PDF = Renderer.RenderHtmlFileAsPdf(@"C:\Users\Tayyab Ali\Desktop\example.html");
        PDF.SaveAs("example.pdf");
        Console.WriteLine("PDF saved successfully.");
    }
}
using IronPdf;
class Program
{
    void Main(string [] args)
    {
        License.LicenseKey = "License-Key";
        // Create a new instance of IronPdf.ChromePdfRenderer
        var Renderer = new ChromePdfRenderer();
        // Render HTML content as PDF
        var PDF = Renderer.RenderHtmlFileAsPdf(@"C:\Users\Tayyab Ali\Desktop\example.html");
        PDF.SaveAs("example.pdf");
        Console.WriteLine("PDF saved successfully.");
    }
}
Imports IronPdf
Friend Class Program
	Private Sub Main(ByVal args() As String)
		License.LicenseKey = "License-Key"
		' Create a new instance of IronPdf.ChromePdfRenderer
		Dim Renderer = New ChromePdfRenderer()
		' Render HTML content as PDF
		Dim PDF = Renderer.RenderHtmlFileAsPdf("C:\Users\Tayyab Ali\Desktop\example.html")
		PDF.SaveAs("example.pdf")
		Console.WriteLine("PDF saved successfully.")
	End Sub
End Class
VB   C#

IronPDF 与 PDFSharpCore 的比较:图 4 - PDF 输出

URL 至 PDF

将网页转换为 PDF 是 IronPDF 的另一项强大功能。这一功能对于将实时网页内容(包括样式和媒体)直接捕获到 PDF 文档中非常有价值。要将网页 URL 转换为 PDF 文档,您可以使用以下代码:

using IronPdf;
class Program
{
    static void Main(string [] args)
    {
        License.LicenseKey = "License-Key";
        // Create a new instance of IronPdf.ChromePdfRenderer
        var Renderer = new ChromePdfRenderer();
        Renderer.RenderingOptions.PaperSize = IronPdf.Rendering.PdfPaperSize.A2;
        // Render HTML content as PDF
        var PDF = Renderer.RenderUrlAsPdf("https://dotnet.microsoft.com/en-us/download/dotnet-framework");
        // Save the PDF as example.pdf
        PDF.SaveAs("webpage.pdf");
        Console.WriteLine("PDF saved successfully.");
    }
}
using IronPdf;
class Program
{
    static void Main(string [] args)
    {
        License.LicenseKey = "License-Key";
        // Create a new instance of IronPdf.ChromePdfRenderer
        var Renderer = new ChromePdfRenderer();
        Renderer.RenderingOptions.PaperSize = IronPdf.Rendering.PdfPaperSize.A2;
        // Render HTML content as PDF
        var PDF = Renderer.RenderUrlAsPdf("https://dotnet.microsoft.com/en-us/download/dotnet-framework");
        // Save the PDF as example.pdf
        PDF.SaveAs("webpage.pdf");
        Console.WriteLine("PDF saved successfully.");
    }
}
Imports IronPdf
Friend Class Program
	Shared Sub Main(ByVal args() As String)
		License.LicenseKey = "License-Key"
		' Create a new instance of IronPdf.ChromePdfRenderer
		Dim Renderer = New ChromePdfRenderer()
		Renderer.RenderingOptions.PaperSize = IronPdf.Rendering.PdfPaperSize.A2
		' Render HTML content as PDF
		Dim PDF = Renderer.RenderUrlAsPdf("https://dotnet.microsoft.com/en-us/download/dotnet-framework")
		' Save the PDF as example.pdf
		PDF.SaveAs("webpage.pdf")
		Console.WriteLine("PDF saved successfully.")
	End Sub
End Class
VB   C#

IronPDF 与 PDFSharpCore 的比较:图 5 - URL 到 PDF 的输出

PDFSharpCore:灵活性和控制性

PDFSharpCore 是 PDFsharp for .NET Core 的部分移植版,它提供了对 PDF 文档创建过程的详细控制。它不会直接将 HTML 转换为 PDF,但提供了大量功能,用于从头开始生成新文档或修改现有 PDF 文件。下面是一个使用 PDFSharpCore 的基本示例:

var doc = new PdfDocument();
var page = doc.AddPage();
var graphics = XGraphics.FromPdfPage(page);
var font = new XFont("Verdana", 20, XFontStyle.Bold);
graphics.DrawString("Hello, World!", font, XBrushes.Black, new XRect(0, 0, page.Width, page.Height), XStringFormats.Center);
doc.Save("newdocument.pdf");
var doc = new PdfDocument();
var page = doc.AddPage();
var graphics = XGraphics.FromPdfPage(page);
var font = new XFont("Verdana", 20, XFontStyle.Bold);
graphics.DrawString("Hello, World!", font, XBrushes.Black, new XRect(0, 0, page.Width, page.Height), XStringFormats.Center);
doc.Save("newdocument.pdf");
Dim doc = New PdfDocument()
Dim page = doc.AddPage()
Dim graphics = XGraphics.FromPdfPage(page)
Dim font = New XFont("Verdana", 20, XFontStyle.Bold)
graphics.DrawString("Hello, World!", font, XBrushes.Black, New XRect(0, 0, page.Width, page.Height), XStringFormats.Center)
doc.Save("newdocument.pdf")
VB   C#

IronPDF 与 PDFSharpCore 的比较:图 6 - PDFSharpCore 输出

处理复杂的 PDF 任务高级功能

IronPDF 高级功能

在处理高级 PDF 任务时,IronPDF 脱颖而出。它具有以下功能

PDF/A 合规性:IronPDF 能够生成 符合 PDF/A 标准的文件 对于需要长期数字保存的企业来说至关重要。该功能可确保 PDF 文件的制作方式符合严格的存档标准,从而长期保存文档的完整性。

using IronPdf;
// Open a PDF 
PdfDocument pdf = PdfDocument.FromFile("example.pdf");
// Save the PDF/A-3b
pdf.SaveAsPdfA("PDFA-Document.pdf", PdfAVersions.PdfA3);
using IronPdf;
// Open a PDF 
PdfDocument pdf = PdfDocument.FromFile("example.pdf");
// Save the PDF/A-3b
pdf.SaveAsPdfA("PDFA-Document.pdf", PdfAVersions.PdfA3);
Imports IronPdf
' Open a PDF 
Private pdf As PdfDocument = PdfDocument.FromFile("example.pdf")
' Save the PDF/A-3b
pdf.SaveAsPdfA("PDFA-Document.pdf", PdfAVersions.PdfA3)
VB   C#

水印和安全:IronPDF 提供强大的工具,用于 添加水印 这对品牌和版权保护至关重要。此外,它还支持全面的安全功能,包括加密 PDF 文件、设置用户权限和添加数字签名。这确保了敏感信息的安全和文档的完整性。

using IronPdf;
class Program
{
    static void Main(string [] args)
    {
        var renderer = new ChromePdfRenderer();
        var pdf = renderer.RenderHtmlAsPdf("<html><body><h1>Hello, IronPdf!</h1></body></html>");
        pdf.ApplyWatermark("<h2 style='color:red'>SAMPLE WATERMARK</h2>", 30, IronPdf.Editing.VerticalAlignment.Middle, IronPdf.Editing.HorizontalAlignment.Center);
        pdf.SecuritySettings.OwnerPassword = "top-secret"; // password to edit the pdf
        pdf.SecuritySettings.UserPassword = "sharable"; // password to open the pdf
        pdf.SaveAs("CombinedPDF.pdf");
        Console.WriteLine("PDF generated successfully!");
    }
}
using IronPdf;
class Program
{
    static void Main(string [] args)
    {
        var renderer = new ChromePdfRenderer();
        var pdf = renderer.RenderHtmlAsPdf("<html><body><h1>Hello, IronPdf!</h1></body></html>");
        pdf.ApplyWatermark("<h2 style='color:red'>SAMPLE WATERMARK</h2>", 30, IronPdf.Editing.VerticalAlignment.Middle, IronPdf.Editing.HorizontalAlignment.Center);
        pdf.SecuritySettings.OwnerPassword = "top-secret"; // password to edit the pdf
        pdf.SecuritySettings.UserPassword = "sharable"; // password to open the pdf
        pdf.SaveAs("CombinedPDF.pdf");
        Console.WriteLine("PDF generated successfully!");
    }
}
Imports IronPdf
Friend Class Program
	Shared Sub Main(ByVal args() As String)
		Dim renderer = New ChromePdfRenderer()
		Dim pdf = renderer.RenderHtmlAsPdf("<html><body><h1>Hello, IronPdf!</h1></body></html>")
		pdf.ApplyWatermark("<h2 style='color:red'>SAMPLE WATERMARK</h2>", 30, IronPdf.Editing.VerticalAlignment.Middle, IronPdf.Editing.HorizontalAlignment.Center)
		pdf.SecuritySettings.OwnerPassword = "top-secret" ' password to edit the pdf
		pdf.SecuritySettings.UserPassword = "sharable" ' password to open the pdf
		pdf.SaveAs("CombinedPDF.pdf")
		Console.WriteLine("PDF generated successfully!")
	End Sub
End Class
VB   C#

IronPDF 与 PDFSharpCore 的比较:图 7 - 受密码保护的 PDF 输出

编辑和合并 PDF:IronPDF 不局限于创建,还提供编辑现有 PDF 文件内容的功能。这包括文本编辑、图像处理和布局调整。此外,它的合并功能允许 合并多个 PDF 文档 将这些文件合并为一个文件,简化了文件管理和分发。

using IronPdf;
class Program
{
    static void Main(string [] args)
    {
        const string html_a = @"<p> [PDF_A] </p>
            <p> [PDF_A] 1st Page </p>
            <div style='page-break-after: always;'></div>
            <p> [PDF_A] 2nd Page</p>";
        const string html_b = @"<p> [PDF_B] </p>
            <p> [PDF_B] 1st Page </p>
            <div style='page-break-after: always;'></div>
            <p> [PDF_B] 2nd Page</p>";
        const string html_c =
            @"<p> Hello Iron </p>
            <p> This is 1st Page </p>
            <div style='page-break-after: always;'></div>
            <p> This is 2nd Page</p>
            <div style='page-break-after: always;'></div>
            <p> This is 3rd Page</p>";
        var renderer = new ChromePdfRenderer();
        var pdfdoc_a = renderer.RenderHtmlAsPdf(html_a);
        var pdfdoc_b = renderer.RenderHtmlAsPdf(html_b);
        var merged = PdfDocument.Merge(pdfdoc_a, pdfdoc_b);
        var pdfdoc_c = renderer.RenderHtmlAsPdf(html_c);
        merged.AppendPdf(pdfdoc_c);
        merged.SaveAs("CombinedDocument.pdf");
        Console.WriteLine("PDF generated successfully!");
    }
}
using IronPdf;
class Program
{
    static void Main(string [] args)
    {
        const string html_a = @"<p> [PDF_A] </p>
            <p> [PDF_A] 1st Page </p>
            <div style='page-break-after: always;'></div>
            <p> [PDF_A] 2nd Page</p>";
        const string html_b = @"<p> [PDF_B] </p>
            <p> [PDF_B] 1st Page </p>
            <div style='page-break-after: always;'></div>
            <p> [PDF_B] 2nd Page</p>";
        const string html_c =
            @"<p> Hello Iron </p>
            <p> This is 1st Page </p>
            <div style='page-break-after: always;'></div>
            <p> This is 2nd Page</p>
            <div style='page-break-after: always;'></div>
            <p> This is 3rd Page</p>";
        var renderer = new ChromePdfRenderer();
        var pdfdoc_a = renderer.RenderHtmlAsPdf(html_a);
        var pdfdoc_b = renderer.RenderHtmlAsPdf(html_b);
        var merged = PdfDocument.Merge(pdfdoc_a, pdfdoc_b);
        var pdfdoc_c = renderer.RenderHtmlAsPdf(html_c);
        merged.AppendPdf(pdfdoc_c);
        merged.SaveAs("CombinedDocument.pdf");
        Console.WriteLine("PDF generated successfully!");
    }
}
Imports IronPdf
Friend Class Program
	Shared Sub Main(ByVal args() As String)
		Const html_a As String = "<p> [PDF_A] </p>
            <p> [PDF_A] 1st Page </p>
            <div style='page-break-after: always;'></div>
            <p> [PDF_A] 2nd Page</p>"
		Const html_b As String = "<p> [PDF_B] </p>
            <p> [PDF_B] 1st Page </p>
            <div style='page-break-after: always;'></div>
            <p> [PDF_B] 2nd Page</p>"
		Const html_c As String = "<p> Hello Iron </p>
            <p> This is 1st Page </p>
            <div style='page-break-after: always;'></div>
            <p> This is 2nd Page</p>
            <div style='page-break-after: always;'></div>
            <p> This is 3rd Page</p>"
		Dim renderer = New ChromePdfRenderer()
		Dim pdfdoc_a = renderer.RenderHtmlAsPdf(html_a)
		Dim pdfdoc_b = renderer.RenderHtmlAsPdf(html_b)
		Dim merged = PdfDocument.Merge(pdfdoc_a, pdfdoc_b)
		Dim pdfdoc_c = renderer.RenderHtmlAsPdf(html_c)
		merged.AppendPdf(pdfdoc_c)
		merged.SaveAs("CombinedDocument.pdf")
		Console.WriteLine("PDF generated successfully!")
	End Sub
End Class
VB   C#

IronPDF 与 PDFSharpCore 的比较:图 8 - 编辑 PDF 输出

PDFSharpCore 高级功能

PDFSharpCore 虽然更注重基本功能,但仍然提供了执行复杂任务的功能,例如

文档修改:PDFSharpCore 可帮助修改现有的 PDF 文档。这包括添加或删除页面、更新文本和插入图像等任务。

using PdfSharpCore.Pdf;
using PdfSharpCore.Pdf.IO;
// Open an existing PDF document
var inputDocument = PdfReader.Open("input.pdf", PdfDocumentOpenMode.Modify);
// Modify the first page
var page = inputDocument.Pages [0];
var graphics = XGraphics.FromPdfPage(page);
var font = new XFont("OpenSans", 20, XFontStyle.Bold);
graphics.DrawString("Added Text", font, XBrushes.Black, new XPoint(50, 100));
// Save the modified document
inputDocument.Save("modified.pdf");
using PdfSharpCore.Pdf;
using PdfSharpCore.Pdf.IO;
// Open an existing PDF document
var inputDocument = PdfReader.Open("input.pdf", PdfDocumentOpenMode.Modify);
// Modify the first page
var page = inputDocument.Pages [0];
var graphics = XGraphics.FromPdfPage(page);
var font = new XFont("OpenSans", 20, XFontStyle.Bold);
graphics.DrawString("Added Text", font, XBrushes.Black, new XPoint(50, 100));
// Save the modified document
inputDocument.Save("modified.pdf");
Imports PdfSharpCore.Pdf
Imports PdfSharpCore.Pdf.IO
' Open an existing PDF document
Private inputDocument = PdfReader.Open("input.pdf", PdfDocumentOpenMode.Modify)
' Modify the first page
Private page = inputDocument.Pages (0)
Private graphics = XGraphics.FromPdfPage(page)
Private font = New XFont("OpenSans", 20, XFontStyle.Bold)
graphics.DrawString("Added Text", font, XBrushes.Black, New XPoint(50, 100))
' Save the modified document
inputDocument.Save("modified.pdf")
VB   C#

图形和绘图:该库使用与 PDFSharp 相同的绘图例程,使开发人员能够将复杂的图形元素整合到 PDF 文档中。这包括绘制形状、使用不同字体和应用颜色。

using PdfSharpCore.Drawing;
using PdfSharpCore.Pdf;
// Create a new PDF document
var document = new PdfDocument();
var page = document.AddPage();
var graphics = XGraphics.FromPdfPage(page);
// Draw a rectangle
graphics.DrawRectangle(XPens.Black, XBrushes.SkyBlue, 40, 100, 250, 120);
// Draw text
var font = new XFont("Verdana", 20, XFontStyle.BoldItalic);
graphics.DrawString("Hello, PDFSharpCore!", font, XBrushes.Black, new XRect(0, 0, page.Width, page.Height), XStringFormats.Center);
// Save the document
document.Save("drawing.pdf");
using PdfSharpCore.Drawing;
using PdfSharpCore.Pdf;
// Create a new PDF document
var document = new PdfDocument();
var page = document.AddPage();
var graphics = XGraphics.FromPdfPage(page);
// Draw a rectangle
graphics.DrawRectangle(XPens.Black, XBrushes.SkyBlue, 40, 100, 250, 120);
// Draw text
var font = new XFont("Verdana", 20, XFontStyle.BoldItalic);
graphics.DrawString("Hello, PDFSharpCore!", font, XBrushes.Black, new XRect(0, 0, page.Width, page.Height), XStringFormats.Center);
// Save the document
document.Save("drawing.pdf");
Imports PdfSharpCore.Drawing
Imports PdfSharpCore.Pdf
' Create a new PDF document
Private document = New PdfDocument()
Private page = document.AddPage()
Private graphics = XGraphics.FromPdfPage(page)
' Draw a rectangle
graphics.DrawRectangle(XPens.Black, XBrushes.SkyBlue, 40, 100, 250, 120)
' Draw text
Dim font = New XFont("Verdana", 20, XFontStyle.BoldItalic)
graphics.DrawString("Hello, PDFSharpCore!", font, XBrushes.Black, New XRect(0, 0, page.Width, page.Height), XStringFormats.Center)
' Save the document
document.Save("drawing.pdf")
VB   C#

IronPDF 与 PDFSharpCore 的比较:图 9 - 图形输出

从数据生成 PDF:PDFSharpCore 擅长从应用程序数据动态创建 PDF 文档。这对于生成报告、发票或任何需要包含动态数据的文档尤其有益。

using PdfSharpCore.Drawing;
using PdfSharpCore.Pdf;
// Create a new PDF document
var document = new PdfDocument();
var page = document.AddPage();
var graphics = XGraphics.FromPdfPage(page);
var font = new XFont("Arial", 12);
// Simulate generation
var data = new List<string> { "Data 1", "Data 2", "Data 3" };
// Generate PDF from data
var yPos = 20;
foreach (var item in data)
{
    graphics.DrawString(item, font, XBrushes.Black, new XRect(0, yPos, page.Width, page.Height), XStringFormats.TopLeft);
    yPos += 20;
}
// Save the document
document.Save("data-generated.pdf");
using PdfSharpCore.Drawing;
using PdfSharpCore.Pdf;
// Create a new PDF document
var document = new PdfDocument();
var page = document.AddPage();
var graphics = XGraphics.FromPdfPage(page);
var font = new XFont("Arial", 12);
// Simulate generation
var data = new List<string> { "Data 1", "Data 2", "Data 3" };
// Generate PDF from data
var yPos = 20;
foreach (var item in data)
{
    graphics.DrawString(item, font, XBrushes.Black, new XRect(0, yPos, page.Width, page.Height), XStringFormats.TopLeft);
    yPos += 20;
}
// Save the document
document.Save("data-generated.pdf");
Imports PdfSharpCore.Drawing
Imports PdfSharpCore.Pdf
' Create a new PDF document
Private document = New PdfDocument()
Private page = document.AddPage()
Private graphics = XGraphics.FromPdfPage(page)
Private font = New XFont("Arial", 12)
' Simulate generation
Private data = New List(Of String) From {"Data 1", "Data 2", "Data 3"}
' Generate PDF from data
Private yPos = 20
For Each item In data
	graphics.DrawString(item, font, XBrushes.Black, New XRect(0, yPos, page.Width, page.Height), XStringFormats.TopLeft)
	yPos += 20
Next item
' Save the document
document.Save("data-generated.pdf")
VB   C#

结论

IronPDF 与 PDFSharpCore 的比较:图 10 - 许可

在.NET项目中集成 PDF 功能时,IronPDF 和 PDFSharpCore 之间的选择值得慎重考虑,其中 IronPDF 的优势尤为突出,原因有以下几点。IronPDF 以其更广泛的特性和功能而与众不同,尤其适合优先考虑网络应用程序和综合 PDF 处理需求的开发人员。

IronPDF 在易用性和灵活性方面表现出色,能让开发人员毫不费力地将 HTML 转换为 PDF,这对于经常以 HTML 格式生成和展示内容的现代网络应用程序来说是至关重要的功能。此外,IronPDF 还支持编辑、合并、安全和符合 PDF/A 标准等高级 PDF 功能,为管理复杂的 PDF 操作提供了强大的工具包。

IronPDF 的与众不同之处不仅在于其全面的功能,还在于它提供了一个 免费试用,让开发人员无需任何初始投资就能亲身体验其功能。对于那些准备将 IronPDF 集成到其开发工作流程中的开发人员,许可证价格为 $749。

< 前一页
在 C# 中使用 iTextSharp 和 IronPDF 分割 PDF 的比较
下一步 >
IronPDF 与 Apryse C# 的比较

准备开始了吗? 版本: 2024.10 刚刚发布

免费NuGet下载 总下载量: 11,108,738 查看许可证 >