产品比较

IronPDF与iText7的比较

Chipego
奇佩戈-卡琳达
2024年八月11日
分享:

介绍

一个体面且简单的PDF操作工具可以简化创建和编辑PDF文档所涉及的许多任务和过程。 在 .NET 生态系统中,有两个流行的库 -IronPDF和 iText —— 允许无需任何Adobe依赖即可生成PDF。 他们都具有多种功能,如创建、编辑、转换等,但本文将侧重于根据三个标准比较这些库:他们所提供的功能的能力、他们提供的文档质量、这些公司采用的产品使用定价政策。

IronPDF 和 iText 概述

IronPDF是一个杰出的.NET PDF库,它使程序员能够轻松创建、修改和与PDF文档进行交互。 它可以用于不同的 .NET 环境,包括 Core、8、7、6 和 Framework,这使得它非常灵活,适用于各种开发需求。 IronPDF is its ability to generate PDFs from HTML content dynamically, providing developers with a powerful tool for creating rich-document solutions.IronPDF的丰富特性,例如HTML转PDF转换、合并PDF、PDF加密以及应用数字签名等。 该文档的编写方式使用户可以轻松理解,同时库本身具有强大的技术支持。

iText 是一个非常受欢迎的 PDF 库,适用于 Java 和 .NET(C#). iText Core 8 提供了一个企业级可编程解决方案,用于创建和操作 PDF 文件。 iText支持许多不同的功能,并在开源许可证下发布。(AGPL)许可证和商业许可证。 这意味着由于其多功能性,它能够在数字化转型项目中覆盖广泛的用例。

跨平台兼容性

IronPDF和iText兼容各种平台; 他们可以在多种不同的系统以及在.NET框架内处理PDF文件。 因此,我们将在下面比较每个产品支持的框架和平台。

IronPDF:

IronPDF 支持平台概述支持多种平台和环境,确保在各种系统中无缝集成和部署:

  • .NET 版本:

    请注意,您提供的内容是空的。请提供要翻译的文本。(C#, VB.NET, F#)

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

    • .NET标准(2.0+)
  • .NET 框架(4.6.2+)
  • 应用环境: IronPDF支持包括Windows、Linux、Mac、Docker、Azure和AWS在内的应用环境。
  • IDEs(集成开发环境): 适用于Microsoft Visual Studio和JetBrains Rider & ReSharper等IDEs。
  • 操作系统和处理器: 支持多种不同的操作系统和处理器,包括Windows、Mac、Linux、x64、x86、ARM。

iText

  • .NET 版本:

    • .NET Core(2.x, 3.x)
  • .NET 框架(4.6.1+)
  • .NET 5+
  • 应用程序环境:iText 支持多种应用程序环境,得益于其对 Java 和 .NET 的支持。(C#), 这些包括; Windows、Mac、Linux 和 Docker。
  • 操作系统: 支持 Windows、macOS 和 Linux 操作系统

关键功能比较:IronPDF 与 iText

IronPDF 主要功能列表和 iText 都提供了一系列可以用来处理PDF文件的功能和工具; 接下来的部分将重点介绍这些功能的一些内容,并比较两个库在执行不同的PDF相关任务时的表现。

IronPDF

  • HTML 转 PDF 转换:支持 HTML、CSS、JavaScript 和图片。
  • PDF文件操作:拆分然后合并文档,更改和编辑现有PDF文档的格式
  • 安全性:PDF 加密与解密。
  • 编辑:添加注释、书签和大纲。
  • 模板:应用页眉、页脚和页码。
  • 水印添加:轻松地在PDF文件中添加文本和图片水印; 利用它使用HTML/CSS来完全控制该过程。
  • PDF盖章: 使用IronPDF在您的PDF文档上盖章图片和文字。

    有关 IronPDF 提供的广泛功能集的更多信息,请访问IronPDF功能页面.

iText

  • PDF创建:支持从头开始创建PDF文档。
  • 表单:创建和编辑PDF表单。
  • 数字签名:签署PDF文档。
  • 压缩:优化PDF文件大小。
  • 内容提取:从PDF中提取文本和图像。
  • 开源:根据AGPL许可证提供。
  • 可定制性:针对高级用例的高度可定制性。

IronPDF 与 iText PDF 功能特性比较

HTML 转换为 PDF

转换使用 IronPDF 将 HTML 内容转化为 PDF这是在许多不同的办公室和工作空间中进行的非常简单的任务。 以下是比较IronPDF和iText如何处理此过程的代码示例。

IronPDF

using IronPdf;

// Disable local disk access or cross-origin requests
Installation.EnableWebSecurity = true;

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

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

// Advanced Example with HTML Assets
// Load external html assets: images, CSS and JavaScript.
// An optional BasePath 'C:\site\assets\' is set as the file location to load assets from
var myAdvancedPdf = renderer.RenderHtmlAsPdf("<img src='icons/iron.png'>", @"C:\site\assets\");
myAdvancedPdf.SaveAs("html-with-assets.pdf");
using IronPdf;

// Disable local disk access or cross-origin requests
Installation.EnableWebSecurity = true;

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

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

// Advanced Example with HTML Assets
// Load external html assets: images, CSS and JavaScript.
// An optional BasePath 'C:\site\assets\' is set as the file location to load assets from
var myAdvancedPdf = renderer.RenderHtmlAsPdf("<img src='icons/iron.png'>", @"C:\site\assets\");
myAdvancedPdf.SaveAs("html-with-assets.pdf");

iText

using iText.Html2pdf;

static void Main(string[] args)
  {
    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;

static void Main(string[] args)
  {
    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);
    }
  }

在将 HTML 转换为 PDF 时,IronPDF 提供了一个简洁方便的工具来执行此任务。 使用 ChromePdfRenderer 将 HTML 内容转换为 PDF,IronPDF 在提供像素完美的 PDF 文档方面表现卓越。 用户可以直接从HTML字符串创建PDF,如第一个示例所示,或者包含外部资源,如图像,并附带一个可选的基本路径,如高级示例中所演示的。 iText,通过使用其 HtmlConverter 类将 HTML 文件转换为 PDF 文档,采取了一种基本方法。

加密PDF文件

使用 IronPDF 加密 PDF 文件PDF文档的加密和解密在许多工作场所至关重要。 要轻松处理这项任务,就必须有一个便捷的工具。 在下面的代码中,我们将看到iText和IronPDF如何处理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");

iText

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

public class EncryptPdf
    {
        public static readonly String DEST = "results/sandbox/security/encrypt_pdf.pdf";
        public static readonly String SRC = "../../../resources/pdfs/hello.pdf";

        public static readonly String OWNER_PASSWORD = "World";
        public static readonly String USER_PASSWORD = "Hello";

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

            new EncryptPdf().ManipulatePdf(DEST);
        }

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

public class EncryptPdf
    {
        public static readonly String DEST = "results/sandbox/security/encrypt_pdf.pdf";
        public static readonly String SRC = "../../../resources/pdfs/hello.pdf";

        public static readonly String OWNER_PASSWORD = "World";
        public static readonly String USER_PASSWORD = "Hello";

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

            new EncryptPdf().ManipulatePdf(DEST);
        }

        protected void ManipulatePdf(String dest)
        {
            PdfDocument document = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest,
                new WriterProperties().SetStandardEncryption(
                    Encoding.UTF8.GetBytes(USER_PASSWORD),
                    Encoding.UTF8.GetBytes(OWNER_PASSWORD),
                    EncryptionConstants.ALLOW_PRINTING,
                    EncryptionConstants.ENCRYPTION_AES_128 
 EncryptionConstants.DO_NOT_ENCRYPT_METADATA
                )));
            document.Close();
        }
    }

IronPDF 为用户提供了一种直接的方式来加密 PDF 文件,同时也允许用户充分控制过程,包括编辑元数据和调整安全设置,例如将文档设置为只读或限制用户操作,如复制和粘贴。 另一方面,iText 使用较低级别且较长的方法,在文档创建过程中应用PDF加密,指定所有者和用户密码以及使用像AES-128这样的加密标准来设定权限,例如打印权。

编辑 PDF 内容

在处理机密或私人信息时,有时可能需要使用 IronPDF 编辑 PDF 中的文本PDF文件的部分内容。下面的代码示例将演示如何使用IronPDF与iText相比红act文字。

IronPDF

using IronPdf;

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

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

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

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

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

pdf.SaveAs("redacted.pdf");

iText

using System;
using System.IO;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
using iText.Kernel.Colors;

string src = "input.pdf";
string dest = "output_redacted.pdf";

using (PdfReader reader = new PdfReader(src))
            {
                using (PdfWriter writer = new PdfWriter(dest))
                {
                    using (PdfDocument pdfDoc = new PdfDocument(reader, writer))
                    {
                        // Iterate through each page
                        for (int pageNum = 1; pageNum <= pdfDoc.GetNumberOfPages(); pageNum++)
                        {
                            PdfPage page = pdfDoc.GetPage(pageNum);
                            PdfCanvas canvas = new PdfCanvas(page);
                            Rectangle[] rectanglesToRedact = { new Rectangle(100, 100, 200, 50) }; // Example: Define rectangles to redact

                            // Overlay black rectangles to simulate redaction
                            foreach (Rectangle rect in rectanglesToRedact)
                            {
                                canvas.SetFillColor(ColorConstants.BLACK)
                                    .Rectangle(rect.GetX(), rect.GetY(), rect.GetWidth(), rect.GetHeight())
                                    .Fill();
                            }
                        }
                    }
                }
using System;
using System.IO;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
using iText.Kernel.Colors;

string src = "input.pdf";
string dest = "output_redacted.pdf";

using (PdfReader reader = new PdfReader(src))
            {
                using (PdfWriter writer = new PdfWriter(dest))
                {
                    using (PdfDocument pdfDoc = new PdfDocument(reader, writer))
                    {
                        // Iterate through each page
                        for (int pageNum = 1; pageNum <= pdfDoc.GetNumberOfPages(); pageNum++)
                        {
                            PdfPage page = pdfDoc.GetPage(pageNum);
                            PdfCanvas canvas = new PdfCanvas(page);
                            Rectangle[] rectanglesToRedact = { new Rectangle(100, 100, 200, 50) }; // Example: Define rectangles to redact

                            // Overlay black rectangles to simulate redaction
                            foreach (Rectangle rect in rectanglesToRedact)
                            {
                                canvas.SetFillColor(ColorConstants.BLACK)
                                    .Rectangle(rect.GetX(), rect.GetY(), rect.GetWidth(), rect.GetHeight())
                                    .Fill();
                            }
                        }
                    }
                }

IronPDF的涂改工具简洁易用,只需几行代码即可简化涂改过程。 这有助于提高PDF编辑任务的效率,并为用户提供一种简单的方式来保护他们的敏感和私人数据安全。

iText,另一方面,与IronPDF不同,它没有提供内置的涂改工具。 但是,它仍然可以使用上面显示的方法来覆盖用户希望编辑的内容中的敏感数据。 这可能会导致问题,因为这些矩形并没有实际删除或正确涂改文本,这意味着其他人可能还能复制和粘贴试图被涂改的数据。

签署PDF文档

能够使用 IronPDF 对 PDF 文档进行数字签名在翻译过程中,我们可以使用 PDF 等文件,然后使其成为一个自动流程,这样可以节省时间。 以下是一些代码片段,将比较IronPDF与iText在执行文档数字签名方面的不同。

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

iText

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

在数字方式给PDF文件签名时,IronPDF 提供了一个简洁而强大的工具来完成这个过程。 其简便性使得这一过程能够迅速进行,为实施此签名需求的开发者节省时间。 iText 在应用数字签名到 PDF 文件时需要一个更长、更复杂的过程,虽然使用不同的界面选项和密钥确实为用户提供了更多的控制权,但这个工具执行这一任务的复杂性可能会阻碍它。

将水印应用于PDF文档

添加和个性化的能力使用 IronPDF 在 PDF 上添加水印通过该软件,可以大大有助于保密、版权保护、品牌推广或任何其他涉及敏感信息的任务。 以下是IronPDF和iText如何在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");

iText

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();

            // Creating a dictionary that maps resource names to graphics state parameter dictionaries
            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();

            // Creating a dictionary that maps resource names to graphics state parameter dictionaries
            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();
        }
    }

IronPDF 的简单直观 API 让用户可以快速地对其 PDF 文件应用自定义水印,同时让他们完全控制整个过程。 它使用 HTML/CSS 进一步简化了过程,同时不失对自定义的控制。 iText向PDF添加水印的方法需要更多手动操作来完成任务,这可能会减慢过程。

在PDF上盖章图像和文字

需要处理 PDF 页面的时刻在 PDF 上用文字或图片盖章这就好比人们需要在 PDF 文件中添加水印一样。 我们现在将比较IronPDF和iText在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");

iText

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();
}

