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
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
Currently we are checking on this issue with high priority and will update further details on or before December 17,2024.
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
Hi Yathavakrishnan,
Perfect, working without problems
Thank you
Regards,
Jesús
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;
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
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
Thank you for your understanding. This is only way to achieve your requirement. Please get back to us if you need any further assistance.
- 8 Replies
- 3 Participants
-
JE Jesús
- Dec 10, 2024 12:41 PM UTC
- Dec 19, 2024 11:35 AM UTC