private async void btnPrint_Click(object sender, RoutedEventArgs e) { while (true) { try { _pdfDocSource.UseSystemRendering = cbxUseSystemRendering.IsChecked.Value; if (_pdfFile == null) { using (Stream stream = this.GetType().GetTypeInfo().Assembly.GetManifestResourceStream("PdfDocumentSourceSamples.Resources.DefaultDocument.pdf")) await _pdfDocSource.LoadFromStreamAsync(stream.AsRandomAccessStream()); } else { await _pdfDocSource.LoadFromFileAsync(_pdfFile); } break; } catch (PdfPasswordException) { fileNameBlock.Text = _pdfFile.Name; passwordBox.Password = string.Empty; if (await passwordDialog.ShowAsync() != ContentDialogResult.Primary) { return; } _pdfDocSource.Credential = new System.Net.NetworkCredential(null, passwordBox.Password); } catch (Exception ex) { MessageDialog md = new MessageDialog(string.Format(Strings.PdfErrorFormat, _pdfFile.Name, ex.Message), Strings.ErrorTitle); await md.ShowAsync(); return; } } btnPrint.IsEnabled = false; try { await _pdfDocSource.ShowPrintUIAsync(); } catch (Exception ex) { MessageDialog md = new MessageDialog(string.Format(Strings.FailedToPrintFmt, ex.Message), Strings.ErrorTitle); await md.ShowAsync(); } finally { btnPrint.IsEnabled = true; } }
async Task LoadPdf(StorageFile file) { _closeToolItem.IsEnabled = false; _useSystemRenderingToolItem.IsEnabled = false; try { while (true) { try { await _pdfDocSource.LoadFromFileAsync(file); break; } catch (PdfPasswordException) { fileNameBlock.Text = file.Name; passwordBox.Password = string.Empty; if (await passwordDialog.ShowAsync() != ContentDialogResult.Primary) { break; } _pdfDocSource.Credential = new System.Net.NetworkCredential(null, passwordBox.Password); } catch (Exception ex) { MessageDialog md = new MessageDialog(string.Format(Strings.PdfErrorFormat, file.Name, ex.Message), Strings.ErrorTitle); await md.ShowAsync(); break; } } } finally { _closeToolItem.IsEnabled = true; _useSystemRenderingToolItem.IsEnabled = true; } }
private async void btnExport_Click(object sender, RoutedEventArgs e) { string fileName = null; while (true) { try { _pdfDocSource.UseSystemRendering = cbxUseSystemRendering.IsChecked.Value; if (_pdfFile == null) { using (Stream stream = this.GetType().GetTypeInfo().Assembly.GetManifestResourceStream("PdfDocumentSourceSamples.Resources.DefaultDocument.pdf")) await _pdfDocSource.LoadFromStreamAsync(stream.AsRandomAccessStream()); fileName = "DefaultDocument.pdf"; } else { await _pdfDocSource.LoadFromFileAsync(_pdfFile); fileName = Path.GetFileName(_pdfFile.Name); } break; } catch (PdfPasswordException) { fileNameBlock.Text = fileName; passwordBox.Password = string.Empty; if (await passwordDialog.ShowAsync() != ContentDialogResult.Primary) { return; } _pdfDocSource.Credential = new System.Net.NetworkCredential(null, passwordBox.Password); } catch (Exception ex) { MessageDialog md = new MessageDialog(string.Format(Strings.PdfErrorFormat, fileName, ex.Message), Strings.ErrorTitle); await md.ShowAsync(); return; } } // prepare ExportFilter object ExportProvider ep = _pdfDocSource.SupportedExportProviders[cbExportProvider.SelectedIndex]; ExportFilter ef = ep.NewExporter() as ExportFilter; ef.UseZipForMultipleFiles = cbxUseSystemRendering.IsChecked.Value; // if ((ef is BmpFilter || ef is JpegFilter || ef is PngFilter || ef is GifFilter)) { // these export filters produce more than one file during export // ask for directory in this case if (cbxUseZipForMultipleFiles.IsChecked == true) { // ask for zip file FileSavePicker fsp = new FileSavePicker(); fsp.DefaultFileExtension = ".zip"; fsp.FileTypeChoices.Add(Strings.ZipFiles, new string[] { ".zip" }); fsp.SuggestedFileName = Path.GetFileNameWithoutExtension(fileName) + ".zip"; ef.StorageFile = await fsp.PickSaveFileAsync(); if (ef.StorageFile == null) { return; } } else { FolderPicker fp = new FolderPicker(); fp.FileTypeFilter.Add("." + ep.DefaultExtension); fp.FileTypeFilter.Add(".zip"); ef.StorageFolder = await fp.PickSingleFolderAsync(); if (ef.StorageFolder == null) { // user cancels an export return; } } } else { // ask for file FileSavePicker fsp = new FileSavePicker(); fsp.DefaultFileExtension = "." + ep.DefaultExtension; fsp.FileTypeChoices.Add(ep.FormatName + " (." + ep.DefaultExtension + ")", new string[] { "." + ep.DefaultExtension }); fsp.FileTypeChoices.Add(Strings.ZipFiles, new string[] { ".zip" }); fsp.SuggestedFileName = Path.GetFileNameWithoutExtension(fileName) + "." + ep.DefaultExtension; ef.StorageFile = await fsp.PickSaveFileAsync(); if (ef.StorageFile == null) { return; } } btnExport.IsEnabled = false; try { await _pdfDocSource.ExportAsync(ef); await ShowPreview(ef); } catch (Exception ex) { MessageDialog md = new MessageDialog(string.Format(Strings.FailedToExportFmt, ex.Message), Strings.ErrorTitle); await md.ShowAsync(); } finally { btnExport.IsEnabled = true; } }