Cant' access a closed stream while trying to save a document

I created a Pdf Document from a byte[] after converting it to a Memorystream.
using (MemoryStream pdfStream = new MemoryStream(inputBuffer))
{
doc = new Document(pdfStream, password);
}

then I make some changes to the document.in this case I just performed a doc.Decrypt()

And then while trying to save the document to a new file or to a MemoryStream I get the “can’t access closed stream” exception.

Both the below line of code error out.

doc.Save(tempFile)
or
MemoryStream ms = new MemoryStream();
doc.Save(ms);

@dennisonkslaw

Thank you for contacting support.

Would you please ensure using Aspose.PDF for .NET 19.11 and share SSCCE code so that we may try to reproduce and investigate it in our environment.

Im using v 17.9

Here is the sample code

private bool RemoveEncryptionFromStream(string encryptedFile, byte[] inputBuffer, string password, out byte[] outputBuffer)
{
bool processStatus = false;
outputBuffer = null;
try
{
using (MemoryStream pdfStream = new MemoryStream(inputBuffer))
{
using (Document document = new Document(pdfStream, password))
{
if (document.CryptoAlgorithm == null )
{
if (encryptedFile.Length > 0)
{
outputBuffer = PdfByteArray(document);
processStatus = true;
}
else
{
MemoryStream ms = new MemoryStream();
document.Save(ms);
outputBuffer = ms.ToArray();
processStatus = true;
}
}
}
}

        }
        catch (Exception e)
        {
            processStatus = false;
        }
        return processStatus;
    }

@dennisonkslaw

The code snippet includes a method call PdfByteArray so please share its definition as well along with your sample document with its password, if any.

sample document is attached.
password to open the file is “t”

private byte[] PdfByteArray(Document doc)
{
MemoryStream ms = new MemoryStream();
doc.Save(ms);
return ms.ToArray();

    }

BA-enc.pdf (19.3 KB)

@dennisonkslaw

We can not reproduce this issue using below code snippet with Aspose.PDF for .NET 19.11 so please upgrade to latest version and then share your kind feedback with us.

bool processStatus = false;
byte[] outputBuffer = null;
byte[] inputBuffer = null;
string password = "t";
string encryptedFile = dataDir + "BA - enc.pdf";

using (var fS = System.IO.File.OpenRead(dataDir + "BA-enc.pdf"))
{
    using (var binaryReader = new BinaryReader(fS))
    {
        inputBuffer = binaryReader.ReadBytes((int)fS.Length);
    }
}
try
{
    using (MemoryStream pdfStream = new MemoryStream(inputBuffer))
    {
        using (Document document = new Document(pdfStream, password))
        {
            if (document.CryptoAlgorithm == null)
            {
                if (encryptedFile.Length > 0)
                {
                    outputBuffer = PdfByteArray(document);
                    processStatus = true;
                }
                else
                {
                    MemoryStream ms = new MemoryStream();
                    document.Save(ms);
                    outputBuffer = ms.ToArray();
                    processStatus = true;
                }
            }
        }
    }
}
catch (Exception e)
{
    processStatus = false;
}

private static byte[] PdfByteArray(Document doc)
{
    MemoryStream ms = new MemoryStream();
    doc.Save(ms);
    return ms.ToArray();
}

Thanks it works now.
Actually I used a separate method for opening the document and once I come out of that function it closed the stream object.

Is there any way to remove all the certificates applied to the pdf file?

@dennisonkslaw

Thank you for the feedback.

Please share a sample document with certificates so that we may investigate further.

ARB certificate.pdf (147.6 KB)
Dennison-Certified1.pdf (19.5 KB)

Attached 2 files with certificate. Can’t open “Dennison-certified1.pdf” it throws an exception. Not sure if that is expected

@dennisonkslaw

Please note that Dennison-certified1.pdf is a secured PDF document with permission password set. So the exception is expected becuase password is needed to modify this document. However, we have logged a ticket with ID PDFNET-47387 to further investigate removing certificates from a PDF document. We will let you know once any update will be available in this regard.

Thanks Farhan for the update.

Regarding “Dennison-certified1.pdf”, the password for the file is “password-1”. This password was set at certificate level. But even if I provide this password to open the file using the API, it fails with an exception.

@dennisonkslaw

We have recorded the information and will let you know once any update will be available in this regard.

1 Like