|
| 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 | +} |
0 commit comments