InvoiceLayoutPrinter.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. 
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Drawing.Printing;
  8. using System.IO;
  9. using System.Runtime.Serialization.Formatters.Binary;
  10. using System.Security.Cryptography;
  11. namespace Dongke.WinForm.Controls.InvoiceLayout
  12. {
  13. public class InvoiceLayoutPrinter
  14. {
  15. #region 常量
  16. #region 保存、读取相关
  17. // TODO 加密保存 解密读取
  18. private static byte[] KEY = new byte[] { 106, 240, 243, 74, 29, 163, 145, 29, 39, 65, 118, 96, 247, 237, 40, 121, 116, 10, 14, 50, 139, 117, 244, 163, 193, 190, 184, 66, 8, 185, 78, 73 };
  19. private static byte[] IV = new byte[] { 26, 122, 25, 28, 97, 211, 152, 32, 223, 110, 120, 242, 170, 35, 96, 93 };
  20. //private static ICryptoTransform _decryptor = null;
  21. #endregion 保存、读取相关
  22. #endregion
  23. #region 成员变量
  24. private PropertyOfLayoutBox Property = null;
  25. private GridItem OneGridItem = null;
  26. private List<LayoutItem> LayoutItems = null;
  27. private int PageWidthPixel = 0;
  28. private int PageHeightPixel = 0;
  29. private RectangleF BackImgRectangleF = RectangleF.Empty;
  30. private float _allOffsetX = 0;
  31. private float _allOffsetY = 0;
  32. private int _printPageNum = 1; // 打印页的当前页码
  33. private int _printTotalPageNum = 1; // 总页数
  34. private DataRow _printData = null;
  35. private DataTable _printGridData = null;
  36. private PrintDocument _printDocument = new PrintDocument();
  37. #endregion
  38. #region 构造函数
  39. static InvoiceLayoutPrinter()
  40. {
  41. //Rijndael age = Rijndael.Create();
  42. //_decryptor = age.CreateDecryptor(KEY, IV);
  43. }
  44. private InvoiceLayoutPrinter()
  45. {
  46. _printDocument.DocumentName = "LayoutDocument";
  47. _printDocument.BeginPrint += new PrintEventHandler(this.printDocument_BeginPrint);
  48. _printDocument.PrintPage += new PrintPageEventHandler(this.PrintDocument_PrintPage);
  49. }
  50. #endregion
  51. private void printDocument_BeginPrint(object sender, PrintEventArgs e)
  52. {
  53. }
  54. private void PrintDocument_PrintPage(object sender, PrintPageEventArgs e)
  55. {
  56. //try
  57. {
  58. e.Graphics.PageUnit = GraphicsUnit.Millimeter;
  59. e.HasMorePages = DrawPage(e.Graphics);
  60. }
  61. //catch (Exception ex)
  62. {
  63. }
  64. }
  65. public static List<Image> Print(byte[] layout, DataRow data, DataTable detail)
  66. {
  67. if (layout == null || layout.Length == 0)
  68. {
  69. throw new System.ArgumentNullException("layout");
  70. }
  71. if (data == null)
  72. {
  73. throw new System.ArgumentNullException("data");
  74. }
  75. //if (detail == null || detail.Rows.Count == 0)
  76. //{
  77. // throw new System.ArgumentNullException("detail");
  78. //}
  79. try
  80. {
  81. InvoiceLayoutPrinter printer = new InvoiceLayoutPrinter();
  82. printer.LayoutItems = new List<LayoutItem>();
  83. printer.ReadLayout(layout);
  84. printer._printData = data;
  85. printer._printGridData = detail;
  86. //return printer.DrawImages().ToArray();
  87. return printer.DrawImages();
  88. }
  89. catch (Exception ex)
  90. {
  91. throw ex;
  92. }
  93. }
  94. public static void Print(string printerName, short copies, byte[] layout, DataRow data, DataTable detail)
  95. {
  96. if (layout == null || layout.Length == 0)
  97. {
  98. throw new System.ArgumentNullException("layout");
  99. }
  100. if (data == null)
  101. {
  102. throw new System.ArgumentNullException("data");
  103. }
  104. //if (detail == null || detail.Rows.Count == 0)
  105. //{
  106. // throw new System.ArgumentNullException("detail");
  107. //}
  108. try
  109. {
  110. InvoiceLayoutPrinter printer = new InvoiceLayoutPrinter();
  111. printer.LayoutItems = new List<LayoutItem>();
  112. printer.ReadLayout(layout);
  113. printer._printData = data;
  114. printer._printGridData = detail;
  115. printer._printDocument.PrinterSettings.PrinterName = printerName;
  116. printer._printDocument.PrinterSettings.Copies = copies;
  117. printer._printDocument.Print();
  118. //return printer.DrawImages().ToArray();
  119. //return printer.DrawImages();
  120. }
  121. catch (Exception ex)
  122. {
  123. throw ex;
  124. }
  125. }
  126. #region 票据打印
  127. private void ReadLayout(byte[] layout)
  128. {
  129. try
  130. {
  131. using (MemoryStream stream = new MemoryStream(layout))
  132. {
  133. stream.Position = 0;
  134. // 反序列化
  135. BinaryFormatter formatter = new BinaryFormatter();
  136. // TODO 解密读取
  137. //if (CryptoStream)
  138. if (true)
  139. {
  140. //using (CryptoStream cStream = new CryptoStream(stream, _decryptor, CryptoStreamMode.Read))
  141. Rijndael age = Rijndael.Create();
  142. using (CryptoStream cStream = new CryptoStream(stream, age.CreateDecryptor(KEY, IV), CryptoStreamMode.Read))
  143. {
  144. this.Property = (PropertyOfLayoutBox)formatter.Deserialize(cStream);
  145. }
  146. }
  147. else
  148. {
  149. this.Property = (PropertyOfLayoutBox)formatter.Deserialize(stream);
  150. }
  151. //_lastItemRF = propertyOflayoutBox.LastItemRF;
  152. //_newItemID = propertyOflayoutBox.NewItemID;
  153. //_paperHeight = propertyOflayoutBox.PaperHeight;
  154. //_paperWidth = propertyOflayoutBox.PaperWidth;
  155. //if (_zoomType != ZoomType.Whole)
  156. //{
  157. // ChangePaperSize();
  158. //}
  159. //else
  160. //{
  161. // InitializeZoom();
  162. //}
  163. //paperArea.BackgroundImage = propertyOflayoutBox.BackgroundImage;
  164. //_backgroundImageName = propertyOflayoutBox.BackgroundImageName;
  165. //_isPrintBackground = propertyOflayoutBox.PrintBackground;
  166. //_isPrintAreaVisible = propertyOflayoutBox.PrintAreaVisible;
  167. ArrayList items = this.Property.Items;
  168. if (items != null && 0 < items.Count)
  169. {
  170. foreach (LayoutItem item in items)
  171. {
  172. //item.Init(this, false);
  173. //item.Init();
  174. this.LayoutItems.Add(item);
  175. if (item.ItemType == ItemType.Grid)
  176. {
  177. this.OneGridItem = item as GridItem;
  178. if (this.OneGridItem.HeadRowsHeight == 0)
  179. {
  180. this.OneGridItem.HeadRowsHeight = this.OneGridItem.RowsHeight;
  181. }
  182. }
  183. }
  184. }
  185. //paperArea.Select();
  186. //RefreshItemsBound();
  187. ////paperArea.Refresh();
  188. //return true;
  189. }
  190. }
  191. catch (Exception ex)
  192. {
  193. throw ex;
  194. }
  195. }
  196. private List<Image> DrawImages()
  197. {
  198. this.PageWidthPixel = LayoutCommon.MillimeterToPixel(this.Property.PaperWidth);
  199. this.PageHeightPixel = LayoutCommon.MillimeterToPixel(this.Property.PaperHeight);
  200. this._printPageNum = 1;
  201. this._printTotalPageNum = 1;
  202. //if (this.OneGridItem != null)
  203. //{
  204. // foreach (LayoutItem item in this.LayoutItems)
  205. // {
  206. // if (item.ItemType == ItemType.Grid)
  207. // {
  208. // GridItem gItem = (item as GridItem);
  209. // gItem.PrintGridSource = this._printGridData;
  210. // _printTotalPageNum = Math.Max(_printTotalPageNum, gItem.PrintTotalPageNum);
  211. // }
  212. // }
  213. //}
  214. List<Image> images = new List<Image>();
  215. //Bitmap pageImage = null;
  216. //Graphics pageGraphics = null;
  217. bool hasNextPage = true;
  218. while (hasNextPage)
  219. {
  220. Bitmap pageImage = new Bitmap(this.PageWidthPixel, this.PageHeightPixel);
  221. pageImage.SetResolution(LayoutCommon.DPI, LayoutCommon.DPI);
  222. using (Graphics pageGraphics = Graphics.FromImage(pageImage))
  223. {
  224. // TODO 白色背景
  225. pageGraphics.Clear(Color.White);
  226. //pageGraphics.FillRectangle(Brushes.White, pageGraphics.ClipBounds);
  227. pageGraphics.PageUnit = GraphicsUnit.Millimeter;
  228. hasNextPage = DrawPage(pageGraphics);
  229. images.Add(pageImage);
  230. }
  231. }
  232. return images;
  233. }
  234. private bool DrawPage(Graphics graphics)
  235. {
  236. //try
  237. {
  238. // 背景打印
  239. if (this.Property.PrintBackground)
  240. {
  241. PrintBackgroundImage(graphics);
  242. }
  243. // Item打印
  244. foreach (LayoutItem layoutItem in this.LayoutItems)
  245. {
  246. switch (layoutItem.ItemType)
  247. {
  248. case ItemType.Text:
  249. DrawTextItem(graphics, (TextItem)layoutItem);
  250. break;
  251. case ItemType.Image:
  252. DrawImageItem(graphics, (ImageItem)layoutItem);
  253. break;
  254. case ItemType.Rectangle:
  255. DrawRectangleItem(graphics, (RectangleItem)layoutItem);
  256. break;
  257. case ItemType.Ellipse:
  258. DrawEllipseItem(graphics, (EllipseItem)layoutItem);
  259. break;
  260. //case ItemType.Line:
  261. // DrawLineItem(graphics, (LineItem)layoutItem);
  262. // break;
  263. case ItemType.Grid:
  264. DrawGridItem(graphics, (GridItem)layoutItem);
  265. break;
  266. case ItemType.PageNum:
  267. DrawPageNumItem(graphics, (PageNumItem)layoutItem);
  268. break;
  269. case ItemType.TotalText:
  270. DrawTotalTextItem(graphics, (TotalTextItem)layoutItem);
  271. break;
  272. default:
  273. //throw new NotImplementedException();
  274. break;
  275. }
  276. }
  277. // 表格Item是否有下一页
  278. if (_printPageNum < _printTotalPageNum)
  279. {
  280. _printPageNum++;
  281. //e.HasMorePages = true;
  282. return true;
  283. }
  284. return false;
  285. }
  286. //catch (Exception ex)
  287. //{
  288. // throw ex;
  289. //}
  290. }
  291. #region 绘制
  292. /// <summary>
  293. /// 打印背景图片
  294. /// </summary>
  295. /// <param name="graphics">用于绘制的System.Drawing.Graphics</param>
  296. private void PrintBackgroundImage(Graphics graphics)
  297. {
  298. if (this.Property.BackgroundImage != null)
  299. {
  300. if (this.BackImgRectangleF.IsEmpty)
  301. {
  302. // 背景图片的高宽比例
  303. float imgWHRate = (float)this.Property.BackgroundImage.Width / (float)this.Property.BackgroundImage.Height;
  304. // 打印纸张的高宽比例
  305. float paperWHRate = this.Property.PaperWidth / this.Property.PaperHeight;
  306. // 缩放背景图片,但保持图片高宽比例
  307. if (imgWHRate > paperWHRate)
  308. {
  309. BackImgRectangleF.Width = this.Property.PaperWidth;
  310. BackImgRectangleF.Height = this.Property.PaperWidth / imgWHRate;
  311. BackImgRectangleF.X = 0;
  312. BackImgRectangleF.Y = (this.Property.PaperHeight - BackImgRectangleF.Height) / 2 + 0;
  313. }
  314. else
  315. {
  316. BackImgRectangleF.Width = this.Property.PaperHeight * imgWHRate;
  317. BackImgRectangleF.Height = this.Property.PaperHeight;
  318. BackImgRectangleF.X = (this.Property.PaperWidth - BackImgRectangleF.Width) / 2 + 0;
  319. BackImgRectangleF.Y = 0;
  320. }
  321. }
  322. // 在指定的范围绘制背景图片
  323. graphics.DrawImage(this.Property.BackgroundImage, BackImgRectangleF);
  324. }
  325. }
  326. /// <summary>
  327. /// 打印文本Item
  328. /// </summary>
  329. /// <param name="graphics">用于绘制的System.Drawing.Graphics</param>
  330. /// <param name="textItem">文本Item</param>
  331. private void DrawTextItem(Graphics graphics, TextItem textItem)
  332. {
  333. // 文本Item打印范围
  334. RectangleF rectangleFM = new RectangleF(
  335. _allOffsetX + textItem.Left,
  336. _allOffsetY + textItem.Top,
  337. textItem.Width,
  338. textItem.Height);
  339. RectangleF rectangleF = rectangleFM;
  340. rectangleF.Inflate(0 - LayoutConsts.TEXT_MARGIN, 0 - LayoutConsts.TEXT_MARGIN);
  341. rectangleFM.Height -= LayoutConsts.TEXT_MARGIN;
  342. object value = null;
  343. if (this._printData.Table.Columns.Contains(textItem.DataMember))
  344. {
  345. value = this._printData[textItem.DataMember];
  346. }
  347. textItem.PrintTextItem(value, graphics, rectangleFM, rectangleF);
  348. }
  349. /// <summary>
  350. /// 打印图片Item
  351. /// </summary>
  352. /// <param name="graphics">用于绘制的System.Drawing.Graphics</param>
  353. /// <param name="imageItem">图片Item</param>
  354. private void DrawImageItem(Graphics graphics, ImageItem imageItem)
  355. {
  356. // 图片Item打印范围
  357. RectangleF rectangleF = new RectangleF(
  358. _allOffsetX + imageItem.Left,
  359. _allOffsetY + imageItem.Top,
  360. imageItem.Width,
  361. imageItem.Height);
  362. // 在指定的范围绘制图片Item
  363. LayoutUtility.DrawImage(graphics, rectangleF, imageItem.Image);
  364. }
  365. /// <summary>
  366. /// 打印矩形Item
  367. /// </summary>
  368. /// <param name="graphics">用于绘制的System.Drawing.Graphics</param>
  369. /// <param name="rectangleItem">矩形Item</param>
  370. private void DrawRectangleItem(Graphics graphics, RectangleItem rectangleItem)
  371. {
  372. // 矩形Item打印范围
  373. RectangleF rectangleF = new RectangleF(
  374. _allOffsetX + rectangleItem.Left,
  375. _allOffsetY + rectangleItem.Top,
  376. rectangleItem.Width,
  377. rectangleItem.Height);
  378. float lineWidthDraw = rectangleItem.LineWidth;
  379. // 在指定的范围绘制矩形Item
  380. LayoutUtility.DrawRectangle(graphics, rectangleF, rectangleItem.LineColor,
  381. rectangleItem.FillColor, lineWidthDraw, rectangleItem.Transparent);
  382. }
  383. /// <summary>
  384. /// 打印椭圆Item
  385. /// </summary>
  386. /// <param name="graphics">用于绘制的System.Drawing.Graphics</param>
  387. /// <param name="ellipseItem">椭圆Item</param>
  388. private void DrawEllipseItem(Graphics graphics, EllipseItem ellipseItem)
  389. {
  390. // 椭圆Item打印范围
  391. RectangleF rectangleF = new RectangleF(
  392. _allOffsetX + ellipseItem.Left,
  393. _allOffsetY + ellipseItem.Top,
  394. ellipseItem.Width,
  395. ellipseItem.Height);
  396. float lineWidthDraw = ellipseItem.LineWidth;
  397. // 在指定的范围绘制椭圆Item
  398. LayoutUtility.DrawEllipse(graphics, rectangleF, ellipseItem.LineColor,
  399. ellipseItem.FillColor, lineWidthDraw, ellipseItem.Transparent);
  400. }
  401. /// <summary>
  402. /// 打印表格Item
  403. /// </summary>
  404. /// <param name="graphics">用于绘制的System.Drawing.Graphics</param>
  405. /// <param name="gridItem">表格Item</param>
  406. private void DrawGridItem(Graphics graphics, GridItem gridItem)
  407. {
  408. // 表格Item打印范围
  409. RectangleF rectangleF = new RectangleF(
  410. _allOffsetX + gridItem.Left,
  411. _allOffsetY + gridItem.Top,
  412. gridItem.Width,
  413. gridItem.Height);
  414. // 在指定的范围绘制表格Item
  415. //gridItem.DrawGridItem(graphics, rectangleF, 1, _printPageNum);
  416. gridItem.PrintGridItem(graphics, rectangleF, _printPageNum);
  417. }
  418. /// <summary>
  419. /// 打印页码Item
  420. /// </summary>
  421. /// <param name="graphics">用于绘制的System.Drawing.Graphics</param>
  422. /// <param name="pageNumItem">页码Item</param>
  423. private void DrawPageNumItem(Graphics graphics, PageNumItem pageNumItem)
  424. {
  425. // 文本Item打印范围
  426. RectangleF rectangleFM = new RectangleF(
  427. _allOffsetX + pageNumItem.Left,
  428. _allOffsetY + pageNumItem.Top,
  429. pageNumItem.Width,
  430. pageNumItem.Height);
  431. RectangleF rectangleF = rectangleFM;
  432. rectangleF.Inflate(0 - LayoutConsts.TEXT_MARGIN, 0 - LayoutConsts.TEXT_MARGIN);
  433. rectangleFM.Height -= LayoutConsts.TEXT_MARGIN;
  434. // 在指定的范围绘制文本Item
  435. LayoutUtility.DrawText(graphics,
  436. rectangleFM,
  437. rectangleF,
  438. pageNumItem.GetPrintText(this._printPageNum, this._printTotalPageNum),
  439. pageNumItem.Font,
  440. pageNumItem.TextColor,
  441. 0,
  442. 0,
  443. 0,
  444. pageNumItem.TextAlign,
  445. pageNumItem.TextAlignVertical,
  446. false,
  447. false,
  448. false);
  449. }
  450. /// <summary>
  451. /// 打印合计Item
  452. /// </summary>
  453. /// <param name="graphics">用于绘制的System.Drawing.Graphics</param>
  454. /// <param name="totalTextItem">合计Item</param>
  455. private void DrawTotalTextItem(Graphics graphics, TotalTextItem totalTextItem)
  456. {
  457. // 文本Item打印范围
  458. RectangleF rectangleFM = new RectangleF(
  459. _allOffsetX + totalTextItem.Left,
  460. _allOffsetY + totalTextItem.Top,
  461. totalTextItem.Width,
  462. totalTextItem.Height);
  463. RectangleF rectangleF = rectangleFM;
  464. rectangleF.Inflate(0 - LayoutConsts.TEXT_MARGIN, 0 - LayoutConsts.TEXT_MARGIN);
  465. rectangleFM.Height -= LayoutConsts.TEXT_MARGIN;
  466. if (_printPageNum != _printTotalPageNum && totalTextItem.NotTotalValue != "[#合计值#]")
  467. {
  468. LayoutUtility.DrawText(graphics,
  469. rectangleFM,
  470. rectangleF,
  471. totalTextItem.NotTotalValue,
  472. totalTextItem.NotTotalFont,
  473. totalTextItem.NotTotalColor,
  474. totalTextItem.LineSpace,
  475. totalTextItem.CharacterSpace,
  476. totalTextItem.CharacterCount,
  477. totalTextItem.NotTotalAlign,
  478. totalTextItem.NotTotalAlignVertical,
  479. totalTextItem.Wrap,
  480. totalTextItem.Clip,
  481. false);
  482. return;
  483. }
  484. // 在指定的范围绘制文本Item
  485. object value = null;
  486. if (this._printData.Table.Columns.Contains(totalTextItem.DataMember))
  487. {
  488. value = this._printData[totalTextItem.DataMember];
  489. }
  490. value = totalTextItem.ResetTextValue(value, true);
  491. LayoutUtility.DrawText(graphics,
  492. rectangleFM,
  493. rectangleF,
  494. (value == null? "": value.ToString()),
  495. totalTextItem.Font,
  496. totalTextItem.TextColor,
  497. totalTextItem.LineSpace,
  498. totalTextItem.CharacterSpace,
  499. totalTextItem.CharacterCount,
  500. totalTextItem.TextAlign,
  501. totalTextItem.NotTotalAlignVertical,
  502. totalTextItem.Wrap,
  503. totalTextItem.Clip,
  504. false);
  505. }
  506. #endregion
  507. #endregion
  508. }
  509. }