This is a migrated thread and some comments may be shown as answers.

Checking if pdf file is password protected

1 Answer 1375 Views
PdfProcessing
This is a migrated thread and some comments may be shown as answers.
Zygmunt
Top achievements
Rank 1
Iron
Iron
Iron
Zygmunt asked on 20 Apr 2021, 10:37 AM

Hello

Is it posible to check if pdf file is password protected using Telerik Tools?

 

All the best

1 Answer, 1 is accepted

Sort by
1
Tanya
Telerik team
answered on 22 Apr 2021, 11:54 AM

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.

Jon
Top achievements
Rank 1
Iron
commented on 22 Oct 2024, 09:15 PM

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!

Dess | Tech Support Engineer, Principal
Telerik team
commented on 25 Oct 2024, 09:55 AM

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.

Jon
Top achievements
Rank 1
Iron
commented on 25 Oct 2024, 02:40 PM

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!

Dess | Tech Support Engineer, Principal
Telerik team
commented on 29 Oct 2024, 09:43 AM

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.

Tags
PdfProcessing
Asked by
Zygmunt
Top achievements
Rank 1
Iron
Iron
Iron
Answers by
Tanya
Telerik team
Share this question
or