From 8e33527bb968d718cec3542fdbd384a63f695fcb Mon Sep 17 00:00:00 2001 From: Julien R Date: Mon, 16 Oct 2023 14:16:10 +0200 Subject: [PATCH] add CheckSum to PdfEmbeddedFileStream checksum has to be an MD5 hash of the file --- .../src/PdfSharp/Pdf.Advanced/PdfEmbeddedFileStream.cs | 7 ++++++- .../src/PdfSharp/Pdf.Advanced/PdfNameDictionary.cs | 4 ++-- .../src/PDFsharp/src/PdfSharp/Pdf/PdfDocument.cs | 10 ++++++---- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/foundation/src/PDFsharp/src/PdfSharp/Pdf.Advanced/PdfEmbeddedFileStream.cs b/src/foundation/src/PDFsharp/src/PdfSharp/Pdf.Advanced/PdfEmbeddedFileStream.cs index 04c12d0c..21a37d53 100644 --- a/src/foundation/src/PDFsharp/src/PdfSharp/Pdf.Advanced/PdfEmbeddedFileStream.cs +++ b/src/foundation/src/PDFsharp/src/PdfSharp/Pdf.Advanced/PdfEmbeddedFileStream.cs @@ -18,13 +18,15 @@ public class PdfEmbeddedFileStream : PdfDictionary /// /// Initializes a new instance of PdfEmbeddedFileStream from a stream. /// - public PdfEmbeddedFileStream(PdfDocument document, Stream stream) : base(document) + /// A 16-byte string which is a MD5 checksum of the bytes of the file + public PdfEmbeddedFileStream(PdfDocument document, Stream stream, string? checksum = null) : base(document) { _data = new byte[stream.Length]; using (stream) { stream.Read(_data, 0, (int)stream.Length); } + _checksum = checksum; Initialize(); } @@ -39,6 +41,8 @@ void Initialize() var now = DateTime.Now; objParams.Elements.SetDateTime("/CreationDate", now); objParams.Elements.SetDateTime("/ModDate", now); + if (!string.IsNullOrEmpty(_checksum)) + objParams.Elements.SetString("/CheckSum", _checksum); Elements.SetObject(Keys.Params, objParams); Stream = new PdfStream(_data, this); @@ -53,6 +57,7 @@ public static bool IsEmbeddedFile(PdfDictionary dictionary) } readonly byte[] _data; + readonly string? _checksum; const string TypeValue = "/EmbeddedFile"; diff --git a/src/foundation/src/PDFsharp/src/PdfSharp/Pdf.Advanced/PdfNameDictionary.cs b/src/foundation/src/PDFsharp/src/PdfSharp/Pdf.Advanced/PdfNameDictionary.cs index 6ab77bf4..7c0bd975 100644 --- a/src/foundation/src/PDFsharp/src/PdfSharp/Pdf.Advanced/PdfNameDictionary.cs +++ b/src/foundation/src/PDFsharp/src/PdfSharp/Pdf.Advanced/PdfNameDictionary.cs @@ -55,7 +55,7 @@ internal void AddNamedDestination(string destinationName, int destinationPage, P PdfNameTreeNode? _dests; - internal void AddEmbeddedFile(string name, Stream stream) + internal void AddEmbeddedFile(string name, Stream stream, string? checksum = null) { if (_embeddedFiles == null) { @@ -64,7 +64,7 @@ internal void AddEmbeddedFile(string name, Stream stream) Elements.SetReference(Keys.EmbeddedFiles, _embeddedFiles.Reference); } - var embeddedFileStream = new PdfEmbeddedFileStream(Owner, stream); + var embeddedFileStream = new PdfEmbeddedFileStream(Owner, stream, checksum); var fileSpecification = new PdfFileSpecification(Owner, embeddedFileStream, name); Owner.Internals.AddObject(fileSpecification); diff --git a/src/foundation/src/PDFsharp/src/PdfSharp/Pdf/PdfDocument.cs b/src/foundation/src/PDFsharp/src/PdfSharp/Pdf/PdfDocument.cs index c5054814..cb62656c 100644 --- a/src/foundation/src/PDFsharp/src/PdfSharp/Pdf/PdfDocument.cs +++ b/src/foundation/src/PDFsharp/src/PdfSharp/Pdf/PdfDocument.cs @@ -770,10 +770,11 @@ public void AddNamedDestination(string destinationName, int destinationPage, Pdf /// /// The name used to refer and to entitle the embedded file. /// The path of the file to embed. - public void AddEmbeddedFile(string name, string path) + /// A 16-byte string which is a MD5 checksum of the bytes of the file + public void AddEmbeddedFile(string name, string path, string? checksum = null) { var stream = new FileStream(path, FileMode.Open); - AddEmbeddedFile(name, stream); + AddEmbeddedFile(name, stream, checksum); } /// @@ -781,8 +782,9 @@ public void AddEmbeddedFile(string name, string path) /// /// The name used to refer and to entitle the embedded file. /// The stream containing the file to embed. - public void AddEmbeddedFile(string name, Stream stream) - => Internals.Catalog.Names.AddEmbeddedFile(name, stream); + /// A 16-byte string which is a MD5 checksum of the bytes of the file + public void AddEmbeddedFile(string name, Stream stream, string? checksum = null) + => Internals.Catalog.Names.AddEmbeddedFile(name, stream, checksum); /// /// Flattens a document (make the fields non-editable).