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();
}
}