Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save documentprocessing/38cbb0dc3c5328fc637b9a9face97ce7 to your computer and use it in GitHub Desktop.
Save documentprocessing/38cbb0dc3c5328fc637b9a9face97ce7 to your computer and use it in GitHub Desktop.
Add Watermark to PDF with QuestPDF for .NET
using QuestPDF.Fluent;
using QuestPDF.Helpers;
using QuestPDF.Infrastructure;
class Program
{
static void Main(string[] args)
{
// Create a PDF document
Document.Create(document =>
{
// Define document structure
document.Page(page =>
{
// Set page size and margin
page.Size(PageSizes.A4);
page.Margin(2, Unit.Centimetre);
// Define page content
page.Content().Layer(layer =>
{
// Main content (e.g., text)
layer.Element()
.PaddingBottom(5, Unit.Centimetre)
.Text("This is the main content of the PDF")
.FontSize(16)
.AlignCenter();
// Watermark overlay
layer.Element()
.AlignCenter()
.AlignMiddle()
.Text("CONFIDENTIAL")
.FontSize(60)
.Color(Colors.Grey.Medium)
.Opacity(0.2f)
.Rotate(45);
});
});
})
.GeneratePdf("WatermarkedDocument.pdf"); // Save the PDF to a file
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment