GridView直接列印
阿新 • • 發佈:2018-11-14
/// <summary> /// 列印功能 /// </summary> /// <param name="panel">列印控制元件</param> /// <param name="title">標題</param> /// <param name="pageSize">紙張型別</param> /// <param name="margins">邊距</param> public virtual void Print(IPrintable panel,stringtitle = null, PaperKind pageSize = PaperKind.A4, System.Drawing.Printing.Margins margins = null) { PrintingSystem ps = new PrintingSystem(); CompositeLink link = new CompositeLink(ps); PrintableComponentLink printableLink = new PrintableComponentLink() { Component = panel }; ps.Links.Add(link); link.Links.Add(printableLink);//link.Landscape = false;//橫向 link.PaperKind = pageSize;//設定紙張大小 if (margins == null) margins = new System.Drawing.Printing.Margins(0, 0, 10, 10); link.Margins = margins; ////判斷是否有標題,有則設定 //if (!string.IsNullOrEmpty(title)) //{// PageHeaderFooter phf = link.PageHeaderFooter as PageHeaderFooter; // phf.Header.Content.Clear(); // phf.Header.Content.AddRange(new string[] { "", title, "" }); // phf.Header.Font = new System.Drawing.Font("宋體", 14, System.Drawing.FontStyle.Bold); // phf.Header.LineAlignment = BrickAlignment.Center; // //phf.Footer.Content.AddRange(new string[] { "", String.Format("列印時間: {0:g}", DateTime.Now), "" }); //} link.CreateDocument(); //建立文件 //ps.PreviewRibbonFormEx.Show();//進行預覽 ps.Print(); }