From 5a7ed7c9eb5146b66cf04ac7afda5223120acf1b Mon Sep 17 00:00:00 2001 From: DGdev91 Date: Fri, 17 Feb 2023 00:13:18 +0100 Subject: [PATCH] PdfMerger: Make possible to override document informations for output document --- .../Writer/PdfDocumentBuilder.cs | 2 +- src/UglyToad.PdfPig/Writer/PdfMerger.cs | 36 +++++++++++-------- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/src/UglyToad.PdfPig/Writer/PdfDocumentBuilder.cs b/src/UglyToad.PdfPig/Writer/PdfDocumentBuilder.cs index ed87db83..53467716 100644 --- a/src/UglyToad.PdfPig/Writer/PdfDocumentBuilder.cs +++ b/src/UglyToad.PdfPig/Writer/PdfDocumentBuilder.cs @@ -50,7 +50,7 @@ namespace UglyToad.PdfPig.Writer /// /// The values of the fields to include in the document information dictionary. /// - public DocumentInformationBuilder DocumentInformation { get; } = new DocumentInformationBuilder(); + public DocumentInformationBuilder DocumentInformation { get; set; } = new DocumentInformationBuilder(); /// /// The current page builders in the document and the corresponding 1 indexed page numbers. Use diff --git a/src/UglyToad.PdfPig/Writer/PdfMerger.cs b/src/UglyToad.PdfPig/Writer/PdfMerger.cs index d46d1625..21e47a32 100644 --- a/src/UglyToad.PdfPig/Writer/PdfMerger.cs +++ b/src/UglyToad.PdfPig/Writer/PdfMerger.cs @@ -19,11 +19,11 @@ /// /// Merge two PDF documents together with the pages from followed by . /// - public static byte[] Merge(string file1, string file2, IReadOnlyList file1Selection = null, IReadOnlyList file2Selection = null, PdfAStandard archiveStandard = PdfAStandard.None) + public static byte[] Merge(string file1, string file2, IReadOnlyList file1Selection = null, IReadOnlyList file2Selection = null, PdfAStandard archiveStandard = PdfAStandard.None, PdfDocumentBuilder.DocumentInformationBuilder docInfoBuilder = null) { using (var output = new MemoryStream()) { - Merge(file1, file2, output, file1Selection, file2Selection, archiveStandard); + Merge(file1, file2, output, file1Selection, file2Selection, archiveStandard, docInfoBuilder); return output.ToArray(); } } @@ -31,7 +31,7 @@ /// /// Merge two PDF documents together with the pages from followed by into the output stream. /// - public static void Merge(string file1, string file2, Stream output, IReadOnlyList file1Selection = null, IReadOnlyList file2Selection = null, PdfAStandard archiveStandard = PdfAStandard.None) + public static void Merge(string file1, string file2, Stream output, IReadOnlyList file1Selection = null, IReadOnlyList file2Selection = null, PdfAStandard archiveStandard = PdfAStandard.None, PdfDocumentBuilder.DocumentInformationBuilder docInfoBuilder = null) { _ = file1 ?? throw new ArgumentNullException(nameof(file1)); _ = file2 ?? throw new ArgumentNullException(nameof(file2)); @@ -40,7 +40,7 @@ { using (var stream2 = File.OpenRead(file2)) { - Merge(new[] { stream1, stream2 }, output, new[] { file1Selection, file2Selection }, archiveStandard); + Merge(new[] { stream1, stream2 }, output, new[] { file1Selection, file2Selection }, archiveStandard, docInfoBuilder); } } } @@ -50,17 +50,17 @@ /// public static byte[] Merge(params string[] filePaths) { - return Merge(PdfAStandard.None, filePaths); + return Merge(PdfAStandard.None, null, filePaths); } /// /// Merge multiple PDF documents together with the pages in the order the file paths are provided. /// - public static byte[] Merge(PdfAStandard archiveStandard, params string[] filePaths) + public static byte[] Merge(PdfAStandard archiveStandard, PdfDocumentBuilder.DocumentInformationBuilder docInfoBuilder, params string[] filePaths) { using (var output = new MemoryStream()) { - Merge(output, archiveStandard, filePaths); + Merge(output, archiveStandard, docInfoBuilder, filePaths); return output.ToArray(); } } @@ -70,13 +70,13 @@ /// public static void Merge(Stream output, params string[] filePaths) { - Merge(output, PdfAStandard.None, filePaths); + Merge(output, PdfAStandard.None, null, filePaths); } /// /// Merge multiple PDF documents together with the pages in the order the file paths are provided into the output stream /// - public static void Merge(Stream output, PdfAStandard archiveStandard, params string[] filePaths) + public static void Merge(Stream output, PdfAStandard archiveStandard, PdfDocumentBuilder.DocumentInformationBuilder docInfoBuilder, params string[] filePaths) { var streams = new List(filePaths.Length); try @@ -87,7 +87,7 @@ streams.Add(File.OpenRead(filePath)); } - Merge(streams, output, null, archiveStandard); + Merge(streams, output, null, archiveStandard, docInfoBuilder); } finally { @@ -101,13 +101,13 @@ /// /// Merge the set of PDF documents. /// - public static byte[] Merge(IReadOnlyList files, IReadOnlyList> pagesBundle = null, PdfAStandard archiveStandard = PdfAStandard.None) + public static byte[] Merge(IReadOnlyList files, IReadOnlyList> pagesBundle = null, PdfAStandard archiveStandard = PdfAStandard.None, PdfDocumentBuilder.DocumentInformationBuilder docInfoBuilder = null) { _ = files ?? throw new ArgumentNullException(nameof(files)); using (var output = new MemoryStream()) { - Merge(files.Select(f => PdfDocument.Open(f)).ToArray(), output, pagesBundle, archiveStandard); + Merge(files.Select(f => PdfDocument.Open(f)).ToArray(), output, pagesBundle, archiveStandard, docInfoBuilder); return output.ToArray(); } } @@ -121,21 +121,27 @@ /// Must be writable /// /// + /// /// - public static void Merge(IReadOnlyList streams, Stream output, IReadOnlyList> pagesBundle = null, PdfAStandard archiveStandard = PdfAStandard.None) + public static void Merge(IReadOnlyList streams, Stream output, IReadOnlyList> pagesBundle = null, PdfAStandard archiveStandard = PdfAStandard.None, PdfDocumentBuilder.DocumentInformationBuilder docInfoBuilder = null) { _ = streams ?? throw new ArgumentNullException(nameof(streams)); _ = output ?? throw new ArgumentNullException(nameof(output)); - Merge(streams.Select(f => PdfDocument.Open(f)).ToArray(), output, pagesBundle, archiveStandard); + Merge(streams.Select(f => PdfDocument.Open(f)).ToArray(), output, pagesBundle, archiveStandard, docInfoBuilder); } - private static void Merge(IReadOnlyList files, Stream output, IReadOnlyList> pagesBundle, PdfAStandard archiveStandard = PdfAStandard.None) + private static void Merge(IReadOnlyList files, Stream output, IReadOnlyList> pagesBundle, PdfAStandard archiveStandard = PdfAStandard.None, PdfDocumentBuilder.DocumentInformationBuilder docInfoBuilder = null) { var maxVersion = files.Select(x=>x.Version).Max(); using (var document = new PdfDocumentBuilder(output, false, PdfWriterType.Default, maxVersion)) { document.ArchiveStandard = archiveStandard; + if (docInfoBuilder != null) + { + document.IncludeDocumentInformation = true; + document.DocumentInformation = docInfoBuilder; + } foreach (var fileIndex in Enumerable.Range(0, files.Count)) { var existing = files[fileIndex];