// </SampleCode1> // <SampleCode2> /// <summary> /// Print a specific range of pages within an XPS document. /// </summary> /// <param name="xpsFilePath">Path to source XPS file</param> /// <returns>Whether the document printed</returns> public static bool PrintDocumentPageRange(string xpsFilePath) { // Create the print dialog object and set options. PrintDialog printDialog = new() { UserPageRangeEnabled = true }; // Display the dialog. This returns true if the user presses the Print button. bool?isPrinted = printDialog.ShowDialog(); if (isPrinted != true) { return(false); } // Print a specific page range within the document. try { // Open the selected document. XpsDocument xpsDocument = new(xpsFilePath, FileAccess.Read); // Get a fixed document sequence for the selected document. FixedDocumentSequence fixedDocSeq = xpsDocument.GetFixedDocumentSequence(); // Create a paginator for all pages in the selected document. DocumentPaginator docPaginator = fixedDocSeq.DocumentPaginator; // Check whether a page range was specified in the print dialog. if (printDialog.PageRangeSelection == PageRangeSelection.UserPages) { // Create a document paginator for the specified range of pages. docPaginator = new DocPaginator(fixedDocSeq.DocumentPaginator, printDialog.PageRange); } // Print to a new file. printDialog.PrintDocument(docPaginator, $"Printing {Path.GetFileName(xpsFilePath)}"); return(true); } catch (Exception e) { MessageBox.Show(e.Message); return(false); } }
private void btnPrint_Click(object sender, RoutedEventArgs e) { PrintDialog dlg = new PrintDialog(); if (dlg.ShowDialog() == true) { List<string> columns = new List<string>(); foreach (DataGridColumn col in dgPassenger.Columns) { columns.Add(col.Header.ToString()); } //dlg.PrintVisual(dgPassenger, "Passenger"); DocPaginator doc = new DocPaginator(dgPassenger, new Size(dlg.PrintableAreaWidth, dlg.PrintableAreaHeight), columns, false, null, null); dlg.PrintDocument(doc, "Passenger Analysis Unit"); } }