Thursday, October 29, 2009

Simple Print Window in WPF (PDF and DOC)

I struggled with this for a few hours because the window would not close when the application closed using a WindowsFormHost and the solution was tons simpilar than I thought to begin with.

You make a window:

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
WindowState="Maximized"
Icon="/MyProj;component/Images/favicon.ico"
Title="MyProj - Print">





Then the code behind is simple:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace MyProj
{
///
/// Interaction logic for PrintPDFWindow.xaml
///

public partial class PrintPDFWindow : Window
{
private System.Windows.Forms.WebBrowser webbrowserOne;
public PrintPDFWindow()
{
InitializeComponent();
}
public PrintPDFWindow(string filePath)
{
InitializeComponent();
Uri uri = new Uri(filePath, UriKind.RelativeOrAbsolute);
this.webBrowser.Source = uri;
}
}
}


You call it like this:

PrintPDFWindow ppw = new PrintPDFWindow(currentMarketingPath + currentMarketingMaterial.MarketingMaterialURL);
ppw.Title = "MyProj - " + currentMarketingMaterial.MarketingMaterialName;
ppw.Show();


That is it!

No comments: