0

In my program I write away PDFs with a password I specify. This is important as I know the password to the PDFs I'm trying to open (not hacking it or anything). I password protect the PDF like so;

Application.Current.Dispatcher.InvokeAsync(new Action(() =>
{
    string sourcePath = sourceFilePath;
    string targetPath = @"C:\ExamplePath";

    useReturnedOHPath = true;

    string sourceFile = Path.Combine(sourcePath, fileName);

    var document = PdfReader.Open(sourceFile);
    var securitySettings = document.SecuritySettings;
    securitySettings.UserPassword = "ExamplePass";

    securitySettings.PermitAccessibilityExtractContent = false;
    securitySettings.PermitAnnotations = false;
    securitySettings.PermitAssembleDocument = false;
    securitySettings.PermitExtractContent = false;
    securitySettings.PermitFormsFill = true;
    securitySettings.PermitFullQualityPrint = false;
    securitySettings.PermitModifyDocument = true;
    securitySettings.PermitPrint = false;

    document.Save(sourceFile);
    MessageBox.Show(sourceFile);

    string cleanPath = CleanFileName(selectedPerson.ID + " " + DateTime.Now.ToString("dd-MM-yyyy hh-mm-ss") + ".pdf");
    ohDestFile = Path.Combine(targetPath, cleanPath);

    File.Copy(sourceFile, ohDestFile, true);
}), DispatcherPriority.ContextIdle);

Because of this I know that the password to the PDF is ExamplePass. Now when I come to open the PDF from within my program I have tried a few methods, simply;

if (selectedOHRecord.Path != string.Empty)
{
     Process.Start(selectedOHRecord.Path);
}

However understandably this just opens Acrobat and asks for a password. I've also tried adding in:

PdfDocument document = PdfReader.Open(selectedOHRecord.Path, "ExamplePass");

Which is taken from the PDFsharp website itself, however when I call this nothing happens at all. Is there a way I can open a PDF and input the password for the user so they don't have to type it?

CBreeze
  • 2,925
  • 4
  • 38
  • 93
  • What's the purpose of your password? – I liked the old Stack Overflow May 03 '16 at 10:11
  • @PDFsharpTeam We'd like to prevent the PDF file from being opened outside of the program it is being opened in, i.e. prevent the user from opening the file in Windows Explorer – CBreeze May 03 '16 at 10:16
  • The PDFsharp API is completely independent from Adobe. There is no way to pass a password from PDFsharp to Adobe Reader. And AFAIK there is no way to pass a password to Adobe Reader using the command line. – I liked the old Stack Overflow May 03 '16 at 12:13
  • @CBreeze FYI: >> When I call `PdfDocument document = PdfReader.Open(selectedOHRecord.Path, "ExamplePass");` nothing happens at all. << This is a misunderstanding. The PDFsharp library is not a PDF viewer! Thus it does not opens the PDF in a viewable form, instead it loads the PDF into memory (so you can read/edit the content only via code). – Alb Oct 11 '20 at 12:55

1 Answers1

0

If the PDF file has a user password then Adobe Reader will prompt for a password.

Your call to PdfReader.Open does not pass the password to Adobe Reader, so it prompts again.

You can set an owner password instead of a user password. There will be no prompt when Adobe Reader opens the file, but editing will be prevented.

Or remove the password as shown in the Unprotect sample, save the PDF file without password to a temporary file and open that with Adobe Reader. Note that the user can save the file from Adobe Reader to a folder of their choice to keep an unprotected copy.
http://www.pdfsharp.net/wiki/UnprotectDocument-sample.ashx