IronPDF 可以帮助您以非常灵活和可定制的方式在 PDF 上添加文本或图片; 它让您完全掌控。 操作 API 非常简单易懂,特别是对于熟悉 HTML/CSS 的开发者来说。 iText 使用其图像和文本印章工具,以便用户更好地控制其PDF文件上显示的内容,尽管这一过程可能会更加手动。

将DOCX转换为PDF

有时候,您可能需要将PDF文件从一种格式转换为另一种格式。 在这种情况下,我们需要使用 IronPDF 将 DOCX 转换为 PDF和比较IronPDF和iText在这个过程中的不同处理方式。

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

iText

iText本身无法处理DOCX到PDF的转换; 相反,它依赖于外部库,如DocX或Aspose.Words。

IronPDF 为开发人员提供了一个简洁明了的工具,用于处理 DOCX 到 PDF 的转换任务,使得将 DOCX 文件转换为 PDF 格式变得简单,无需外部库。 iText 则依赖外部库来完成这项任务。

代码示例比较摘要

iText C# HTML to PDF 替代软件

有关更详细的示例,请访问IronPDF 从 HTML 创建 PDF 的示例.

定价和许可:IronPDF versus iText Library

IronPDF 定价和许可

IronPDF 许可选项拥有不同级别和购买许可证的附加功能。 开发者也可以购买Iron Suite这让您可以以两个产品的价格访问Iron Software的所有产品。 如果您尚未准备购买许可证,IronPDF提供了一种免费试用版持续30天。

  • 永久许可证: 根据您团队的规模、项目需求和地点数量提供一系列永久许可证。 每种许可类型都包括电子邮件支持。
  • Lite License:此授权费用为 $749,支持一名开发人员、一个地点和一个项目。
  • Plus License(加强许可): 支持三名开发者、三个地点和三个项目,这是从轻量版许可升级的下一步,费用为$1,499。Plus许可还提供聊天支持和电话支持,除了基本的电子邮件支持。
  • 专业许可证: 此许可证适用于较大的团队,支持10名开发者、10个位置和10个项目,价格为2999美元。它提供与之前等级相同的联系支持渠道,但也提供屏幕共享支持。
  • 免版税重新分发: IronPDF 的许可还提供额外 $1,999 的免版税重新分发覆盖。
  • 持续不断的产品支持: IronPDF为用户提供持续的产品更新、安全功能升级以及他们工程团队的支持,费用为每年999美元或一次性支付1999美元,享受5年的服务。
  • Iron Suite: 只需 $1,498,您即可访问所有Iron Software产品,包括IronPDF、IronOCR、IronWord、IronXL、IronBarcode、IronQR、IronZIP、IronPrint和IronWebScraper。

    IronPDF 许可比较

