Search code examples
c#.netazurenuget-packagecloud-storage

Determining if a PDF File with Cloud File path is Password protected or Not (path starting with https:\\)


I am Using DotNetZip (from NuGet Package). This will normally work for a File path having ("@C:) but once if use the Cloud File Path like (https:) it is giving me IBM437 error. I can use this package but need to introduce some encoding which is the other problem.

I tried using SharpZipLib (another NuGet Package) but couldn’t find any information on internet related to it for a Cloud File Path. (http:).

I also tried Spire PDF but it throws File doesn't exist (Parameter 'fileName') error for password protected PDF.

URL’s for NuGet Packages - DotNetZip - https://www.nuget.org/packages/DotNetZip/ SharpZipLib- https://www.nuget.org/packages/SharpZipLib.NETStandard/

Please let me know if any more information is required.


Solution

  • You can try to download the .pdf file as a local temp file and then check could open it without a password. Just try the C# console app below:

    using Spire.Pdf;
    using System;
    using System.IO;
    using System.Net;
    
    namespace PDFtest
    {
        class Program
        {
            static void Main(string[] args)
            {
                var passwordPDFURL = "https://stantest1016.blob.core.windows.net/static/testPass.pdf";
                var noPassWordPDFURL = "https://stantest1016.blob.core.windows.net/static/testNoPass.pdf";
    
                var tempFilePath = "d:/temp/temp.pdf";
    
                //download file as a temp pdf file
                WebClient webClient = new WebClient();
                webClient.DownloadFile(passwordPDFURL, tempFilePath);
                try
                {
                    PdfDocument pdf = new PdfDocument(tempFilePath);
                    Console.WriteLine("PDF has been opened successfully with no password");
                }
                catch (Exception e) {
    
                    Console.WriteLine(e.Message);
                }
                //remove temp file
                File.Delete(tempFilePath);
                
            }
        }
    }
    

    Test password protected .pdf Result:

    enter image description here

    Test no password protected .pdf Result: enter image description here