Skip to content

Commit 9724fa7

Browse files
committed
Add EncryptPdf example and README section
Add console example demonstrating PDF encryption with user/owner passwords. Add corresponding README section.
1 parent 8fef412 commit 9724fa7

3 files changed

Lines changed: 79 additions & 0 deletions

File tree

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,23 @@ public async Task<Stream> ConvertToAccessiblePdfA(string pdfPath)
461461
}
462462
```
463463

464+
### PDF Encryption
465+
*Password-protect PDFs with user and owner passwords:*
466+
467+
```csharp
468+
public async Task<Stream> CreateEncryptedPdf()
469+
{
470+
var builder = new HtmlRequestBuilder()
471+
.AddDocument(doc => doc.SetBody("<html><body><h1>Confidential</h1></body></html>"))
472+
.SetPdfOutputOptions(o => o
473+
.SetEncryption(userPassword: "reader123", ownerPassword: "admin456"))
474+
.WithPageProperties(pp => pp.UseChromeDefaults());
475+
476+
var request = builder.Build();
477+
return await _sharpClient.HtmlToPdfAsync(request);
478+
}
479+
```
480+
464481
### Flatten PDFs
465482
*Flatten PDF forms and annotations (v2.8+):*
466483

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
</Project>

examples/EncryptPdf/Program.cs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using Gotenberg.Sharp.API.Client;
2+
using Gotenberg.Sharp.API.Client.Domain.Builders;
3+
using Gotenberg.Sharp.API.Client.Domain.Settings;
4+
using Gotenberg.Sharp.API.Client.Infrastructure.Pipeline;
5+
6+
using Microsoft.Extensions.Configuration;
7+
8+
var config = new ConfigurationBuilder()
9+
.SetBasePath(AppContext.BaseDirectory)
10+
.AddJsonFile("appsettings.json")
11+
.Build();
12+
13+
var options = new GotenbergSharpClientOptions();
14+
config.GetSection(nameof(GotenbergSharpClient)).Bind(options);
15+
16+
var destinationDirectory = args.Length > 0 ? args[0] : Path.Combine(Directory.GetCurrentDirectory(), "output");
17+
Directory.CreateDirectory(destinationDirectory);
18+
19+
var path = await CreateEncryptedPdf(destinationDirectory, options);
20+
Console.WriteLine($"Encrypted PDF created: {path}");
21+
Console.WriteLine("Open with password: reader123");
22+
23+
static async Task<string> CreateEncryptedPdf(string destinationDirectory, GotenbergSharpClientOptions options)
24+
{
25+
using var handler = new HttpClientHandler();
26+
using var authHandler = !string.IsNullOrWhiteSpace(options.BasicAuthUsername) && !string.IsNullOrWhiteSpace(options.BasicAuthPassword)
27+
? new BasicAuthHandler(options.BasicAuthUsername, options.BasicAuthPassword) { InnerHandler = handler }
28+
: null;
29+
30+
using var httpClient = new HttpClient(authHandler ?? (HttpMessageHandler)handler)
31+
{
32+
BaseAddress = options.ServiceUrl,
33+
Timeout = options.TimeOut
34+
};
35+
36+
var sharpClient = new GotenbergSharpClient(httpClient);
37+
38+
// Create a password-protected PDF
39+
var builder = new HtmlRequestBuilder()
40+
.AddDocument(doc => doc.SetBody(@"
41+
<html><body>
42+
<h1>Confidential Report</h1>
43+
<p>This document is password protected.</p>
44+
</body></html>"))
45+
.SetPdfOutputOptions(o => o
46+
.SetEncryption(
47+
userPassword: "reader123", // Required to open the PDF
48+
ownerPassword: "admin456")) // Required to change permissions
49+
.WithPageProperties(pp => pp.UseChromeDefaults());
50+
51+
var request = builder.Build();
52+
var response = await sharpClient.HtmlToPdfAsync(request);
53+
54+
var resultPath = Path.Combine(destinationDirectory, $"Encrypted-{DateTime.Now:yyyyMMddHHmmss}.pdf");
55+
56+
await using var destinationStream = File.Create(resultPath);
57+
await response.CopyToAsync(destinationStream, CancellationToken.None);
58+
59+
return resultPath;
60+
}

0 commit comments

Comments
 (0)