InvoiceLayoutPrinter.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  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. using Curtain.Log;
  12. namespace Dongke.WinForm.Controls.InvoiceLayout
  13. {
  14. public class InvoiceLayoutPrinter
  15. {
  16. #region 常量
  17. #region 保存、读取相关
  18. // TODO 加密保存 解密读取
  19. 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 };
  20. private static byte[] IV = new byte[] { 26, 122, 25, 28, 97, 211, 152, 32, 223, 110, 120, 242, 170, 35, 96, 93 };
  21. private static ICryptoTransform _decryptor = null;
  22. #endregion 保存、读取相关
  23. #endregion
  24. #region 成员变量
  25. private PropertyOfLayoutBox Property = null;
  26. private GridItem OneGridItem = null;
  27. private List<LayoutItem> LayoutItems = null;
  28. private int PageWidthPixel = 0;
  29. private int PageHeightPixel = 0;
  30. private RectangleF BackImgRectangleF = RectangleF.Empty;
  31. private float _allOffsetX = 0;
  32. private float _allOffsetY = 0;
  33. private int _printPageNum = 1; // 打印页的当前页码
  34. private int _printTotalPageNum = 1; // 总页数
  35. private DataRow _printData = null;
  36. private DataTable _printGridData = null;
  37. private PrintDocument _printDocument = null;
  38. /// <summary>
  39. /// 0:正常,1:旋转180,
  40. /// </summary>
  41. private int _printDirectionKind = 0;
  42. #endregion
  43. #region 构造函数
  44. static InvoiceLayoutPrinter()
  45. {
  46. Rijndael age = Rijndael.Create();
  47. _decryptor = age.CreateDecryptor(KEY, IV);
  48. }
  49. private InvoiceLayoutPrinter()
  50. {
  51. }
  52. #endregion
  53. private void printDocument_BeginPrint(object sender, PrintEventArgs e)
  54. {
  55. }
  56. private void PrintDocument_PrintPage(object sender, PrintPageEventArgs e)
  57. {
  58. try
  59. {
  60. ////Logger.Debug("打印开始");
  61. e.Graphics.PageUnit = GraphicsUnit.Millimeter;
  62. e.HasMorePages = DrawPage(e.Graphics);
  63. //Logger.Debug("打印结束");
  64. }
  65. catch (Exception ex)
  66. {
  67. Logger.Error(ex);
  68. }
  69. }
  70. public static List<Image> Print(byte[] layout, DataRow data, DataTable detail)
  71. {
  72. if (layout == null || layout.Length == 0)
  73. {
  74. throw new System.ArgumentNullException("layout");
  75. }
  76. if (data == null)
  77. {
  78. throw new System.ArgumentNullException("data");
  79. }
  80. //if (detail == null || detail.Rows.Count == 0)
  81. //{
  82. // throw new System.ArgumentNullException("detail");
  83. //}
  84. try
  85. {
  86. InvoiceLayoutPrinter printer = new InvoiceLayoutPrinter();
  87. printer.LayoutItems = new List<LayoutItem>();
  88. printer.ReadLayout(layout);
  89. printer._printData = data;
  90. printer._printGridData = detail;
  91. //return printer.DrawImages().ToArray();
  92. return printer.DrawImages();
  93. }
  94. catch (Exception ex)
  95. {
  96. throw ex;
  97. }
  98. }
  99. public static void Print(string printerName, short copies, byte[] layout, DataRow data, DataTable detail, int printDirectionKind = 0)
  100. {
  101. // 复用打印对象模式
  102. PrintReuse(printerName, copies, layout, data, detail, printDirectionKind);
  103. return;
  104. //Logger.Debug("打印开始0");
  105. if (layout == null || layout.Length == 0)
  106. {
  107. throw new System.ArgumentNullException("layout");
  108. }
  109. if (data == null)
  110. {
  111. throw new System.ArgumentNullException("data");
  112. }
  113. //if (detail == null || detail.Rows.Count == 0)
  114. //{
  115. // throw new System.ArgumentNullException("detail");
  116. //}
  117. string ex_pp = "异常位置:0";
  118. try
  119. {
  120. //Logger.Debug("打印开始1");
  121. InvoiceLayoutPrinter printer = new InvoiceLayoutPrinter();
  122. printer._printDirectionKind = printDirectionKind;
  123. printer.LayoutItems = new List<LayoutItem>();
  124. printer.ReadLayout(layout);
  125. printer._printData = data;
  126. printer._printGridData = detail;
  127. //Logger.Debug("打印开始2");
  128. ex_pp = "异常位置:1";
  129. using (printer._printDocument = new PrintDocument())
  130. {
  131. ex_pp = "异常位置:2";
  132. printer._printDocument.DocumentName = "LayoutDocument";
  133. printer._printDocument.BeginPrint += new PrintEventHandler(printer.printDocument_BeginPrint);
  134. printer._printDocument.PrintPage += new PrintPageEventHandler(printer.PrintDocument_PrintPage);
  135. printer._printDocument.PrinterSettings.PrinterName = printerName;
  136. printer._printDocument.PrinterSettings.Copies = copies;
  137. ex_pp = "异常位置:3";
  138. //Logger.Debug("打印开始3");
  139. printer._printDocument.Print();
  140. //Logger.Debug("打印开始4");
  141. ex_pp = "异常位置:4";
  142. //return printer.DrawImages().ToArray();
  143. //return printer.DrawImages();
  144. }
  145. ex_pp = "异常位置:5";
  146. //GC.Collect();
  147. }
  148. catch (Exception ex)
  149. {
  150. //throw ex;
  151. throw new Exception(ex_pp, ex);
  152. }
  153. }
  154. public static Dictionary<string, InvoiceLayoutPrinter> PPSS = new Dictionary<string, InvoiceLayoutPrinter>();
  155. public static void PrintReuse(string printerName, short copies, byte[] layout, DataRow data, DataTable detail, int printDirectionKind = 0)
  156. {
  157. //Logger.Debug("打印开始0");
  158. if (layout == null || layout.Length == 0)
  159. {
  160. throw new System.ArgumentNullException("layout");
  161. }
  162. if (data == null)
  163. {
  164. throw new System.ArgumentNullException("data");
  165. }
  166. //if (detail == null || detail.Rows.Count == 0)
  167. //{
  168. // throw new System.ArgumentNullException("detail");
  169. //}
  170. string ex_pp = "异常位置:10";
  171. try
  172. {
  173. //Logger.Debug("打印开始1");
  174. InvoiceLayoutPrinter printer;
  175. lock (PPSS)
  176. {
  177. if (PPSS.ContainsKey(printerName))
  178. {
  179. ex_pp = "异常位置:11";
  180. printer = PPSS[printerName];
  181. }
  182. else
  183. {
  184. ex_pp = "异常位置:12";
  185. printer = new InvoiceLayoutPrinter();
  186. ex_pp = "异常位置:12-1";
  187. printer._printDocument = new PrintDocument();
  188. printer._printDocument.DocumentName = "LayoutDocument";
  189. //printer._printDocument.BeginPrint += new PrintEventHandler(printer.printDocument_BeginPrint);
  190. printer._printDocument.PrintPage += new PrintPageEventHandler(printer.PrintDocument_PrintPage);
  191. ex_pp = "异常位置:12-2";
  192. printer._printDocument.PrinterSettings.PrinterName = printerName;
  193. PPSS.Add(printerName, printer);
  194. }
  195. }
  196. lock (printer)
  197. {
  198. ex_pp = "异常位置:13";
  199. printer._printDirectionKind = printDirectionKind;
  200. printer.LayoutItems = new List<LayoutItem>();
  201. printer.ReadLayoutReuse(layout);
  202. printer._printData = data;
  203. printer._printGridData = detail;
  204. printer._printDocument.PrinterSettings.Copies = copies;
  205. //Logger.Debug("打印开始2");
  206. ex_pp = "异常位置:14";
  207. printer._printDocument.Print();
  208. }
  209. ex_pp = "异常位置:15";
  210. //GC.Collect();
  211. }
  212. catch (Exception ex)
  213. {
  214. //throw ex;
  215. throw new Exception(ex_pp, ex);
  216. }
  217. }
  218. #region 票据打印
  219. private void ReadLayout(byte[] layout)
  220. {
  221. try
  222. {
  223. using (MemoryStream stream = new MemoryStream(layout))
  224. {
  225. stream.Position = 0;
  226. // 反序列化
  227. BinaryFormatter formatter = new BinaryFormatter();
  228. // TODO 解密读取
  229. //if (CryptoStream)
  230. if (true)
  231. {
  232. //using (CryptoStream cStream = new CryptoStream(stream, _decryptor, CryptoStreamMode.Read))
  233. Rijndael age = Rijndael.Create();
  234. using (CryptoStream cStream = new CryptoStream(stream, age.CreateDecryptor(KEY, IV), CryptoStreamMode.Read))
  235. {
  236. this.Property = (PropertyOfLayoutBox)formatter.Deserialize(cStream);
  237. }
  238. }
  239. else
  240. {
  241. this.Property = (PropertyOfLayoutBox)formatter.Deserialize(stream);
  242. }
  243. //_lastItemRF = propertyOflayoutBox.LastItemRF;
  244. //_newItemID = propertyOflayoutBox.NewItemID;
  245. //_paperHeight = propertyOflayoutBox.PaperHeight;
  246. //_paperWidth = propertyOflayoutBox.PaperWidth;
  247. //if (_zoomType != ZoomType.Whole)
  248. //{
  249. // ChangePaperSize();
  250. //}
  251. //else
  252. //{
  253. // InitializeZoom();
  254. //}
  255. //paperArea.BackgroundImage = propertyOflayoutBox.BackgroundImage;
  256. //_backgroundImageName = propertyOflayoutBox.BackgroundImageName;
  257. //_isPrintBackground = propertyOflayoutBox.PrintBackground;
  258. //_isPrintAreaVisible = propertyOflayoutBox.PrintAreaVisible;
  259. ArrayList items = this.Property.Items;
  260. if (items != null && 0 < items.Count)
  261. {
  262. foreach (LayoutItem item in items)
  263. {
  264. //item.Init(this, false);
  265. //item.Init();
  266. this.LayoutItems.Add(item);
  267. if (item.ItemType == ItemType.Grid)
  268. {
  269. this.OneGridItem = item as GridItem;
  270. if (this.OneGridItem.HeadRowsHeight == 0)
  271. {
  272. this.OneGridItem.HeadRowsHeight = this.OneGridItem.RowsHeight;
  273. }
  274. }
  275. }
  276. }
  277. //paperArea.Select();
  278. //RefreshItemsBound();
  279. ////paperArea.Refresh();
  280. //return true;
  281. }
  282. }
  283. catch (Exception ex)
  284. {
  285. throw ex;
  286. }
  287. }
  288. private void ReadLayoutReuse(byte[] layout)
  289. {
  290. try
  291. {
  292. using (MemoryStream stream = new MemoryStream(layout))
  293. {
  294. stream.Position = 0;
  295. // 反序列化
  296. BinaryFormatter formatter = new BinaryFormatter();
  297. // TODO 解密读取
  298. //if (CryptoStream)
  299. if (true)
  300. {
  301. using (CryptoStream cStream = new CryptoStream(stream, _decryptor, CryptoStreamMode.Read))
  302. //Rijndael age = Rijndael.Create();
  303. //using (CryptoStream cStream = new CryptoStream(stream, age.CreateDecryptor(KEY, IV), CryptoStreamMode.Read))
  304. {
  305. this.Property = (PropertyOfLayoutBox)formatter.Deserialize(cStream);
  306. }
  307. }
  308. else
  309. {
  310. this.Property = (PropertyOfLayoutBox)formatter.Deserialize(stream);
  311. }
  312. //_lastItemRF = propertyOflayoutBox.LastItemRF;
  313. //_newItemID = propertyOflayoutBox.NewItemID;
  314. //_paperHeight = propertyOflayoutBox.PaperHeight;
  315. //_paperWidth = propertyOflayoutBox.PaperWidth;
  316. //if (_zoomType != ZoomType.Whole)
  317. //{
  318. // ChangePaperSize();
  319. //}
  320. //else
  321. //{
  322. // InitializeZoom();
  323. //}
  324. //paperArea.BackgroundImage = propertyOflayoutBox.BackgroundImage;
  325. //_backgroundImageName = propertyOflayoutBox.BackgroundImageName;
  326. //_isPrintBackground = propertyOflayoutBox.PrintBackground;
  327. //_isPrintAreaVisible = propertyOflayoutBox.PrintAreaVisible;
  328. ArrayList items = this.Property.Items;
  329. if (items != null && 0 < items.Count)
  330. {
  331. foreach (LayoutItem item in items)
  332. {
  333. //item.Init(this, false);
  334. //item.Init();
  335. this.LayoutItems.Add(item);
  336. if (item.ItemType == ItemType.Grid)
  337. {
  338. this.OneGridItem = item as GridItem;
  339. if (this.OneGridItem.HeadRowsHeight == 0)
  340. {
  341. this.OneGridItem.HeadRowsHeight = this.OneGridItem.RowsHeight;
  342. }
  343. }
  344. }
  345. }
  346. //paperArea.Select();
  347. //RefreshItemsBound();
  348. ////paperArea.Refresh();
  349. //return true;
  350. }
  351. }
  352. catch (Exception ex)
  353. {
  354. throw ex;
  355. }
  356. }
  357. private List<Image> DrawImages()
  358. {
  359. this.PageWidthPixel = LayoutCommon.MillimeterToPixel(this.Property.PaperWidth);
  360. this.PageHeightPixel = LayoutCommon.MillimeterToPixel(this.Property.PaperHeight);
  361. this._printPageNum = 1;
  362. this._printTotalPageNum = 1;
  363. //if (this.OneGridItem != null)
  364. //{
  365. // foreach (LayoutItem item in this.LayoutItems)
  366. // {
  367. // if (item.ItemType == ItemType.Grid)
  368. // {
  369. // GridItem gItem = (item as GridItem);
  370. // gItem.PrintGridSource = this._printGridData;
  371. // _printTotalPageNum = Math.Max(_printTotalPageNum, gItem.PrintTotalPageNum);
  372. // }
  373. // }
  374. //}
  375. List<Image> images = new List<Image>();
  376. //Bitmap pageImage = null;
  377. //Graphics pageGraphics = null;
  378. bool hasNextPage = true;
  379. while (hasNextPage)
  380. {
  381. Bitmap pageImage = new Bitmap(this.PageWidthPixel, this.PageHeightPixel);
  382. pageImage.SetResolution(LayoutCommon.DPI, LayoutCommon.DPI);
  383. using (Graphics pageGraphics = Graphics.FromImage(pageImage))
  384. {
  385. // TODO 白色背景
  386. pageGraphics.Clear(Color.White);
  387. //pageGraphics.FillRectangle(Brushes.White, pageGraphics.ClipBounds);
  388. pageGraphics.PageUnit = GraphicsUnit.Millimeter;
  389. hasNextPage = DrawPage(pageGraphics);
  390. images.Add(pageImage);
  391. }
  392. }
  393. return images;
  394. }
  395. private bool DrawPage(Graphics graphics)
  396. {
  397. if (_printDirectionKind == 1)
  398. {
  399. //System.Drawing.Drawing2D.Matrix mtxSave = e.Graphics.Transform;
  400. //Matrix mtxRotate = layoutGraphics.Transform;
  401. //mtxRotate.Rotate(180);
  402. //e.Graphics.Transform.Rotate(180);
  403. graphics.TranslateTransform(this.Property.PaperWidth, this.Property.PaperHeight);
  404. graphics.RotateTransform(180);
  405. }
  406. //try
  407. {
  408. // 背景打印
  409. if (this.Property.PrintBackground)
  410. {
  411. PrintBackgroundImage(graphics);
  412. }
  413. // Item打印
  414. foreach (LayoutItem layoutItem in this.LayoutItems)
  415. {
  416. switch (layoutItem.ItemType)
  417. {
  418. case ItemType.Text:
  419. DrawTextItem(graphics, (TextItem)layoutItem);
  420. break;
  421. case ItemType.Image:
  422. DrawImageItem(graphics, (ImageItem)layoutItem);
  423. break;
  424. case ItemType.Rectangle:
  425. DrawRectangleItem(graphics, (RectangleItem)layoutItem);
  426. break;
  427. case ItemType.Ellipse:
  428. DrawEllipseItem(graphics, (EllipseItem)layoutItem);
  429. break;
  430. //case ItemType.Line:
  431. // DrawLineItem(graphics, (LineItem)layoutItem);
  432. // break;
  433. case ItemType.Grid:
  434. DrawGridItem(graphics, (GridItem)layoutItem);
  435. break;
  436. case ItemType.PageNum:
  437. DrawPageNumItem(graphics, (PageNumItem)layoutItem);
  438. break;
  439. case ItemType.TotalText:
  440. DrawTotalTextItem(graphics, (TotalTextItem)layoutItem);
  441. break;
  442. default:
  443. //throw new NotImplementedException();
  444. break;
  445. }
  446. }
  447. // 表格Item是否有下一页
  448. if (_printPageNum < _printTotalPageNum)
  449. {
  450. _printPageNum++;
  451. //e.HasMorePages = true;
  452. return true;
  453. }
  454. return false;
  455. }
  456. //catch (Exception ex)
  457. //{
  458. // throw ex;
  459. //}
  460. }
  461. #region 绘制
  462. /// <summary>
  463. /// 打印背景图片
  464. /// </summary>
  465. /// <param name="graphics">用于绘制的System.Drawing.Graphics</param>
  466. private void PrintBackgroundImage(Graphics graphics)
  467. {
  468. if (this.Property.BackgroundImage != null)
  469. {
  470. if (this.BackImgRectangleF.IsEmpty)
  471. {
  472. // 背景图片的高宽比例
  473. float imgWHRate = (float)this.Property.BackgroundImage.Width / (float)this.Property.BackgroundImage.Height;
  474. // 打印纸张的高宽比例
  475. float paperWHRate = this.Property.PaperWidth / this.Property.PaperHeight;
  476. // 缩放背景图片,但保持图片高宽比例
  477. if (imgWHRate > paperWHRate)
  478. {
  479. BackImgRectangleF.Width = this.Property.PaperWidth;
  480. BackImgRectangleF.Height = this.Property.PaperWidth / imgWHRate;
  481. BackImgRectangleF.X = 0;
  482. BackImgRectangleF.Y = (this.Property.PaperHeight - BackImgRectangleF.Height) / 2 + 0;
  483. }
  484. else
  485. {
  486. BackImgRectangleF.Width = this.Property.PaperHeight * imgWHRate;
  487. BackImgRectangleF.Height = this.Property.PaperHeight;
  488. BackImgRectangleF.X = (this.Property.PaperWidth - BackImgRectangleF.Width) / 2 + 0;
  489. BackImgRectangleF.Y = 0;
  490. }
  491. }
  492. // 在指定的范围绘制背景图片
  493. graphics.DrawImage(this.Property.BackgroundImage, BackImgRectangleF);
  494. }
  495. }
  496. /// <summary>
  497. /// 打印文本Item
  498. /// </summary>
  499. /// <param name="graphics">用于绘制的System.Drawing.Graphics</param>
  500. /// <param name="textItem">文本Item</param>
  501. private void DrawTextItem(Graphics graphics, TextItem textItem)
  502. {
  503. // 文本Item打印范围
  504. RectangleF rectangleFM = new RectangleF(
  505. _allOffsetX + textItem.Left,
  506. _allOffsetY + textItem.Top,
  507. textItem.Width,
  508. textItem.Height);
  509. RectangleF rectangleF = rectangleFM;
  510. rectangleF.Inflate(0 - LayoutConsts.TEXT_MARGIN, 0 - LayoutConsts.TEXT_MARGIN);
  511. rectangleFM.Height -= LayoutConsts.TEXT_MARGIN;
  512. object value = null;
  513. if (this._printData.Table.Columns.Contains(textItem.DataMember))
  514. {
  515. value = this._printData[textItem.DataMember];
  516. }
  517. textItem.PrintTextItem(value, graphics, rectangleFM, rectangleF);
  518. }
  519. /// <summary>
  520. /// 打印图片Item
  521. /// </summary>
  522. /// <param name="graphics">用于绘制的System.Drawing.Graphics</param>
  523. /// <param name="imageItem">图片Item</param>
  524. private void DrawImageItem(Graphics graphics, ImageItem imageItem)
  525. {
  526. // 图片Item打印范围
  527. RectangleF rectangleF = new RectangleF(
  528. _allOffsetX + imageItem.Left,
  529. _allOffsetY + imageItem.Top,
  530. imageItem.Width,
  531. imageItem.Height);
  532. // 在指定的范围绘制图片Item
  533. LayoutUtility.DrawImage(graphics, rectangleF, imageItem.Image);
  534. }
  535. /// <summary>
  536. /// 打印矩形Item
  537. /// </summary>
  538. /// <param name="graphics">用于绘制的System.Drawing.Graphics</param>
  539. /// <param name="rectangleItem">矩形Item</param>
  540. private void DrawRectangleItem(Graphics graphics, RectangleItem rectangleItem)
  541. {
  542. // 矩形Item打印范围
  543. RectangleF rectangleF = new RectangleF(
  544. _allOffsetX + rectangleItem.Left,
  545. _allOffsetY + rectangleItem.Top,
  546. rectangleItem.Width,
  547. rectangleItem.Height);
  548. float lineWidthDraw = rectangleItem.LineWidth;
  549. // 在指定的范围绘制矩形Item
  550. LayoutUtility.DrawRectangle(graphics, rectangleF, rectangleItem.LineColor,
  551. rectangleItem.FillColor, lineWidthDraw, rectangleItem.Transparent);
  552. }
  553. /// <summary>
  554. /// 打印椭圆Item
  555. /// </summary>
  556. /// <param name="graphics">用于绘制的System.Drawing.Graphics</param>
  557. /// <param name="ellipseItem">椭圆Item</param>
  558. private void DrawEllipseItem(Graphics graphics, EllipseItem ellipseItem)
  559. {
  560. // 椭圆Item打印范围
  561. RectangleF rectangleF = new RectangleF(
  562. _allOffsetX + ellipseItem.Left,
  563. _allOffsetY + ellipseItem.Top,
  564. ellipseItem.Width,
  565. ellipseItem.Height);
  566. float lineWidthDraw = ellipseItem.LineWidth;
  567. // 在指定的范围绘制椭圆Item
  568. LayoutUtility.DrawEllipse(graphics, rectangleF, ellipseItem.LineColor,
  569. ellipseItem.FillColor, lineWidthDraw, ellipseItem.Transparent);
  570. }
  571. /// <summary>
  572. /// 打印表格Item
  573. /// </summary>
  574. /// <param name="graphics">用于绘制的System.Drawing.Graphics</param>
  575. /// <param name="gridItem">表格Item</param>
  576. private void DrawGridItem(Graphics graphics, GridItem gridItem)
  577. {
  578. // 表格Item打印范围
  579. RectangleF rectangleF = new RectangleF(
  580. _allOffsetX + gridItem.Left,
  581. _allOffsetY + gridItem.Top,
  582. gridItem.Width,
  583. gridItem.Height);
  584. // 在指定的范围绘制表格Item
  585. //gridItem.DrawGridItem(graphics, rectangleF, 1, _printPageNum);
  586. gridItem.PrintGridItem(graphics, rectangleF, _printPageNum);
  587. }
  588. /// <summary>
  589. /// 打印页码Item
  590. /// </summary>
  591. /// <param name="graphics">用于绘制的System.Drawing.Graphics</param>
  592. /// <param name="pageNumItem">页码Item</param>
  593. private void DrawPageNumItem(Graphics graphics, PageNumItem pageNumItem)
  594. {
  595. // 文本Item打印范围
  596. RectangleF rectangleFM = new RectangleF(
  597. _allOffsetX + pageNumItem.Left,
  598. _allOffsetY + pageNumItem.Top,
  599. pageNumItem.Width,
  600. pageNumItem.Height);
  601. RectangleF rectangleF = rectangleFM;
  602. rectangleF.Inflate(0 - LayoutConsts.TEXT_MARGIN, 0 - LayoutConsts.TEXT_MARGIN);
  603. rectangleFM.Height -= LayoutConsts.TEXT_MARGIN;
  604. // 在指定的范围绘制文本Item
  605. LayoutUtility.DrawText(graphics,
  606. rectangleFM,
  607. rectangleF,
  608. pageNumItem.GetPrintText(this._printPageNum, this._printTotalPageNum),
  609. pageNumItem.Font,
  610. pageNumItem.TextColor,
  611. 0,
  612. 0,
  613. 0,
  614. pageNumItem.TextAlign,
  615. pageNumItem.TextAlignVertical,
  616. false,
  617. false,
  618. false);
  619. }
  620. /// <summary>
  621. /// 打印合计Item
  622. /// </summary>
  623. /// <param name="graphics">用于绘制的System.Drawing.Graphics</param>
  624. /// <param name="totalTextItem">合计Item</param>
  625. private void DrawTotalTextItem(Graphics graphics, TotalTextItem totalTextItem)
  626. {
  627. // 文本Item打印范围
  628. RectangleF rectangleFM = new RectangleF(
  629. _allOffsetX + totalTextItem.Left,
  630. _allOffsetY + totalTextItem.Top,
  631. totalTextItem.Width,
  632. totalTextItem.Height);
  633. RectangleF rectangleF = rectangleFM;
  634. rectangleF.Inflate(0 - LayoutConsts.TEXT_MARGIN, 0 - LayoutConsts.TEXT_MARGIN);
  635. rectangleFM.Height -= LayoutConsts.TEXT_MARGIN;
  636. if (_printPageNum != _printTotalPageNum && totalTextItem.NotTotalValue != "[#合计值#]")
  637. {
  638. LayoutUtility.DrawText(graphics,
  639. rectangleFM,
  640. rectangleF,
  641. totalTextItem.NotTotalValue,
  642. totalTextItem.NotTotalFont,
  643. totalTextItem.NotTotalColor,
  644. totalTextItem.LineSpace,
  645. totalTextItem.CharacterSpace,
  646. totalTextItem.CharacterCount,
  647. totalTextItem.NotTotalAlign,
  648. totalTextItem.NotTotalAlignVertical,
  649. totalTextItem.Wrap,
  650. totalTextItem.Clip,
  651. false);
  652. return;
  653. }
  654. // 在指定的范围绘制文本Item
  655. object value = null;
  656. if (this._printData.Table.Columns.Contains(totalTextItem.DataMember))
  657. {
  658. value = this._printData[totalTextItem.DataMember];
  659. }
  660. value = totalTextItem.ResetTextValue(value, true);
  661. LayoutUtility.DrawText(graphics,
  662. rectangleFM,
  663. rectangleF,
  664. (value == null? "": value.ToString()),
  665. totalTextItem.Font,
  666. totalTextItem.TextColor,
  667. totalTextItem.LineSpace,
  668. totalTextItem.CharacterSpace,
  669. totalTextItem.CharacterCount,
  670. totalTextItem.TextAlign,
  671. totalTextItem.NotTotalAlignVertical,
  672. totalTextItem.Wrap,
  673. totalTextItem.Clip,
  674. false);
  675. }
  676. #endregion
  677. #endregion
  678. }
  679. }