Reputation: 1882
I have used a particular pull request from PDFsharp library to add digital signature to PDF using PDFsharp. It works perfectly fine if it is an unsigned document.
When I try to add a second signature to an already signed PDF, it gets an error "/sigflags" already exists.
Is it possible to append to a "/sigflags"? Instead of trying to add it?
private void AddSignatureComponents(object sender, EventArgs e)
{
var catalog = Document.Catalog;
if (catalog.AcroForm == null)
catalog.AcroForm = new PdfAcroForm(Document);
catalog.AcroForm.Elements.Add(PdfAcroForm.Keys.SigFlags, new PdfInteger(3));
var signature = new PdfSignatureField(Document);
var paddedContents = new PdfString("", PdfStringFlags.HexLiteral, maximumSignatureLength.Value);
var paddedRange = new PdfArray(Document, byteRangePaddingLength, new PdfInteger(0), new PdfInteger(0), new PdfInteger(0), new PdfInteger(0));
signature.Contents = paddedContents;
signature.ByteRange = paddedRange;
signature.Reason = Options.Reason;
signature.Location = Options.Location;
signature.Rectangle = new PdfRectangle(Options.Rectangle);
signature.AppearanceHandler = Options.AppearanceHandler ?? new DefaultAppearanceHandler()
{
Location = Options.Location,
Reason = Options.Reason,
Signer = Certificate.GetNameInfo(X509NameType.SimpleName, false)
};
signature.PrepareForSave();
this.contentsTraker = new PositionTracker(paddedContents);
this.rangeTracker = new PositionTracker(paddedRange);
foreach (var pagenumber in Options.PageNumber)
{
var index = pagenumber - 1;
if (!Document.Pages[index].Elements.ContainsKey(PdfPage.Keys.Annots))
Document.Pages[index].Elements.Add(PdfPage.Keys.Annots, new PdfArray(Document));
try
{
(Document.Pages[index].Elements[PdfPage.Keys.Annots] as PdfArray).Elements.Add(signature);
}
catch
{
if (Document.Pages[index].Elements.ContainsKey(PdfPage.Keys.Annots))
{
Document.Pages[index].Elements.Remove(PdfPage.Keys.Annots);
Document.Pages[index].Elements.Add(PdfPage.Keys.Annots, new PdfArray(Document));
}
(Document.Pages[index].Elements[PdfPage.Keys.Annots] as PdfArray).Elements.Add(signature);
}
}
catalog.AcroForm.Fields.Elements.Add(signature);
}
Upvotes: 4
Views: 1975
Reputation: 11
The SigFlags entry in AcroForm should only occur once and only if there are Signature fields or a Digital Signature. This is why you are getting an error with the second signature. The PdfInteger value bit 0 should only be 1 if there are Signature fields and bit 1 should only be 1 if there are Digital Signatures. By setting it to 3 you are indicating (correctly in your case) that there are both Signature fields and Digital signatures. All you need to do in your code is check if the SigFlags entry is present in AcroForm and if it is overwrite it otherwise add it.
if (catalog.AcroForm == null)
{ catalog.AcroForm = new PdfAcroForm(Document);}
if (catalog.AcroForm.Elements.ContainsKey(PdfAcroForm.Keys.SigFlags))
{ catalog.AcroForm.Elements[PdfAcroForm.Keys.SigFlags] = new PdfInteger(3);}
else
{ catalog.AcroForm.Elements.Add(PdfAcroForm.Keys.SigFlags, new PdfInteger(3));}
Upvotes: 1