Skip to content

Commit 4872a12

Browse files
committed
Add WatermarkAndRotate example and README sections
Add console example demonstrating watermark and rotation options. Add README sections for watermark/rotation and split features.
1 parent 26d55e0 commit 4872a12

3 files changed

Lines changed: 95 additions & 0 deletions

File tree

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,38 @@ public async Task<Stream> FastConversion()
492492
}
493493
```
494494

495+
### Watermark & Rotation
496+
*Add text watermarks and rotate PDF pages — available on all request types:*
497+
498+
```csharp
499+
public async Task<Stream> CreateWatermarkedPdf()
500+
{
501+
var builder = new HtmlRequestBuilder()
502+
.AddDocument(doc => doc.SetBody("<html><body><h1>Report</h1></body></html>"))
503+
.SetWatermarkOptions(w => w.SetTextWatermark("DRAFT", "1-3"))
504+
.SetRotationOptions(r => r.SetAngle(90).SetPages("2"))
505+
.WithPageProperties(pp => pp.UseChromeDefaults());
506+
507+
var request = builder.Build();
508+
return await _sharpClient.HtmlToPdfAsync(request);
509+
}
510+
```
511+
512+
### Split PDFs
513+
*Split generated PDFs into chunks or extract specific pages:*
514+
515+
```csharp
516+
public async Task<Stream> SplitPdf()
517+
{
518+
var builder = new HtmlRequestBuilder()
519+
.AddDocument(doc => doc.SetBody("<html><body>Multi-page content</body></html>"))
520+
.SetSplitOptions(s => s.SplitByPages("1-3,5", unify: true));
521+
522+
var request = builder.Build();
523+
return await _sharpClient.HtmlToPdfAsync(request);
524+
}
525+
```
526+
495527
### Custom Page Properties
496528
*Fine-tune page dimensions and properties:*
497529

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using Gotenberg.Sharp.API.Client;
2+
using Gotenberg.Sharp.API.Client.Domain.Builders;
3+
using Gotenberg.Sharp.API.Client.Domain.ValueObjects;
4+
using Gotenberg.Sharp.API.Client.Domain.Settings;
5+
using Gotenberg.Sharp.API.Client.Infrastructure.Pipeline;
6+
7+
using Microsoft.Extensions.Configuration;
8+
9+
var config = new ConfigurationBuilder()
10+
.SetBasePath(AppContext.BaseDirectory)
11+
.AddJsonFile("appsettings.json")
12+
.Build();
13+
14+
var options = new GotenbergSharpClientOptions();
15+
config.GetSection(nameof(GotenbergSharpClient)).Bind(options);
16+
17+
var destinationDirectory = args.Length > 0 ? args[0] : Path.Combine(Directory.GetCurrentDirectory(), "output");
18+
Directory.CreateDirectory(destinationDirectory);
19+
20+
var path = await CreateWatermarkedAndRotatedPdf(destinationDirectory, options);
21+
Console.WriteLine($"Watermarked & rotated PDF created: {path}");
22+
23+
static async Task<string> CreateWatermarkedAndRotatedPdf(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+
// Demonstrates watermark, stamp, rotation, and split options
39+
var builder = new HtmlRequestBuilder()
40+
.AddDocument(doc => doc.SetBody(@"
41+
<html><body>
42+
<h1>Cross-Cutting Features Demo</h1>
43+
<p>This PDF has a text watermark and is rotated 90 degrees.</p>
44+
<p>Page 2 content here...</p>
45+
</body></html>"))
46+
// Add a text watermark behind the content
47+
.SetWatermarkOptions(w => w.SetTextWatermark("CONFIDENTIAL"))
48+
// Rotate all pages 90 degrees
49+
.SetRotationOptions(r => r.SetAngle(RotationAngle.Degrees90))
50+
.WithPageProperties(pp => pp.UseChromeDefaults());
51+
52+
var request = builder.Build();
53+
var response = await sharpClient.HtmlToPdfAsync(request);
54+
55+
var resultPath = Path.Combine(destinationDirectory, $"WatermarkRotate-{DateTime.Now:yyyyMMddHHmmss}.pdf");
56+
57+
await using var destinationStream = File.Create(resultPath);
58+
await response.CopyToAsync(destinationStream, CancellationToken.None);
59+
60+
return resultPath;
61+
}
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>

0 commit comments

Comments
 (0)