private void OpenSourceFile_Click(object sender, RoutedEventArgs e) { Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog(); string xpsFilePath = String.Empty; // Set filter for file extension and default file extension dlg.DefaultExt = ".txt"; dlg.Filter = "Office Files(*.docx;*.doc;*.xlsx;*.xls;*.pptx;*.ppt)|*.docx;*.doc;*.xlsx;*.xls;*.pptx;*.ppt"; // Display OpenFileDialog by calling ShowDialog method Nullable <bool> result = dlg.ShowDialog(); // Get the selected file name and display in a TextBox if (result == true) { string filename = dlg.FileName; xpsFilePath = System.Environment.CurrentDirectory + "\\" + dlg.SafeFileName + ".xps"; SourceUrl.Text = filename; } var convertResults = OfficeToXps.ConvertToXps(SourceUrl.Text, ref xpsFilePath); switch (convertResults.Result) { case ConversionResult.OK: xpsDoc = new System.Windows.Xps.Packaging.XpsDocument(xpsFilePath, FileAccess.ReadWrite); documentViewer1.Document = xpsDoc.GetFixedDocumentSequence(); officeFileOpen_Status = true; break; case ConversionResult.InvalidFilePath: // Handle bad file path or file missing break; case ConversionResult.UnexpectedError: // This should only happen if the code is modified poorly break; case ConversionResult.ErrorUnableToInitializeOfficeApp: // Handle Office 2007 (Word | Excel | PowerPoint) not installed break; case ConversionResult.ErrorUnableToOpenOfficeFile: // Handle source file being locked or invalid permissions break; case ConversionResult.ErrorUnableToAccessOfficeInterop: // Handle Office 2007 (Word | Excel | PowerPoint) not installed break; case ConversionResult.ErrorUnableToExportToXps: // Handle Microsoft Save As PDF or XPS Add-In missing for 2007 break; } }
private void DestinationLoaction_Click(object sender, RoutedEventArgs e) { if (officeFileOpen_Status) { FolderBrowserDialog folderBrowserDialog1 = new FolderBrowserDialog(); DialogResult result = folderBrowserDialog1.ShowDialog(); string localpath = String.Empty; localpath = folderBrowserDialog1.SelectedPath + "\\OutputXpsDocument.xps"; var convertResults = OfficeToXps.ConvertToXps(SourceUrl.Text, ref localpath); switch (convertResults.Result) { case ConversionResult.OK: System.Windows.Xps.Packaging.XpsDocument xpsDoc = new System.Windows.Xps.Packaging.XpsDocument(localpath, FileAccess.ReadWrite); break; case ConversionResult.InvalidFilePath: System.Windows.MessageBox.Show("Files Missing in the mentioned Path", "InValid Path", MessageBoxButton.OK, MessageBoxImage.Information); break; case ConversionResult.UnexpectedError: // This should only happen if the code is modified poorly break; case ConversionResult.ErrorUnableToInitializeOfficeApp: System.Windows.MessageBox.Show("Microsoft Office not Installed in the Machine", "Software Incompatibility", MessageBoxButton.OK, MessageBoxImage.Information); break; case ConversionResult.ErrorUnableToOpenOfficeFile: // Handle source file being locked or invalid permissions break; case ConversionResult.ErrorUnableToAccessOfficeInterop: System.Windows.MessageBox.Show("Microsoft Office not Installed in the Machine", "Software Incompatibility", MessageBoxButton.OK, MessageBoxImage.Information); break; case ConversionResult.ErrorUnableToExportToXps: // Handle Microsoft Save As PDF or XPS Add-In missing for 2007 break; } System.Windows.MessageBox.Show("File Saved Successfully", "Status", MessageBoxButton.OK, MessageBoxImage.Information); } else { System.Windows.MessageBox.Show("Please select a office File (*.docx,*.doc,*.els,*.elsx,*.ppt,*.pptx)", "Select a MS Office File", MessageBoxButton.OK, MessageBoxImage.Information); } }