產品比較

PDFSharp HTML 轉換PDF範例與教程比較

發佈 2024年8月11日
分享:

介紹

當涉及到在 .NET 中管理 PDF 文檔時,開發人員可以選擇各種庫,每個庫都提供獨特的功能和能力。在這些庫中, IronPDF 和 PDFsharp 脫穎而出,滿足不同的需求和偏好。

在本文中,我們將仔細研究這些程式庫提供的各種功能,以及它們各自的支援、價格、文件和相容性。

IronPDF 和 PDFsharp 概述

IronPDF 是一個強大的 .NET PDF 庫,使開發人員可以輕鬆地建立、編輯和提取 PDF 內容。用戶可以使用 IronPDF 將 HTML、ASPX、MVC 和圖片等各種格式轉換成 PDF 文件。 IronPDF 具有多種功能,例如文本編輯、圖片處理和元數據更改、水印和加密 PDF 文件等。它以易於使用的界面以及令人印象深刻的功能集而聞名,使開發人員更輕鬆地處理 PDF 文檔。

PDFsharp 是一個基於 .NET 的開源庫,專注於動態生成 PDF 文檔。該產品完全用 C# 實現,支持繪製文本、圖形和圖像。它還允許進行合併或拆分現有 PDF 文件等修改操作。對於希望找到一個簡單操作 PDF 文件而不是帶有許多附加功能的基本庫的人來說,PDFsharp 是一個不錯的選擇。

跨平台兼容性

IronPDF 和 PDFsharp 都在 .NET 環境中運行,從 6.1 版開始,PDFsharp 支持 .NET Framework 4.7.2 或更高版本、.NET Standard 2.0 和 .NET 6 或更高版本,並且可以在 Linux 和 macOS 等 .NET 兼容平台以及 Windows 上運行。

IronPDF 支持最新的平台相容性與各種 .NET 版本,允許開發人員在 Windows、Linux、Mac、Azure、Docker 和 AWS 等應用環境中工作,從而確保與眾多作業系統和開發環境的通用相容性。

