Thursday, 13 March 2014

Function to determine if a pdf document is password protected using aspose.pdf, C#, DotNet


Function to detect encrypted Pdf document using C#, DotNet, Aspose.pdf

#region "Description"
/*       
 Method given in blog can be used to determine if a pdf document contains or not

 1) Add reference to Aspose.Pdf dll in your project

 2) Add reference to Aspose.Pdf namespace in your class
 e.g. (using Aspose.Pdf;)

 3) Add method isJavascriptExistInPdf() in your class

 
*/      
#endregion


#region "Method"

public bool isJavascriptExistInPdf(Stream streamPdfDoc)
{
    bool bIsJavascriptExistInPDF = false;
    try
    {
        streamPdfDoc.Position = 0;
        //function to check existence of JavaScript present in pdf document using Aspose.pdf , C# , DotNet
        //First add reference to Aspose.PDF in your project
        //using Aspose.Pdf;

        //Create memory stream to hold pdf stream without JavaScript
        MemoryStream pdfStreamWithoutJanascript = new MemoryStream();

        //Create object of PdfJavaScriptStripper class using Aspose.pdf
        Aspose.Pdf.Facades.PdfJavaScriptStripper objTest = new Aspose.Pdf.Facades.PdfJavaScriptStripper();

        //Use Strip() method of PdfJavaScriptStripper class to strip JavaScript present in pdf stream
        //it returns true if JavaScript is present in PDF document
        bIsJavascriptExistInPDF = objTest.Strip(streamPdfDoc, pdfStreamWithoutJanascript);
        return bIsJavascriptExistInPDF;
    }
    catch
    {
    }
    return bIsJavascriptExistInPDF;
}

#endregion


No comments:

Post a Comment