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(); }
//</SnippetWriteAsyncToXpsWithVisual> private void SaveVisualsAsync(XpsDocumentWriter xpsdw, List <Visual> vc) { _xpsdwActive = xpsdw; xpsdw.WritingCompleted += new WritingCompletedEventHandler(AsyncSaveCompleted); xpsdw.WritingProgressChanged += new WritingProgressChangedEventHandler(AsyncSavingProgress); // Setup for writing multiple visuals VisualsToXpsDocument vToXpsD = (VisualsToXpsDocument)xpsdw.CreateVisualsCollator(); _batchProgress = 0; _activeVtoXPSD = vToXpsD; // Iterate through all visuals in the collection foreach (Visual v in vc) { vToXpsD.WriteAsync(v); // Write each visual to single page. } }// end:SaveVisualsAsync()