Apply Header on Signature pdf - output pdf signature is invalid

We are using Aspose.pdf.dll 21.7
We tried to apply header on the pdf having valid digital signature.
After applying the header the output pdf showing invalid signature.

Below is the sample code

Aspose.Pdf.Facades.PdfFileStamp fileStamp = new Aspose.Pdf.Facades.PdfFileStamp();
fileStamp.BindPdf(InputFile);
Aspose.Pdf.Facades.FontStyle fontStyle = Aspose.Pdf.Facades.FontStyle.Courier;
Aspose.Pdf.Facades.FormattedText formattedText =
new Aspose.Pdf.Facades.FormattedText(Text,
System.Drawing.Color.Black,
System.Drawing.Color.Transparent,
fontStyle,
EncodingType.Winansi, false, FontSize);
fileStamp.AddHeader(formattedText, topMargin: (float)TopMargin);
fileStamp.Save(OutputFile);
fileStamp.Dispose();
Output_1.pdf (736.7 KB)
Sample_Original.pdf (733.6 KB)

@venkatmallu

We have managed to reproduce the same issue at our side. For the sake of correction, we have logged this problem in our issue tracking system as PDFNET-51441. You will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.

Hi Team,
May I know the update on the fix of this issue.

@venkatmallu

Currently, your issue is pending for analysis and is in the queue. Once we complete the analysis of your issue, we will then be able to provide you an estimate.

Hi Team,
We have dependency on this solution to eliminate the usage of other product (ActivePDF) from our appllication. Can you please help with update ASAP . Thanks

@venkatmallu

You reported this issue in free support forum and it will be treated with normal priority. To speed up the progress of issue’s resolution, we suggest you please check our paid support policies from following link.
Paid Support Policies

Hi Tahir,

We have paid support and updated in the below post, please help.

https://helpdesk.aspose.com/tickets.php?id=8035

@venkatmallu

Your query will be answered in paid support forum.

Hi Tahir / Team,
We have this initial attached file
We are applying header on this file following below steps

1) We are removing the Signature using --> it will remove the highlighted part from pdf.
image.png (18.3 KB)

Aspose.Pdf.Facades.PdfFileSignature pdfSign = new Aspose.Pdf.Facades.PdfFileSignature();
pdfSign.BindPdf(inputPath);
if (pdfSign.ContainsSignature())
{
var sigNames = pdfSign.GetSignNames();
for (int index = 0; index < sigNames.Count; index++)
{
pdfSign.RemoveSignature(sigNames[index]);
}
}

2) Applying watermark --> sample method
Aspose.Pdf.Facades.FormattedText formattedText = new Aspose.Pdf.Facades.FormattedText(
sBannerText,
System.Drawing.Color.LightGray,
Aspose.Pdf.Facades.FontStyle.Helvetica,
Aspose.Pdf.Facades.EncodingType.Winansi,
true,
fontSize);

  1. As the singed document is from different source, can we add the removed Signature back to the pdf after applying watermark

Also as per your previous message, can you please elabore based on my comments inline below
for Containerize.IdentityServer - Single sign-on to access all applications and services of Aspose Pty Ltd.

--------------------------------------your message-----------------------
Tomislav Medic posted 05/09/2022 11:56 am
Hi Venkata,

Please note the team investigated the issue.

When adding a stamp, all pages in the document will be changed and this will break the signature.
Also, Adobe Acrobat does not allow you to add stamps to a signed document, as this will break the signature.
Please, remove the digital signatures, add a stamp, and then sign the file again.
Venkat>> Sign file again means with valid certificate?

However, you can improve the message displayed in the document if you use this code. Perhaps this will help the customer.
var inputPdf = dataDir + “51441_.pdf”;
var outputPdf = dataDir + “51441_out.pdf”;
using (var fileStamp = new Aspose.Pdf.Facades.PdfFileStamp())
{
using (var fsInput = new FileStream(inputPdf, FileMode.Open))
{
using (var fsOutput = new FileStream(outputPdf, FileMode.Create))
{
//Copy stream//can use CopyTo
byte[] buffer = new byte[16 * 1024];
int read;
while ((read = fsInput.Read(buffer, 0, buffer.Length)) > 0)
fsOutput.Write(buffer, 0, read);

        fileStamp.BindPdf(fsOutput);
        var fontStyle = Aspose.Pdf.Facades.FontStyle.Courier;
        var formattedText = new Aspose.Pdf.Facades.FormattedText(
            "my text for stamp",
            System.Drawing.Color.Black,
            System.Drawing.Color.Transparent,
            fontStyle, EncodingType.Winansi, false, 10);
        fileStamp.AddHeader(formattedText, topMargin: 5f);
        fileStamp.Document.Save();
    }
}

}

This keeps the signature structure and allows us to use special options (but it stays “red”).
Venkat>> can you please elabore more

@venkatmallu

As per the feedback shared with you in paid support thread, the signatures will break/damaged as soon as you make any change to the PDF document. As per the Adobe Acrobat specifications, any change in the signed document will revoke the signature validity. The suggested workaround is using stream and editing/modifying the PDF on-fly. It is using incremental save approach to the document in order to improve the message displayed in the document.

Recommended approach is that you remove the signatures first, do modifications like add stamps, etc. and sign the file again (yes, sign it with a valid certificate).

can you please share the sample screenshot how it will improve the message displayed with this code snippet

The suggested workaround is using stream and editing/modifying the PDF on-fly. It is using incremental save approach to the document in order to improve the message displayed in the document.

@venkatmallu

We have updated the ticket information as per your provided feedback and will let you know after performing analysis.