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 9b403097..5942e0c0 100644
--- a/src/foundation/src/PDFsharp/src/PdfSharp/Pdf.Advanced/PdfEmbeddedFileStream.cs
+++ b/src/foundation/src/PDFsharp/src/PdfSharp/Pdf.Advanced/PdfEmbeddedFileStream.cs
@@ -1,4 +1,4 @@
-// PDFsharp - A .NET library for processing PDF
+// PDFsharp - A .NET library for processing PDF
// See the LICENSE file in the solution root for more information.
namespace PdfSharp.Pdf.Advanced
@@ -12,13 +12,16 @@ 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();
}
@@ -32,6 +35,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);
@@ -46,6 +51,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 760122f3..f595e069 100644
--- a/src/foundation/src/PDFsharp/src/PdfSharp/Pdf.Advanced/PdfNameDictionary.cs
+++ b/src/foundation/src/PDFsharp/src/PdfSharp/Pdf.Advanced/PdfNameDictionary.cs
@@ -62,7 +62,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)
{
@@ -71,7 +71,7 @@ internal void AddEmbeddedFile(string name, Stream stream)
Elements.SetReference(Keys.EmbeddedFiles, _embeddedFiles.Reference ?? throw TH.InvalidOperationException_ReferenceMustNotBeNull());
}
- 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 d1eb2a39..e5e7647b 100644
--- a/src/foundation/src/PDFsharp/src/PdfSharp/Pdf/PdfDocument.cs
+++ b/src/foundation/src/PDFsharp/src/PdfSharp/Pdf/PdfDocument.cs
@@ -879,10 +879,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);
}
///
@@ -890,8 +891,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).