Created
September 19, 2015 12:54
-
-
Save ichengzi/a82de7420935a2a37e55 to your computer and use it in GitHub Desktop.
使用MergeAndValidatePrintTicket,VisualsToXpsDocument批量visual(网络搜集)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace XamlConsolePrinter | |
{ | |
using System.Printing; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using System.Threading.Tasks.Schedulers; | |
using System.Windows; | |
using System.Windows.Controls; | |
using System.Windows.Xps; | |
internal class Program | |
{ | |
private class PrintControl : UserControl | |
{ | |
public PrintControl() | |
{ | |
var textBlock = new TextBlock {Text = "Hi there!"}; | |
Content = textBlock; | |
} | |
} | |
private static void Main(string[] args) | |
{ | |
var scheduler = new StaTaskScheduler(numberOfThreads: 1); | |
var task = Task.Factory | |
.StartNew(() => | |
{ | |
var printQueue = LocalPrintServer.GetDefaultPrintQueue(); | |
var ticket = new PrintTicket | |
{ | |
OutputColor = OutputColor.Color, | |
PageMediaType = PageMediaType.Plain, | |
PageOrientation = PageOrientation.Landscape, | |
PageMediaSize = new PageMediaSize(PageMediaSizeName.Roll04Inch), | |
PageBorderless = PageBorderless.Borderless | |
}; | |
var result = printQueue.MergeAndValidatePrintTicket(printQueue.UserPrintTicket, ticket); | |
var pageMediaSize = result.ValidatedPrintTicket.PageMediaSize; | |
if (!pageMediaSize.Height.HasValue || !pageMediaSize.Width.HasValue) | |
return; | |
// XAML control to print | |
var visual = new PrintControl(); | |
visual.Arrange(new Rect(new Size(pageMediaSize.Width.Value, pageMediaSize.Height.Value))); | |
var documentWriter = PrintQueue.CreateXpsDocumentWriter(printQueue); | |
var visualsToXpsDocument = (VisualsToXpsDocument) documentWriter.CreateVisualsCollator(); | |
if (visualsToXpsDocument == null) return; | |
visualsToXpsDocument.Write(visual); | |
visualsToXpsDocument.EndBatchWrite(); | |
}, CancellationToken.None, TaskCreationOptions.None, scheduler); | |
task.Wait(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment