跳過到頁腳內容
產品比較

IronPDF vs iTextPdf: Complete .NET PDF Library Comparison

IronPDF 的優點在於其基於 Chromium 的 HTML 渲染引擎,提供完整的 CSS3 和 JavaScript 支援以及內建的 DOCX 轉換功能。 相較之下,iTextPdf 提供高級企業功能,採用 AGPL 或商業許可,但需要外部相依性才能實現現代 Web 標準和文件轉換。

對於從事 PDF 開發的人員來說,擁有一個可靠的 PDF 生成和操作庫至關重要。 在 .NET 生態系統中,有兩個流行的 PDF 庫脫穎而出——IronPDFiTextPdf——它們各自提供了創建、編輯和管理 PDF 文件的有效工具。 本文從功能、文件品質和定價策略等方面對這些函式庫進行了深入比較。

IronPDF 和 iTextPdf 是什麼?

IronPDF 如何簡化 PDF 開發?

IronPDF 是一個可靠的 .NET PDF 管理庫,相容於各種 .NET 環境(Core 8、7、6、Framework 等)。 它提供了一套完整的功能,包括HTML 轉 PDF合併 PDF加密和數位簽章。 IronPDF 的文件簡單明了,使用者可以獲得可靠的技術支援。 開發者經常透過 Stack Overflow 討論和其他原始碼共享平台找到常見問題的解決方案。 該程式庫提供API 參考,用於提供詳細的實作指導,並支援透過 NuGet 套件進行進階安裝方法。 為了快速實施,請查閱快速入門指南安裝概述,以便迅速上手。

iTextPdf為何適合企業應用?

