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;
////////////////////////////////////////////////////////////////////////////////////////////////