-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathProgram.cs
More file actions
74 lines (59 loc) · 3.09 KB
/
Program.cs
File metadata and controls
74 lines (59 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using Gotenberg.Sharp.API.Client;
using Gotenberg.Sharp.API.Client.Domain.Builders;
using Gotenberg.Sharp.API.Client.Domain.Settings;
using Gotenberg.Sharp.API.Client.Infrastructure.Pipeline;
using Microsoft.Extensions.Configuration;
var config = new ConfigurationBuilder()
.SetBasePath(AppContext.BaseDirectory)
.AddJsonFile("appsettings.json")
.Build();
var options = new GotenbergSharpClientOptions();
config.GetSection(nameof(GotenbergSharpClient)).Bind(options);
var sourceDirectory = args.Length > 0 ? args[0] : Path.Combine(AppContext.BaseDirectory, "resources", "OfficeDocs");
var destinationDirectory = args.Length > 1 ? args[1] : Path.Combine(Directory.GetCurrentDirectory(), "output");
Directory.CreateDirectory(destinationDirectory);
var path = await ConvertWithLibreOfficeOptions(sourceDirectory, destinationDirectory, options);
Console.WriteLine($"PDF with LibreOffice options created: {path}");
static async Task<string> ConvertWithLibreOfficeOptions(string sourceDirectory, string destinationDirectory, GotenbergSharpClientOptions options)
{
var handler = new HttpClientHandler();
HttpMessageHandler effectiveHandler = handler;
if (!string.IsNullOrWhiteSpace(options.BasicAuthUsername) && !string.IsNullOrWhiteSpace(options.BasicAuthPassword))
effectiveHandler = new BasicAuthHandler(options.BasicAuthUsername, options.BasicAuthPassword) { InnerHandler = handler };
using var httpClient = new HttpClient(effectiveHandler, disposeHandler: true)
{
BaseAddress = options.ServiceUrl,
Timeout = options.TimeOut
};
var client = new GotenbergSharpClient(httpClient);
// Demonstrates LibreOffice-specific conversion options
var builder = new MergeOfficeBuilder()
.WithAsyncAssets(async b => b.AddItems(await GetDocsAsync(sourceDirectory)))
.SetLibreOfficeOptions(o => o
// Image compression
.SetQuality(85)
.SetReduceImageResolution()
.SetMaxImageResolution(300)
// Export options
.SetExportBookmarks()
.SetExportFormFields(false)
.SetUpdateIndexes()
// Native watermark
.SetNativeWatermarkText("DRAFT")
.SetNativeWatermarkFontName("Arial")
)
.SetPdfOutputOptions(o => o.SetPdfFormat(PdfFormat.A2b));
var response = await client.MergeOfficeDocsAsync(builder).ConfigureAwait(false);
var resultPath = Path.Combine(destinationDirectory, $"LibreOfficeOptions-{DateTime.Now:yyyyMMddHHmmss}.pdf");
await using var destinationStream = File.Create(resultPath);
await response.CopyToAsync(destinationStream, CancellationToken.None);
return resultPath;
}
static async Task<IEnumerable<KeyValuePair<string, byte[]>>> GetDocsAsync(string sourceDirectory)
{
var paths = Directory.GetFiles(sourceDirectory, "*.*", SearchOption.TopDirectoryOnly);
var names = paths.Select(p => new FileInfo(p).Name);
var tasks = paths.Select(f => File.ReadAllBytesAsync(f));
var docs = await Task.WhenAll(tasks);
return names.Select((name, index) => KeyValuePair.Create(name, docs[index])).Take(10);
}