Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support multiple signatures #5

Open
wants to merge 3 commits into
base: signature-feature
Choose a base branch
from
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
1 change: 1 addition & 0 deletions src/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<PackageVersion Include="GitVersion.MsBuild" Version="5.12.0" />
<PackageVersion Include="Microsoft.CSharp" Version="4.7.0" />
<PackageVersion Include="System.Drawing.Common" Version="4.7.3" />
<PackageVersion Include="System.Formats.Asn1" Version="8.0.1" />
<PackageVersion Include="System.IO.Compression.ZipFile" Version="4.3.0" />
<PackageVersion Include="System.Runtime.Serialization.Primitives" Version="4.3.0" />
<PackageVersion Include="System.Diagnostics.FileVersionInfo" Version="4.3.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,10 +324,11 @@
<Compile Include="..\PdfSharp\Pdf.Signatures\DefaultSignatureAppearanceHandler.cs" Link="Pdf.Signatures\DefaultSignatureAppearanceHandler.cs" />
<Compile Include="..\PdfSharp\Pdf.Signatures\DefaultSigner.cs" Link="Pdf.Signatures\DefaultSigner.cs" />
<Compile Include="..\PdfSharp\Pdf.Signatures\ISigner.cs" Link="Pdf.Signatures\ISigner.cs" />
<Compile Include="..\PdfSharp\Pdf.Signatures\PdfArrayWithPadding.cs" Link="Pdf.Signatures\PdfArrayWithPadding.cs" />
<Compile Include="..\PdfSharp\Pdf.Signatures\PdfSignatureHandler.cs" Link="Pdf.Signatures\PdfSignatureHandler.cs" />
<Compile Include="..\PdfSharp\Pdf.Signatures\PdfSignatureOptions.cs" Link="Pdf.Signatures\PdfSignatureOptions.cs" />
<Compile Include="..\PdfSharp\Pdf.Signatures\RangedStream.cs" Link="Pdf.Signatures\RangedStream.cs" />
<Compile Include="..\PdfSharp\Pdf.Signatures\PdfSignatureValue.cs" Link="Pdf.Signatures\PdfSignatureValue.cs" />
<Compile Include="..\PdfSharp\Pdf.Signatures\ISignatureRenderer.cs" Link="Pdf.Signatures\ISignatureRenderer.cs" />
<Compile Include="..\PdfSharp\Pdf.Signatures\DefaultSignatureRenderer.cs" Link="Pdf.Signatures\DefaultSignatureRenderer.cs" />
<Compile Include="..\PdfSharp\Pdf.Signatures\PdfSigner.cs" Link="Pdf.Signatures\PdfSigner.cs" />
<Compile Include="..\PdfSharp\Pdf.Structure\PdfAttributesBase.cs" Link="Pdf.Structure\PdfAttributesBase.cs" />
<Compile Include="..\PdfSharp\Pdf.Structure\PdfLayoutAttributes.cs" Link="Pdf.Structure\PdfLayoutAttributes.cs" />
<Compile Include="..\PdfSharp\Pdf.Structure\PdfMarkedContentReference.cs" Link="Pdf.Structure\PdfMarkedContentReference.cs" />
Expand Down Expand Up @@ -434,6 +435,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="System.Formats.Asn1" />
<PackageReference Include="System.Security.Cryptography.Pkcs" />
<PackageReference Include="BouncyCastle.Cryptography" />
</ItemGroup>
Expand Down
20 changes: 11 additions & 9 deletions src/foundation/src/PDFsharp/src/PdfSharp-wpf/PdfSharp-wpf.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -319,15 +319,16 @@
<Compile Include="..\PdfSharp\Pdf.Security\PdfSecurityHandler.cs" Link="Pdf.Security\PdfSecurityHandler.cs" />
<Compile Include="..\PdfSharp\Pdf.Security\PdfSecuritySettings.cs" Link="Pdf.Security\PdfSecuritySettings.cs" />
<Compile Include="..\PdfSharp\Pdf.Security\PdfStandardSecurityHandler.cs" Link="Pdf.Security\PdfStandardSecurityHandler.cs" />
<Compile Include="..\PdfSharp\Pdf.Signatures\BouncySigner.cs" Link="Pdf.Signatures\BouncySigner.cs" />
<Compile Include="..\PdfSharp\Pdf.Signatures\DefaultSignatureAppearanceHandler.cs" Link="Pdf.Signatures\DefaultSignatureAppearanceHandler.cs" />
<Compile Include="..\PdfSharp\Pdf.Signatures\DefaultSigner.cs" Link="Pdf.Signatures\DefaultSigner.cs" />
<Compile Include="..\PdfSharp\Pdf.Signatures\ISigner.cs" Link="Pdf.Signatures\ISigner.cs" />
<Compile Include="..\PdfSharp\Pdf.Signatures\PdfArrayWithPadding.cs" Link="Pdf.Signatures\PdfArrayWithPadding.cs" />
<Compile Include="..\PdfSharp\Pdf.Signatures\PdfSignatureHandler.cs" Link="Pdf.Signatures\PdfSignatureHandler.cs" />
<Compile Include="..\PdfSharp\Pdf.Signatures\PdfSignatureOptions.cs" Link="Pdf.Signatures\PdfSignatureOptions.cs" />
<Compile Include="..\PdfSharp\Pdf.Signatures\RangedStream.cs" Link="Pdf.Signatures\RangedStream.cs" />
<Compile Include="..\PdfSharp\Pdf.Structure\PdfAttributesBase.cs" Link="Pdf.Structure\PdfAttributesBase.cs" />
<Compile Include="..\PdfSharp\Pdf.Signatures\BouncySigner.cs" Link="Pdf.Signatures\BouncySigner.cs" />
<Compile Include="..\PdfSharp\Pdf.Signatures\DefaultSignatureAppearanceHandler.cs" Link="Pdf.Signatures\DefaultSignatureAppearanceHandler.cs" />
<Compile Include="..\PdfSharp\Pdf.Signatures\DefaultSigner.cs" Link="Pdf.Signatures\DefaultSigner.cs" />
<Compile Include="..\PdfSharp\Pdf.Signatures\ISigner.cs" Link="Pdf.Signatures\ISigner.cs" />
<Compile Include="..\PdfSharp\Pdf.Signatures\PdfSignatureOptions.cs" Link="Pdf.Signatures\PdfSignatureOptions.cs" />
<Compile Include="..\PdfSharp\Pdf.Signatures\PdfSignatureValue.cs" Link="Pdf.Signatures\PdfSignatureValue.cs" />
<Compile Include="..\PdfSharp\Pdf.Signatures\ISignatureRenderer.cs" Link="Pdf.Signatures\ISignatureRenderer.cs" />
<Compile Include="..\PdfSharp\Pdf.Signatures\DefaultSignatureRenderer.cs" Link="Pdf.Signatures\DefaultSignatureRenderer.cs" />
<Compile Include="..\PdfSharp\Pdf.Signatures\PdfSigner.cs" Link="Pdf.Signatures\PdfSigner.cs" />
<Compile Include="..\PdfSharp\Pdf.Structure\PdfAttributesBase.cs" Link="Pdf.Structure\PdfAttributesBase.cs" />
<Compile Include="..\PdfSharp\Pdf.Structure\PdfLayoutAttributes.cs" Link="Pdf.Structure\PdfLayoutAttributes.cs" />
<Compile Include="..\PdfSharp\Pdf.Structure\PdfMarkedContentReference.cs" Link="Pdf.Structure\PdfMarkedContentReference.cs" />
<Compile Include="..\PdfSharp\Pdf.Structure\PdfMarkInformation.cs" Link="Pdf.Structure\PdfMarkInformation.cs" />
Expand Down Expand Up @@ -440,6 +441,7 @@
</ItemGroup>-->

