We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Exceptions while trying to use PdfFileSignature Save method

I’m using Aspose.Pdf 7.2 and I’m getting a “Startxref not found” exception while using the PdfFileSignature Save method with a MemoryStream using the following code.




[C#]
////////////////////////////////////////////////////////////////////////////////////////////////

//Create a new Aspose PDF document to use to add the signature
Document l_PdfToSign = new Document(outputStream);

//Create the pdf File signing object
PdfFileSignature l_pdfSigner = new PdfFileSignature(l_PdfToSign);
//Set the signature time to the current time
signature.Date = DateTime.Now;

//Retrieve the visual signature
if (null == metaData.SignatureAppearanceFile)
{
l_pdfSigner.SignatureAppearanceStream = GenerateSignatureAppearance(signature);
}
else
{
l_pdfSigner.SignatureAppearanceStream = GenerateFileMemoryStream(metaData.SignatureAppearanceFile);
}

//Add the current signature to the pdf
l_pdfSigner.Sign(l_Page.PageNumber, metaData.Reason, metaData.AuthenticatedSigner, metaData.Location, true, l_Page.SignatureArea, signature);

//This should work but there seems to be a bug in the Aspose library
//Save signed pdf to output stream
outputStream.Position = 0;
l_pdfSigner.Save(outputStream);

////////////////////////////////////////////////////////////////////////////////////////////////


However if I change the end slightly to the following work around it works:


[C#]
////////////////////////////////////////////////////////////////////////////////////////////////

//Save signed pdf to output stream
MemoryStream l_TempStream = new MemoryStream();

l_pdfSigner.Save(tempStream);
outputStream.Position = l_TempStream.Position = 0;

l_TempStream.CopyTo(outputStream);

////////////////////////////////////////////////////////////////////////////////////////////////

The key difference is that I'm trying to save to the MemoryStream that the Aspose.PDF.Document is created from in the problem code while I'm saving to a temporary MemoryStream and then copying that stream to the MemoryStream that the Aspose.PDF.Document is created from in the example that works.

I would like to not have to use the work around.

Thanks for the help.


>>>> UPDATE <<<<

Actually the above work around saves the original source document to the memory stream without the signature added.

The only way to save the pdf with the signature in it and retrieve it in the form of a memory stream is the following (which is really ugly and, as we are working with secure information, we'd rather not generate intermediate files on disk):

[C#]
////////////////////////////////////////////////////////////////////////////////////////////////

//Save the signed pdf
FileStream file = new FileStream("temp.pdf", FileMode.OpenOrCreate, FileAccess.ReadWrite);
file.Position = 0;

l_pdfSigner.Save(file);
file.Flush();
file.Close();

//Reopen the now saved pdf in stream form

// Read all bytes in from the file on the disk.
byte[] l_FileBytes = System.IO.File.ReadAllBytes("temp.pdf ");

// Create a memory stream from those bytes.
outputStream = new MemoryStream(l_FileBytes);

// Rewind the stream position back to zero so it is ready for next reader.
outputStream.Position = 0;

////////////////////////////////////////////////////////////////////////////////////////////////

Hi Andrew,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Sorry for delay in response.

I am testing your scenario with the latest version of Aspose.Pdf for .NET and I will reply you shortly with my test results.

Sorry for the inconvenience,

Hi,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

<o:p> </o:p>

Thank
you for being patient.<o:p></o:p>

<o:p> </o:p>

After
further testing your issue with my own test document, I am getting an “Object
Not Found” exception. I have registered an issue in our issue tracking system
with issue id: PDFNEWNET-34199. However, as you have mentioned that you
are getting “Startxref not found” exception, I would request you to share your template file to
help us test the issue with your template file too. This will help us identify whether
the issue is same or different. <o:p></o:p>

<o:p> </o:p>

Sorry
for the inconvenience,<o:p></o:p>

Hi Andrew,


Thanks for your patience. We have further investigated the issue, please try following code snippet it will help you to accomplish the task.

Aspose.Pdf.Document pdf1 = new
Aspose.Pdf.Document(@“d:\input.pdf”);<o:p></o:p>

using (MemoryStream outputStream = new MemoryStream())

{

//Save the resultant PDF document

pdf1.Save(outputStream);

//Create the pdf File signing object

PdfFileSignature l_pdfSigner = new PdfFileSignature(pdf1);

PKCS1 signature = new PKCS1(@"d:\test.pfx", "password");

//Set the signature time to the current time

signature.Date = DateTime.Now;

//Add the current signature to the pdf

l_pdfSigner.Sign(1, "Reason", "AuthenticatedSigner", "Location", true,

new System.Drawing.Rectangle(100, 100, 200, 200), signature);

//Save signed pdf to output stream

l_pdfSigner.Save(outputStream);

using (Stream file = File.Create(@"d:\output.pdf"))

{

outputStream.WriteTo(file);

}

}

Please feel free to contact us for any further assistance.


Best Regards,

The issues you have found earlier (filed as PDFNEWNET-34199) have been fixed in Aspose.Pdf for .NET 9.3.0.

Blog post for this release can be viewed over this link


This message was posted using Notification2Forum from Downloads module by Aspose Notifier.