iText 许可证

  • AGPL 许可证: iText Core 库是开源的,免费提供给用户。 要在此许可模型下使用iText,用户必须遵守其条款,且在此许可下对iText所做的任何修改也必须按照AGPL许可模型发布。
  • 商业许可证: iText为不符合AGPL条款的开发者项目提供商业许可模式,并通过报价模式定价。

文档和支持:IronPDF与iText

IronPDF

  • 全面文档:广泛且用户友好的文档,涵盖其提供的所有功能。
  • 24/5 支持:提供活跃的工程师支持。
  • 视频教程:详细的视频指南可在YouTube上找到。
  • 社区论坛:为了提供额外支持而设立的活跃社区。
  • 定期更新:每月产品更新,确保具备最新功能和安全补丁。

iText

  • 文档: 涵盖 iText 软件功能的深度文档。
  • 示例和教程: 有如何使用其各种功能的教程以及代码示例。
  • GitHub:开发者可以轻松地将他们遇到的任何问题或错误提交到 iText GitHub 仓库,并与 iText 团队进行交流。
  • 更新: iText 提供定期更新和改进。

    有关IronPDF文档和支持的更多详情,请访问IronPDF文档Iron Software YouTube频道.

结论

在.NET的PDF操作工具领域中,IronPDF和iText都提供了针对各种开发需求的强大功能。 IronPDF在多个平台(包括.NET Core、Framework 和 Standard)上的简单集成上脱颖而出,还包括诸如HTML到PDF转换和高级安全选项等全面功能。 另一方面,以其Java传统著称的iText提供强大的PDF生成和操作工具,支持开源和商业许可证,强调多功能性和可定制性。

在这些工具之间的选择最终取决于项目需求、许可偏好和所需的技术支持水平。 无论是选择 IronPDF 的简单灵活性还是选择开源 PDF 库 iText 的广泛功能集,开发人员都有充足的资源来有效地简化其应用中的 PDF 工作流程。

您可以尝试 0 天免费试用 查看他们的可用功能。

Chipego
软件工程师
Chipego 拥有出色的倾听技巧,这帮助他理解客户问题并提供智能解决方案。他在 2023 年加入 Iron Software 团队,此前他获得了信息技术学士学位。IronPDF 和 IronOCR 是 Chipego 主要专注的两个产品,但他对所有产品的了解每天都在增长,因为他不断找到支持客户的新方法。他喜欢 Iron Software 的合作氛围,公司各地的团队成员贡献他们丰富的经验,以提供有效的创新解决方案。当 Chipego 离开办公桌时,你经常可以发现他在看书或踢足球。
< 前一页
IronPDF与Textcontrol的对比
下一步 >
IronPDF与Winnovative PDF Library for .NET的比较