private async void MainWindow_ContentRendered(object sender, EventArgs e) { await Task.Delay(1000); var pageSize = new Size(1280 * 8, 720 * 8); var document = new FixedDocument(); document.DocumentPaginator.PageSize = pageSize; var fixedPage = new FixedPage { Width = pageSize.Width, Height = pageSize.Height, }; for (int i = 0; i < 100; i++) { fixedPage.Children.Add(new Border { Width = pageSize.Width, Height = pageSize.Height, Background = new VisualBrush { Visual = DemoImage, } }); } fixedPage.Measure(pageSize); fixedPage.Arrange(new Rect(new Point(), pageSize)); fixedPage.UpdateLayout(); // Add page to document var pageContent = new PageContent(); ((IAddChild)pageContent).AddChild(fixedPage); document.Pages.Add(pageContent); // Send to the printer. var pd = new PrintDialog(); pd.PrintDocument(document.DocumentPaginator, "正在打印……"); return; using var _localPrintServer = new LocalPrintServer(); var _currentPrintQueue = _localPrintServer.DefaultPrintQueue; _xpsDocumentWriter = PrintQueue.CreateXpsDocumentWriter(_currentPrintQueue); //_xpsDocumentWriter.WritingProgressChanged += XpsDocumentWriter_WritingProgressChanged; //_xpsDocumentWriter.WritingPrintTicketRequired += XpsDocumentWriterOnWritingPrintTicketRequired; //_xpsDocumentWriter.WritingCancelled += XpsDocumentWriterOnWritingCancelled; //_xpsDocumentWriter.WritingCompleted += XpsDocumentWriterOnWritingCompleted; _vToXspd = (VisualsToXpsDocument)_xpsDocumentWriter.CreateVisualsCollator(); _vToXspd.BeginBatchWrite(); for (int i = 0; i < 100; i++) { _vToXspd?.WriteAsync(RootPanel); } _vToXspd?.EndBatchWrite(); }
private void btnPrintVisuals_Click(object sender, RoutedEventArgs e) { //Get hold of the visual you want to print. List <Visual> visuals = GetVisuals(); // Create a Print dialog. PrintDialog printDialog = new PrintDialog(); if (printDialog.ShowDialog() != true) { return; } // Get the default print queue PrintQueue printQueue = printDialog.PrintQueue; // Get an XpsDocumentWriter for the default print queue XpsDocumentWriter xpsdw = PrintQueue.CreateXpsDocumentWriter(printQueue); VisualsToXpsDocument vtxd = (VisualsToXpsDocument)xpsdw.CreateVisualsCollator(); //Indicate we want any writes to be performed in a batch operation. vtxd.BeginBatchWrite(); //Write out each visual. visuals.ForEach(delegate(Visual visual) { //Scale the visual Visual scaledVisual = ScaleVisual(visual, printQueue); vtxd.Write(scaledVisual); }); //Mark the end of the batch operation. vtxd.EndBatchWrite(); }