Can I save twice to the same stream?

Q:

I’m signing a document twice, and each time saving to a memory stream incrementally. My code looks something like this:

`
using (var ms = new MemoryStream())
{
using (PDFDoc doc = new PDFDoc(infile))
{
// […sign the document…]
doc.Save(ms, 0);
}
ms.Position = 0;
using (var doc = new PDFDoc(ms))
{
// […sign the document with a second signature…]
doc.Save(ms, SDFDoc.SaveOptions.e_incremental);
// […save ms to file…]
}
}

`

The save after the second signature results in a corrupted file. What’s going wrong?

A:

You should save to a second stream different than the one you loaded the PDF from. For example:

`
using (var ms = new MemoryStream())
{
using (PDFDoc doc = new PDFDoc(infile))
{
// […sign the document…]
doc.Save(ms, 0);
}
ms.Position = 0;
using (var doc = new PDFDoc(ms))
{
// […sign the document with a second signature…]
using (var ms1 = new MemoryStream())
{
doc.Save(ms1, SDFDoc.SaveOptions.e_incremental);
// […save ms1 to file…]
}
}
}

`