本文共 4176 字,大约阅读时间需要 13 分钟。
1: using System;
2: using System.Windows;
3: using System.Windows.Controls;
4: using System.Windows.Printing;
5:
6: namespace HelloPrinter
7: {
8: public partial class MainPage : UserControl
9: {
10: public MainPage()
11: {
12: InitializeComponent();
13: }
14:
15: private void button1_Click(object sender, RoutedEventArgs e)
16: {
17: PrintDocument printDoc = new PrintDocument();
18: printDoc.DocumentName = "Hello World from Silverlight";
19: printDoc.PrintPage += new EventHandler(printDoc_PrintPage);
20: printDoc.Print();
21: }
22:
23: void printDoc_PrintPage(object sender, PrintPageEventArgs e)
24: {
25: StackPanel panel = new StackPanel() { Orientation = Orientation.Horizontal };
26: panel.Children.Add(new TextBlock() { Text = "Hello ", FontFamily = new System.Windows.Media.FontFamily("Arial"), FontSize = 12 });
27: panel.Children.Add(new TextBlock() { Text = "World", FontFamily = new System.Windows.Media.FontFamily("Arial"), FontSize = 12 });
28: e.PageVisual = panel;
29: e.HasMorePages = false;
30: }
31: }
32: }
1: protected function button1_clickHandler(event:MouseEvent):void
2: {
3: var job : FlexPrintJob = new FlexPrintJob();
4: job.printAsBitmap = false;
5: if(job.start()) {
6: var group : HGroup = new HGroup();
7: group.height = job.pageHeight;
8: group.width = job.pageWidth;
9: var text : SimpleText = new SimpleText();
10: text.text = "Hello ";
11: text.setStyle("fontFamily", "Arial");
12: text.setStyle("fontSize",12);
13: group.addElement(text);
14: text = new SimpleText();
15: text.setStyle("fontFamily", "Arial");
16: text.setStyle("fontSize",12);
17: text.text = "World";
18: group.addElement(text);
19:
20: addElement(group);
21:
22: job.addObject(group, FlexPrintJobScaleType.NONE);
23:
24: job.send();
25: removeElement(group);
26: }
本文转自
冷秋寒 51CTO博客,原文链接:http://blog.51cto.com/kevinfan/271746,如需转载请自行联系原作者