Encryption & Metadata
PDF Encryption
CobaltPdf can password-protect the generated PDF using AES encryption (via PDFsharp). You can restrict individual operations such as printing, copying, and modification independently.
Basic Encryption
using static CobaltPdf.Configuration.PdfGenerationOptions;
var pdf = await renderer
.WithEncryption(new PdfEncryptionOptions
{
UserPassword = "open-me", // password to open the document
OwnerPassword = "admin-secret" // password to override permissions (optional)
})
.RenderUrlAsPdfAsync("https://example.com");
pdf.SaveAs("protected.pdf");
If OwnerPassword is omitted, CobaltPdf generates a cryptographically secure random owner password internally.
PdfEncryptionOptions Reference
| Property | Type | Default | Description |
|---|---|---|---|
UserPassword |
string |
required | Password required to open the document |
OwnerPassword |
string? |
auto-generated | Password to override permission restrictions |
AllowPrinting |
bool |
true |
Permit the document to be printed |
AllowCopying |
bool |
false |
Permit text and content extraction |
AllowModification |
bool |
false |
Permit the document to be modified |
Restricting All Permissions
var pdf = await renderer
.WithEncryption(new PdfEncryptionOptions
{
UserPassword = "view-only",
AllowPrinting = false,
AllowCopying = false,
AllowModification = false
})
.RenderUrlAsPdfAsync("https://example.com/confidential");
Read-Only with Printing Allowed
var pdf = await renderer
.WithEncryption(new PdfEncryptionOptions
{
UserPassword = "print-ok",
AllowPrinting = true, // printing allowed
AllowCopying = false, // copy/paste blocked
AllowModification = false
})
.RenderUrlAsPdfAsync("https://example.com/report");
PDF Metadata
PDF metadata is embedded in the document's Information Dictionary and is visible to users through "Document Properties" in most PDF viewers. It also aids search engines and document management systems.
Setting Metadata
var pdf = await renderer
.WithMetadata(m =>
{
m.Title = "Q4 Financial Report";
m.Author = "Alice Smith";
m.Subject = "Quarterly earnings summary for Q4 2025";
m.Keywords = "finance, quarterly, earnings, 2025";
m.Creator = "MyApp v2.1";
})
.RenderUrlAsPdfAsync("https://example.com/q4-report");
MetadataOptions Reference
| Property | Type | Default | Description |
|---|---|---|---|
Title |
string? |
null |
Document title |
Author |
string? |
null |
Author name |
Subject |
string? |
null |
Subject or description |
Keywords |
string? |
null |
Comma-separated keywords |
Creator |
string? |
null |
Application that created the content |
Producer |
string? |
"CobaltPdf" |
PDF producer (note: PDFsharp may override this) |
CreationDate |
DateTime? |
null |
Creation date. null = current UTC time |
Combining Encryption and Metadata
Encryption and metadata can be applied together. CobaltPdf runs both through PDFsharp in a single document pass for efficiency:
var pdf = await renderer
.WithMetadata(m =>
{
m.Title = "Confidential Contract";
m.Author = "Legal Department";
})
.WithEncryption(new PdfEncryptionOptions
{
UserPassword = "contract2025",
AllowCopying = false
})
.RenderUrlAsPdfAsync("https://example.com/contract");