silverlight 調用預設印表機

來源:互聯網
上載者:User

列印輔助類。

首先添加

using System.Windows.Printing;

命名空間

 1 public class SilverPrint 2     { 3         //設定每一項之間的間距 4         int listPrintIndex = 0; 5         private List<PrintStr> listStr = null; 6         public void Print(List<PrintStr> strs) 7         { 8             listStr = strs; 9             PrintDocument printDoc = new PrintDocument();10             printDoc.PrintPage += OnPrintPage;11             PrinterFallbackSettings settings = new PrinterFallbackSettings();12             settings.ForceVector = true;13             printDoc.Print("dd", settings, true);//dd是文檔的名字14         }15         public void OnPrintPage(object sender, PrintPageEventArgs e)16         {17             Canvas printSurface = new Canvas();18             //得到最頂端位置19             double topPosition = e.PageMargins.Top;20             //遍曆當前的ListBox.Items21             while (listPrintIndex < listStr.Count)22             {23                 //執行個體化TextBlock用來存放每一行的值24                 TextBlock txt = new TextBlock();25                 txt.FontSize = listStr[listPrintIndex].FontSize;26                 txt.Text = listStr[listPrintIndex].Content;27                 double measuredHeight = txt.ActualHeight;28                 //如果列印的當前行高度不合適的話,則進行分頁29                 if (measuredHeight > (e.PrintableArea.Height - topPosition))30                 {31                     e.HasMorePages = true;32                     topPosition = e.PageMargins.Top;33                     break;34                 }35                 //設定TextBlock在Canvas中的位置36                 txt.SetValue(Canvas.TopProperty, topPosition);37                 txt.SetValue(Canvas.LeftProperty, e.PageMargins.Left);38                 //將TextBlock添加到列印的元素中去39                 printSurface.Children.Add(txt);40                 listPrintIndex++;41                 //追加高度42                 topPosition = topPosition + measuredHeight;43             }44             e.PageVisual = printSurface;45         }46     }47     //列印內容類別48     public class PrintStr49     {50        public string Content { set; get; }51        public int FontSize { set; get; }52        public PrintStr(string str,int size=10)53        {54            this.Content = str;55            this.FontSize = size;56        }57     }

列印直接調用

new SilverPrint().Print(printStrs);

如果是silverlight5.0之前會彈出預覽列印。。。如果是silverlight5.0 允許瀏覽器外允許 增加許可權。就可以不用彈出預覽列印,直接列印了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.