Convert CSV/excel/pdf memory stream to PDF file with digital sign and password protection

image.png (153.7 KB)

Error: System.Exception: ‘The root object missing or invalid’ . PFA.

public MemoryStream AsposePDF(MemoryStream pdfStream)
{
Aspose.Pdf.Document exportDoc = new Aspose.Pdf.Document(pdfStream);

        PdfFileSecurity fileSecurity = new PdfFileSecurity();
        fileSecurity.BindPdf(exportDoc);
        fileSecurity.EncryptFile("user", "owner", DocumentPrivilege.Print, KeySize.x256, Algorithm.AES);
        MemoryStream stream = new MemoryStream();
        fileSecurity.Save(stream);
        stream.Seek(0, SeekOrigin.Begin);
        exportDoc = new Aspose.Pdf.Document(stream, "owner");

        using (PdfFileSignature signature = new PdfFileSignature(exportDoc))
        {
            PKCS7 pkcs = new PKCS7(HttpContext.Current.Server.MapPath("~") + "\\test-cert-EA.pfx", "2020");// Use PKCS7/PKCS7Detached objects
            DocMDPSignature docMdpSignature = new DocMDPSignature(pkcs, DocMDPAccessPermissions.NoChanges);
            System.Drawing.Rectangle rect = new System.Drawing.Rectangle(100, 100, 200, 100);
            // Set signature appearance
            signature.SignatureAppearance = HttpContext.Current.Server.MapPath("~") + "\\images\\Logo.png";
            // Create any of the three signature types
            signature.Certify(1, "Signature Reason", "Contact", "Location", true, rect, docMdpSignature);
            // Save output PDF file
            signature.Save(stream);
        }
     
        // Close the stream object
        pdfStream.Close();

        return stream;          
    }

@nchada

We were unable to replicate this issue in our environment while using demograph.pdf with above code snippet. Would you kindly share a sample console application with all source files so that we can try to reproduce the issue in our environment and address it accordingly.

attached is the sample project.C#.zip (3.3 MB)

@nchada

We tested the scenario in our environment while using your application. We did not test it using the Package GrapeCity.XXX as it required valid license. Please check following complete code snippet of CustomExportPdf.aspx.cs which was used for testing and it did not produce any error:

protected void Page_Load(object sender, EventArgs e)
        {
            Response.ContentType = "application/pdf";
            Response.Clear();   
            Response.AddHeader("content-disposition", "attachment;filename=MyPDF.PDF");
          
            MemoryStream pdfStream = new MemoryStream();           

            Response.ContentType = "application/pdf";
            Response.Clear();
            Response.AddHeader("content-disposition", "attachment;filename=MyPDF.PDF");
            FileStream stream = new FileStream(Server.MapPath("~") + @"\RpxReports\demograph.pdf", FileMode.Open);
            MemoryStream ms = new MemoryStream();
            stream.CopyTo(pdfStream);
            AsposePDF(pdfStream).WriteTo(Response.OutputStream);
            Response.End();

        }

        public MemoryStream  AsposePDF(MemoryStream pdfStream)
        {
            Aspose.Pdf.Document exportDoc = new Aspose.Pdf.Document(pdfStream);

            
            MemoryStream stream = new MemoryStream();
            

            using (PdfFileSignature signature = new PdfFileSignature(exportDoc))
            {
                PKCS7 pkcs = new PKCS7(HttpContext.Current.Server.MapPath("~") + "\\test-cert-EA.pfx", "2020");// Use PKCS7/PKCS7Detached objects
                DocMDPSignature docMdpSignature = new DocMDPSignature(pkcs, DocMDPAccessPermissions.NoChanges);
                System.Drawing.Rectangle rect = new System.Drawing.Rectangle(100, 100, 200, 100);
                // Set signature appearance
                signature.SignatureAppearance = HttpContext.Current.Server.MapPath("~") + "\\images\\Logo.png";
                // Create any of the three signature types
                signature.Certify(1, "Signature Reason", "Contact", "Location", true, rect, docMdpSignature);
                // Save output PDF file
                signature.Save(stream);
            }
            stream.Seek(0, SeekOrigin.Begin);
            exportDoc = new Aspose.Pdf.Document(stream);

            PdfFileSecurity fileSecurity = new PdfFileSecurity();
            fileSecurity.BindPdf(exportDoc);
            fileSecurity.EncryptFile("user", "owner", DocumentPrivilege.Print, KeySize.x256, Algorithm.AES);
            fileSecurity.Save(stream);

            // Close the stream object
            pdfStream.Close();

            return stream;          
        }

We request you to please share a simple console application which is able to replicate the issue in our environment and does not contain extra resources. We will again test it in our environment and address it accordingly.

with the given code changes pdf is generating but not protecting with the password.

please send us a code sample which protects the pdf and csv file with the password and digital sign.

Note: memory stream as an input parameter.

Thanks

@nchada

We were able to replicate the issue in our environment that API was not setting password for the PDF. Please note that same code snippet worked fine in a console application and we need to further investigate the reasons of why it is not working in this particular scenario.

For the purpose, we have logged an issue as PDFNET-47886 in our issue tracking system. We will further look into details of it and keep you posted with the status of its correction. Please be patient and spare us little time.

We are sorry for the inconvenience.