<ItemGroup>
<PackageReference Include="System.Formats.Asn1" />
<PackageReference Include="System.Security.Cryptography.Pkcs" />
<PackageReference Include="BouncyCastle.Cryptography" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ public string Name
string name = Elements.GetString(Keys.T);
return name;
}
set
{
Elements.SetString(Keys.T, value);
}
}

/// <summary>
Expand Down Expand Up @@ -267,6 +271,10 @@ public PdfAcroFieldCollection Fields
/// </summary>
public sealed class PdfAcroFieldCollection : PdfArray
{
PdfAcroFieldCollection(PdfDocument document)
: base(document)
{ }

internal PdfAcroFieldCollection(PdfArray array)
: base(array)
{ }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,34 @@ public PdfAcroField.PdfAcroFieldCollection Fields
}
PdfAcroField.PdfAcroFieldCollection? _fields;

/// <summary>
/// Gets the flattened field-hierarchy of this AcroForm
/// </summary>
public IEnumerable<PdfAcroField> GetAllFields()
{
var fields = new List<PdfAcroField>();
if (Fields != null)
{
for (var i = 0; i < Fields.Elements.Count; i++)
{
var field = Fields[i];
TraverseFields(field, ref fields);
}
}
return fields;
}

private static void TraverseFields(PdfAcroField parentField, ref List<PdfAcroField> fieldList)
{
fieldList.Add(parentField);
for (var i = 0; i < parentField.Fields.Elements.Count; i++)
{
var field = parentField.Fields[i];
if (!string.IsNullOrEmpty(field.Name))
TraverseFields(field, ref fieldList);
}
}

