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.PdfOptions;
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");
Combining Encryption and Metadata
Encryption and metadata can be applied together. CobaltPdf runs both through PDFsharp in a single document pass:
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");
Tip
For embedding document properties such as title, author, and keywords, see the Metadata article.