Hi friends here i will show you complete code with example to show you word document in viewer in wpf application. Let's start to learn document viewer,
Main window
Main window
<Window x:Class="DocumentShow.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="70"></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Label Name="lable1" Margin="3,6,0,0" Content="Show Word File:" VerticalAlignment="Top" HorizontalAlignment="Left" />
<TextBox Name="txt_filename" VerticalAlignment="Top" HorizontalAlignment="Stretch" Margin="110,10,300,0" HorizontalContentAlignment="Left" />
<Button HorizontalAlignment="Right" VerticalAlignment="Top" Width="150" Content="Select File(word)" Name="wordbutton" Margin="0,10,130,0" Click="wordbutton_Click" />
<Button HorizontalAlignment="Left" Margin="407,10,0,0" VerticalAlignment="Top" Content="View Word File" Width="100" Name="viewdoc" Click="viewdoc_Click" />
<DocumentViewer Name="dc" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="0,37,0,0" Grid.RowSpan="2"/>
</Grid>
</Window>
MainWindow.cs
using System;
using System.IO;
using System.Windows;
using System.Windows.Xps.Packaging;
using Microsoft.Office.Interop.Word;
using Microsoft.Win32;
using Word = Microsoft.Office.Interop.Word;
namespace DocumentShow
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : System.Windows.Window
{
public MainWindow()
{
InitializeComponent();
}
private void wordbutton_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
// Set filter and RestoreDirectory
openFileDialog.RestoreDirectory = true;
openFileDialog.Filter = "Word documents(*.doc;*.docx)|*.doc;*.docx";
bool? result = openFileDialog.ShowDialog();
if (result == true)
{
if (openFileDialog.FileName.Length > 0)
{
txt_filename.Text = openFileDialog.FileName;
}
}
}
private XpsDocument ConvertWordToXps(string wordFilename, string xpsFilename)
{
// Create a WordApplication and host word document
Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
try
{
wordApp.Documents.Open(wordFilename);
// To Invisible the word document
wordApp.Application.Visible = false;
// Minimize the opened word document
wordApp.WindowState = WdWindowState.wdWindowStateMinimize;
Document doc = wordApp.ActiveDocument;
doc.SaveAs(xpsFilename, WdSaveFormat.wdFormatXPS);
XpsDocument xpsDocument = new XpsDocument(xpsFilename, FileAccess.Read);
return xpsDocument;
}
catch (Exception ex)
{
MessageBox.Show("Error occurs, The error message is " + ex.ToString());
return null;
}
finally
{
wordApp.Documents.Close();
((_Application)wordApp).Quit(WdSaveOptions.wdDoNotSaveChanges);
}
}
private void viewdoc_Click(object sender, RoutedEventArgs e)
{
string wordDocument = txt_filename.Text;
if (string.IsNullOrEmpty(wordDocument) || !File.Exists(wordDocument))
{
MessageBox.Show("The file is invalid. Please select an existing file again.");
}
else
{
string convertedXpsDoc = string.Concat(System.IO.Path.GetTempPath(), "\\", Guid.NewGuid().ToString(), ".xps");
XpsDocument xpsDocument = ConvertWordToXps(wordDocument, convertedXpsDoc);
if (xpsDocument == null)
{
return;
}
dc.Document = xpsDocument.GetFixedDocumentSequence();
}
}
}
}
Let's see the output snap of it,
0 comments:
Post a Comment