1 Answer, 1 is accepted
Hello Zygmunt,
The import settings of the PdfFormatProvider class (used to import PDF documents in PdfProcessing) expose an event that is fired when the document is password-protected - UserPasswordNeeded. You can use that event to determine whether a password is needed for opening the document. For more details and examples on the matter, please check the Settings help topic for PdfFormatProvider.
I hope this information is helpful.
Regards,
Tanya
Progress Telerik
Тhe web is about to get a bit better!
The Progress Hack-For-Good Challenge has started. Learn how to enter and make the web a worthier place: https://progress-worthyweb.devpost.com.
In my case, I'm merging PDF documents. When one needs a password to allow editing, I get an exception when I try to merge it. I'd like to detect the password requirement prior to trying to merge it.
I tried using the UserPasswordNeeded and OwnerPasswordNeeded events to set a boolean when a password is needed, but I think I've got something wrong. Can you show an example of setting a boolean if either a User or Owner password is needed?
Thanks!
Hi, Jon,
The UserPasswordNeeded event is fired when a user password is needed to open the document. The password can be specified in the PasswordNeededEventArgs.Password property. If the document is encrypted it will require the OwnerPassword.
I have prepared a sample project for your reference that merges two PDF documents into one. Doc1.pdf is password protected with password="telerik". Here is my code snippet for reference:
using Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.Import;
using Telerik.Windows.Documents.Fixed.FormatProviders.Pdf;
using Telerik.Windows.Documents.Fixed.Model;
using System.Diagnostics;
PdfFormatProvider provider = new PdfFormatProvider();
PdfImportSettings settings = new PdfImportSettings();
settings.UserPasswordNeeded += (s, a) =>
{
a.Password = "telerik";
};
provider.ImportSettings = settings;
RadFixedDocument fixedDocument1;
using (Stream stream = File.OpenRead("Doc1.pdf"))
{
fixedDocument1 = provider.Import(stream);
}
RadFixedDocument fixedDocument2;
using (Stream stream = File.OpenRead("Doc2.pdf"))
{
fixedDocument2 = provider.Import(stream);
}
fixedDocument1.Merge(fixedDocument2);
string outputFilePath = "output.pdf";
File.Delete(outputFilePath);
using (Stream output = File.OpenWrite(outputFilePath))
{
provider.Export(fixedDocument1, output);
}
Process.Start(new ProcessStartInfo() { FileName = outputFilePath, UseShellExecute = true });
I hope you find this information helpful. Please, let me know if there is anything else I can assist you with.
Thanks for your help on this.
In my case, my code won't know the password. It will just be a case where a user uploaded a password protected document and later, the code tried to merge it, but couldn't.
When a user tries to merge a password protected document, I'd like to tell them it can't be done because it is password protected. Or when a user uploads a password protected document, I'd like to tell them it can't be merged. So I just want to check if the PDF is password protected.
Thanks!
Hi, Jon,
If you need to skip merging the documents with password protection, a possible approach to identify if such a password is required for the document, you can handle the PdfImportSettings.DocumentUnhandledException. The event is expected to be fired when a password is not provided. Hence, you can skip the document with the further merging operation:
PdfFormatProvider provider = new PdfFormatProvider();
PdfImportSettings settings = new PdfImportSettings();
settings.DocumentUnhandledException += Settings_DocumentUnhandledException;
void Settings_DocumentUnhandledException(object? sender, DocumentUnhandledExceptionEventArgs e)
{
if (e.Exception.Message.Contains("Password is not correct."))
{
//you can skip this PDF document from merging
}
}
provider.ImportSettings = settings;
I hope this will fit the requirements you need to cover.