System.InvalidOperationException 'DialogResult can only be set after Window has been created and displayed as a dialog box.'

Hi:

When I open a password-protected pdf file by pressing a button. If the PdfViewer control is created in XAML, opening a protected file is successful through the standard dialog box. However, if the control is created in the c# part, clicking on the "Open" button results in an exception after entering the password. The two projects and the image with the exception are attached.

Pdf Password: P123456


Best Regards,


Jesús


Attachment: ReadPdfProtected_ca0e2d42.rar

8 Replies

YM Yathavakrishnan Mohan Syncfusion Team December 11, 2024 01:24 PM UTC

 

Hi Jesús,

 

We have successfully reproduced the reported issue based on the details you provided, and we are currently  analyzing it. We will share additional information on December 13th, 2024



Regards,
Yathavakrishnan M



YM Yathavakrishnan Mohan Syncfusion Team December 13, 2024 02:06 PM UTC

Currently we are checking on this issue with high priority and will update further details on or before December 17,2024.



YM Yathavakrishnan Mohan Syncfusion Team December 17, 2024 12:43 PM UTC

After examining the given sample, the error was caused by loading the PDF document before the PdfViewer control was rendered or loaded in the window. The PdfViewer is only rendered once it is added to the view or window. Therefore, it is advisable to load the PDF document only after the control has been rendered or loaded. To determine if the control is rendered or loaded, the `PdfViewer.Loaded` event can be used. Please see the revised code snippet below.

 private void btnOpen_Click(object sender, RoutedEventArgs e)

 {

    // Add the PDFViewer to View using button click

     MainGrid.Children.Add(pdfviewer);

 }

 

pdfviewer.Loaded += Pdfviewer_Loaded;

//The event is triggered after the PDFViewer is loaded

private void Pdfviewer_Loaded(object sender, RoutedEventArgs e)

{

   // Load the document to PDFViewer using loaded event

    pdfviewer.Load(filePath);

    CheckPdfProtection1(filePath);

}

 

Additionally, We have modified the sample that you have provided and attached below for your reference.


Attachment: PDFViewer_Sample_8d78b232.zip


JE Jesús December 17, 2024 03:42 PM UTC

Hi  Yathavakrishnan,

Perfect, working without problems

Thank you


Regards,


Jesús



JE Jesús replied to Jesús December 17, 2024 10:18 PM UTC

Hi

Is there a way to get the result of the dialog box that requests the password? The objective is to determine if you have clicked accept, cancel or close the dialog window



Regards;



YM Yathavakrishnan Mohan Syncfusion Team December 18, 2024 11:22 AM UTC

Hi Jesús,


In the WPF PDFViewer control, we cannot access the password dialog box. However, only if the password is proper, will the document get loaded. You can track whether the document has been loaded or not using the "PDFViewer.DocumentLoaded" event. Please refer to the code snippet below
 

    PDFViewer.DocumentLoaded += PDFViewer_DocumentLoaded;

 

//The event is triggered after the document is loaded in the PDFViewer

private void PDFViewer_DocumentLoaded(object sender, EventArgs args)

{

   

}


 

Regards,

Yathavakrishnan M
 



JE Jesús December 18, 2024 12:38 PM UTC

Hi  Yathavakrishnan,

It is what I had programmed, ask the question in case there was a more precise alternative. For my part there are no more questions

Thank you


Regards,


Jesús




KG Krithika Ganesan Syncfusion Team December 19, 2024 11:35 AM UTC

Thank you for your understanding. This is only way to achieve your requirement. Please get back to us if you need any further assistance.


Loader.
Up arrow icon