IronPDF:

  • .NET 版本: (C#, VB.NET, F#)

    • .NET Core (8, 7, 6, 5, 和 3.1+)

    • .NET標準 (2.0+)
  • .NET Framework (4.6.2+)
  • .NET 專案類型: 適用於 Blazor、MAUI、WPF、控制台應用程式等專案類型
  • 應用程式環境: IronPDF 在 Windows、Linux、Mac、Docker、Azure 和 AWS 應用程式環境中運行
  • IDE: 適用於 Microsoft Visual Studio 和 JetBrains Rider & ReSharper 等 IDE

PDFsharp

  • .NET版本:

    • .NET Framework (4.7.2+)

    • .NET標準 (2.0+)
  • .NET 6+
  • .NET 專案類型: 可以與 Blazor、MAUI 和 ASP.NET 等專案類型和技術一起使用
  • 應用環境: PDFsharp 支援在 Windows、macOS 和 Linux 上運行的專案

功能比較概覽:IronPDF 與 PDFsharp

IronPDF 提供多種處理 PDF 文件的功能,例如生成、修改和提取 PDF 內容,將 HTML、ASPX、MVC 和圖片轉換為 PDF 格式以及編輯文本、圖片和元數據;在 PDF 頁面上添加浮水印或加密 PDF 文件。

另一方面,PDFsharp 專注於創建和操作 PFD 文件;它允許您在頁面上畫出文本行或段落,或者在頁面上插入矩形和圓形等圖形。

IronPDF

  • HTML 轉換為 PDF:IronPDF 支援現代網頁標準 (CSS3, HTML5, JavaScript),可以創建高保真PDF文件。
  • PDF加密:用戶可以利用IronPDF的強大加密工具來加密和解密PDF文件,為其PDF文件添加一層額外的安全性。
  • PDF編輯:IronPDF庫包含合併、拆分、格式化和修改現有PDF文件的功能。
  • PDF文件的數字簽名:IronPDF允許用戶數字簽署他們的PDF文件。
  • 水印:輕鬆將文本和圖像水印應用到PDF文件;利用HTML/CSS充分控制整個過程。
  • PDF蓋章:使用IronPDF將圖像和文本蓋章到您的PDF文件中。

PDFsharp:

  • 創建PDF: PDFsharp可以從任何.NET語言創建和生成PDF文件
  • PDF操作: 修改、合併和拆分PDF
  • 在您的PDF文件上繪製圖形: PDFsharp提供了在PDF文檔上繪製形狀、線條和圖像的功能
  • 加密: 使用PDFsharp的加密工具加密您的PDF文件

IronPDF 與 PDFsharp 之間功能的比較

將HTML轉換為PDF

在各種工作場所和環境中,轉換 HTML轉PDF 是一個簡單但必須的任務;在這個過程中,IronPDF 和 PDFsharp 之間的比較在下面的代碼示例中說明。

IronPDF:

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.
var myAdvancedPdf = renderer.RenderHtmlAsPdf("<img src='icons/iron.png'>", @"C:\site\assets\");
myAdvancedPdf.SaveAs("html-with-assets.pdf");
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.
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.
Dim myAdvancedPdf = renderer.RenderHtmlAsPdf("<img src='icons/iron.png'>", "C:\site\assets\")
myAdvancedPdf.SaveAs("html-with-assets.pdf")
VB   C#

PDFsharp:

PDFsharp 本身無法處理 HTML 到 PDF 的轉換;相反,它需要 HtmlRenderer.PdfSharp 庫來執行此任務。這個 HTML 渲染器可以使用靜態渲染代碼處理 HTML 到 PDF 的轉換。

using System.IO;
using PdfSharp.Pdf;
using TheArtOfDev.HtmlRenderer.PdfSharp;

static void Main(string[] args)
{
    string html = @"<h1>Hello World</h1>";
    PdfDocument pdf = PdfGenerator.GeneratePdf(html, PageSize.A4);
    const string filename = "HtmlToPdfExample.pdf";
    pdf.Save(filename);
}
using System.IO;
using PdfSharp.Pdf;
using TheArtOfDev.HtmlRenderer.PdfSharp;

static void Main(string[] args)
{
    string html = @"<h1>Hello World</h1>";
    PdfDocument pdf = PdfGenerator.GeneratePdf(html, PageSize.A4);
    const string filename = "HtmlToPdfExample.pdf";
    pdf.Save(filename);
}
Imports System.IO
Imports PdfSharp.Pdf
Imports TheArtOfDev.HtmlRenderer.PdfSharp

Shared Sub Main(ByVal args() As String)
	Dim html As String = "<h1>Hello World</h1>"
	Dim pdf As PdfDocument = PdfGenerator.GeneratePdf(html, PageSize.A4)
	Const filename As String = "HtmlToPdfExample.pdf"
	pdf.Save(filename)
End Sub
VB   C#

IronPDF 提供了一種方便且簡單的方法,將 HTML 代碼、HTML 片段或 HTML 模板轉換為 PDF 格式,同時允許用戶擁有大量的控制權。IronPDF 對現代網頁標準有良好的支持,能夠創建與源網頁一致的 PDF 文件。

另一方面,PDFsharp 目前不支持 HTML 轉 PDF 轉換,也無法解析 HTML 文件;相反,可以搭配 HtmlRenderer.PdfSharp 庫使用 PDFsharp 來完成此任務。

在 PDF 文件中編輯文本

有時用戶可能希望 編輯 部分 PDF 文件的內容,特別是在處理私密或敏感資訊時。

IronPDF:

using IronPdf;

PdfDocument pdf = PdfDocument.FromFile("novel.pdf");

// Redact the 'are' phrase from all pages
pdf.RedactTextOnAllPages("are");

pdf.SaveAs("redacted.pdf");
using IronPdf;

PdfDocument pdf = PdfDocument.FromFile("novel.pdf");

// Redact the 'are' phrase from all pages
pdf.RedactTextOnAllPages("are");

pdf.SaveAs("redacted.pdf");
Imports IronPdf

Private pdf As PdfDocument = PdfDocument.FromFile("novel.pdf")

' Redact the 'are' phrase from all pages
pdf.RedactTextOnAllPages("are")

pdf.SaveAs("redacted.pdf")
VB   C#

PDFsharp:

PDFsharp目前不提供任何內建功能來修訂PDF內容;然而,由於修訂涉及永久移除或隱藏敏感信息,您可以嘗試利用PDFsharp的圖形繪製工具手動覆蓋您希望修訂的內容。以下是一個如何實現此目的的範例:

using System;
using PdfSharp.Pdf;
using PdfSharp.Drawing;

namespace PdfRedactionExample
{
    class Program
    {
        static void Main(string[] args)
        {
            // Load an existing PDF document
            PdfDocument document = PdfReader.Open("input.pdf", PdfDocumentOpenMode.Modify);
            // Get the first page of the document
            PdfPage page = document.Pages[0];
            // Create a graphics object to write on the page
            XGraphics gfx = XGraphics.FromPdfPage(page);
            // Define the area to redact (for example, a rectangle covering text)
            XRect redactRect = new XRect(100, 100, 200, 50); // Adjust coordinates and size as needed
            // Fill the redaction area with a black rectangle
            gfx.DrawRectangle(XBrushes.Black, redactRect);
            // Save the modified document
            document.Save("output.pdf");
            // Optionally, close the document
            document.Close();
        }
    }
}
using System;
using PdfSharp.Pdf;
using PdfSharp.Drawing;

namespace PdfRedactionExample
{
    class Program
    {
        static void Main(string[] args)
        {
            // Load an existing PDF document
            PdfDocument document = PdfReader.Open("input.pdf", PdfDocumentOpenMode.Modify);
            // Get the first page of the document
            PdfPage page = document.Pages[0];
            // Create a graphics object to write on the page
            XGraphics gfx = XGraphics.FromPdfPage(page);
            // Define the area to redact (for example, a rectangle covering text)
            XRect redactRect = new XRect(100, 100, 200, 50); // Adjust coordinates and size as needed
            // Fill the redaction area with a black rectangle
            gfx.DrawRectangle(XBrushes.Black, redactRect);
            // Save the modified document
            document.Save("output.pdf");
            // Optionally, close the document
            document.Close();
        }
    }
}
Imports System
Imports PdfSharp.Pdf
Imports PdfSharp.Drawing

Namespace PdfRedactionExample
	Friend Class Program
		Shared Sub Main(ByVal args() As String)
			' Load an existing PDF document
			Dim document As PdfDocument = PdfReader.Open("input.pdf", PdfDocumentOpenMode.Modify)
			' Get the first page of the document
			Dim page As PdfPage = document.Pages(0)
			' Create a graphics object to write on the page
			Dim gfx As XGraphics = XGraphics.FromPdfPage(page)
			' Define the area to redact (for example, a rectangle covering text)
			Dim redactRect As New XRect(100, 100, 200, 50) ' Adjust coordinates and size as needed
			' Fill the redaction area with a black rectangle
			gfx.DrawRectangle(XBrushes.Black, redactRect)
			' Save the modified document
			document.Save("output.pdf")
			' Optionally, close the document
			document.Close()
		End Sub
	End Class
End Namespace
VB   C#

在 PDF 文件內容的編輯方面,IronPDF 提供了一種直接從 PDF 文件中編輯文本的簡單方法。使用者可以輕鬆地指定需要編輯的文本,並且其直觀且簡潔的 API 允許快速、高效地完成此任務。

另一方面,PDFsharp 目前不支持內建的編輯功能。然而,使用者可以利用其圖形繪製功能手動覆蓋內容進行編輯,儘管這種方法需要更多的手動操作和自定義。

數位簽署PDF檔案

如果您需要 簽署 PDF 文件 數位方式處理可能會非常耗時。節省時間的一個方法是以程式化方式來進行。在以下的程式碼範例中,我們將比較 IronPDF 與 PDFsharp 之間的文件簽署方式。

IronPDF:

using IronPdf;
using IronPdf.Signing;
using System.Security.Cryptography.X509Certificates;

// Create X509Certificate2 object with X509KeyStorageFlags set to Exportable
X509Certificate2 cert = new X509Certificate2("IronSoftware.pfx", "123456", X509KeyStorageFlags.Exportable);

// Create PdfSignature object
var sig = new PdfSignature(cert);

// Sign PDF document
PdfDocument pdf = PdfDocument.FromFile("document.pdf");
pdf.Sign(sig);
pdf.SaveAs("signed.pdf");
using IronPdf;
using IronPdf.Signing;
using System.Security.Cryptography.X509Certificates;

// Create X509Certificate2 object with X509KeyStorageFlags set to Exportable
X509Certificate2 cert = new X509Certificate2("IronSoftware.pfx", "123456", X509KeyStorageFlags.Exportable);

// Create PdfSignature object
var sig = new PdfSignature(cert);

// Sign PDF document
PdfDocument pdf = PdfDocument.FromFile("document.pdf");
pdf.Sign(sig);
pdf.SaveAs("signed.pdf");
Imports IronPdf
Imports IronPdf.Signing
Imports System.Security.Cryptography.X509Certificates

' Create X509Certificate2 object with X509KeyStorageFlags set to Exportable
Private cert As New X509Certificate2("IronSoftware.pfx", "123456", X509KeyStorageFlags.Exportable)

' Create PdfSignature object
Private sig = New PdfSignature(cert)

' Sign PDF document
Private pdf As PdfDocument = PdfDocument.FromFile("document.pdf")
pdf.Sign(sig)
pdf.SaveAs("signed.pdf")
VB   C#

PDFsharp:

PDFsharp 不支援內建的 PDF 檔案數位簽名,而是依賴如 iTextSharp 等外部庫來完成這個任務。

程式化地對 PDF 文件進行數位簽名可以大大簡化工作流程。IronPDF 內建支援數位簽名,使得過程簡單明瞭,只需幾行程式碼即可完成任務。然而,PDFsharp 沒有內建的數位簽名功能,因此需要像 iTextSharp 這樣的第三方庫,這會使實現更加複雜。

為 PDF 添加浮水印

在處理私人文件、版權保護、品牌或任何涉及敏感記錄的任務時,能夠添加和個性化浮水印是非常重要的。 浮水印 在 PDF 檔案上添加浮水印可以非常有用。在本文中,我們比較了 IronPDF 和 PDFsharp 添加浮水印到 PDF 文件的效果。

IronPDF:

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");
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")
VB   C#

PDFsharp:

using PdfSharp.Pdf;
using PdfSharp.Drawing;

PdfDocument document = PdfReader.Open("Input.pdf", PdfDocumentOpenMode.Modify);
XGraphics gfx = XGraphics.FromPdfPage(document.Pages[0]);
XFont font = new XFont("Verdana", 40, XFontStyle.BoldItalic);
XBrush brush = new XSolidBrush(XColor.FromArgb(128, 255, 0, 0));
string watermarkText = "SAMPLE";
XSize textSize = gfx.MeasureString(watermarkText, font);
XPoint center = new XPoint((document.Pages[0].Width - textSize.Width) / 2, (document.Pages[0].Height - textSize.Height) / 2);
gfx.DrawString(watermarkText, font, brush, center, XStringFormats.Center);

// Save the document
document.Save("Watermarked.pdf");
using PdfSharp.Pdf;
using PdfSharp.Drawing;

PdfDocument document = PdfReader.Open("Input.pdf", PdfDocumentOpenMode.Modify);
XGraphics gfx = XGraphics.FromPdfPage(document.Pages[0]);
XFont font = new XFont("Verdana", 40, XFontStyle.BoldItalic);
XBrush brush = new XSolidBrush(XColor.FromArgb(128, 255, 0, 0));
string watermarkText = "SAMPLE";
XSize textSize = gfx.MeasureString(watermarkText, font);
XPoint center = new XPoint((document.Pages[0].Width - textSize.Width) / 2, (document.Pages[0].Height - textSize.Height) / 2);
gfx.DrawString(watermarkText, font, brush, center, XStringFormats.Center);

// Save the document
document.Save("Watermarked.pdf");
Imports PdfSharp.Pdf
Imports PdfSharp.Drawing

Private document As PdfDocument = PdfReader.Open("Input.pdf", PdfDocumentOpenMode.Modify)
Private gfx As XGraphics = XGraphics.FromPdfPage(document.Pages(0))
Private font As New XFont("Verdana", 40, XFontStyle.BoldItalic)
Private brush As XBrush = New XSolidBrush(XColor.FromArgb(128, 255, 0, 0))
Private watermarkText As String = "SAMPLE"
Private textSize As XSize = gfx.MeasureString(watermarkText, font)
Private center As New XPoint((document.Pages(0).Width - textSize.Width) \ 2, (document.Pages(0).Height - textSize.Height) \ 2)
gfx.DrawString(watermarkText, font, brush, center, XStringFormats.Center)

' Save the document
document.Save("Watermarked.pdf")
VB   C#

將浮水印添加到 PDF 檔案中,對於涉及隱私、版權保護和品牌的任務至關重要。IronPDF 提供了一個簡單的 API,用戶可以使用 HTML/CSS 高效地應用自訂浮水印,提供靈活性和易於實現的同時,還賦予用戶對過程的充分控制。另一方面,PDFsharp 需要更多的手動操作,利用圖形和文本繪製方法將浮水印疊加到 PDF 頁面上。

DOCX 轉換成 PDF 檔案

將不同類型的檔案轉換成 PDF 在建立 PDF 檔案時是至關重要的。在這個例子中,我們將集中在 DOCX 文件格式 並比較 IronPDF 和 PDFsharp 如何完成此任務。

IronPDF:

using IronPdf;

// Instantiate Renderer
DocxToPdfRenderer renderer = new DocxToPdfRenderer();

// Render from DOCX file
PdfDocument pdf = renderer.RenderDocxAsPdf("Modern-chronological-resume.docx");

// Save the PDF
pdf.SaveAs("pdfFromDocx.pdf");
using IronPdf;

// Instantiate Renderer
DocxToPdfRenderer renderer = new DocxToPdfRenderer();

// Render from DOCX file
PdfDocument pdf = renderer.RenderDocxAsPdf("Modern-chronological-resume.docx");

// Save the PDF
pdf.SaveAs("pdfFromDocx.pdf");
Imports IronPdf

' Instantiate Renderer
Private renderer As New DocxToPdfRenderer()

' Render from DOCX file
Private pdf As PdfDocument = renderer.RenderDocxAsPdf("Modern-chronological-resume.docx")

' Save the PDF
pdf.SaveAs("pdfFromDocx.pdf")
VB   C#

PDFsharp:

PDFsharp 目前不支持直接將 DOCX 轉換為 PDF 檔案格式。相反地,如果用戶希望進行此任務,則必須使用諸如 DocX 等外部庫。

在 DOCX 轉 PDF 轉換方面,IronPDF 為此任務提供了一種簡單而簡潔的方法,透過其 DocxToPdfRenderer 直接支持 DOCX 轉 PDF 轉換。這允許無縫渲染並儲存生成的 PDF。

PDFsharp 本身不支援 DOCX 轉 PDF 轉換;用戶必須依賴諸如 DocX 等外部庫來實現這一功能。

在 PDF 上加蓋圖像和文字

有時,如在應用浮水印時,PDF 頁面可能需要被 已加章 帶有某些內容。在本節中,我們將檢查IronPDF與PDFsharp在PDF文件上蓋章的功能。

IronPDF:

using IronPdf;
using IronPdf.Editing;

ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Example HTML Document!</h1>");

// Create text stamper
TextStamper textStamper = new TextStamper()
{
    Text = "Text Stamper!",
    FontFamily = "Bungee Spice",
    UseGoogleFont = true,
    FontSize = 30,
    IsBold = true,
    IsItalic = true,
    VerticalAlignment = VerticalAlignment.Top,
};

// Stamp the text stamper
pdf.ApplyStamp(textStamper);
pdf.SaveAs("stampText.pdf");

// Create image stamper
ImageStamper imageStamper = new ImageStamper(new Uri("https://ironpdf.com/img/svgs/iron-pdf-logo.svg"))
{
    VerticalAlignment = VerticalAlignment.Top,
};

// Stamp the image stamper
pdf.ApplyStamp(imageStamper, 0);
pdf.SaveAs("stampImage.pdf");
using IronPdf;
using IronPdf.Editing;

ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Example HTML Document!</h1>");

// Create text stamper
TextStamper textStamper = new TextStamper()
{
    Text = "Text Stamper!",
    FontFamily = "Bungee Spice",
    UseGoogleFont = true,
    FontSize = 30,
    IsBold = true,
    IsItalic = true,
    VerticalAlignment = VerticalAlignment.Top,
};

// Stamp the text stamper
pdf.ApplyStamp(textStamper);
pdf.SaveAs("stampText.pdf");

// Create image stamper
ImageStamper imageStamper = new ImageStamper(new Uri("https://ironpdf.com/img/svgs/iron-pdf-logo.svg"))
{
    VerticalAlignment = VerticalAlignment.Top,
};

// Stamp the image stamper
pdf.ApplyStamp(imageStamper, 0);
pdf.SaveAs("stampImage.pdf");
Imports IronPdf
Imports IronPdf.Editing

Private renderer As New ChromePdfRenderer()
Private pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Example HTML Document!</h1>")

' Create text stamper
Private textStamper As New TextStamper() With {
	.Text = "Text Stamper!",
	.FontFamily = "Bungee Spice",
	.UseGoogleFont = True,
	.FontSize = 30,
	.IsBold = True,
	.IsItalic = True,
	.VerticalAlignment = VerticalAlignment.Top
}

' Stamp the text stamper
pdf.ApplyStamp(textStamper)
pdf.SaveAs("stampText.pdf")

' Create image stamper
Dim imageStamper As New ImageStamper(New Uri("https://ironpdf.com/img/svgs/iron-pdf-logo.svg")) With {.VerticalAlignment = VerticalAlignment.Top}

' Stamp the image stamper
pdf.ApplyStamp(imageStamper, 0)
pdf.SaveAs("stampImage.pdf")
VB   C#

PDFsharp:

using PdfSharp.Drawing;
using PdfSharp.Pdf;
using System.Drawing;

// Load an existing PDF document
PdfDocument document = PdfReader.Open("Input.pdf", PdfDocumentOpenMode.Modify);
// Iterate through each page in the document
 foreach (PdfPage page in document.Pages)
 {
    // Get an XGraphics object for drawing on the page
    XGraphics gfx = XGraphics.FromPdfPage(page);

    // Define a font and brush for stamping text
    XFont font = new XFont("Arial", 12);
    XBrush brush = XBrushes.Red;

     // Stamp text onto the page
     gfx.DrawString("Confidential", font, brush, 50, 50);

     // Stamp an image onto the page
     XImage image = XImage.FromFile("stamp.png"); // Replace with your image path
     gfx.DrawImage(image, 100, 100, image.PixelWidth / 2, image.PixelHeight / 2);
}

document.Save("StampedOutput.pdf");
using PdfSharp.Drawing;
using PdfSharp.Pdf;
using System.Drawing;

// Load an existing PDF document
PdfDocument document = PdfReader.Open("Input.pdf", PdfDocumentOpenMode.Modify);
// Iterate through each page in the document
 foreach (PdfPage page in document.Pages)
 {
    // Get an XGraphics object for drawing on the page
    XGraphics gfx = XGraphics.FromPdfPage(page);

    // Define a font and brush for stamping text
    XFont font = new XFont("Arial", 12);
    XBrush brush = XBrushes.Red;

     // Stamp text onto the page
     gfx.DrawString("Confidential", font, brush, 50, 50);

     // Stamp an image onto the page
     XImage image = XImage.FromFile("stamp.png"); // Replace with your image path
     gfx.DrawImage(image, 100, 100, image.PixelWidth / 2, image.PixelHeight / 2);
}

document.Save("StampedOutput.pdf");
Imports PdfSharp.Drawing
Imports PdfSharp.Pdf
Imports System.Drawing

' Load an existing PDF document
Private document As PdfDocument = PdfReader.Open("Input.pdf", PdfDocumentOpenMode.Modify)
' Iterate through each page in the document
 For Each page As PdfPage In document.Pages
	' Get an XGraphics object for drawing on the page
	Dim gfx As XGraphics = XGraphics.FromPdfPage(page)

	' Define a font and brush for stamping text
	Dim font As New XFont("Arial", 12)
	Dim brush As XBrush = XBrushes.Red

	 ' Stamp text onto the page
	 gfx.DrawString("Confidential", font, brush, 50, 50)

	 ' Stamp an image onto the page
	 Dim image As XImage = XImage.FromFile("stamp.png") ' Replace with your image path
'INSTANT VB WARNING: Instant VB cannot determine whether both operands of this division are integer types - if they are then you should use the VB integer division operator:
	 gfx.DrawImage(image, 100, 100, image.PixelWidth / 2, image.PixelHeight / 2)
 Next page

document.Save("StampedOutput.pdf")
VB   C#

IronPDF 使用戶能夠輕鬆地向 PDF 文件添加文本和圖像,並使整個過程保持簡單明瞭。它讓用戶可以完全掌控過程的各個方面,特別是對那些熟悉 HTML/CSS 的用戶。

另一方面,PDFsharp 需要用戶更多地手動操作其 XGraphics 對象,其中文本和圖像使用特定的坐標和圖形參數直接印在每個頁面上。

加密 PDF 文件

無論是在辦公室、學校還是圖書館, 加密和解密 PDF 文件是许多工作环境的一部分。因此,拥有一个能够无障碍执行此功能的程序可能是必不可少的。 本节将比较 IronPDF 和 PDFsharp 如何处理 PDF 加密任务。

IronPDF:

using IronPdf;
using System;

// Open an Encrypted File, alternatively create a new PDF from Html
var pdf = PdfDocument.FromFile("encrypted.pdf", "password");

// Edit file metadata
pdf.MetaData.Author = "Satoshi Nakamoto";
pdf.MetaData.Keywords = "SEO, Friendly";
pdf.MetaData.ModifiedDate = DateTime.Now;

// Edit file security settings
// 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.Password = "my-password";
pdf.SaveAs("secured.pdf");
using IronPdf;
using System;

// Open an Encrypted File, alternatively create a new PDF from Html
var pdf = PdfDocument.FromFile("encrypted.pdf", "password");

// Edit file metadata
pdf.MetaData.Author = "Satoshi Nakamoto";
pdf.MetaData.Keywords = "SEO, Friendly";
pdf.MetaData.ModifiedDate = DateTime.Now;

// Edit file security settings
// 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.Password = "my-password";
pdf.SaveAs("secured.pdf");
Imports IronPdf
Imports System

' Open an Encrypted File, alternatively create a new PDF from Html
Private pdf = PdfDocument.FromFile("encrypted.pdf", "password")

' Edit file metadata
pdf.MetaData.Author = "Satoshi Nakamoto"
pdf.MetaData.Keywords = "SEO, Friendly"
pdf.MetaData.ModifiedDate = DateTime.Now

' Edit file security settings
' 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.Password = "my-password"
pdf.SaveAs("secured.pdf")
VB   C#

PDFsharp:

PDFsharp 本身不支持 PDF 加密。然而,用戶可以安裝 PdfSharp.Pdf.IO 庫來協助完成此任務。

using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
using PdfSharp.Pdf.Security;

PdfDocument document = PdfReader.Open("Input.pdf", PdfDocumentOpenMode.Modify);
string userPassword = "userpassword";
string ownerPassword = "ownerpassword";

PdfSecuritySettings securitySettings = document.SecuritySettings;
securitySettings.DocumentSecurityLevel = PdfDocumentSecurityLevel.Encrypted128Bit;
securitySettings.UserPassword = userPassword;
securitySettings.OwnerPassword = ownerPassword;
securitySettings.PermitAccessibilityExtractContent = false;
securitySettings.PermitAnnotationsAndFieldsEdit = false;
securitySettings.PermitAssembleDocument = false;
securitySettings.PermitExtractContent = false;
securitySettings.PermitFormsFill = true;
securitySettings.PermitFullQualityPrint = true;
securitySettings.PermitModifyDocument = true;
securitySettings.PermitPrint = true;

document.Save("EncryptedOutput.pdf");
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
using PdfSharp.Pdf.Security;

PdfDocument document = PdfReader.Open("Input.pdf", PdfDocumentOpenMode.Modify);
string userPassword = "userpassword";
string ownerPassword = "ownerpassword";

PdfSecuritySettings securitySettings = document.SecuritySettings;
securitySettings.DocumentSecurityLevel = PdfDocumentSecurityLevel.Encrypted128Bit;
securitySettings.UserPassword = userPassword;
securitySettings.OwnerPassword = ownerPassword;
securitySettings.PermitAccessibilityExtractContent = false;
securitySettings.PermitAnnotationsAndFieldsEdit = false;
securitySettings.PermitAssembleDocument = false;
securitySettings.PermitExtractContent = false;
securitySettings.PermitFormsFill = true;
securitySettings.PermitFullQualityPrint = true;
securitySettings.PermitModifyDocument = true;
securitySettings.PermitPrint = true;

document.Save("EncryptedOutput.pdf");
Imports PdfSharp.Pdf
Imports PdfSharp.Pdf.IO
Imports PdfSharp.Pdf.Security

Private document As PdfDocument = PdfReader.Open("Input.pdf", PdfDocumentOpenMode.Modify)
Private userPassword As String = "userpassword"
Private ownerPassword As String = "ownerpassword"

Private securitySettings As PdfSecuritySettings = document.SecuritySettings
securitySettings.DocumentSecurityLevel = PdfDocumentSecurityLevel.Encrypted128Bit
securitySettings.UserPassword = userPassword
securitySettings.OwnerPassword = ownerPassword
securitySettings.PermitAccessibilityExtractContent = False
securitySettings.PermitAnnotationsAndFieldsEdit = False
securitySettings.PermitAssembleDocument = False
securitySettings.PermitExtractContent = False
securitySettings.PermitFormsFill = True
securitySettings.PermitFullQualityPrint = True
securitySettings.PermitModifyDocument = True
securitySettings.PermitPrint = True

document.Save("EncryptedOutput.pdf")
VB   C#

IronPDF 是一款具有強大加密功能的工具,可讓您輕鬆地加密PDF並管理安全設置,如唯讀訪問、複製粘貼限制和打印許可權。您還可以為加密文件添加密碼保護並自定義元數據;總之,IronPDF 是一個全方位的安全PDF管理解決方案。

PDFsharp 沒有任何原生的加密支持,但它可以使用第三方庫,如 PdfSharp.Pdf.IO 來執行這項功能。您可以設置不同級別的加密、創建用戶或所有者密碼,並定義打印權限或內容提取等其他文檔操作的許可權。

程式範例比較摘要

價格和授權:IronPDF vs. PDFsharp

IronPDF 價格與授權

IronPDF 具有不同层级和额外功能的许可证可供购买。开发人员还可以购买 IronSuite ,讓您以兩個產品的價格獲得所有IronSoftware的產品。如果您還沒準備購買授權,IronPDF提供 免費試用 30天。

  • 永久授權:根據團隊規模、專案需求和地點數量,提供一系列永久授權。每種類型的授權都會附帶電子郵件支援。
  • Lite License:此授權費用為$749美元,支援一名開發人員、一個地點和一個專案。
  • Plus License:支援三名開發人員、三個地點和三個專案,這是Lite License的升級版,費用為$1,499美元。Plus License除了基本的電子郵件支援外,還提供聊天支援和電話支援。
  • Professional License:此授權適合較大型團隊,支援十名開發人員、十個地點和十個專案,費用為$2,999美元。它提供與前一級相同的聯絡支援渠道,還包括螢幕分享支援。
  • 免版稅再分發:IronPDF的授權也提供額外費用$1,999美元的免版稅再分發保障。
  • 不中斷的產品支援:IronPDF提供持續的產品更新、安全功能升級,以及其工程團隊的支援,年費為$999美元或者一次性購價$1,999美元,可享受5年保障。
  • IronSuite:費用為$1,498美元,可獲得所有Iron Software產品的訪問權,包括IronPDF, IronOCR, IronWord, IronXL, IronBarcode, IronQR, IronZIP, IronPrint和IronWebScraper。

PDFsharp 授權

PDFsharp 是一個開源庫,可以免費使用。然而,這帶來的代價是它的功能比 IronPDF 等競爭對手少得多且不夠先進。

文件和支援:IronPDF 與 PDFsharp 的比較

IronPDF 提供詳盡的文件和支援選項,確保開發人員可以輕鬆整合並使用其功能。

IronPDF

  • 全方位文件: 詳盡且易於使用的文件,涵蓋所有功能。
  • 24/5 支援: 提供有效的工程師支援。
  • 影片教學: YouTube 上提供逐步說明的影片指南。
  • 社群論壇: 活躍的社群可提供額外支援。
  • 定期更新: 每月產品更新,確保最新功能和安全性修補程式。

PDFsharp

  • 文件:基本文件可在網站上獲得。
  • 論壇:通過論壇提供社區驅動的支援。
  • 支援:沒有商業支援選項,但有一個報告問題的存儲庫。

欲了解更多IronPDF文件和支援的詳情,請訪問 IronPDF 文件檔案 和該 IronSoftware YouTube 頻道.

結論

總之,當在 .NET 環境中選擇 IronPDF 和 PDFsharp 進行 PDF 文件管理時,開發人員應考慮其具體需求和專案要求。IronPDF 以其廣泛的功能集脫穎而出,包括 HTML 轉 PDF、加密功能以及對各種 .NET 平臺和開發環境的強大支援。它提供了全面的文件說明、多个支援渠道以及靈活的授權選項,儘管需要付費,可以通過 NuGet 套件管理器輕鬆安裝到您的專案中。

另一方面,PDFsharp 作為開源庫,可免費使用,但缺乏像 HTML 轉 PDF 和 PDF 加密的高級功能。它可能需要額外的庫並且缺乏商業支援選項,主要適合於基本的 PDF 操作任务。

< 上一頁
SpirePDF C# HTML轉PDF教學與庫比較
下一個 >
Accusoft Prizmdoc PDF Viewer 教程和比較

準備開始了嗎? 版本: 2024.10 剛剛發布

免費 NuGet 下載 總下載次數: 10,993,239 查看許可證 >