After signing a PDF that was created using Aspose.Pdf, it shows that the signature is invalid.
We use the following code to create the pdf (Note: We are rasterizing a PDF document into images and then creating a new PDF with those images).
We have tried using Aspose.Pdf v 10.6, 10.8 and 11. All give the signature invalid when opening the document in adobe reader.
I have attached the documents as well as screenshots.
The code we are using is below (Please use your own .pfx as the one in the sample is for production ):
static void Main(string[] args)
{
try
{
string inputDocument = @“E:\Aspose\input.pdf”;
string OutputDirectory = @“E:\Aspose”;
byte[] licenceBytes = File.ReadAllBytes(@“E:\Aspose\Aspose.Total.lic”);
MemoryStream msPdfLic = new MemoryStream(licenceBytes);
Aspose.Pdf.License pdfLicense = new Aspose.Pdf.License();
pdfLicense.SetLicense(msPdfLic);
//1 - convert to images
byte[] inputPdf = File.ReadAllBytes(inputDocument);
MemoryStream memStr = new MemoryStream(inputPdf);
Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(memStr);
List<byte[]> imagesCreated = new List<byte[]>();
for (int pageCount = 1; pageCount <= pdfDocument.Pages.Count; pageCount++)
{
MemoryStream imageStream = new MemoryStream();
Aspose.Pdf.Devices.Resolution resolution = new Aspose.Pdf.Devices.Resolution(150);
// where Quality [0-100], 100 is Maximum
Aspose.Pdf.Devices.JpegDevice jpegDevice = new Aspose.Pdf.Devices.JpegDevice(resolution, 100);
jpegDevice.Process(pdfDocument.Pages[pageCount], imageStream);
imagesCreated.Add(imageStream.ToArray());
imageStream.Close();
}
//1 - complete
//2 - convert images to pdf
Aspose.Pdf.Document doc = new Aspose.Pdf.Document();
foreach (byte[] imageBytes in imagesCreated)
{
Page page = doc.Pages.Add();
MemoryStream imageStream = new MemoryStream(imageBytes);
page.PageInfo.Margin.Bottom = 0;
page.PageInfo.Margin.Top = 0;
page.PageInfo.Margin.Left = 0;
page.PageInfo.Margin.Right = 0;
using (System.Drawing.Bitmap myimage = new System.Drawing.Bitmap(imageStream))
{
page.PageInfo.Height = myimage.Height;
page.PageInfo.Width = myimage.Width;
}
Aspose.Pdf.Image image1 = new Aspose.Pdf.Image();
//Global.log.Debug("[ConvertImagesToPdf] - \tAdding image to page");
image1.ImageStream = imageStream;
page.Paragraphs.Add(image1);
}
MemoryStream ms = new MemoryStream();
byte[] outputDocument = null;
try
{
doc.Save(ms);
outputDocument = ms.ToArray();
}
catch (Exception ex)
{
Console.Out.WriteLine("Exception : " + ex.Message);
}
ms.Close();
//saving rasterized Pdf for debug
File.WriteAllBytes(OutputDirectory + “output_rasterized.pdf”, outputDocument);
//2 - complete
//3 - sign
using (Document document = new Document(OutputDirectory + “output_rasterized.pdf”))
{
using (PdfFileSignature signature = new PdfFileSignature(document))
{
PKCS7 pkcs = new PKCS7(@“C:\certstore\GPAPI.pfx”, “PasswordGoesHere”); // Use PKCS7/PKCS7Detached objects
DocMDPSignature docMdpSignature = new DocMDPSignature(pkcs, DocMDPAccessPermissions.FillingInForms);
System.Drawing.Rectangle rect = new System.Drawing.Rectangle(100, 100, 200, 100);
signature.Certify(1, “Signature Reason”, “Contact”, “Location”, true, rect, docMdpSignature);
//save output PDF file
signature.Save(OutputDirectory + “output_signed.pdf”);
}
}
}
catch (Exception ex)
{
Console.Out.WriteLine("Exception : " + ex.Message);
}
Console.ReadLine();
}