We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

File Format PDF - Error when running a .netcore console appliaction

Trying to create a .NetCore console application that will take an existing pdf file and add encryption with a security password. 
1. I could not add the 
Xamarin, UWP, .NET Core and .NET Platforms
Syncfusion.Compression.Portable.dll
Syncfusion.Pdf.Portable.dll 

I added Syncfusion.Compression.Base46 (v 15.4.0.17) & Syncfusion.Pdf.Base46 (v. 15.4.0.17) instead.
Am I adding the incorrect libraries? Is there a way to write a console application to add password to a directory of pdf files?

When I tried to run, I fail at security.UserPassword = "fakepassword";

The exception I get:
  • Exception thrown: 'System.TypeInitializationException' in Syncfusion.Pdf.Base.dll
  • Failed to load pdf The type initializer for 'Syncfusion.Pdf.PdfDocument' threw an exception. : System.TypeLoadException: Could not load type 'System.Drawing.Text.PrivateFontCollection' from assembly 'System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
       at Syncfusion.Pdf.PdfDocument..cctor()


This is the program:

class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            var currentPath = String.Format($"{Directory.GetCurrentDirectory()}\\FilesToEncrypt\\");
            var encryptedPath = String.Format($"{Directory.GetCurrentDirectory()}\\EncryptedFiles\\");
            if (!Directory.Exists(currentPath) && !Directory.Exists(encryptedPath))
            {
                Console.WriteLine($"Directory does not exist {currentPath} or {encryptedPath}");
                return;
            }
            string[] files = Directory.GetFiles(currentPath, "*.pdf", SearchOption.TopDirectoryOnly);
            if(files.Length == 0)
            {
                Console.WriteLine($"Directory does not contain pdf files.");
                return;
            }
            Console.WriteLine($"file count {files.Length}");
            try
            {
                foreach (var file in files)
                {
                    var fileName = Path.GetFileName(file);
                    Console.WriteLine($"File being encrytped {fileName}");
                    PdfLoadedDocument document = new PdfLoadedDocument(file);
                    PdfSecurity security = document.Security;
                    security.KeySize = PdfEncryptionKeySize.Key256Bit;
                    security.Algorithm = PdfEncryptionAlgorithm.AES;
                    security.UserPassword = "fakepassword";
                    document.Save($"{encryptedPath}\\{fileName}");
                    document.Close(true);
                }
            }
            catch (Exception exception)
            {
                Debug.WriteLine($"Failed to load pdf {exception.Message} : {exception.InnerException}");
            }

            Console.ReadLine();
        }
    }


3 Replies

SP Sathya Ponnusamy Syncfusion Team November 30, 2017 02:35 PM UTC

Hi Ericka, 

Thank you for contacting Syncfusion support. 

We are unable to reproduce the reported issue. We suspect that the issue raised due to improper assembly references. Please find the below steps to add online nuget package as reference to the Asp.Net Core project:  

1.              Right-click the solution and select Manage NuGet Packages.  as shown in screenshot below:  
 
 
 
 
 
2.              Select the Settings icon in the Package Manager UI outlined below or use the Tools > Options command and scroll to NuGet Package Manager
 
 


3.              In the package source window click the “+” icon to add a new online nuget source and in “Source” text box provide the online nuget package link (https://nuget.syncfusion.com/nuget_aspnetcore/nuget/getsyncfusionpackages/aspnetcore ) and add any “Name” which you need and click Update and then click OK.  
 
 
 
4.              Now in the browse window you will find all the nuget packages and you can select a nuget package and install from them.  
 
 
 
After installing nuget packages run the sample with same code snippet which you shared with us. 

Please let us know if you need any assistance in this. 
 
Regards, 
Sathya P 



EO Ericka Opp November 30, 2017 03:56 PM UTC

Thank you for the suggestions. You were right I was pointing at the URL for UWP library vs ASPNetCore. But Now I am using the url http://nuget.syncfusion.com/nuget_aspnetcore/nuget/getsyncfusionpackages/aspnetcore
but I tried adding either the suggested libraries for 
Syncfusion.Compression.Portable.dll
Syncfusion.Pdf.Portable.dll 
or 

But now I get an error that Security is not found in the PDFLoadedDocument object. 
Also I am using :

using Syncfusion.Pdf.Parsing;
using Syncfusion.Pdf.Security;

But PDFSecurity is not found either.
Do you have any suggestions or should I just not use a console application??



SP Sathya Ponnusamy Syncfusion Team December 1, 2017 11:17 AM UTC

Hi Ericka, 

Thanks for your update. 

On further analyzed we have found that you are using Essential PDF version 14.4.0.20 in your application. But, we have provided security feature only from the version 15.4.0.17. Could you please try installing the latest nuget package and let us know if the required feature is available.  

Regards, 
Sathya 


Loader.
Live Chat Icon For mobile
Up arrow icon