Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -12,13 +12,16 @@ public class PdfEmbeddedFileStream : PdfDictionary
/// <summary>
/// Initializes a new instance of PdfEmbeddedFileStream from a stream.
/// </summary>
public PdfEmbeddedFileStream(PdfDocument document, Stream stream) : base(document)
/// <param name="checksum">A 16-byte string which is a MD5 checksum of the bytes of the file</param>
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();
}

Expand All @@ -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);
Expand All @@ -46,6 +51,7 @@ public static bool IsEmbeddedFile(PdfDictionary dictionary)
}

readonly byte[] _data;
readonly string? _checksum;

const string TypeValue = "/EmbeddedFile";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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);

Expand Down
10 changes: 6 additions & 4 deletions src/foundation/src/PDFsharp/src/PdfSharp/Pdf/PdfDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -879,19 +879,21 @@ public void AddNamedDestination(string destinationName, int destinationPage, Pdf
/// </summary>
/// <param name="name">The name used to refer and to entitle the embedded file.</param>
/// <param name="path">The path of the file to embed.</param>
public void AddEmbeddedFile(string name, string path)
/// <param name="checksum">A 16-byte string which is a MD5 checksum of the bytes of the file</param>
public void AddEmbeddedFile(string name, string path, string? checksum = null)
{
var stream = new FileStream(path, FileMode.Open);
AddEmbeddedFile(name, stream);
AddEmbeddedFile(name, stream, checksum);
}

/// <summary>
/// Adds an embedded file to the document.
/// </summary>
/// <param name="name">The name used to refer and to entitle the embedded file.</param>
/// <param name="stream">The stream containing the file to embed.</param>
public void AddEmbeddedFile(string name, Stream stream)
=> Internals.Catalog.Names.AddEmbeddedFile(name, stream);
/// <param name="checksum">A 16-byte string which is a MD5 checksum of the bytes of the file</param>
public void AddEmbeddedFile(string name, Stream stream, string? checksum = null)
=> Internals.Catalog.Names.AddEmbeddedFile(name, stream, checksum);

/// <summary>
/// Flattens a document (make the fields non-editable).
Expand Down