|
| 1 | +// Copyright 2019-2026 Chris Mohan, Jaben Cargman |
| 2 | +// and GotenbergSharpApiClient Contributors |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | + |
| 16 | +using Gotenberg.Sharp.API.Client.Domain.ValueObjects; |
| 17 | + |
| 18 | +using Newtonsoft.Json.Linq; |
| 19 | + |
| 20 | +namespace Gotenberg.Sharp.API.Client.Domain.Builders; |
| 21 | + |
| 22 | +/// <summary> |
| 23 | +/// Builds standalone PDF engine requests. Use the static factory methods to create |
| 24 | +/// builders for specific operations (flatten, rotate, split, encrypt, metadata). |
| 25 | +/// </summary> |
| 26 | +public sealed class PdfEngineBuilder<TRequest> |
| 27 | + : BaseBuilder<TRequest, PdfEngineBuilder<TRequest>> |
| 28 | + where TRequest : PdfEngineRequest |
| 29 | +{ |
| 30 | + internal PdfEngineBuilder(TRequest request) : base(request) |
| 31 | + { |
| 32 | + } |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Adds PDF files to process. |
| 36 | + /// </summary> |
| 37 | + public PdfEngineBuilder<TRequest> WithPdfs(Action<AssetBuilder> action) |
| 38 | + { |
| 39 | + if (action == null) throw new ArgumentNullException(nameof(action)); |
| 40 | + |
| 41 | + action(new AssetBuilder(this.Request.Assets ??= new AssetDictionary())); |
| 42 | + |
| 43 | + return this; |
| 44 | + } |
| 45 | + |
| 46 | + /// <summary> |
| 47 | + /// Adds PDF files asynchronously. |
| 48 | + /// </summary> |
| 49 | + public PdfEngineBuilder<TRequest> WithPdfsAsync(Func<AssetBuilder, Task> asyncAction) |
| 50 | + { |
| 51 | + if (asyncAction == null) throw new ArgumentNullException(nameof(asyncAction)); |
| 52 | + |
| 53 | + this.BuildTasks.Add(asyncAction(new AssetBuilder(this.Request.Assets ??= new AssetDictionary()))); |
| 54 | + |
| 55 | + return this; |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +/// <summary> |
| 60 | +/// Static factory for creating PDF engine builders for specific operations. |
| 61 | +/// </summary> |
| 62 | +public static class PdfEngineBuilders |
| 63 | +{ |
| 64 | + /// <summary> |
| 65 | + /// Creates a builder for flattening PDF form fields into static content. |
| 66 | + /// </summary> |
| 67 | + public static PdfEngineBuilder<FlattenPdfRequest> Flatten() |
| 68 | + { |
| 69 | + return new PdfEngineBuilder<FlattenPdfRequest>(new FlattenPdfRequest()); |
| 70 | + } |
| 71 | + |
| 72 | + /// <summary> |
| 73 | + /// Creates a builder for rotating PDF pages. |
| 74 | + /// </summary> |
| 75 | + public static PdfEngineBuilder<RotatePdfRequest> Rotate(RotationAngle angle, PageRanges? pages = null) |
| 76 | + { |
| 77 | + var request = new RotatePdfRequest |
| 78 | + { |
| 79 | + RotateAngle = angle ?? throw new ArgumentNullException(nameof(angle)), |
| 80 | + RotatePages = pages |
| 81 | + }; |
| 82 | + return new PdfEngineBuilder<RotatePdfRequest>(request); |
| 83 | + } |
| 84 | + |
| 85 | + /// <summary> |
| 86 | + /// Creates a builder for rotating PDF pages. |
| 87 | + /// </summary> |
| 88 | + public static PdfEngineBuilder<RotatePdfRequest> Rotate(int angleDegrees, string? pages = null) |
| 89 | + { |
| 90 | + return Rotate( |
| 91 | + RotationAngle.Create(angleDegrees), |
| 92 | + pages != null ? PageRanges.Create(pages) : null); |
| 93 | + } |
| 94 | + |
| 95 | + /// <summary> |
| 96 | + /// Creates a builder for splitting PDFs. |
| 97 | + /// </summary> |
| 98 | + public static PdfEngineBuilder<SplitPdfRequest> Split(SplitMode mode, string span, bool unify = false) |
| 99 | + { |
| 100 | + var request = new SplitPdfRequest |
| 101 | + { |
| 102 | + Mode = mode, |
| 103 | + Span = !string.IsNullOrWhiteSpace(span) ? span : throw new ArgumentException("Span must not be null or whitespace.", nameof(span)), |
| 104 | + Unify = unify |
| 105 | + }; |
| 106 | + return new PdfEngineBuilder<SplitPdfRequest>(request); |
| 107 | + } |
| 108 | + |
| 109 | + /// <summary> |
| 110 | + /// Creates a builder for encrypting PDFs with passwords. |
| 111 | + /// </summary> |
| 112 | + public static PdfEngineBuilder<EncryptPdfRequest> Encrypt(string userPassword, string? ownerPassword = null) |
| 113 | + { |
| 114 | + if (string.IsNullOrWhiteSpace(userPassword)) |
| 115 | + throw new ArgumentException("User password is required.", nameof(userPassword)); |
| 116 | + |
| 117 | + var request = new EncryptPdfRequest |
| 118 | + { |
| 119 | + UserPassword = userPassword, |
| 120 | + OwnerPassword = ownerPassword |
| 121 | + }; |
| 122 | + return new PdfEngineBuilder<EncryptPdfRequest>(request); |
| 123 | + } |
| 124 | + |
| 125 | + /// <summary> |
| 126 | + /// Creates a builder for reading metadata from PDFs. Returns JSON. |
| 127 | + /// </summary> |
| 128 | + public static PdfEngineBuilder<ReadMetadataRequest> ReadMetadata() |
| 129 | + { |
| 130 | + return new PdfEngineBuilder<ReadMetadataRequest>(new ReadMetadataRequest()); |
| 131 | + } |
| 132 | + |
| 133 | + /// <summary> |
| 134 | + /// Creates a builder for writing metadata to PDFs. |
| 135 | + /// </summary> |
| 136 | + public static PdfEngineBuilder<WriteMetadataRequest> WriteMetadata(JObject metadata) |
| 137 | + { |
| 138 | + var request = new WriteMetadataRequest |
| 139 | + { |
| 140 | + Metadata = metadata ?? throw new ArgumentNullException(nameof(metadata)) |
| 141 | + }; |
| 142 | + return new PdfEngineBuilder<WriteMetadataRequest>(request); |
| 143 | + } |
| 144 | + |
| 145 | + /// <summary> |
| 146 | + /// Creates a builder for writing metadata to PDFs. |
| 147 | + /// </summary> |
| 148 | + public static PdfEngineBuilder<WriteMetadataRequest> WriteMetadata(IDictionary<string, object> metadata) |
| 149 | + { |
| 150 | + return WriteMetadata(JObject.FromObject(metadata)); |
| 151 | + } |
| 152 | +} |
0 commit comments