0

I am using PdfPrintingNet library to load pdf documents in my application. The end-goal is for the user to open as many pdf documents as he/she wants and my app will keep them all in a TabControl. But when I try to open any pdf document I get an error: NullRefernceException when I try to create the view that implements PDFViewer.

I also made sure that my app is referencing PdfPrintingNet. I am at a loss here.

UPDATE: SEE UPDATED POST BELOW

So today I managed to make some tweaks to my code. First off changed PDFDisplayView.xaml

from

<Grid>
        <!-- winforms host with embedded PDFPrint.net viewer -->
        <WindowsFormsHost x:Name="_pdfViewerHost" Background="{DynamicResource {x:Static SystemColors.ControlBrushKey}}">
            <pdfview:PdfViewer IsCalledFromWPF="True" />
        </WindowsFormsHost>
    </Grid> 

to

<Grid>
        <!-- winforms host with embedded PDFPrint.net viewer -->
        <WindowsFormsHost x:Name="_pdfViewerHost" Background="{DynamicResource {x:Static SystemColors.ControlBrushKey}}">

        </WindowsFormsHost>
    </Grid>

I removed the pdfView from it because I think the issue is that its being called before being initialized.

What Im doing now is that in my OpenPdfDocument method I am creating an instance of PdfViewer and then making it a child of _pdfViewerHost.

    /// <summary>
            /// Open PDF document.
            /// </summary>
            void OpenPdfDocument()
            {
                //var pdfViewer = _pdfViewerHost.Child as PdfViewer;
                PdfViewer pdfViewer = new PdfViewer();
                if (pdfViewer != null)
                {
                    try
                    {
                        _pdfViewerHost.Child = pdfViewer;
  /**********************Breaks Here*******************************************/
                        PdfOpenFileStatus status = pdfViewer.OpenDocument(_fileNameAndPath); 
 /****************************************************************************/

                        if (status.Result != PdfOpenFileStatus.PdfOpenFileResult.OK)
                        {
                            MessageWindow.Show("There was a problem opening the PDF document." + Environment.NewLine +
                                            "Status: " + status.Status.ToString(), "Error", "OK", string.Empty);
                        }
                        else
                        {
                            // set pdf viewer license
                            pdfViewer.SetLicenseInfo("MyCompanyLicense", "MyLicenseKey");

                            // set up viewer
                            pdfViewer.ShowBookmarks = true;
                            pdfViewer.ToolbarOpenVisible = false;
                            pdfViewer.ToolbarEmailVisible = false;
                            pdfViewer.ToolbarPrintVisible = false;
                            pdfViewer.ToolbarSaveVisible = false;
                            pdfViewer.ToolbarDocumentInfoVisible = false;
                            pdfViewer.BackColor = System.Drawing.SystemColors.Control;
                            pdfViewer.BorderColor = System.Drawing.Color.FromArgb(203, 203, 203);

                            // link clicked and text not found callbacks
                            pdfViewer.LinkClicked += PdfViewer_LinkClicked;
                            pdfViewer.NotFoundText += PdfViewer_NotFoundText;

                            // set up view screen parameters
                            UpdatePdfViewerLocationAndSize();
                        }
                    }
                    catch (Exception ex)
                    {

                    }

                }
            }

The funny thing is that after I click CONTINUE in Visual Studio my application runs and I am able to see the PDFViewer application with my selected pdf document loaded. And I have full use of the PDFViewer controls.

Can someone help me understand why I am getting this error? Is there some extra step?

user2529011
  • 705
  • 3
  • 11
  • 21

0 Answers0