在實際環境中測試
在生產環境中測試無浮水印。
在任何需要的地方都能運作。
一個體面且簡單的 PDF 操作工具可以簡化創建和編輯 PDF 文件所涉及的許多任務和過程。 在 .NET 生態系統中,有兩個流行的庫——IronPDF以及 iText—允許在不依賴任何 Adobe 的情況下生成 PDF。 它們都具有多種功能,例如創建、編輯、轉換等,但本文將重點比較這些庫在三個標準上的表現:它們所提供功能的能力、它們提供的文檔質量、這些公司對其產品所採用的定價政策。
IronPDF 是一個著名的 .NET PDF 庫,允許程式設計師輕鬆創建、修改和操作 PDF 文件。 它可以用於不同的 .NET 環境,包括 Core、8、7、6 和 Framework,使其對於各種開發需求具有高度靈活性。 關鍵功能IronPDF其豐富的功能集包括 HTML 轉換為 PDF、合併 PDF 的能力、PDF 加密和應用數字簽名等。 文件以用户能够轻松理解的方式编写,同时该库本身具有强大的技术支持。
iText 是最受歡迎的 PDF 程式庫之一,適用於 Java 以及 .NET。(C#). iText Core 8 提供了一個企業級的可編程解決方案,用於創建和操作 PDF 文件。 iText 提供對許多不同功能的支援,並以開源形式發佈。(AGPL)授權和商業授權。 這意味著,由於其多功能性,它能夠在數位轉型專案中涵蓋廣泛的使用案例。
IronPDF 和 iText 相容於各種平台; 他們可以在許多不同的系統中處理 PDF,也可以在 .NET 框架內處理。 因此,我們將比較每個產品所支持的框架和平台。
IronPDF 支援平台概述支持廣泛的平台和環境,確保在各種系統中無縫整合和部署:
IronPDF 主要功能列表和 iText 都提供了一系列可以用來處理 PDF 文件的功能和工具; 接下來的這一部分將著重於深入瞭解這些功能,以及這兩個庫在執行不同PDF相關任務時的比較。
PDF 加蓋: 使用 IronPDF 在您的 PDF 文件上加蓋圖像和文字。
欲了解 IronPDF 所提供的廣泛功能的更多資訊,請造訪IronPDF 功能頁面.
轉換使用 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");
Imports IronPdf
' Disable local disk access or cross-origin requests
Installation.EnableWebSecurity = True
' Instantiate Renderer
Dim renderer = New ChromePdfRenderer()
' Create a PDF from an HTML string using C#
Dim 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
Dim 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);
}
}
Imports iText.Html2pdf
Shared Sub Main(ByVal args() As String)
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
在將 HTML 轉換為 PDF 時,IronPDF 提供了一個簡潔且方便的工具來執行此任務。 利用 ChromePdfRenderer 將 HTML 內容轉換為 PDF,IronPDF 在為用戶提供像素完美的 PDF 文件方面表現出色。 用戶可以直接從 HTML 字串建立 PDF,如第一個範例所示,或在高級範例中藉由可選的基準路徑包括外部資產,如圖像。 iText,採用基本的方法,使用其 HtmlConverter 類從 HTML 文件創建 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");
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")
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();
}
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
IronPDF 為使用者提供了簡單的方法來加密 PDF 文件,同時也讓使用者在過程中擁有充足的控制,包括編輯元數據及調整安全設置,例如將文件設為唯讀或限制使用者動作,如複製和貼上。 另一方面,iText 使用較低階且較長的方法,在文件創建過程中應用 PDF 加密,指定擁有者和用戶密碼以及使用像 AES-128 這樣的加密標準的列印權限。
偶爾在處理機密或私人信息時,可能需要使用 IronPDF 在 PDF 中編輯文字PDF 文件的部分內容。以下代碼範例將演示如何使用 IronPDF 與 iText 進行文字遮蔽。
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");
Imports IronPdf
Private pdf As PdfDocument = 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();
}
}
}
}
Imports System
Imports System.IO
Imports iText.Kernel.Pdf
Imports iText.Layout
Imports iText.Layout.Element
Imports iText.Layout.Properties
Imports iText.Kernel.Colors
Private src As String = "input.pdf"
Private dest As String = "output_redacted.pdf"
Using reader As New PdfReader(src)
Using writer As New PdfWriter(dest)
Using pdfDoc As New PdfDocument(reader, writer)
' Iterate through each page
Dim pageNum As Integer = 1
Do While pageNum <= pdfDoc.GetNumberOfPages()
Dim page As PdfPage = pdfDoc.GetPage(pageNum)
Dim canvas As New PdfCanvas(page)
Dim rectanglesToRedact() As Rectangle = { New Rectangle(100, 100, 200, 50) } ' Example: Define rectangles to redact
' Overlay black rectangles to simulate redaction
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
End Using
IronPDF 的修訂工具精簡且易於使用,只需幾行程式碼即可精簡修訂過程。 這有助於提高 PDF 編輯任務的效率,並為用戶提供輕鬆保護其敏感和私人數據的方式。
另一方面,iText 並不像 IronPDF 那樣提供內建的修訂工具。 但是,它仍然可以使用上面顯示的方法來覆蓋敏感數據,從而遮蓋使用者希望刪除的內容。 然而,這可能導致問題,因為這些矩形並未真正刪除或正確遮蔽文字,這意味著其他人可能會複製和貼上試圖被遮蔽的資料。
能夠使用 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");
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")
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}");
}
}
}
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
在對 PDF 文件進行數位簽名時,IronPDF 提供了一個精簡但功能強大的工具來完成此過程。 其簡單性使流程能夠快速進行,為任何開發人員節省時間,以滿足他們的簽署需求。 iText 需要更長且更複雜的過程來對 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");
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")
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();
}
}
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()
' Creating a dictionary that maps resource names to graphics state parameter dictionaries
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
IronPDF 簡單直觀的 API 允許用戶快速地對 PDF 文件套用自訂浮水印,同時讓他們在整個過程中擁有完全的控制權。 其利用 HTML/CSS 進一步簡化了過程,同時不失去對自訂的任何控制。 iText 對於在 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");
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")
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();
}
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
IronPDF 可以幫助您以非常靈活且可自訂的方式將文字或圖片添加到 PDF。 它讓您完全掌控。 對於熟悉 HTML/CSS 的開發人員來說,這個 API 容易理解和使用。 iText 利用其影像和文字加盖工具,让使用者对 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");
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")
iText
iText 本身無法處理 DOCX 到 PDF 的轉換; 相反,它依賴於外部庫,如 DocX 或 Aspose.Words。
IronPDF 為開發人員提供了一個簡單明瞭的工具,用於處理 DOCX 到 PDF 的轉換任務,使 DOCX 文件能輕鬆轉換為 PDF 格式,且不需要外部程式庫。 另一方面,iText 依賴於外部庫來執行此任務。
如需更詳細的範例,請造訪IronPDF 從 HTML 創建 PDF 的範例.
IronPDF 授權選項擁有不同的等級和額外功能可供購買許可證。 開發人員還可以購買IronSuite可以讓您以兩個產品的價格使用Iron Software的所有產品。 如果您還沒有準備好購買許可證,IronPDF 提供一個免費試用版持續30天。
IronSuite:只需 1,498 美元,即可使用包括 IronPDF、IronOCR、IronWord、IronXL、IronBarcode、IronQR、IronZIP、IronPrint 和 IronWebScraper 在內的所有 Iron Software 產品。
更新: iText 提供定期的更新和改進。
如需有關 IronPDF 文件和支援的更多詳細資訊,請造訪IronPDF 文件檔案和該Iron Software YouTube 頻道.
在 .NET 的 PDF 操作工具領域中,IronPDF 和 iText 都提供了針對各種開發需求的強大功能。 IronPDF 憑藉其在多種平台上的簡單整合脫穎而出,包括 .NET Core、Framework 和 Standard,以及全面的功能,例如 HTML 到 PDF 的轉換和高級安全選項。 另一方面,iText 因其 Java 來源而聞名,提供強大的 PDF 生成和操作工具,涵蓋開源和商業許可,強調多功能性和自定義。
選擇這些工具最終取決於專案需求、授權偏好以及所需的技術支援水準。 無論選擇 IronPDF 的簡單性和靈活性,還是功能豐富的開源 PDF 庫 iText,開發人員都有足夠的資源在其應用程式中有效地簡化 PDF 工作流程。
您可以嘗試使用 0 天免費試用 查看他們的可用功能。