Skip to content

Commit 2c691cf

Browse files
committed
Add LibreOfficeOptions example and README section
Add console example demonstrating LibreOffice-specific options including image compression, bookmarks, form fields, and native watermarks. Add corresponding README section.
1 parent bbdea37 commit 2c691cf

3 files changed

Lines changed: 96 additions & 0 deletions

File tree

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,26 @@ public async Task<Stream> DoOfficeMerge(string sourceDirectory)
222222
return await _sharpClient.MergeOfficeDocsAsync(request);
223223
}
224224
```
225+
226+
### LibreOffice Conversion Options
227+
*Fine-tune LibreOffice conversions with image compression, bookmarks, and native watermarks:*
228+
229+
```csharp
230+
public async Task<Stream> ConvertWithOptions(string sourceDirectory)
231+
{
232+
var builder = new MergeOfficeBuilder()
233+
.WithAsyncAssets(async a => a.AddItems(await GetDocsAsync(sourceDirectory)))
234+
.SetLibreOfficeOptions(o => o
235+
.SetQuality(85)
236+
.SetReduceImageResolution()
237+
.SetMaxImageResolution(300)
238+
.SetExportBookmarks()
239+
.SetNativeWatermarkText("DRAFT"));
240+
241+
var request = await builder.BuildAsync();
242+
return await _sharpClient.MergeOfficeDocsAsync(request);
243+
}
244+
```
225245
### Markdown to PDF
226246
*Markdown to PDF conversion with embedded assets:*
227247

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>
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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 sourceDirectory = args.Length > 0 ? args[0] : Path.Combine(AppContext.BaseDirectory, "resources", "OfficeDocs");
17+
var destinationDirectory = args.Length > 1 ? args[1] : Path.Combine(Directory.GetCurrentDirectory(), "output");
18+
Directory.CreateDirectory(destinationDirectory);
19+
20+
var path = await ConvertWithLibreOfficeOptions(sourceDirectory, destinationDirectory, options);
21+
Console.WriteLine($"PDF with LibreOffice options created: {path}");
22+
23+
static async Task<string> ConvertWithLibreOfficeOptions(string sourceDirectory, 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 client = new GotenbergSharpClient(httpClient);
37+
38+
// Demonstrates LibreOffice-specific conversion options
39+
var builder = new MergeOfficeBuilder()
40+
.WithAsyncAssets(async b => b.AddItems(await GetDocsAsync(sourceDirectory)))
41+
.SetLibreOfficeOptions(o => o
42+
// Image compression
43+
.SetQuality(85)
44+
.SetReduceImageResolution()
45+
.SetMaxImageResolution(300)
46+
// Export options
47+
.SetExportBookmarks()
48+
.SetExportFormFields(false)
49+
.SetUpdateIndexes()
50+
// Native watermark
51+
.SetNativeWatermarkText("DRAFT")
52+
.SetNativeWatermarkFontName("Arial")
53+
)
54+
.SetPdfOutputOptions(o => o.SetPdfFormat(PdfFormat.A2b));
55+
56+
var response = await client.MergeOfficeDocsAsync(builder).ConfigureAwait(false);
57+
58+
var resultPath = Path.Combine(destinationDirectory, $"LibreOfficeOptions-{DateTime.Now:yyyyMMddHHmmss}.pdf");
59+
60+
await using var destinationStream = File.Create(resultPath);
61+
await response.CopyToAsync(destinationStream, CancellationToken.None);
62+
63+
return resultPath;
64+
}
65+
66+
static async Task<IEnumerable<KeyValuePair<string, byte[]>>> GetDocsAsync(string sourceDirectory)
67+
{
68+
var paths = Directory.GetFiles(sourceDirectory, "*.*", SearchOption.TopDirectoryOnly);
69+
var names = paths.Select(p => new FileInfo(p).Name);
70+
var tasks = paths.Select(f => File.ReadAllBytesAsync(f));
71+
var docs = await Task.WhenAll(tasks);
72+
73+
return names.Select((name, index) => KeyValuePair.Create(name, docs[index])).Take(10);
74+
}

0 commit comments

Comments
 (0)