
如何在不降低質量的情況下減少PDF文件大小(在線工具)
浮水印是PDF文件中的一個重要元素,提供所有權、真實性或機密性的視覺指示。 它們可以阻止未經授權的使用並協助保護智慧財產權,對於企業和個人來說都至關重要。在本文中,我們將比較兩個強大的程式庫——IronPDF和QuestPDF,重點介紹它們用於在C#中新增浮水印到PDF文件的功能。
IronPDF概述
主要特點
IronPDF是一個強大的PDF程式庫,能夠讓開發者無縫地建立、編輯和操作PDF文件。 與浮水印相關的主要特點包括:
- **靈活的浮水印:**支援文字和圖像浮水印,允許在字體、大小、顏色和透明度方面進行自定義。
- **簡單的整合:**與.NET應用程式相容,使其可以直接在現有項目中實施。
- **豐富的格式選擇:**提供廣泛的浮水印樣式選擇,增強了文件的視覺吸引力。
- **轉換工具:**將HTML、URL、圖像等轉換為PDF格式。
安裝和設置
要開始使用IronPDF,請按照以下步驟進行:
-
在您的套件管理控制台中通過運行以下命令來安裝 IronPDF NuGet 套件:
-
在您的C#文件中新增必要的命名空間:
using IronPdf;Imports IronPdf
使用IronPDF將浮水印新增到PDF文件中
IronPDF利用HTML字串和CSS樣式來向PDF文件新增完全可自定義的浮水印。 浮水印工具可以採用任何HTML字串,即使它包含如圖像和CSS樣式等資產,並將其應用到PDF文件作為浮水印。
using IronPdf;
class Program
{
static void Main()
{
// Load an existing PDF document.
PdfDocument pdf = PdfDocument.FromFile("existing.pdf");
// Define the watermark using HTML and CSS.
string watermark = "<img src='https://ironsoftware.com/img/products/ironpdf-logo-text-dotnet.svg'><h1 style='color:red;'>CONFIDENTIAL</h1>";
// Apply the watermark with specified rotation and opacity.
pdf.ApplyWatermark(watermark, rotation: 45, opacity: 80);
// Save the watermarked document.
pdf.SaveAs("watermarked.pdf");
}
}Imports IronPdf
Friend Class Program
Shared Sub Main()
' Load an existing PDF document.
Dim pdf As PdfDocument = PdfDocument.FromFile("existing.pdf")
' Define the watermark using HTML and CSS.
Dim watermark As String = "<img src='https://ironsoftware.com/img/products/ironpdf-logo-text-dotnet.svg'><h1 style='color:red;'>CONFIDENTIAL</h1>"
' Apply the watermark with specified rotation and opacity.
pdf.ApplyWatermark(watermark, rotation:= 45, opacity:= 80)
' Save the watermarked document.
pdf.SaveAs("watermarked.pdf")
End Sub
End Class輸出PDF文件
如您所見,我們建立了一個新的字串變數,其中包含了我們的浮水印內容。 這是一個包含標題和圖像的HTML字串。 當我們使用ApplyWatermark方法時,我們可以設置自定義旋轉和不透明度。
如果您想了解更多高級範例以及IronPDF提供的其他功能,請務必查看操作指南!
QuestPDF概述
主要特點
QuestPDF是一個現代化的PDF程式庫,強調易用性和對開發者友好的設計。 與浮水印相關的主要特點包括:
- **聲明式API:**使用流暢的API,讓開發者可以以清晰和直觀的方式定義浮水印。
- **高度可定制化:**支持多種型別的浮水印,包括文字、圖像和形狀,具有廣泛的自定制選項。
- **性能集中:**針對速度和效率進行優化,適合大批量PDF生成。
安裝和設置
要安裝QuestPDF,請按照以下步驟進行:
-
使用以下命令安裝QuestPDF NuGet套件:
Install-Package QuestPDFSHELL -
在您的C#文件中包含必要的命名空間:
using QuestPDF;Imports QuestPDF
使用QuestPDF新增浮水印
QuestPDF對將浮水印應用到PDF文件提供了不同的方法。 使用QuestPDF,這是通過浮水印插槽進行的(在背景和前景),可用於將浮水印內容新增到某個頁面或PDF的所有頁面。
using QuestPDF.Fluent;
using QuestPDF.Helpers;
using QuestPDF.Infrastructure;
public class WatermarkExample
{
public static void Main()
{
// Set the license type to Community for QuestPDF.
QuestPDF.Settings.License = LicenseType.Community;
// Create a PDF document with a defined structure.
Document.Create(container =>
{
container.Page(page =>
{
page.Margin(50);
// Add a foreground watermark.
page.Foreground().Element(watermark =>
{
watermark.Text("DRAFT")
.FontSize(40)
.FontColor(Colors.Red.Medium)
.AlignLeft();
});
// Add the main content of the page.
page.Content().Element(ComposeContent);
});
})
.GeneratePdf("watermarked_document.pdf");
}
private static IContainer ComposeContent(IContainer container)
{
// Define the layout and content of the PDF.
container.Column(column =>
{
column.Spacing(10);
column.Item().Text("This is the main content of the PDF.");
column.Item().Text("Add more content as needed.");
});
return container; // Return the container to maintain method signature.
}
}Imports QuestPDF.Fluent
Imports QuestPDF.Helpers
Imports QuestPDF.Infrastructure
Public Class WatermarkExample
Public Shared Sub Main()
' Set the license type to Community for QuestPDF.
QuestPDF.Settings.License = LicenseType.Community
' Create a PDF document with a defined structure.
Document.Create(Sub(container)
container.Page(Sub(page)
page.Margin(50)
' Add a foreground watermark.
page.Foreground().Element(Sub(watermark)
watermark.Text("DRAFT").FontSize(40).FontColor(Colors.Red.Medium).AlignLeft()
End Sub)
' Add the main content of the page.
page.Content().Element(AddressOf ComposeContent)
End Sub)
End Sub).GeneratePdf("watermarked_document.pdf")
End Sub
Private Shared Function ComposeContent(ByVal container As IContainer) As IContainer
' Define the layout and content of the PDF.
container.Column(Sub(column)
column.Spacing(10)
column.Item().Text("This is the main content of the PDF.")
column.Item().Text("Add more content as needed.")
End Sub)
Return container ' Return the container to maintain method signature.
End Function
End Class輸出PDF文件
在Main方法中,我們首先建立一個頁面邊距為50單位的文件。 然後我們建立了我們想要使用的浮水印,它是紅色的簡單文字"DRAFT",字體大小為40,左對齊。 這種將浮水印應用於PDF文件的方法與IronPDF的精簡方法相比更為嚴格和複雜。 使用QuestPDF,您可能對浮水印的外觀和位置的控制較少。
浮水印功能比較
易用性
IronPDF提供了一種簡單的方法,其豐富的文件和範例,使初學者易於使用。 QuestPDF的聲明式API進一步簡化了流程,允許簡潔的程式碼,從而提升生產力。
自定義選擇
兩個程式庫都為浮水印提供了廣泛的自定制。 IronPDF允許對文字和圖像進行詳細的樣式設置,而QuestPDF提供了更靈活的方式來排列元素並支持複雜的設計,適合創意設計。
性能
在性能方面,兩個程式庫都表現良好,但QuestPDF可能因其高效設計在速度上具有優勢。 建議在實際場景中測試這兩個程式庫,以確定哪一個最適合您的特定使用案例。
授權和定價
IronPDF授權選項
IronPDF在商業授權模型下運行。
QuestPDF授權選項
QuestPDF提供開源授權,並可選擇商業支持。 這使得它成為對於希望強大功能,但不希望有重大財務投入的開發者來說,一個具有成本效益的選擇。
總結

IronPDF和QuestPDF都是在C#中為PDF新增浮水印的強大程式庫。 IronPDF在詳細的自定義選項和使用者友好的方法上表現優異,使其成為需要特定格式使用者的理想選擇。 另一方面,QuestPDF憑藉其現代API設計和性能效率脫穎而出,吸引那些尋求快速和直觀解決方案的開發者。
對於需要廣泛自定義的情景,IronPDF可能是首選。而QuestPDF則適合優先考慮速度和易用性的項目。
立即嘗試IronPDF,通過下載免費試用版,探索它如何將您的C# PDF項目提升到新的水平!

Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。
相關文章