iTextPdf 是一個 Java 和 .NET (C#) 的高階 PDF 函式庫,專注於企業級文件處理。 它既可在 AGPL 許可下使用,也可在商業許可下使用,可靈活滿足各種專案需求。 iTextPdf 具有高度可自訂性,使其成為複雜 PDF 任務的理想選擇,例如文件加密、數位簽章和表單建立。 該庫一直是企業環境中必不可少的工具,這些企業需要強大的 PDF 處理功能,並符合PDF/A 標準以進行存檔。 需要符合第 508 條無障礙標準的組織通常會選擇支援通用無障礙標準的企業級解決方案。

IronPDF 和 iTextPdf 支援哪些平台?

IronPDF 和 iTextPdf 都支援跨平台功能,因此可以靈活地滿足 .NET 中各種應用程式的需求。 以下是各庫相容性的詳細說明。

  • .NET 版本:相容於 .NET Core (8, 7, 6, 5, 3.1+)、.NET Standard (2.0+) 和 .NET Framework (4.6.2+)。 -應用程式環境:可在 Windows、Linux、Mac、Docker、Azure 和 AWS 環境中無縫運作。 -支援的 IDE:與 Microsoft Visual Studio 和 JetBrains Rider & ReSharper 配合得很好。 -作業系統與處理器:支援 Windows、Mac、Linux、x64、x86、ARM。

  • .NET 版本:相容於 .NET Core 8、7、6、5、3.1+,.NET Standard 2.0+,.NET Framework 4.6.2+ -應用程式環境:可在 Windows、Linux、Mac、Docker、Azure 和 AWS 上執行 支援的整合開發環境:Microsoft Visual Studio 和JetBrains Rider & ReSharper 作業系統與處理器:Windows、Mac、Linux、x64、x86、ARM -行動平台透過 MAUI 支援 Android -容器支援:可擴充架構的遠端容器部署

  • .NET 版本:支援 .NET Core (2.x, 3.x)、.NET Framework (4.6.1+) 和 .NET 5+。 -應用環境:相容於 Windows、macOS、Linux 和 Docker。

iTextPdf 可以部署在哪些地方?

  • .NET 版本:支援 .NET Core 2.x、3.x、.NET Framework 4.6.1+ 和 .NET 5+ -應用環境:相容於 Windows、macOS、Linux 和 Docker -企業部署:針對採用 AGPL 或商業授權的伺服器環境進行了最佳化

IronPDF 與 iTextPdf 的主要特色有哪些?

以下是各庫主要功能的詳細比較。

IronPDF提供哪些功能?

  • HTML 轉 PDF 轉換:支援 HTML、CSS、JavaScript 和圖片。
  • PDF 處理:拆分、合併和編輯 PDF 文件。 -安全性:具備 PDF 加密和解密功能。 -編輯:允許新增註解、書籤和大綱。 -範本:套用頁首、頁尾和頁碼。 -浮水印:支援使用 HTML/CSS 控制的文字和圖像浮水印。
  • PDF 加蓋圖章:在 PDF 檔案上新增圖像和文字印章。

有關 IronPDF 功能的更多信息,請訪問IronPDF 功能頁面。 該庫還支援從各種來源建立 PDF 、在PDF 格式之間進行轉換以及使用專業工具整理 PDF

iTextPdf 提供哪些企業級功能?

  • PDF 建立:支援從頭開始建立 PDF 文件。 -表單:提供建立和編輯 PDF 表單的功能。 -數位簽章:簽署 PDF 文件。 -壓縮:優化 PDF 檔案大小。 -內容提取:從 PDF 中提取文字和圖像。 -可自訂性:針對複雜項目提供高度可自訂性。

這兩個函式庫是如何處理 HTML 到 PDF 的轉換的?

這兩個庫都支援 HTML 轉 PDF,但它們在方法和易用性方面有所不同。 IronPDF 採用Chromium 渲染引擎,可實現像素級精確度,而 iTextPdf 使用其 pdfHTML 插件,但 CSS 支援較為有限。 IronPDF 支援HTML 檔案HTML 字串URL作為輸入來源,包括完整的JavaScript 渲染。 對於複雜的佈局,IronPDF 可處理CSS 螢幕和列印媒體類型,並支援自訂邊距以實現精確控制。

IronPDF

using IronPdf;

// Instantiate the renderer
var renderer = new ChromePdfRenderer();

// Create a PDF from an HTML string
var pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>");
pdf.SaveAs("output.pdf");

// Advanced example with external assets
var myAdvancedPdf = renderer.RenderHtmlAsPdf("<img src='icons/iron.png'>", @"C:\site\assets\");
myAdvancedPdf.SaveAs("html-with-assets.pdf");

// Render with custom options
renderer.RenderingOptions.MarginTop = 50;
renderer.RenderingOptions.MarginBottom = 50;
renderer.RenderingOptions.CssMediaType = IronPdf.Rendering.PdfCssMediaType.Print;
var customPdf = renderer.RenderHtmlAsPdf("<h1>Custom Settings</h1>");
customPdf.SaveAs("custom-settings.pdf");
using IronPdf;

// Instantiate the renderer
var renderer = new ChromePdfRenderer();

// Create a PDF from an HTML string
var pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>");
pdf.SaveAs("output.pdf");

// Advanced example with external assets
var myAdvancedPdf = renderer.RenderHtmlAsPdf("<img src='icons/iron.png'>", @"C:\site\assets\");
myAdvancedPdf.SaveAs("html-with-assets.pdf");

// Render with custom options
renderer.RenderingOptions.MarginTop = 50;
renderer.RenderingOptions.MarginBottom = 50;
renderer.RenderingOptions.CssMediaType = IronPdf.Rendering.PdfCssMediaType.Print;
var customPdf = renderer.RenderHtmlAsPdf("<h1>Custom Settings</h1>");
customPdf.SaveAs("custom-settings.pdf");
Imports IronPdf

' Instantiate the renderer
Dim renderer As New ChromePdfRenderer()

' Create a PDF from an HTML string
Dim pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>")
pdf.SaveAs("output.pdf")

' Advanced example with external assets
Dim myAdvancedPdf = renderer.RenderHtmlAsPdf("<img src='icons/iron.png'>", "C:\site\assets\")
myAdvancedPdf.SaveAs("html-with-assets.pdf")

' Render with custom options
renderer.RenderingOptions.MarginTop = 50
renderer.RenderingOptions.MarginBottom = 50
renderer.RenderingOptions.CssMediaType = IronPdf.Rendering.PdfCssMediaType.Print
Dim customPdf = renderer.RenderHtmlAsPdf("<h1>Custom Settings</h1>")
customPdf.SaveAs("custom-settings.pdf")
$vbLabelText   $csharpLabel

iTextPdf

using iText.Html2pdf;
using System.IO;

public class HtmlToPdf
{
    public static void ConvertHtmlToPdf()
    {
        using (FileStream htmlSource = File.Open("input.html", FileMode.Open))
        using (FileStream pdfDest = File.Open("output.pdf", FileMode.Create))
        {
            ConverterProperties converterProperties = new ConverterProperties();
            HtmlConverter.ConvertToPdf(htmlSource, pdfDest, converterProperties);
        }
    }
}
using iText.Html2pdf;
using System.IO;

public class HtmlToPdf
{
    public static void ConvertHtmlToPdf()
    {
        using (FileStream htmlSource = File.Open("input.html", FileMode.Open))
        using (FileStream pdfDest = File.Open("output.pdf", FileMode.Create))
        {
            ConverterProperties converterProperties = new ConverterProperties();
            HtmlConverter.ConvertToPdf(htmlSource, pdfDest, converterProperties);
        }
    }
}
Imports iText.Html2pdf
Imports System.IO

Public Class HtmlToPdf
	Public Shared Sub ConvertHtmlToPdf()
		Using htmlSource As FileStream = File.Open("input.html", FileMode.Open)
		Using pdfDest As FileStream = File.Open("output.pdf", FileMode.Create)
			Dim converterProperties As New ConverterProperties()
			HtmlConverter.ConvertToPdf(htmlSource, pdfDest, converterProperties)
		End Using
		End Using
	End Sub
End Class
$vbLabelText   $csharpLabel

IronPDF 提供了一種簡單直接的HTML 轉 PDF轉換方法,支援 HTML、CSS 和 JavaScript。 它可以直接從HTML 字串轉換,也可以包含具有可選基本路徑的資源。 該程式庫還支援用於複雜專案的HTML ZIP 檔案和用於改善渲染效果的響應式 CSS 。 對於動態內容,IronPDF 提供JavaScript 執行渲染延遲選項,以確保頁面完全載入。 其他渲染功能包括視口控制自訂紙張尺寸。 iTextPdf 需要更多設置,主要專注於基於文件的轉換,對現代 Web 標準的支援有限。

哪個圖書館提供更好的PDF加密?

當安全至關重要時,加密必不可少。 以下是各個庫的處理方式。 IronPDF 提供完整的安全功能,包括AES 加密權限管理。 該庫支援元資料管理清理,以刪除潛在的有害內容。

IronPDF

using IronPdf;

// Load an encrypted PDF or create a new one
var pdf = PdfDocument.FromFile("encrypted.pdf", "password");

// Set document security settings
pdf.SecuritySettings.MakePdfDocumentReadOnly("secret-key");
pdf.SecuritySettings.AllowUserCopyPasteContent = false;
pdf.SecuritySettings.AllowUserAnnotations = false;
pdf.SecuritySettings.AllowUserPrinting = IronPdf.Security.PdfPrintSecurity.NoPrint;
pdf.SecuritySettings.AllowUserFormData = false;
pdf.Password = "my-password";

// Set owner password for administrative access
pdf.SecuritySettings.OwnerPassword = "owner-password";

// Save with encryption
pdf.SaveAs("secured.pdf");
using IronPdf;

// Load an encrypted PDF or create a new one
var pdf = PdfDocument.FromFile("encrypted.pdf", "password");

// Set document security settings
pdf.SecuritySettings.MakePdfDocumentReadOnly("secret-key");
pdf.SecuritySettings.AllowUserCopyPasteContent = false;
pdf.SecuritySettings.AllowUserAnnotations = false;
pdf.SecuritySettings.AllowUserPrinting = IronPdf.Security.PdfPrintSecurity.NoPrint;
pdf.SecuritySettings.AllowUserFormData = false;
pdf.Password = "my-password";

// Set owner password for administrative access
pdf.SecuritySettings.OwnerPassword = "owner-password";

// Save with encryption
pdf.SaveAs("secured.pdf");
Imports IronPdf

' Load an encrypted PDF or create a new one
Dim pdf = PdfDocument.FromFile("encrypted.pdf", "password")

' Set document security settings
pdf.SecuritySettings.MakePdfDocumentReadOnly("secret-key")
pdf.SecuritySettings.AllowUserCopyPasteContent = False
pdf.SecuritySettings.AllowUserAnnotations = False
pdf.SecuritySettings.AllowUserPrinting = IronPdf.Security.PdfPrintSecurity.NoPrint
pdf.SecuritySettings.AllowUserFormData = False
pdf.Password = "my-password"

' Set owner password for administrative access
pdf.SecuritySettings.OwnerPassword = "owner-password"

' Save with encryption
pdf.SaveAs("secured.pdf")
$vbLabelText   $csharpLabel

iTextPdf

using iText.Kernel.Pdf;
using System.Text;

public class EncryptPdf
{
    public static readonly string DEST = "encrypt_pdf.pdf";
    public static readonly string OWNER_PASSWORD = "World";
    public static readonly string USER_PASSWORD = "Hello";

    protected void ManipulatePdf(string dest)
    {
        PdfDocument document = new PdfDocument(new PdfReader("input.pdf"), new PdfWriter(dest,
            new WriterProperties().SetStandardEncryption(
                Encoding.UTF8.GetBytes(USER_PASSWORD),
                Encoding.UTF8.GetBytes(OWNER_PASSWORD),
                EncryptionConstants.ALLOW_PRINTING,
                EncryptionConstants.ENCRYPTION_AES_128)));
        document.Close();
    }
}
using iText.Kernel.Pdf;
using System.Text;

public class EncryptPdf
{
    public static readonly string DEST = "encrypt_pdf.pdf";
    public static readonly string OWNER_PASSWORD = "World";
    public static readonly string USER_PASSWORD = "Hello";

    protected void ManipulatePdf(string dest)
    {
        PdfDocument document = new PdfDocument(new PdfReader("input.pdf"), new PdfWriter(dest,
            new WriterProperties().SetStandardEncryption(
                Encoding.UTF8.GetBytes(USER_PASSWORD),
                Encoding.UTF8.GetBytes(OWNER_PASSWORD),
                EncryptionConstants.ALLOW_PRINTING,
                EncryptionConstants.ENCRYPTION_AES_128)));
        document.Close();
    }
}
Imports iText.Kernel.Pdf
Imports System.Text

Public Class EncryptPdf
	Public Shared ReadOnly DEST As String = "encrypt_pdf.pdf"
	Public Shared ReadOnly OWNER_PASSWORD As String = "World"
	Public Shared ReadOnly USER_PASSWORD As String = "Hello"

	Protected Sub ManipulatePdf(ByVal dest As String)
		Dim document As New PdfDocument(New PdfReader("input.pdf"), New PdfWriter(dest, (New WriterProperties()).SetStandardEncryption(Encoding.UTF8.GetBytes(USER_PASSWORD), Encoding.UTF8.GetBytes(OWNER_PASSWORD), EncryptionConstants.ALLOW_PRINTING, EncryptionConstants.ENCRYPTION_AES_128)))
		document.Close()
	End Sub
End Class
$vbLabelText   $csharpLabel

IronPDF 提供簡單易用的加密功能文件權限控制。 該庫支援自訂日誌記錄,用於安全審計和企業部署的許可證金鑰管理。 為了提高安全性,IronPDF 提供PDF 檔案版本控制登錄配置選項。 iTextPdf 需要詳細的設置,尤其需要關注加密標準。 對於高級安全需求,IronPDF 還支援帶有 HSM 的數位簽名以滿足企業要求,並支援 Kerberos 身份驗證以滿足安全環境的需求。

IronPDF 和 iTextPdf 如何處理內容編輯?

對PDF文件中的資訊進行編輯對於保護隱私和安全至關重要。 以下是各個函式庫對此功能的支援方式。 IronPDF 提供完整的編輯功能和PDF 清理功能,可刪除潛在的有害內容。 該庫還提供PDF 扁平化功能,可將表單和註解轉換為靜態內容。

IronPDF

using IronPdf;

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

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

// Redact specific text on specific pages
pdf.RedactTextOnPage(0, "confidential");

// Use regular expressions for pattern matching
pdf.RedactTextOnAllPages(@"\b\d{3}-\d{2}-\d{4}\b"); // Redact SSN pattern

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

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

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

// Redact specific text on specific pages
pdf.RedactTextOnPage(0, "confidential");

// Use regular expressions for pattern matching
pdf.RedactTextOnAllPages(@"\b\d{3}-\d{2}-\d{4}\b"); // Redact SSN pattern

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

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

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

' Redact specific text on specific pages
pdf.RedactTextOnPage(0, "confidential")

' Use regular expressions for pattern matching
pdf.RedactTextOnAllPages("\b\d{3}-\d{2}-\d{4}\b") ' Redact SSN pattern

pdf.SaveAs("redacted.pdf")
$vbLabelText   $csharpLabel

iTextPdf

using iText.Kernel.Pdf;
using iText.Kernel.Colors;

// Define areas to redact on each page
Rectangle[] rectanglesToRedact = { new Rectangle(100, 100, 200, 50) };

// Draw black rectangles to cover sensitive areas
using (PdfDocument pdfDoc = new PdfDocument(new PdfReader("input.pdf"), new PdfWriter("output_redacted.pdf")))
{
    for (int pageNum = 1; pageNum <= pdfDoc.GetNumberOfPages(); pageNum++)
    {
        PdfPage page = pdfDoc.GetPage(pageNum);
        PdfCanvas canvas = new PdfCanvas(page);
        foreach (Rectangle rect in rectanglesToRedact)
        {
            canvas.SetFillColor(ColorConstants.BLACK)
                  .Rectangle(rect.GetX(), rect.GetY(), rect.GetWidth(), rect.GetHeight())
                  .Fill();
        }
    }
}
using iText.Kernel.Pdf;
using iText.Kernel.Colors;

// Define areas to redact on each page
Rectangle[] rectanglesToRedact = { new Rectangle(100, 100, 200, 50) };

// Draw black rectangles to cover sensitive areas
using (PdfDocument pdfDoc = new PdfDocument(new PdfReader("input.pdf"), new PdfWriter("output_redacted.pdf")))
{
    for (int pageNum = 1; pageNum <= pdfDoc.GetNumberOfPages(); pageNum++)
    {
        PdfPage page = pdfDoc.GetPage(pageNum);
        PdfCanvas canvas = new PdfCanvas(page);
        foreach (Rectangle rect in rectanglesToRedact)
        {
            canvas.SetFillColor(ColorConstants.BLACK)
                  .Rectangle(rect.GetX(), rect.GetY(), rect.GetWidth(), rect.GetHeight())
                  .Fill();
        }
    }
}
Imports iText.Kernel.Pdf
Imports iText.Kernel.Colors

' Define areas to redact on each page
Private rectanglesToRedact() As Rectangle = { New Rectangle(100, 100, 200, 50) }

' Draw black rectangles to cover sensitive areas
Using pdfDoc As New PdfDocument(New PdfReader("input.pdf"), New PdfWriter("output_redacted.pdf"))
	Dim pageNum As Integer = 1
	Do While pageNum <= pdfDoc.GetNumberOfPages()
		Dim page As PdfPage = pdfDoc.GetPage(pageNum)
		Dim canvas As New PdfCanvas(page)
		For Each rect As Rectangle In rectanglesToRedact
			canvas.SetFillColor(ColorConstants.BLACK).Rectangle(rect.GetX(), rect.GetY(), rect.GetWidth(), rect.GetHeight()).Fill()
		Next rect
		pageNum += 1
	Loop
End Using
$vbLabelText   $csharpLabel

IronPDF 提供了一個方便的文字編輯工具,可以輕鬆隱藏所有頁面上的敏感文字。 該庫還提供文字替換功能,用於更新文件內容,以及PDF DOM 存取功能,用於進行詳細的內容操作。 若要擷取編輯前的內容,可以使用文字和圖像擷取或以程式方式解析 PDF 檔案。 iTextPdf 需要手動定義和應用黑色矩形來覆蓋敏感區域。 為了徹底清理文檔,IronPDF 還提供PDF 清理功能,以刪除潛在的惡意內容,以及PDF 扁平化功能,以將互動式元素轉換為靜態內容。

哪個圖書館讓數位簽章更方便?

PDF文件自動簽名可以節省大量時間。以下是IronPDF和iTextPdf處理數位簽章方式的比較。 IronPDF 支援多種簽章方法,包括基於憑證的簽章HSM 整合。 該圖書館還維護修訂歷史記錄,用於追蹤文件變更和簽名有效性。

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)
{
    SigningContact = "support@ironsoftware.com",
    SigningLocation = "Chicago, USA",
    SigningReason = "Document Approval"
};

// Sign PDF document
PdfDocument pdf = PdfDocument.FromFile("document.pdf");
pdf.Sign(sig);

// Apply timestamp
var timestampedSig = new PdfSignature(cert)
{
    TimestampHashAlgorithm = TimestampHashAlgorithm.SHA256,
    TimeStampUrl = "___PROTECTED_URL_245___"
};

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)
{
    SigningContact = "support@ironsoftware.com",
    SigningLocation = "Chicago, USA",
    SigningReason = "Document Approval"
};

// Sign PDF document
PdfDocument pdf = PdfDocument.FromFile("document.pdf");
pdf.Sign(sig);

// Apply timestamp
var timestampedSig = new PdfSignature(cert)
{
    TimestampHashAlgorithm = TimestampHashAlgorithm.SHA256,
    TimeStampUrl = "___PROTECTED_URL_245___"
};

pdf.SaveAs("signed.pdf");
Imports IronPdf
Imports IronPdf.Signing
Imports System.Security.Cryptography.X509Certificates

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

' Create PdfSignature object
Dim sig As New PdfSignature(cert) With {
    .SigningContact = "support@ironsoftware.com",
    .SigningLocation = "Chicago, USA",
    .SigningReason = "Document Approval"
}

' Sign PDF document
Dim pdf As PdfDocument = PdfDocument.FromFile("document.pdf")
pdf.Sign(sig)

' Apply timestamp
Dim timestampedSig As New PdfSignature(cert) With {
    .TimestampHashAlgorithm = TimestampHashAlgorithm.SHA256,
    .TimeStampUrl = "___PROTECTED_URL_245___"
}

pdf.SaveAs("signed.pdf")
$vbLabelText   $csharpLabel

iTextPdf

using System;
using System.IO;
using iText.Kernel.Pdf;
using iText.Signatures;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Pkcs;
using Org.BouncyCastle.X509;

class Program
{
    static void Main(string[] args)
    {
        string src = "input.pdf";
        string dest = "output_signed.pdf";
        string pfxFile = "your_certificate.pfx";
        string pfxPassword = "your_password";

        try
        {
            // Load your certificate
            Pkcs12Store ks = new Pkcs12Store(new FileStream(pfxFile, FileMode.Open), pfxPassword.ToCharArray());
            string alias = null;
            foreach (string al in ks.Aliases)
            {
                if (ks.IsKeyEntry(al))
                {
                    alias = al;
                    break;
                }
            }
            ICipherParameters pk = ks.GetKey(alias).Key;
            X509CertificateEntry[] chain = ks.GetCertificateChain(alias);
            X509Certificate2 cert = new X509Certificate2(chain[0].Certificate.GetEncoded());

            // Create output PDF with signed content
            using (PdfReader reader = new PdfReader(src))
            {
                using (PdfWriter writer = new PdfWriter(dest))
                {
                    using (PdfDocument pdfDoc = new PdfDocument(reader, writer))
                    {
                        // Create the signer
                        PdfSigner signer = new PdfSigner(pdfDoc, writer, new StampingProperties().UseAppendMode());

                        // Configure signature appearance
                        PdfSignatureAppearance appearance = signer.GetSignatureAppearance();
                        appearance.SetReason("Digital Signature");
                        appearance.SetLocation("Your Location");
                        appearance.SetContact("Your Contact");

                        // Create signature
                        IExternalSignature pks = new PrivateKeySignature(pk, "SHA-256");
                        signer.SignDetached(pks, chain, null, null, null, 0, PdfSigner.CryptoStandard.CMS);
                    }
                }
            }
            Console.WriteLine($"PDF digitally signed successfully: {dest}");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Error signing PDF: {ex.Message}");
        }
    }
}
using System;
using System.IO;
using iText.Kernel.Pdf;
using iText.Signatures;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Pkcs;
using Org.BouncyCastle.X509;

class Program
{
    static void Main(string[] args)
    {
        string src = "input.pdf";
        string dest = "output_signed.pdf";
        string pfxFile = "your_certificate.pfx";
        string pfxPassword = "your_password";

        try
        {
            // Load your certificate
            Pkcs12Store ks = new Pkcs12Store(new FileStream(pfxFile, FileMode.Open), pfxPassword.ToCharArray());
            string alias = null;
            foreach (string al in ks.Aliases)
            {
                if (ks.IsKeyEntry(al))
                {
                    alias = al;
                    break;
                }
            }
            ICipherParameters pk = ks.GetKey(alias).Key;
            X509CertificateEntry[] chain = ks.GetCertificateChain(alias);
            X509Certificate2 cert = new X509Certificate2(chain[0].Certificate.GetEncoded());

            // Create output PDF with signed content
            using (PdfReader reader = new PdfReader(src))
            {
                using (PdfWriter writer = new PdfWriter(dest))
                {
                    using (PdfDocument pdfDoc = new PdfDocument(reader, writer))
                    {
                        // Create the signer
                        PdfSigner signer = new PdfSigner(pdfDoc, writer, new StampingProperties().UseAppendMode());

                        // Configure signature appearance
                        PdfSignatureAppearance appearance = signer.GetSignatureAppearance();
                        appearance.SetReason("Digital Signature");
                        appearance.SetLocation("Your Location");
                        appearance.SetContact("Your Contact");

                        // Create signature
                        IExternalSignature pks = new PrivateKeySignature(pk, "SHA-256");
                        signer.SignDetached(pks, chain, null, null, null, 0, PdfSigner.CryptoStandard.CMS);
                    }
                }
            }
            Console.WriteLine($"PDF digitally signed successfully: {dest}");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Error signing PDF: {ex.Message}");
        }
    }
}
Imports System
Imports System.IO
Imports iText.Kernel.Pdf
Imports iText.Signatures
Imports Org.BouncyCastle.Crypto
Imports Org.BouncyCastle.Pkcs
Imports Org.BouncyCastle.X509

Friend Class Program
	Shared Sub Main(ByVal args() As String)
		Dim src As String = "input.pdf"
		Dim dest As String = "output_signed.pdf"
		Dim pfxFile As String = "your_certificate.pfx"
		Dim pfxPassword As String = "your_password"

		Try
			' Load your certificate
			Dim ks As New Pkcs12Store(New FileStream(pfxFile, FileMode.Open), pfxPassword.ToCharArray())
			Dim [alias] As String = Nothing
			For Each al As String In ks.Aliases
				If ks.IsKeyEntry(al) Then
					[alias] = al
					Exit For
				End If
			Next al
			Dim pk As ICipherParameters = ks.GetKey([alias]).Key
			Dim chain() As X509CertificateEntry = ks.GetCertificateChain([alias])
			Dim cert As New X509Certificate2(chain(0).Certificate.GetEncoded())

			' Create output PDF with signed content
			Using reader As New PdfReader(src)
				Using writer As New PdfWriter(dest)
					Using pdfDoc As New PdfDocument(reader, writer)
						' Create the signer
						Dim signer As New PdfSigner(pdfDoc, writer, (New StampingProperties()).UseAppendMode())

						' Configure signature appearance
						Dim appearance As PdfSignatureAppearance = signer.GetSignatureAppearance()
						appearance.SetReason("Digital Signature")
						appearance.SetLocation("Your Location")
						appearance.SetContact("Your Contact")

						' Create signature
						Dim pks As IExternalSignature = New PrivateKeySignature(pk, "SHA-256")
						signer.SignDetached(pks, chain, Nothing, Nothing, Nothing, 0, PdfSigner.CryptoStandard.CMS)
					End Using
				End Using
			End Using
			Console.WriteLine($"PDF digitally signed successfully: {dest}")
		Catch ex As Exception
			Console.WriteLine($"Error signing PDF: {ex.Message}")
		End Try
	End Sub
End Class
$vbLabelText   $csharpLabel

在將數位簽章套用至 PDF 檔案時,IronPDF 提供了一種使用 X509 憑證的簡單且有效率的方法。 其 API 簡化了集成,同時不犧牲安全控制。 IronPDF 還支援版本歷史記錄跟踪,以維護文件完整性,並支援多種簽名類型,包括視覺簽名。 該庫與HSM 設備整合以提高安全性並支援證書驗證。 為了確保文件工作流程的安全,請探索簽名和安全 PDF功能。 iTextPdf 的簽章流程提供了更多自訂選項,但設定較為複雜。開發者可以獲得更精細的控制,但憑證處理和簽章配置的學習曲線也更陡峭。

這兩個圖書館是如何為PDF文件添加浮水印的?

在 PDF 文件上添加浮水印對於品牌推廣、保密性和版權保護至關重要。 以下是 IronPDF 和 iTextPdf 如何為 PDF 文件添加浮水印的方法。 IronPDF 提供靈活的浮水印選項,並支援 HTML/CSS 。 該庫還支援背景層和前景層,用於複雜的疊加。

IronPDF

using IronPdf;

// Stamps a Watermark onto a new or existing PDF
var renderer = new ChromePdfRenderer();

var pdf = renderer.RenderUrlAsPdf("___PROTECTED_URL_246___");

// Add text watermark
pdf.ApplyWatermark("<h2 style='color:red'>SAMPLE</h2>", 30, IronPdf.Editing.VerticalAlignment.Middle, IronPdf.Editing.HorizontalAlignment.Center);

// Add image watermark
pdf.ApplyWatermark("<img src='logo.png' style='width:200px'>", 45, IronPdf.Editing.VerticalAlignment.Bottom, IronPdf.Editing.HorizontalAlignment.Right);

// Add complex HTML watermark with transparency
string watermarkHtml = @"
<div style='font-size:80px; color:rgba(255,0,0,0.3); font-family:Arial; transform:rotate(-45deg);'>
    CONFIDENTIAL
</div>";
pdf.ApplyWatermark(watermarkHtml, 0, 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("___PROTECTED_URL_246___");

// Add text watermark
pdf.ApplyWatermark("<h2 style='color:red'>SAMPLE</h2>", 30, IronPdf.Editing.VerticalAlignment.Middle, IronPdf.Editing.HorizontalAlignment.Center);

// Add image watermark
pdf.ApplyWatermark("<img src='logo.png' style='width:200px'>", 45, IronPdf.Editing.VerticalAlignment.Bottom, IronPdf.Editing.HorizontalAlignment.Right);

// Add complex HTML watermark with transparency
string watermarkHtml = @"
<div style='font-size:80px; color:rgba(255,0,0,0.3); font-family:Arial; transform:rotate(-45deg);'>
    CONFIDENTIAL
</div>";
pdf.ApplyWatermark(watermarkHtml, 0, 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
Dim renderer As New ChromePdfRenderer()

Dim pdf = renderer.RenderUrlAsPdf("___PROTECTED_URL_246___")

' Add text watermark
pdf.ApplyWatermark("<h2 style='color:red'>SAMPLE</h2>", 30, IronPdf.Editing.VerticalAlignment.Middle, IronPdf.Editing.HorizontalAlignment.Center)

' Add image watermark
pdf.ApplyWatermark("<img src='logo.png' style='width:200px'>", 45, IronPdf.Editing.VerticalAlignment.Bottom, IronPdf.Editing.HorizontalAlignment.Right)

' Add complex HTML watermark with transparency
Dim watermarkHtml As String = "
<div style='font-size:80px; color:rgba(255,0,0,0.3); font-family:Arial; transform:rotate(-45deg);'>
    CONFIDENTIAL
</div>"
pdf.ApplyWatermark(watermarkHtml, 0, IronPdf.Editing.VerticalAlignment.Middle, IronPdf.Editing.HorizontalAlignment.Center)

pdf.SaveAs("C:\Path\To\Watermarked.pdf")
$vbLabelText   $csharpLabel

iTextPdf

using iText.IO.Font;
using iText.IO.Font.Constants;
using iText.Kernel.Colors;
using iText.Kernel.Font;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas;
using iText.Kernel.Pdf.Extgstate;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;

public class TransparentWatermark 
{
    public static readonly string DEST = "results/sandbox/stamper/transparent_watermark.pdf";
    public static readonly string SRC = "../../../resources/pdfs/hero.pdf";

    public static void Main(string[] args) 
    {
        FileInfo file = new FileInfo(DEST);
        file.Directory.Create();

        new TransparentWatermark().ManipulatePdf(DEST);
    }

    protected void ManipulatePdf(string dest) 
    {
        PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
        PdfCanvas under = new PdfCanvas(pdfDoc.GetFirstPage().NewContentStreamBefore(), new PdfResources(), pdfDoc);
        PdfFont font = PdfFontFactory.CreateFont(FontProgramFactory.CreateFont(StandardFonts.HELVETICA));
        Paragraph paragraph = new Paragraph("This watermark is added UNDER the existing content")
                .SetFont(font)
                .SetFontSize(15);

        Canvas canvasWatermark1 = new Canvas(under, pdfDoc.GetDefaultPageSize())
                .ShowTextAligned(paragraph, 297, 550, 1, TextAlignment.CENTER, VerticalAlignment.TOP, 0);
        canvasWatermark1.Close();
        PdfCanvas over = new PdfCanvas(pdfDoc.GetFirstPage());
        over.SetFillColor(ColorConstants.BLACK);
        paragraph = new Paragraph("This watermark is added ON TOP OF the existing content")
                .SetFont(font)
                .SetFontSize(15);

        Canvas canvasWatermark2 = new Canvas(over, pdfDoc.GetDefaultPageSize())
                .ShowTextAligned(paragraph, 297, 500, 1, TextAlignment.CENTER, VerticalAlignment.TOP, 0);
        canvasWatermark2.Close();
        paragraph = new Paragraph("This TRANSPARENT watermark is added ON TOP OF the existing content")
                .SetFont(font)
                .SetFontSize(15);
        over.SaveState();

        PdfExtGState gs1 = new PdfExtGState();
        gs1.SetFillOpacity(0.5f);
        over.SetExtGState(gs1);
        Canvas canvasWatermark3 = new Canvas(over, pdfDoc.GetDefaultPageSize())
                .ShowTextAligned(paragraph, 297, 450, 1, TextAlignment.CENTER, VerticalAlignment.TOP, 0);
        canvasWatermark3.Close();
        over.RestoreState();

        pdfDoc.Close();
    }
}
using iText.IO.Font;
using iText.IO.Font.Constants;
using iText.Kernel.Colors;
using iText.Kernel.Font;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas;
using iText.Kernel.Pdf.Extgstate;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;

public class TransparentWatermark 
{
    public static readonly string DEST = "results/sandbox/stamper/transparent_watermark.pdf";
    public static readonly string SRC = "../../../resources/pdfs/hero.pdf";

    public static void Main(string[] args) 
    {
        FileInfo file = new FileInfo(DEST);
        file.Directory.Create();

        new TransparentWatermark().ManipulatePdf(DEST);
    }

    protected void ManipulatePdf(string dest) 
    {
        PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
        PdfCanvas under = new PdfCanvas(pdfDoc.GetFirstPage().NewContentStreamBefore(), new PdfResources(), pdfDoc);
        PdfFont font = PdfFontFactory.CreateFont(FontProgramFactory.CreateFont(StandardFonts.HELVETICA));
        Paragraph paragraph = new Paragraph("This watermark is added UNDER the existing content")
                .SetFont(font)
                .SetFontSize(15);

        Canvas canvasWatermark1 = new Canvas(under, pdfDoc.GetDefaultPageSize())
                .ShowTextAligned(paragraph, 297, 550, 1, TextAlignment.CENTER, VerticalAlignment.TOP, 0);
        canvasWatermark1.Close();
        PdfCanvas over = new PdfCanvas(pdfDoc.GetFirstPage());
        over.SetFillColor(ColorConstants.BLACK);
        paragraph = new Paragraph("This watermark is added ON TOP OF the existing content")
                .SetFont(font)
                .SetFontSize(15);

        Canvas canvasWatermark2 = new Canvas(over, pdfDoc.GetDefaultPageSize())
                .ShowTextAligned(paragraph, 297, 500, 1, TextAlignment.CENTER, VerticalAlignment.TOP, 0);
        canvasWatermark2.Close();
        paragraph = new Paragraph("This TRANSPARENT watermark is added ON TOP OF the existing content")
                .SetFont(font)
                .SetFontSize(15);
        over.SaveState();

        PdfExtGState gs1 = new PdfExtGState();
        gs1.SetFillOpacity(0.5f);
        over.SetExtGState(gs1);
        Canvas canvasWatermark3 = new Canvas(over, pdfDoc.GetDefaultPageSize())
                .ShowTextAligned(paragraph, 297, 450, 1, TextAlignment.CENTER, VerticalAlignment.TOP, 0);
        canvasWatermark3.Close();
        over.RestoreState();

        pdfDoc.Close();
    }
}
Imports iText.IO.Font
Imports iText.IO.Font.Constants
Imports iText.Kernel.Colors
Imports iText.Kernel.Font
Imports iText.Kernel.Pdf
Imports iText.Kernel.Pdf.Canvas
Imports iText.Kernel.Pdf.Extgstate
Imports iText.Layout
Imports iText.Layout.Element
Imports iText.Layout.Properties

Public Class TransparentWatermark
	Public Shared ReadOnly DEST As String = "results/sandbox/stamper/transparent_watermark.pdf"
	Public Shared ReadOnly SRC As String = "../../../resources/pdfs/hero.pdf"

	Public Shared Sub Main(ByVal args() As String)
		Dim file As New FileInfo(DEST)
		file.Directory.Create()

		Call (New TransparentWatermark()).ManipulatePdf(DEST)
	End Sub

	Protected Sub ManipulatePdf(ByVal dest As String)
		Dim pdfDoc As New PdfDocument(New PdfReader(SRC), New PdfWriter(dest))
		Dim under As New PdfCanvas(pdfDoc.GetFirstPage().NewContentStreamBefore(), New PdfResources(), pdfDoc)
		Dim font As PdfFont = PdfFontFactory.CreateFont(FontProgramFactory.CreateFont(StandardFonts.HELVETICA))
		Dim paragraph As Paragraph = (New Paragraph("This watermark is added UNDER the existing content")).SetFont(font).SetFontSize(15)

		Dim canvasWatermark1 As Canvas = (New Canvas(under, pdfDoc.GetDefaultPageSize())).ShowTextAligned(paragraph, 297, 550, 1, TextAlignment.CENTER, VerticalAlignment.TOP, 0)
		canvasWatermark1.Close()
		Dim over As New PdfCanvas(pdfDoc.GetFirstPage())
		over.SetFillColor(ColorConstants.BLACK)
		paragraph = (New Paragraph("This watermark is added ON TOP OF the existing content")).SetFont(font).SetFontSize(15)

		Dim canvasWatermark2 As Canvas = (New Canvas(over, pdfDoc.GetDefaultPageSize())).ShowTextAligned(paragraph, 297, 500, 1, TextAlignment.CENTER, VerticalAlignment.TOP, 0)
		canvasWatermark2.Close()
		paragraph = (New Paragraph("This TRANSPARENT watermark is added ON TOP OF the existing content")).SetFont(font).SetFontSize(15)
		over.SaveState()

		Dim gs1 As New PdfExtGState()
		gs1.SetFillOpacity(0.5F)
		over.SetExtGState(gs1)
		Dim canvasWatermark3 As Canvas = (New Canvas(over, pdfDoc.GetDefaultPageSize())).ShowTextAligned(paragraph, 297, 450, 1, TextAlignment.CENTER, VerticalAlignment.TOP, 0)
		canvasWatermark3.Close()
		over.RestoreState()

		pdfDoc.Close()
	End Sub
End Class
$vbLabelText   $csharpLabel

IronPDF 的 API 可實現快速浮水印應用,並提供 HTML 和 CSS 自訂的靈活性。 這種方法無需繁瑣的設定即可創造視覺上清晰的浮水印。為了提供更精細的控制,IronPDF 支援背景層和前景層,可用於複雜的文件疊加、多個浮水印以及自訂定位。 該庫還提供條碼整合以進行跟踪,以及SVG 圖形支援以實現可縮放浮水印。 對於進階需求,可探索轉換 PDF 頁面以及繪製線條和矩形的功能。 iTextPdf 提供高度自訂的浮水印放置功能,可透過詳細的配置選項實現,但需要更多的編碼工作。 ### 哪個庫提供更好的圖像和文字浮水印功能?

在 PDF 上添加內容類似於水印,但側重於添加特定元素,例如圖像或文本,以進行標記或品牌推廣。 以下是 IronPDF 和 iTextPdf 執行此任務的方式。 IronPDF 提供豐富的沖壓功能靈活的定位選項。 該庫還支援直接在 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,
    HorizontalAlignment = HorizontalAlignment.Center,
    Opacity = 50,
    Rotation = -45
};

// Stamp the text stamper on all pages
pdf.ApplyStamp(textStamper);

// Create HTML stamper for complex layouts
HtmlStamper htmlStamper = new HtmlStamper()
{
    Html = @"<div style='border:2px solid red; padding:10px;'>
             <h3>APPROVED</h3>
             <p>Date: " + DateTime.Now.ToShortDateString() + @"</p>
             </div>",
    VerticalAlignment = VerticalAlignment.Bottom,
    HorizontalAlignment = HorizontalAlignment.Right,
    Width = 200,
    Height = 100
};

pdf.ApplyStamp(htmlStamper);

// Create image stamper
ImageStamper imageStamper = new ImageStamper(new Uri("___PROTECTED_URL_247___"))
{
    VerticalAlignment = VerticalAlignment.Top,
    HorizontalAlignment = HorizontalAlignment.Left,
    MaxWidth = new Length(150),
    MaxHeight = new Length(150)
};

// Stamp the image stamper on specific pages
pdf.ApplyStamp(imageStamper, new[] { 0, 2, 4 });

pdf.SaveAs("stamped.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,
    HorizontalAlignment = HorizontalAlignment.Center,
    Opacity = 50,
    Rotation = -45
};

// Stamp the text stamper on all pages
pdf.ApplyStamp(textStamper);

// Create HTML stamper for complex layouts
HtmlStamper htmlStamper = new HtmlStamper()
{
    Html = @"<div style='border:2px solid red; padding:10px;'>
             <h3>APPROVED</h3>
             <p>Date: " + DateTime.Now.ToShortDateString() + @"</p>
             </div>",
    VerticalAlignment = VerticalAlignment.Bottom,
    HorizontalAlignment = HorizontalAlignment.Right,
    Width = 200,
    Height = 100
};

pdf.ApplyStamp(htmlStamper);

// Create image stamper
ImageStamper imageStamper = new ImageStamper(new Uri("___PROTECTED_URL_247___"))
{
    VerticalAlignment = VerticalAlignment.Top,
    HorizontalAlignment = HorizontalAlignment.Left,
    MaxWidth = new Length(150),
    MaxHeight = new Length(150)
};

// Stamp the image stamper on specific pages
pdf.ApplyStamp(imageStamper, new[] { 0, 2, 4 });

pdf.SaveAs("stamped.pdf");
Imports IronPdf
Imports IronPdf.Editing

Dim renderer As New ChromePdfRenderer()

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

' Create text stamper
Dim textStamper As New TextStamper() With {
    .Text = "Text Stamper!",
    .FontFamily = "Bungee Spice",
    .UseGoogleFont = True,
    .FontSize = 30,
    .IsBold = True,
    .IsItalic = True,
    .VerticalAlignment = VerticalAlignment.Top,
    .HorizontalAlignment = HorizontalAlignment.Center,
    .Opacity = 50,
    .Rotation = -45
}

' Stamp the text stamper on all pages
pdf.ApplyStamp(textStamper)

' Create HTML stamper for complex layouts
Dim htmlStamper As New HtmlStamper() With {
    .Html = "<div style='border:2px solid red; padding:10px;'>" &
            "<h3>APPROVED</h3>" &
            "<p>Date: " & DateTime.Now.ToShortDateString() & "</p>" &
            "</div>",
    .VerticalAlignment = VerticalAlignment.Bottom,
    .HorizontalAlignment = HorizontalAlignment.Right,
    .Width = 200,
    .Height = 100
}

pdf.ApplyStamp(htmlStamper)

' Create image stamper
Dim imageStamper As New ImageStamper(New Uri("___PROTECTED_URL_247___")) With {
    .VerticalAlignment = VerticalAlignment.Top,
    .HorizontalAlignment = HorizontalAlignment.Left,
    .MaxWidth = New Length(150),
    .MaxHeight = New Length(150)
}

' Stamp the image stamper on specific pages
pdf.ApplyStamp(imageStamper, {0, 2, 4})

pdf.SaveAs("stamped.pdf")
$vbLabelText   $csharpLabel

iTextPdf

using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;

public void StampPDF(string inputPdfPath, string outputPdfPath, string stampText)
{
    PdfDocument pdfDoc = new PdfDocument(new PdfReader(inputPdfPath), new PdfWriter(outputPdfPath));

    Document doc = new Document(pdfDoc);

    // Add stamp (text) to each page
    int numPages = pdfDoc.GetNumberOfPages();
    for (int i = 1; i <= numPages; i++)
    {
        doc.ShowTextAligned(new Paragraph(stampText),
                            36, 36, i, iText.Layout.Properties.TextAlignment.LEFT,
                            iText.Layout.Properties.VerticalAlignment.TOP, 0);
    }

    doc.Close();
}
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;

public void StampPDF(string inputPdfPath, string outputPdfPath, string stampText)
{
    PdfDocument pdfDoc = new PdfDocument(new PdfReader(inputPdfPath), new PdfWriter(outputPdfPath));

    Document doc = new Document(pdfDoc);

    // Add stamp (text) to each page
    int numPages = pdfDoc.GetNumberOfPages();
    for (int i = 1; i <= numPages; i++)
    {
        doc.ShowTextAligned(new Paragraph(stampText),
                            36, 36, i, iText.Layout.Properties.TextAlignment.LEFT,
                            iText.Layout.Properties.VerticalAlignment.TOP, 0);
    }

    doc.Close();
}
Imports iText.Kernel.Pdf
Imports iText.Layout
Imports iText.Layout.Element

Public Sub StampPDF(ByVal inputPdfPath As String, ByVal outputPdfPath As String, ByVal stampText As String)
	Dim pdfDoc As New PdfDocument(New PdfReader(inputPdfPath), New PdfWriter(outputPdfPath))

	Dim doc As New Document(pdfDoc)

	' Add stamp (text) to each page
	Dim numPages As Integer = pdfDoc.GetNumberOfPages()
	For i As Integer = 1 To numPages
		doc.ShowTextAligned(New Paragraph(stampText), 36, 36, i, iText.Layout.Properties.TextAlignment.LEFT, iText.Layout.Properties.VerticalAlignment.TOP, 0)
	Next i

	doc.Close()
End Sub
$vbLabelText   $csharpLabel

IronPDF 的圖像和文字蓋章方法簡單易用,用途廣泛,可以輕鬆地在 PDF 頁面上添加品牌內容或標籤。 此 API 使用熟悉的 HTML/CSS 樣式元素,方便使用者進行自訂。 對於複雜的印章需求,IronPDF可有效地支援多個印章Google Fonts 整合Web 字體。 該庫還支援直接在 PDF 上繪製文字和點陣圖,並支援條碼蓋章以進行庫存追蹤。 如需進行精確控制,請探索"平移 PDF 物件""縮放 PDF 物件"功能。 iTextPdf 還提供圖像和文字蓋章功能,但配置需要更多手動設定和 PDF 佈局結構方面的知識。 雖然 iTextPdf 的設定需要更多精力,但它能夠直接在 PDF 頁面上操作內容,從而提供可靠的標記工具。

這兩個函式庫都能將 DOCX 轉換為 PDF 嗎?

在某些專案中,需要將 DOCX 檔案轉換為 PDF 格式。 以下比較 IronPDF 和 iTextPdf 處理此任務的方式,並著重指出它們之間的差異。 IronPDF 提供DOCX 到 PDF 的原生轉換功能,並支援包括RTF 文件在內的其他格式。

IronPDF

using IronPdf;

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

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

// Advanced rendering with options
renderer.RenderingOptions.MarginTop = 50;
renderer.RenderingOptions.MarginBottom = 50;

// Handle mail merge
var mergeFields = new Dictionary<string, string>
{
    ["Name"] = "John Doe",
    ["Date"] = DateTime.Now.ToShortDateString()
};
PdfDocument mergedPdf = renderer.RenderDocxAsPdf("template.docx", mergeFields);

// 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");

// Advanced rendering with options
renderer.RenderingOptions.MarginTop = 50;
renderer.RenderingOptions.MarginBottom = 50;

// Handle mail merge
var mergeFields = new Dictionary<string, string>
{
    ["Name"] = "John Doe",
    ["Date"] = DateTime.Now.ToShortDateString()
};
PdfDocument mergedPdf = renderer.RenderDocxAsPdf("template.docx", mergeFields);

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

' Instantiate Renderer
Dim renderer As New DocxToPdfRenderer()

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

' Advanced rendering with options
renderer.RenderingOptions.MarginTop = 50
renderer.RenderingOptions.MarginBottom = 50

' Handle mail merge
Dim mergeFields As New Dictionary(Of String, String) From {
    {"Name", "John Doe"},
    {"Date", DateTime.Now.ToShortDateString()}
}
Dim mergedPdf As PdfDocument = renderer.RenderDocxAsPdf("template.docx", mergeFields)

' Save the PDF
pdf.SaveAs("pdfFromDocx.pdf")
$vbLabelText   $csharpLabel

iTextPdf

與 IronPDF 不同,iTextPdf 缺乏將 DOCX 轉換為 PDF 的內建支援。 要執行此轉換,開發人員必須依賴 DocX 或 Aspose.Words 等第三方程式庫,首先將 DOCX 檔案轉換為 PDF 相容格式,然後使用 iTextPdf 進行處理或修改。

IronPDF 提供了一個簡單易用的內建解決方案,將 DOCX 轉換為 PDF ,無需額外的程式庫。 這使其非常適合需要快速、整合轉換的開發人員。 IronPDF 也支援將舊版文件格式的RTF 轉換為 PDF ,將文件工作流程的Markdown 轉換為 PDF ,將結構化資料的XML 轉換為 PDF ,以及將影像轉換為 PDF,包括多幀 TIFF 支援。 為了改善文件處理,請探索郵件合併功能目錄產生功能。 相較之下,iTextPdf 依賴外部程式庫進行 DOCX 轉換,需要額外的設定和依賴項,這增加了專案的複雜性。

IronPDF 和 iTextPdf 對 Bootstrap 和現代 CSS 的支持程度如何?

從 Bootstrap 風格的 Web 應用程式產生 PDF 時,完整的框架支援可確保設計一致性,而無需並行 CSS 檔案或佈局修改。 了解HTML 到 PDF 的分頁符號響應式設計注意事項對於專業輸出至關重要。 為了獲得更好的效果,請使用CSS 媒體類型自訂渲染延遲

IronPDF是否支援現代CSS框架?

IronPDF 的 Chromium 引擎提供以下方面的全面支援: Bootstrap 5:完整的 Flexbox 佈局、CSS Grid、實用類別、所有元件 Bootstrap 4:完整的卡片系統、導航、彈性實用程式、響應式設計

  • Tailwind CSS:所有實用類別都支援瀏覽器精確渲染。 -基礎:完整的網格系統和元件庫 -現代CSS3: Flexbox、CSS Grid、自訂屬性、動畫、轉場效果

實際驗證:IronPDF 使用其響應式 CSS 功能,以像素級精度渲染Bootstrap 主頁和所有官方範例。 該庫還支援自訂紙張尺寸視口控制,以改善佈局渲染效果。 對於複雜的佈局,請使用渲染選項頁面方向設定。 該庫支援國際語言和UTF-8編碼,適用於全球應用。

iTextPdf的CSS有哪些限制?

iTextPDF 使用 pdfHTML 並選擇性地支援 CSS3:

  • Flexbox 支援有限:已在 7.1.15 版本中添加,但尚不完整 -不支援 CSS Grid:不支援基於網格的 Bootstrap 佈局。 Bootstrap 3 的限制:現代 Bootstrap 4/5 元件需要變通方法。 -手動佈局轉換:複雜的佈局通常需要 PDF 專用程式碼。

iTextPdf 的文檔明確指出,高級 CSS 功能可能無法如預期渲染,因此開發人員需要測試每個 Bootstrap 元件,並且通常需要建立簡化的佈局。

開發影響:團隊必須維護單獨的 PDF 生成佈局程式碼,或對 Bootstrap 元件進行廣泛的測試和修改,這會增加開發時間並降低設計一致性。

有關 Bootstrap 框架的完整指南和 CSS3 渲染功能,請參閱Bootstrap 和 Flexbox CSS 指南。 其他資源包括像素級精確的格式設定技巧字體管理以及用於保持一致排版的字體間距調整解決方案。 對於 Web 應用程序,可以探索ASPX 到 PDF 的轉換Angular.js 的 PDF 生成功能。

程式碼範例告訴我們 IronPDF 與 iTextPdf 有哪些不同?

! IronPDF 和 iTextPDF 庫的功能對比表,展示了 PDF 操作功能,包括 HTML 轉換、加密、編輯、簽名、水印、蓋章和 DOCX 支援。

如需更詳細的範例,請造訪IronPDF 範例或瀏覽程式碼範例庫示範部分提供了 IronPDF 功能的互動式範例。 其他資源包括並行 PDF 生成多執行緒處理以及高效能應用程式的非同步範例

IronPDF 和 iTextPdf 在定價和許可方面有何區別?

IronPDF有哪些定價方案?

IronPDF提供不同等級的許可證和附加功能供用戶購買。 開發者還可以購買Iron Suite ,以兩份產品的價格獲得所有 Iron Software 產品。 如果您尚未準備好購買許可證,IronPDF 提供包含所有功能的免費試用版供您評估。 許可頁面提供了所有可用選項的詳細資訊。 對於企業部署,請探索許可證密鑰管理以程式設計方式設定許可證密鑰

-永久許可證:根據您的團隊規模、專案需求和地點數量,提供一系列永久許可證。 每種許可證類型都包含電子郵件支援服務。

  • Lite 許可證:此許可證價格為$799 ,支援一名開發者、一個地點和一個專案。
  • Plus 授權:支援三個開發者、三個地點和三個項目,這是比精簡版授權更進一步的版本,價格為$1,199 。 Plus 許可證除了提供基本的電子郵件支援外,還提供聊天支援和電話支援。 -專業許可證:此許可證適用於較大的團隊,支援十名開發人員、十個地點和十個項目,價格為$2,399 。 它提供與前幾個層級相同的聯絡支援管道,但也提供螢幕共享支援。 -免版稅再分發: IronPDF 的許可還提供免版稅再分發保障,只需額外支付$2,399即可。 -不間斷的產品支援: IronPDF 提供持續的產品更新、安全功能升級以及來自其工程團隊的支持,價格為每年 999 美元,或一次性購買 1,999 美元即可獲得 5 年的保障。
  • Iron Suite:只需$1,498 ,即可存取所有 Iron Software 產品,包括 IronPDF、IronOCR、IronWord、IronXL、IronBarcode、IronQR、IronZIP、IronPrint 和 IronWebScraper。

IronPDF 的價格比較顯示,它提供三種永久許可等級(Lite、Plus 和 Professional),價格從 749 美元到 2,999 美元不等,Iron Suite 套裝促銷活動則以 2 件產品的價格提供 9 件產品。

iTextPdf如何處理授權問題?

  • AGPL 許可證: iTextPDF Core 庫是根據 AGPL 許可證開源的,在特定條件下可以免費使用。 開發者必須以相同的授權協議發布任何修改版本。 -商業許可:對於不符合 AGPL 條件的開發者,iTextPDF 透過報價模式提供商業許可。

有關許可升級和過渡,請造訪IronPDF 許可升級許可證金鑰應用指南有助於實施,許可證金鑰配置提供部署選項。 若要解決授權問題,請參閱"無法連線至授權伺服器""授權擴充功能"

IronPDF 和 iTextPdf 提供哪些文件和支援?

-全面的文檔:內容豐富且易於使用的文檔,涵蓋了它所提供的所有功能。

  • 24/5 支援:提供線上工程師支援。 -影片教學: YouTube 上提供逐步影片指南。 -社群論壇:活躍的社群成員可獲得更多支援。 -定期更新:每月進行產品更新,以確保最新功能和安全性修補程式。

-完整文件:內容詳盡、易於使用的文檔,涵蓋所有功能 -提供 24/5 全天候支援:可提供積極的工程支援。 -影片教學YouTube上的逐步影片指南 -社群論壇:活躍的社群成員可透過 Stack Overflow 和 GitHub 獲得更多支援。 -定期更新:每月產品更新確保提供最新功能和安全補丁 -程式碼範例:包含大量實際應用程式碼範例的程式庫 -故障排除指南:提供詳細的快速故障排除指南,並針對特定平台的問題進行解答。

對於詳細的故障排除,IronPDF 提供了常見問題的指南,例如AWS 部署Azure 配置記憶體管理Docker 整合IIS 設定效能最佳化。 平台特定指南涵蓋Windows 部署Linux 設定macOS 支援。 其他資源包括工程請求指南Azure 日誌管理AWS 日誌檔案安全性 CVE 資訊

-文件: iText PDF 文件全面涵蓋了所有可用功能。 -範例和教學:程式碼範例和教學課程幫助開發人員入門。

  • GitHub 社群:開發者可以回報問題、提交拉取請求,並與 iTextPDF 團隊互動。 -定期更新: iTextPDF 提供頻繁的更新和改進。

有關 IronPDF 文件和支援的更多詳細信息,請訪問IronPDF 文件IronSoftware YouTube 頻道。 瀏覽操作指南以了解特定功能,查看API 參考以了解詳細實現,以及學習教學以了解完整知識。 VB.NET PDF 教學課程F# PDF 函式庫指南提供了特定語言的資源。 對於現代應用程式框架,請探索Blazor 教學課程MAUI PDF 檢視器XAML 到 PDF 的轉換。 ## 高階 .NET 開發人員應該選擇哪個 PDF 函式庫?

在 .NET 的 PDF 處理工具領域, IronPDF和 iTextPdf 都為開發人員提供了有價值的解決方案。 IronPDF 的顯著特點是能夠輕鬆整合到 .NET 平台,並具備無需外部依賴即可將 DOCX 轉換為 PDF 等使用者友善功能。 對於進階渲染需求,IronPDF 支援JavaScript 執行WebGL 支援以及動態內容的自訂渲染延遲。 該程式庫還包括JavaScript 訊息監聽器和用於互動式 PDF 的自訂 JavaScript 執行。 為處理複雜的渲染場景,探索網路空閒等待渲染延遲配置初始渲染優化。 相較之下,iTextPdf 以其多功能性和豐富的功能而聞名,尤其是在與其他工具結合使用時,仍然非常有效,儘管它需要額外的依賴項才能進行 DOCX 轉換。

為了提高效能,IronPDF 提供非同步處理並行生成多執行緒支持,以高效處理大量 PDF 操作。 該庫還提供高級功能,例如PDF 壓縮線性化以實現快速網頁瀏覽,以及符合 PDF/A歸檔要求。 其他企業功能包括支援 ZUGFeRD 的 PDF/A-3電子發票、符合 PDF/UA無障礙標準的 PDF 以及PDF 版本控制。 對於雲端部署,IronPDF 提供遠端引擎選項Docker 容器支援以及原生引擎與遠端引擎的比較

最終,在 IronPDF 和 iTextPdf 之間進行選擇取決於專案的特定需求、許可偏好和所需的支援程度。 這兩個函式庫都提供了可靠的方法來簡化 .NET 應用程式中的 PDF 工作流程。 對於注重現代 Web 標準和易用性的開發者而言,IronPDF 基於 Chrome 的渲染功能和完整的 API 使其成為絕佳之選。該庫提供的PDF 創建轉換整理教程提供了詳盡的指導。 對於專業的文件處理,請探索PDF 報告產生讀取 PDF 文字以及PDF 到 HTML 的轉換。 其他功能包括紙張列印網路印表機支援光柵化為影像Base64 轉換。 需要 AGPL 授權或進行大量底層 PDF 操作的使用者可能會發現 iTextPdf 更合適。

如需與其他 PDF 庫進行進一步比較,請探索Aspose 與 IronPDFApryse 與 IronPDFQuestPDF 與 IronPDF以及Syncfusion 與 IronPDF 的比較,以便根據您的 PDF 開發需求做出明智的決定。 有關部署指導,請查看IronPDF 安裝程式選項軟體安裝程式整合部署最佳實務。 該庫的里程碑突出了持續改進,包括Chrome 渲染增強相容性更新PDFium DOM 更新穩定性改進

請注意iTextPdf 是其各自所有者的註冊商標。 本網站與 iTextPdf 無任何關聯,也未獲得其認可或贊助。 所有產品名稱、標誌和品牌均為其各自所有者的財產。 文中比較僅供參考,反映的是撰寫本文時可公開取得的資訊。

常見問題解答

如何在 C# 中將 HTML 轉換為 PDF?

您可以使用 IronPDF 的 RenderHtmlAsPdf 方法將 HTML 字串轉換成 PDF。您也可以使用 RenderHtmlFileAsPdf 將 HTML 檔案轉換成 PDF。

IronPDF 在 PDF 創建和處理方面提供哪些功能?

IronPDF 提供 HTML 至 PDF 轉換、PDF 合併、加密、數位簽名、水印以及 DOCX 至 PDF 轉換等功能。

IronPDF 如何簡化 PDF 簽署?

IronPDF 允許您使用 X509 憑證對 PDF 檔案套用數位簽章,提供一種直接的方法來保護您的文件。

IronPdf 可以在不同的作業系統上使用嗎?

是的,IronPDF 支持跨平台兼容性。它可以在 Windows、Linux 和 Mac 上無縫運作,並與 .NET Core、.NET Standard 和 .NET Framework 相容。

在文件加密方面,iTextPDF 與 IronPDF 有何不同?

iTextPDF 提供加密標準的詳細設定,而 IronPDF 則透過容易實作的內建加密方法簡化流程。

IronPdf 用戶可獲得哪些支援資源?

IronPDF 透過全面的文件、視訊教學、社群論壇和 24/5 工程師支援,提供廣泛的支援。

IronPDF 如何處理 DOCX 至 PDF 的轉換?

IronPDF 包含 DOCX 至 PDF 的原生轉換功能,可無縫整合而不需要額外的函式庫。

IronPDF 有哪些授權選項?

IronPDF 提供 Lite、Plus、Professional 等不同層級的永久 License,以及免權利金再發行和持續產品支援的選項。

如何使用 IronPDF 為 PDF 套用水印?

IronPdf 可讓您使用 HTML 和 CSS 輕鬆套用水印,使您能夠快速新增文字或圖片水印,並根據需要進行自訂。

IronPDF 為什麼適合使用 .NET 應用程式的開發人員?

IronPDF 採用簡化的 API 設計,對使用者友善,使其能有效率地執行 HTML 轉換為 PDF、加密和數位簽章等工作,非常適合 .NET 開發人員使用。

Curtis Chau
技術撰稿人

Curtis Chau 擁有電腦科學學士學位(卡爾頓大學),專長於前端開發,精通 Node.js、TypeScript、JavaScript 和 React。Curtis 對製作直覺且美觀的使用者介面充滿熱情,他喜歡使用現代化的架構,並製作結構良好且視覺上吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 也有濃厚的興趣,他喜歡探索整合硬體與軟體的創新方式。在空閒時間,他喜歡玩遊戲和建立 Discord bots,將他對技術的熱愛與創意結合。

iText Logo

厭倦了昂貴的續費和過時的產品更新嗎?

iText 輕鬆轉換為我們的工程遷移支援和更優惠的價格。

IronPDF Logo