1

I am trying to create dinamically a WPF WebBrowser, load a PDF from a file in c:\temp\MyPdf.pdf and finally to print it in the default printer.

I have used AcroPDFLib as below code shows:

System.Windows.Controls.WebBrowser wbPdfWebViewer = new System.Windows.Controls.WebBrowser();                
wbPdfWebViewer.Navigate(new System.Uri("c:\\temp\\MyPdf.pdf"));

// Sleep some time until pdf is loaded in webbrowser
Thread.Sleep(2000);

var pdfdoc = wbPdfWebViewer.Document as AcroPDFLib.AcroPDF;
if (pdfdoc != null)
{
    pdfdoc.Print();
}

but it does not work. PDF is not being printed (Simply nothing happens). I have ensured that pdfdoc is not null.

Note: Above code is called from a STA Thread.

ATTEMPT #2: I also have tried to create a WPF Window with a Webbrowser control:

<Window x:Class="My.Apps.WPF.PdfWebViewerContainer"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="wPdfWebViewer" Height="300" Width="300"
    Visibility="Hidden">

    <Grid>
        <WebBrowser x:Name="wbPdfWebViewer" />
    </Grid>
</Window>

As I do not want WPF window to be visible I set this to hidden.

Then:

wPdfWebViewer _pdfWebViewer;

private void PrintPdf(string file)
{
   _pdfWebViewer = new wPdfWebViewer();
   _pdfWebViewer.wbPdfWebViewer.LoadCompleted += wbPdfWebViewer_LoadCompleted;
   _pdfWebViewer.wbPdfWebViewer.Navigate(new System.Uri(file));
}

private void wbPdfWebViewer_LoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e)
{        
    var pdfdoc = _pdfWebViewer.wbPdfWebViewer.Document as AcroPDFLib.AcroPDF;
    if (pdfdoc != null)
    {
        pdfdoc.Print();
    }
}

Problem here is that LoadCompleted will only be fired if WPF Window is visible. It does not fire if it is hidden. Anyway, pdf is not being printed.

Willy
  • 9,848
  • 22
  • 141
  • 284

0 Answers0