private void OnCreateXPS(object sender, RoutedEventArgs e) { var c = new GregorianCalendar(); int weekNumber = c.GetWeekOfYear(menus[0].Day, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday); string fileName = String.Format("menuplan{0}", weekNumber); var dlg = new SaveFileDialog { FileName = fileName, DefaultExt = "xps", Filter = "XPS Documents|*.xps|All Files|*.*", AddExtension = true }; if (dlg.ShowDialog() == true) { var doc = new XpsDocument(dlg.FileName, FileAccess.Write, CompressionOption.Fast); XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(doc); writer.WritingCompleted += delegate { doc.Close(); }; writer.WriteAsync(fixedDocument); //writer.Write(fixedDocument); //doc.Close(); } }
/// <summary> /// Prints this instance. /// </summary> public bool Print() { try { PrintDialog printDlg = new System.Windows.Controls.PrintDialog(); bool? results = printDlg.ShowDialog(); if (results == null || results == false) { return(false); } //get selected printer capabilities System.Printing.PrintCapabilities capabilities = printDlg.PrintQueue.GetPrintCapabilities(printDlg.PrintTicket); //get the size of the printer page System.Windows.Size printSize = new System.Windows.Size( capabilities.PageImageableArea.ExtentHeight, capabilities.PageImageableArea.ExtentWidth); // Build print view this.Height = printSize.Height; this.Width = printSize.Width; Measure(printSize); Arrange(new System.Windows.Rect(printSize)); XpsDocumentWriter xpsdw = PrintQueue.CreateXpsDocumentWriter(printDlg.PrintQueue); printDlg.PrintTicket.PageOrientation = PageOrientation.Landscape; xpsdw.WriteAsync(this); } catch (Exception ex) { MessageBox.Show(ex.GetBaseException().Message, "Printing Error", MessageBoxButton.OK, MessageBoxImage.Error); return(false); } return(true); }
public void Print_Document() { //----------------< Print_Document() >----------------------- //----< Get_Print_Dialog_and_Printer >---- PrintDialog printDialog = new PrintDialog(); printDialog.PrintQueue = LocalPrintServer.GetDefaultPrintQueue(); printDialog.PrintTicket = printDialog.PrintQueue.DefaultPrintTicket; //----</ Get_Print_Dialog_and_Printer >---- //*set the default page orientation based on the desired output. printDialog.PrintTicket.PageOrientation = PageOrientation.Landscape; printDialog.PrintTicket.PageScalingFactor = 90; printDialog.PrintTicket.PageMediaSize = new PageMediaSize(PageMediaSizeName.ISOA4); //printDialog.PrintableAreaHeight ; //*get //printDialog.PrintableAreaWidth; //get //printDialog.PrintDocument. printDialog.PrintTicket.PageBorderless = PageBorderless.None; if (printDialog.ShowDialog() == true) { //----< print >---- // set the print ticket for the document sequence and write it to the printer. _document.PrintTicket = printDialog.PrintTicket; //-< send_document_to_printer >- XpsDocumentWriter writer = PrintQueue.CreateXpsDocumentWriter(printDialog.PrintQueue); writer.WriteAsync(_document, printDialog.PrintTicket); //-</ send_document_to_printer >- //----</ print >---- } //----------------</ Print_Document() >----------------------- }
/* Send it */ private void PrintPages(XpsDocumentWriter xpsdw, FixedDocumentSequence fixdoc) { m_docWriter = xpsdw; xpsdw.WritingCompleted += new WritingCompletedEventHandler(AsyncCompleted); xpsdw.WritingProgressChanged += new WritingProgressChangedEventHandler(AsyncProgress); xpsdw.WriteAsync(fixdoc); }
WriteAsyncAsTask( this XpsDocumentWriter @this, FixedDocument document, CancellationToken cancellationToken = default(CancellationToken) ) { var tcs = WriteAsyncAsTaskCore(@this, cancellationToken); @this.WriteAsync(document); return(tcs.Task); }
/// <summary> /// Print the document. /// </summary> private void PrintDoc() { PrintTicket printTicket = _printDlg.PrintTicket; FlowDocument flowDocForPrinting = GetFlowDocumentForPrinting(printTicket); // In case the user has changed some settings (e.g., orientation or page size) // by going to the printer preferences dialog from the print dialog itself, // set the printer queue's User ticket to the one attached to the print dialog. _printDlg.PrintQueue.UserPrintTicket = printTicket; // Print the FlowDocument _printDlg.PrintQueue.CurrentJobSettings.Description = (String.IsNullOrEmpty(_documentName) ? Guid.NewGuid().ToString() : _documentName); XpsDocumentWriter docWriter = PrintQueue.CreateXpsDocumentWriter(_printDlg.PrintQueue); // Use our IDocumentPaginator implementation so we can insert headers and footers, // if present. // PrintableAreaHeight and Width are passed to the paginator to establish the // true printable area for the document. HeaderFooterDocumentPaginator paginator = new HeaderFooterDocumentPaginator( ((IDocumentPaginatorSource)flowDocForPrinting).DocumentPaginator, null, null, _printDlg.PrintableAreaHeight, _printDlg.PrintableAreaWidth); if (_asyncPrintFlag == false) { try { docWriter.Write(paginator, printTicket); } catch (PrintingCanceledException) { } } else { // Changes for Async printing start here: Application.Current.MainWindow.Opacity = 0.7; PrintProgressWindow dlg = new PrintProgressWindow(docWriter); dlg.PageNumber = 0; docWriter.WritingProgressChanged += new WritingProgressChangedEventHandler(dlg.OnWritingProgressChanged); docWriter.WritingCompleted += new WritingCompletedEventHandler(dlg.OnWritingCompleted); docWriter.WriteAsync(paginator, printTicket); dlg.ShowDialog(); // Reset the flag here for next printing invocation. _asyncPrintFlag = false; } }
// </GetPrintTicket> // <PrintXpsDocument> /// <summary> /// Asynchronously, add the XPS document together with a print ticket to the print queue. /// </summary> /// <param name="xpsFilePath">Path to source XPS file.</param> /// <param name="printQueue">The print queue to print to.</param> /// <param name="printTicket">The print ticket for the selected print queue.</param> public static void PrintXpsDocumentAsync(string xpsFilePath, PrintQueue printQueue, PrintTicket printTicket) { // Create an XpsDocumentWriter object for the print queue. XpsDocumentWriter xpsDocumentWriter = PrintQueue.CreateXpsDocumentWriter(printQueue); // Open the selected document. XpsDocument xpsDocument = new(xpsFilePath, FileAccess.Read); // Get a fixed document sequence for the selected document. FixedDocumentSequence fixedDocSeq = xpsDocument.GetFixedDocumentSequence(); // Asynchronously, add the XPS document together with a print ticket to the print queue. xpsDocumentWriter.WriteAsync(fixedDocSeq, printTicket); }
private void PrintDocument() { PrintDialog printDialog = new PrintDialog(); printDialog.PrintQueue = LocalPrintServer.GetDefaultPrintQueue(); printDialog.PrintTicket = printDialog.PrintQueue.DefaultPrintTicket; printDialog.PrintTicket.PageOrientation = PageOrientation.Landscape; printDialog.PrintTicket.PageScalingFactor = 90; printDialog.PrintTicket.PageMediaSize = new PageMediaSize(PageMediaSizeName.ISOA4); printDialog.PrintTicket.PageBorderless = PageBorderless.None; if (printDialog.ShowDialog().GetValueOrDefault()) { document.PrintTicket = printDialog.PrintTicket; XpsDocumentWriter writer = PrintQueue.CreateXpsDocumentWriter(printDialog.PrintQueue); writer.WriteAsync(document, printDialog.PrintTicket); } }
public void PrintDocumentAsync(FixedDocument fixedDocument) { //Get a hold of a PrintQueue. PrintQueue printQueue = GetPrintQueue(); //Create a document writer to print to. xpsDocumentWriter = PrintQueue.CreateXpsDocumentWriter(printQueue); //We want to know when the printing progress has changed so //we can update the UI. xpsDocumentWriter.WritingProgressChanged += PrintAsync_WritingProgressChanged; //We also want to know when the print job has finished, allowing //us to check for any problems. xpsDocumentWriter.WritingCompleted += PrintAsync_Completed; StartLongPrintingOperation(fixedDocument.Pages.Count); //Print the FixedDocument asynchronously. xpsDocumentWriter.WriteAsync(fixedDocument); }
public bool Print(Printer printer, int pageCount, Func <Size, PageOrientation> getOrientation, Func <int, int, Size, Visual> getPage, Action <WritingProgressChangedEventArgs> progressChanged, Action <WritingCompletedEventArgs> printCompleted) { if (printer == null || string.IsNullOrEmpty(printer.Name)) { return(false); } using (var printQueue = GetPrinter(printer.Name)) { if (printQueue == null) { return(false); } var printTicket = GetPrintTicket(printQueue, printer.Ticket); /*The first way * var printDialog = new PrintDialog(); * printDialog.PrintQueue = printer; * printDialog.PrintTicket = printTicket; */ //The second way printQueue.UserPrintTicket = printTicket; //Page Size 1 inch = 96 pixels Size originalPageSize; try { var pageImageableArea = printQueue.GetPrintCapabilities(printTicket).PageImageableArea; originalPageSize = new Size(pageImageableArea.ExtentWidth, pageImageableArea.ExtentHeight); } catch { originalPageSize = new Size(printTicket.PageMediaSize.Width.Value, printTicket.PageMediaSize.Height.Value); } //Page Orientation if (getOrientation != null) { if (getOrientation(originalPageSize) == PageOrientation.Landscape) { //exchange width and height originalPageSize = new Size(originalPageSize.Height, originalPageSize.Width); printTicket.PageOrientation = System.Printing.PageOrientation.Landscape; } else { printTicket.PageOrientation = System.Printing.PageOrientation.Portrait; } } else { if (printTicket.PageOrientation == System.Printing.PageOrientation.Landscape) { originalPageSize = new Size(originalPageSize.Height, originalPageSize.Width); } } try { //The first way //printDialog.PrintDocument(new LabelDocPaginator(pageCount, originalPageSize, getSpecifiedPage), "Print Label"); //The second way DisposeXpsDocWriter(); _printCompleted = printCompleted; _progressChanged = progressChanged; _xpsDocWriter = PrintQueue.CreateXpsDocumentWriter(printQueue); _xpsDocWriter.WritingCompleted += WritingCompleted; _xpsDocWriter.WritingProgressChanged += WritingProgressChanged; _docPaginator = new PrintDocPaginator(pageCount, originalPageSize, getPage); _xpsDocWriter.WriteAsync(_docPaginator); } catch (Exception ex) { DisposeXpsDocWriter(); _printCompleted = null; _progressChanged = null; return(false); } } return(true); }
private void printButton_Click(object sender, RoutedEventArgs e) { int copies; if (!int.TryParse(copiesTextBox.Text, out copies) || copies <= 0) { TaskDialog td = new TaskDialog(Window.GetWindow(this), "Invalid Input", "The value entered in the Copies field is not a valid. The value must be between 1 and " + int.MaxValue.ToString() + " inclusive.", MessageType.Error); td.ShowDialog(); copiesTextBox.SelectAll(); copiesTextBox.Focus(); return; } printButton.IsEnabled = false; SavePrintSettings(); PrintQueue printer = selectedPrinter; FixedDocument document = (FixedDocument)documentViewer.Document; PrintTicket ticket = printer.DefaultPrintTicket; if (duplexCombo.SelectedItem == oneSide) { ticket.Duplexing = Duplexing.OneSided; } else if (duplexCombo.SelectedItem == longDuplex) { ticket.Duplexing = Duplexing.TwoSidedLongEdge; } else if (duplexCombo.SelectedItem == shortDuplex) { ticket.Duplexing = Duplexing.TwoSidedShortEdge; } ticket.PagesPerSheet = int.Parse(((FrameworkElement)pagesPerSheetCombo.SelectedItem).Tag.ToString()); document.PrintTicket = ticket; bool manDuplex = duplexCombo.SelectedItem == manualDuplex; bool collated = collationCombo.SelectedIndex == 0; BackstageEvents.StaticUpdater.InvokeForceBackstageClose(this, EventArgs.Empty); if (!manDuplex) { Dispatcher.BeginInvoke(() => { XpsDocumentWriter xps = PrintQueue.CreateXpsDocumentWriter(printer); BackstageEvents.StaticUpdater.InvokePrintStarted(this, new PrintEventArgs(xps)); ticket.Collation = collated ? Collation.Collated : Collation.Uncollated; ticket.CopyCount = copies; xps.WriteAsync(document, XpsDocumentNotificationLevel.ReceiveNotificationEnabled); }); } else { Dispatcher.BeginInvoke(() => { new ManualDuplex(document, printer, collated, copies); }); } }
private void SaveDocument(string fileName, FixedDocument document) { //Delete any existing file. File.Delete(fileName); //Create a new XpsDocument at the given location. XpsDocument xpsDocument = new XpsDocument(fileName, FileAccess.ReadWrite); //Create a new XpsDocumentWriter for the XpsDocument object. xdw = XpsDocument.CreateXpsDocumentWriter(xpsDocument); //We want to be notified of when the progress changes. xdw.WritingProgressChanged += delegate(object sender, WritingProgressChangedEventArgs e) { //Update the value of the progress bar. pbSaveProgress.Value = e.Number; }; //We want to be notified of when the operation is complete. xdw.WritingCompleted += delegate(object sender, WritingCompletedEventArgs e) { //We're finished with the XPS document so close it. //This step is important. xpsDocument.Close(); string msg = "Saving complete."; if (e.Error != null) { msg = string.Format("An error occurred whilst " + "saving the document.\n\n{0}", e.Error.Message); } else if (e.Cancelled) { //Delete the incomplete file. File.Delete(fileName); msg = string.Format("Saving cancelled by user."); } //Inform the user of the print operation's exit status. MessageBox.Show(msg, "Recipe_07_11", MessageBoxButton.OK, MessageBoxImage.Information); spProgressMask.Visibility = Visibility.Collapsed; }; //Show the long operation mask with the cancel button and progress bar. spProgressMask.Visibility = Visibility.Visible; pbSaveProgress.Maximum = document.Pages.Count; pbSaveProgress.Value = 0; //Write the document to the Xps file asynchronously. xdw.WriteAsync(document); }