PdfMerger: Add optional parameter to set desired PDF/A standard

This commit is contained in:
DGdev91
2023-02-16 16:00:12 +01:00
parent 3d6a14bea6
commit 1759c235f0

View File

@@ -19,11 +19,11 @@
/// <summary>
/// Merge two PDF documents together with the pages from <paramref name="file1"/> followed by <paramref name="file2"/>.
/// </summary>
public static byte[] Merge(string file1, string file2, IReadOnlyList<int> file1Selection = null, IReadOnlyList<int> file2Selection = null)
public static byte[] Merge(string file1, string file2, IReadOnlyList<int> file1Selection = null, IReadOnlyList<int> file2Selection = null, PdfAStandard archiveStandard = PdfAStandard.None)
{
using (var output = new MemoryStream())
{
Merge(file1, file2, output, file1Selection, file2Selection);
Merge(file1, file2, output, file1Selection, file2Selection, archiveStandard);
return output.ToArray();
}
}
@@ -31,7 +31,7 @@
/// <summary>
/// Merge two PDF documents together with the pages from <paramref name="file1"/> followed by <paramref name="file2"/> into the output stream.
/// </summary>
public static void Merge(string file1, string file2, Stream output, IReadOnlyList<int> file1Selection = null, IReadOnlyList<int> file2Selection = null)
public static void Merge(string file1, string file2, Stream output, IReadOnlyList<int> file1Selection = null, IReadOnlyList<int> file2Selection = null, PdfAStandard archiveStandard = PdfAStandard.None)
{
_ = 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 });
Merge(new[] { stream1, stream2 }, output, new[] { file1Selection, file2Selection }, archiveStandard);
}
}
}
@@ -49,10 +49,18 @@
/// Merge multiple PDF documents together with the pages in the order the file paths are provided.
/// </summary>
public static byte[] Merge(params string[] filePaths)
{
return Merge(PdfAStandard.None, filePaths);
}
/// <summary>
/// Merge multiple PDF documents together with the pages in the order the file paths are provided.
/// </summary>
public static byte[] Merge(PdfAStandard archiveStandard, params string[] filePaths)
{
using (var output = new MemoryStream())
{
Merge(output, filePaths);
Merge(output, archiveStandard, filePaths);
return output.ToArray();
}
}
@@ -60,7 +68,15 @@
/// <summary>
/// Merge multiple PDF documents together with the pages in the order the file paths are provided into the output stream
/// </summary>
public static void Merge(Stream output, params string[] filePaths)
public static void Merge(Stream output, params string[] filePaths)
{
Merge(output, PdfAStandard.None, filePaths);
}
/// <summary>
/// Merge multiple PDF documents together with the pages in the order the file paths are provided into the output stream
/// </summary>
public static void Merge(Stream output, PdfAStandard archiveStandard, params string[] filePaths)
{
var streams = new List<Stream>(filePaths.Length);
try
@@ -71,7 +87,7 @@
streams.Add(File.OpenRead(filePath));
}
Merge(streams, output, null);
Merge(streams, output, null, archiveStandard);
}
finally
{
@@ -85,13 +101,13 @@
/// <summary>
/// Merge the set of PDF documents.
/// </summary>
public static byte[] Merge(IReadOnlyList<byte[]> files, IReadOnlyList<IReadOnlyList<int>> pagesBundle = null)
public static byte[] Merge(IReadOnlyList<byte[]> files, IReadOnlyList<IReadOnlyList<int>> pagesBundle = null, PdfAStandard archiveStandard = PdfAStandard.None)
{
_ = files ?? throw new ArgumentNullException(nameof(files));
using (var output = new MemoryStream())
{
Merge(files.Select(f => PdfDocument.Open(f)).ToArray(), output, pagesBundle);
Merge(files.Select(f => PdfDocument.Open(f)).ToArray(), output, pagesBundle, archiveStandard);
return output.ToArray();
}
}
@@ -104,20 +120,22 @@
/// </param>
/// <param name="output">Must be writable</param>
/// <param name="pagesBundle"></param>
/// <param name="archiveStandard"></param>
/// </summary>
public static void Merge(IReadOnlyList<Stream> streams, Stream output, IReadOnlyList<IReadOnlyList<int>> pagesBundle = null)
public static void Merge(IReadOnlyList<Stream> streams, Stream output, IReadOnlyList<IReadOnlyList<int>> pagesBundle = null, PdfAStandard archiveStandard = PdfAStandard.None)
{
_ = streams ?? throw new ArgumentNullException(nameof(streams));
_ = output ?? throw new ArgumentNullException(nameof(output));
Merge(streams.Select(f => PdfDocument.Open(f)).ToArray(), output, pagesBundle);
Merge(streams.Select(f => PdfDocument.Open(f)).ToArray(), output, pagesBundle, archiveStandard);
}
private static void Merge(IReadOnlyList<PdfDocument> files, Stream output, IReadOnlyList<IReadOnlyList<int>> pagesBundle)
private static void Merge(IReadOnlyList<PdfDocument> files, Stream output, IReadOnlyList<IReadOnlyList<int>> pagesBundle, PdfAStandard archiveStandard = PdfAStandard.None)
{
var maxVersion = files.Select(x=>x.Version).Max();
using (var document = new PdfDocumentBuilder(output, false, PdfWriterType.Default, maxVersion))
{
document.ArchiveStandard = archiveStandard;
foreach (var fileIndex in Enumerable.Range(0, files.Count))
{
var existing = files[fileIndex];