/// <summary>
/// Predefined keys of this dictionary.
/// The description comes from PDF 1.4 Reference.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using PdfSharp.Pdf.IO;
using PdfSharp.Drawing;
using PdfSharp.Pdf.Annotations;
using PdfSharp.Pdf.Signatures;

namespace PdfSharp.Pdf.AcroForms
{
Expand All @@ -17,13 +18,40 @@ public sealed class PdfSignatureField : PdfAcroField
/// </summary>
internal PdfSignatureField(PdfDocument document)
: base(document)
{ }
{
Elements[PdfAcroField.Keys.FT] = new PdfName("/Sig");
}

internal PdfSignatureField(PdfDictionary dict)
: base(dict)
{ }

public IAnnotationAppearanceHandler CustomAppearanceHandler { get; internal set; }
/// <summary>
/// Gets or sets the value for this field
/// </summary>
public new PdfSignatureValue? Value
{
get
{
if (sigValue is null)
{
var dict = Elements.GetValue(PdfAcroField.Keys.V) as PdfDictionary;
if (dict is not null)
sigValue = new PdfSignatureValue(dict);
}
return sigValue;
}
set
{
if (value is not null)
Elements.SetReference(PdfAcroField.Keys.V, value);
else
Elements.Remove(PdfAcroField.Keys.V);
}
}
PdfSignatureValue? sigValue;

public IAnnotationAppearanceHandler? CustomAppearanceHandler { get; internal set; }

/// <summary>
/// Creates the custom appearance form X object for the annotation that represents
Expand Down Expand Up @@ -87,7 +115,9 @@ internal override void WriteDictionaryElement(PdfWriter writer, PdfName key)

/// <summary>
/// Predefined keys of this dictionary.
/// The description comes from PDF 1.4 Reference.
/// The description comes from PDF 1.4 Reference.<br></br>
/// TODO: These are wrong !
/// The keys are for a <see cref="PdfSignatureValue"/>, not for a <see cref="PdfSignatureField"/>
/// </summary>
public new class Keys : PdfAcroField.Keys
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public PdfPages Pages
if (_pages == null)
{
_pages = (PdfPages?)Elements.GetValue(Keys.Pages, VCF.CreateIndirect) ?? NRT.ThrowOnNull<PdfPages>();
if (Owner.IsImported)
if (Owner.IsImported && Owner._openMode != PdfDocumentOpenMode.Append)
_pages.FlattenPageTree();
}
return _pages;
Expand Down Expand Up @@ -150,16 +150,33 @@ public PdfNameDictionary Names
PdfNameDictionary? _names;

/// <summary>
/// Gets the AcroForm dictionary of this document.
/// Gets or sets the AcroForm dictionary of this document.
/// </summary>
public PdfAcroForm AcroForm
public PdfAcroForm? AcroForm
{
get
{
if (_acroForm == null)
_acroForm = (PdfAcroForm?)Elements.GetValue(Keys.AcroForm)??NRT.ThrowOnNull<PdfAcroForm>();
_acroForm = (PdfAcroForm?)Elements.GetValue(Keys.AcroForm);
return _acroForm;
}
set
{
if (value != null)
{
if (!value.IsIndirect)
_document.IrefTable.Add(value);
Elements.SetReference(Keys.AcroForm, value);
_acroForm = value;
}
else
{
if (AcroForm != null && AcroForm.Reference != null)
_document.IrefTable.Remove(AcroForm.Reference);
Elements.Remove(Keys.AcroForm);
_acroForm = null;
}
}
}
PdfAcroForm? _acroForm;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public PdfContent(PdfDictionary dict) // HACK PdfContent
: base(dict)
{
// A PdfContent dictionary is always unfiltered.
Decode();
Owner.IrefTable.IgnoreModify(Decode); // decode modifies the object, ignore that
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ public PdfContent AppendContent()
{
Debug.Assert(Owner != null);

SetModified();
if (Owner._openMode != PdfDocumentOpenMode.Append)
SetModified();

PdfContent content = new PdfContent(Owner);
Owner.IrefTable.Add(content);
Debug.Assert(content.Reference != null);
Expand All @@ -64,7 +66,9 @@ public PdfContent PrependContent()
{
Debug.Assert(Owner != null);

SetModified();
if (Owner._openMode != PdfDocumentOpenMode.Append)
SetModified();

PdfContent content = new PdfContent(Owner);
Owner.IrefTable.Add(content);
Debug.Assert(content.Reference != null);
Expand Down
Loading