TextItem.cs 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341
  1. /*******************************************************************************
  2. * Copyright(c) 2012 dongke All rights reserved. / Confidential
  3. * 类的信息:
  4. * 1.程序名称:TextItem.cs
  5. * 2.功能描述: 文本Item
  6. * 编辑履历:
  7. * 作者 日期 版本 修改内容
  8. * 欧阳涛 2012/09/14 1.00 新建
  9. *******************************************************************************/
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Drawing;
  13. using System.Globalization;
  14. using System.IO;
  15. using Curtain.Framework.Barcode;
  16. using Curtain.Framework.Barcode.OneD;
  17. using Curtain.Framework.Barcode.QRCode;
  18. using ZXing;
  19. using ZXing.Common;
  20. using ZXing.QrCode;
  21. using ZXing.QrCode.Internal;
  22. namespace Dongke.WinForm.Controls.InvoiceLayout
  23. {
  24. [Serializable]
  25. public class TextItem : LayoutItem, IBoundItem
  26. {
  27. #region 成员变量
  28. private string _dataMember; // 对应数据源中的字段名
  29. private string _displayValue; // 编辑模式显示的值
  30. private object _formatType; // 格式类别
  31. private string _format; // 文本格式
  32. private Font _font; // 文本字体
  33. private bool _isUnderline; // 下划线
  34. private bool _isStrikeout; // 删除线
  35. private bool _wrap; // 是否自动换行
  36. private bool _clip; // 是否剪裁延伸到边框外侧的文本
  37. private Color _textColor; // 文本颜色
  38. private TextAlignment _textAlign; // 文本对齐方式
  39. private TextAlignment _textAlignVertical; // 文本垂直对齐
  40. private float _characterSpace; // 文字间隔
  41. private float _lineSpace; // 文字行间隔
  42. private int _characterCount; // 行文字数
  43. private string _defaultValue; // 文本默认值
  44. private string _culture; // culture
  45. private string _defaultItemName; // 默认项目名
  46. private string _textItemName; // 文本项目名
  47. private bool _isDataBinding; // 是否数据绑定
  48. //[NonSerialized]
  49. //private string _text; // 文本
  50. //[NonSerialized]
  51. //private object _dataValue; // 数据源中的值
  52. //[NonSerialized]
  53. //private object _printDataValue; // 打印数据源中的值
  54. //[NonSerialized]
  55. //private DataBoundField _boundField; // 数据源字段
  56. //[NonSerialized]
  57. //private DataBoundField _printBoundField; // 打印数据源字段
  58. //[NonSerialized]
  59. //private Font _fontDraw; // 绘制字体
  60. //[NonSerialized]
  61. //private float _lineSpaceDraw; // 绘制行间隔
  62. //[NonSerialized]
  63. //private float _characterSpaceDraw; // 绘制文字间隔
  64. //[NonSerialized]
  65. //protected RectangleF _marginRectangleFDraw; // 绘制范围
  66. private int _travelDocItemID; // 项目ID
  67. private string _itemCode; // 项目ID
  68. private int _itemRefClass; // 分类
  69. private ItemStyle _itemStyle; // 项目类别
  70. private string _itemSample; // 项目示例
  71. #endregion 成员变量
  72. #region 属性
  73. /// <summary>
  74. /// 对应数据源中的字段名
  75. /// </summary>
  76. public string DataMember
  77. {
  78. get
  79. {
  80. return _dataMember;
  81. }
  82. set
  83. {
  84. if (string.IsNullOrEmpty(value))
  85. {
  86. value = string.Empty;
  87. }
  88. if (!value.Equals(_dataMember))
  89. {
  90. _dataMember = value;
  91. //if (Owner != null)
  92. //{
  93. // if (Owner.LayoutMode == LayoutMode.Preview)
  94. // {
  95. // Owner.RefreshTextItemBound(this);
  96. // }
  97. // Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  98. //}
  99. }
  100. }
  101. }
  102. /// <summary>
  103. /// 编辑模式显示的值
  104. /// </summary>
  105. public string DisplayValue
  106. {
  107. get
  108. {
  109. return _displayValue;
  110. }
  111. set
  112. {
  113. if (string.IsNullOrEmpty(value))
  114. {
  115. value = string.Empty;
  116. }
  117. if (!value.Equals(_displayValue))
  118. {
  119. _displayValue = value;
  120. //if (Owner != null)
  121. //{
  122. // Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  123. //}
  124. }
  125. }
  126. }
  127. /// <summary>
  128. /// 格式类别
  129. /// </summary>
  130. public object FormatType
  131. {
  132. get
  133. {
  134. return _formatType;
  135. }
  136. set
  137. {
  138. _formatType = value;
  139. //if (Owner != null)
  140. //{
  141. // Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  142. //}
  143. }
  144. }
  145. /// <summary>
  146. /// 文本格式
  147. /// </summary>
  148. public string Format
  149. {
  150. get
  151. {
  152. return _format;
  153. }
  154. set
  155. {
  156. if (string.IsNullOrEmpty(value))
  157. {
  158. value = string.Empty;
  159. }
  160. if (!value.Equals(_format))
  161. {
  162. _format = value;
  163. //if (Owner != null)
  164. //{
  165. // Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  166. //}
  167. }
  168. }
  169. }
  170. /// <summary>
  171. /// 文本字体
  172. /// </summary>
  173. public Font Font
  174. {
  175. get
  176. {
  177. return _font;
  178. }
  179. set
  180. {
  181. if (!_font.Equals(value))
  182. {
  183. _font = value;
  184. _isUnderline = _font.Underline;
  185. _isStrikeout = _font.Strikeout;
  186. //float zoom = base.GetZoom();
  187. //_fontDraw = new Font(_font.FontFamily, _font.Size * zoom, _font.Style & (FontStyle.Regular | FontStyle.Bold | FontStyle.Italic));
  188. //if (Owner != null)
  189. //{
  190. // Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  191. //}
  192. }
  193. }
  194. }
  195. /// <summary>
  196. /// 是否自动文本换行
  197. /// </summary>
  198. public bool Wrap
  199. {
  200. get
  201. {
  202. return _wrap;
  203. }
  204. set
  205. {
  206. if (_wrap != value)
  207. {
  208. _wrap = value;
  209. _clip = !_wrap;
  210. //if (Owner != null)
  211. //{
  212. // Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  213. //}
  214. }
  215. }
  216. }
  217. /// <summary>
  218. /// 是否剪裁延伸到边框外侧的文本
  219. /// </summary>
  220. public bool Clip
  221. {
  222. get
  223. {
  224. _clip = !_wrap;
  225. return _clip;
  226. }
  227. }
  228. /// <summary>
  229. /// 文本颜色
  230. /// </summary>
  231. public Color TextColor
  232. {
  233. get
  234. {
  235. return _textColor;
  236. }
  237. set
  238. {
  239. if (!_textColor.Equals(value))
  240. {
  241. _textColor = value;
  242. //if (Owner != null)
  243. //{
  244. // Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  245. //}
  246. }
  247. }
  248. }
  249. /// <summary>
  250. /// 文本对齐方式
  251. /// </summary>
  252. public TextAlignment TextAlign
  253. {
  254. get
  255. {
  256. return _textAlign;
  257. }
  258. set
  259. {
  260. if (_textAlign != value)
  261. {
  262. _textAlign = value;
  263. //if (Owner != null)
  264. //{
  265. // Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  266. //}
  267. }
  268. }
  269. }
  270. /// <summary>
  271. /// 文本垂直对齐方式
  272. /// </summary>
  273. public TextAlignment TextAlignVertical
  274. {
  275. get
  276. {
  277. return _textAlignVertical;
  278. }
  279. set
  280. {
  281. if (_textAlignVertical != value)
  282. {
  283. _textAlignVertical = value;
  284. //if (Owner != null)
  285. //{
  286. // Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  287. //}
  288. }
  289. }
  290. }
  291. /// <summary>
  292. /// 文字间隔
  293. /// </summary>
  294. public float CharacterSpace
  295. {
  296. get
  297. {
  298. return _characterSpace;
  299. }
  300. set
  301. {
  302. if (_characterSpace != value)
  303. {
  304. _characterSpace = value;
  305. //if (Owner != null)
  306. //{
  307. // Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  308. //}
  309. }
  310. }
  311. }
  312. /// <summary>
  313. /// 文字行间隔
  314. /// </summary>
  315. public float LineSpace
  316. {
  317. get
  318. {
  319. return _lineSpace;
  320. }
  321. set
  322. {
  323. if (_lineSpace != value)
  324. {
  325. _lineSpace = value;
  326. //if (Owner != null)
  327. //{
  328. // Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  329. //}
  330. }
  331. }
  332. }
  333. /// <summary>
  334. /// 行文字数
  335. /// </summary>
  336. public int CharacterCount
  337. {
  338. get
  339. {
  340. return _characterCount;
  341. }
  342. set
  343. {
  344. if (_characterCount != value)
  345. {
  346. _characterCount = value;
  347. //if (Owner != null)
  348. //{
  349. // Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  350. //}
  351. }
  352. }
  353. }
  354. /// <summary>
  355. /// 文本默认值
  356. /// </summary>
  357. public string DefaultValue
  358. {
  359. get
  360. {
  361. return _defaultValue;
  362. }
  363. set
  364. {
  365. if (string.IsNullOrEmpty(value))
  366. {
  367. value = string.Empty;
  368. }
  369. if (!value.Equals(_defaultValue))
  370. {
  371. _defaultValue = value;
  372. //if (Owner != null)
  373. //{
  374. // Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  375. //}
  376. }
  377. }
  378. }
  379. /// <summary>
  380. /// culture
  381. /// </summary>
  382. public string Culture
  383. {
  384. get
  385. {
  386. return _culture;
  387. }
  388. set
  389. {
  390. if (string.IsNullOrEmpty(value))
  391. {
  392. value = string.Empty;
  393. }
  394. if (!value.Equals(_culture))
  395. {
  396. _culture = value;
  397. //if (Owner != null)
  398. //{
  399. // Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  400. //}
  401. }
  402. }
  403. }
  404. /// <summary>
  405. /// 文本项目名
  406. /// </summary>
  407. public string TextItemName
  408. {
  409. get
  410. {
  411. return _textItemName;
  412. }
  413. set
  414. {
  415. if (string.IsNullOrEmpty(value))
  416. {
  417. value = string.Empty;
  418. }
  419. if (!value.Equals(_textItemName))
  420. {
  421. _textItemName = value;
  422. //if (Owner != null)
  423. //{
  424. // Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  425. //}
  426. }
  427. }
  428. }
  429. /// <summary>
  430. /// 默认项目名
  431. /// </summary>
  432. public string DefaultItemName
  433. {
  434. get
  435. {
  436. return _defaultItemName;
  437. }
  438. set
  439. {
  440. if (string.IsNullOrEmpty(value))
  441. {
  442. value = string.Empty;
  443. }
  444. if (!value.Equals(_defaultItemName))
  445. {
  446. _defaultItemName = value;
  447. //if (Owner != null)
  448. //{
  449. // Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  450. //}
  451. }
  452. }
  453. }
  454. ///// <summary>
  455. ///// 数据源中的值
  456. ///// </summary>
  457. //internal object DataValue
  458. //{
  459. // get
  460. // {
  461. // return _dataValue;
  462. // }
  463. // set
  464. // {
  465. // if (value != _dataValue
  466. // || (value != null && !value.Equals(_dataValue)))
  467. // {
  468. // _dataValue = value;
  469. // }
  470. // }
  471. //}
  472. ///// <summary>
  473. ///// 打印数据源中的值
  474. ///// </summary>
  475. //internal object PrintDataValue
  476. //{
  477. // get
  478. // {
  479. // return _printDataValue;
  480. // }
  481. // set
  482. // {
  483. // if (value != _printDataValue
  484. // || (value != null && !value.Equals(_printDataValue)))
  485. // {
  486. // _printDataValue = value;
  487. // }
  488. // }
  489. //}
  490. /// <summary>
  491. /// 是否数据绑定
  492. /// </summary>
  493. internal bool IsDataBinding
  494. {
  495. get
  496. {
  497. return _isDataBinding;
  498. }
  499. set
  500. {
  501. _isDataBinding = value;
  502. }
  503. }
  504. ///// <summary>
  505. ///// 数据源字段
  506. ///// </summary>
  507. //internal DataBoundField BoundField
  508. //{
  509. // get
  510. // {
  511. // return _boundField;
  512. // }
  513. // set
  514. // {
  515. // _boundField = value;
  516. // }
  517. //}
  518. ///// <summary>
  519. ///// 打印数据源字段
  520. ///// </summary>
  521. //internal DataBoundField PrintBoundField
  522. //{
  523. // get
  524. // {
  525. // return _printBoundField;
  526. // }
  527. // set
  528. // {
  529. // _printBoundField = value;
  530. // }
  531. //}
  532. ///// <summary>
  533. ///// 绘制字体
  534. ///// </summary>
  535. //internal Font FontDraw
  536. //{
  537. // get
  538. // {
  539. // return _fontDraw;
  540. // }
  541. //}
  542. ///// <summary>
  543. ///// 绘制文字间隔
  544. ///// </summary>
  545. //internal float CharacterSpaceDraw
  546. //{
  547. // get
  548. // {
  549. // return _characterSpaceDraw;
  550. // }
  551. //}
  552. ///// <summary>
  553. ///// 绘制行间隔
  554. ///// </summary>
  555. //internal float LineSpaceDraw
  556. //{
  557. // get
  558. // {
  559. // return _lineSpaceDraw;
  560. // }
  561. //}
  562. /// <summary>
  563. /// 项目ID
  564. /// </summary>
  565. public int TravelDocItemID
  566. {
  567. get
  568. {
  569. return _travelDocItemID;
  570. }
  571. set
  572. {
  573. if (_travelDocItemID != value)
  574. {
  575. _travelDocItemID = value;
  576. //if (Owner != null)
  577. //{
  578. // Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  579. //}
  580. }
  581. if (0 < _travelDocItemID)
  582. {
  583. _isDataBinding = true;
  584. }
  585. else
  586. {
  587. _isDataBinding = false;
  588. }
  589. }
  590. }
  591. /// <summary>
  592. /// 项目编码
  593. /// </summary>
  594. public string ItemCode
  595. {
  596. get
  597. {
  598. return this._itemCode;
  599. }
  600. set
  601. {
  602. if (this._itemCode != value)
  603. {
  604. this._itemCode = value;
  605. }
  606. if (string.IsNullOrEmpty(this._itemCode))
  607. {
  608. this._isDataBinding = false;
  609. }
  610. else
  611. {
  612. this._isDataBinding = true;
  613. }
  614. }
  615. }
  616. /// <summary>
  617. /// 分类
  618. /// </summary>
  619. public int ItemRefClass
  620. {
  621. get
  622. {
  623. return _itemRefClass;
  624. }
  625. set
  626. {
  627. if (value != _itemRefClass)
  628. {
  629. _itemRefClass = value;
  630. //if (Owner != null)
  631. //{
  632. // Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  633. //}
  634. }
  635. }
  636. }
  637. /// <summary>
  638. /// 项目类别
  639. /// </summary>
  640. public ItemStyle ItemStyle
  641. {
  642. get
  643. {
  644. return _itemStyle;
  645. }
  646. set
  647. {
  648. if (value != _itemStyle)
  649. {
  650. _itemStyle = value;
  651. //this._picCode = null;
  652. //if (Owner != null)
  653. //{
  654. // Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  655. //}
  656. }
  657. }
  658. }
  659. /// <summary>
  660. /// 项目示例
  661. /// </summary>
  662. public string ItemSample
  663. {
  664. get
  665. {
  666. return _itemSample;
  667. }
  668. set
  669. {
  670. if (string.IsNullOrEmpty(value))
  671. {
  672. value = string.Empty;
  673. }
  674. if (!value.Equals(_itemSample))
  675. {
  676. _itemSample = value;
  677. //if (Owner != null)
  678. //{
  679. // Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  680. //}
  681. }
  682. }
  683. }
  684. public bool PicColumn
  685. {
  686. get;
  687. set;
  688. }
  689. public bool PicMargin
  690. {
  691. get;
  692. set;
  693. }
  694. public bool FixedRatio
  695. {
  696. get;
  697. set;
  698. }
  699. #endregion 属性
  700. #region 构造函数
  701. /// <summary>
  702. /// 构造函数
  703. /// </summary>
  704. internal TextItem()
  705. : this(ItemType.Text)
  706. {
  707. }
  708. ///// <summary>
  709. ///// 构造函数
  710. ///// </summary>
  711. //internal TextItem(LayoutBox box)
  712. // : this(box, ItemType.Text)
  713. //{
  714. //}
  715. /// <summary>
  716. /// 构造函数
  717. /// </summary>
  718. internal TextItem(/*LayoutBox box,*/ ItemType itemType)
  719. : base(/*box, */itemType)
  720. {
  721. //_width = LayoutCommon.DefaultTextItemSizeWidth;
  722. //_height = LayoutCommon.DefaultTextItemSizeHeight;
  723. _displayValue = string.Empty;
  724. _format = string.Empty;
  725. _defaultValue = string.Empty;
  726. //_font = LayoutCommon.DefaultItemFont;
  727. //_fontDraw = LayoutCommon.DefaultItemFont;
  728. _textColor = Color.Black;
  729. _textAlign = TextAlignment.LeftOrTop;
  730. _characterSpace = 0f;
  731. _lineSpace = 0f;
  732. _characterCount = 0;
  733. //_dataValue = null;
  734. //_text = string.Empty;
  735. _itemRefClass = 0;
  736. _itemStyle = ItemStyle.Other;
  737. _isDataBinding = false;
  738. //_boundField = new DataBoundField();
  739. //_boundField.IsDataBound = false;
  740. //_boundField.BoundFieldIndex = -1;
  741. //_printBoundField = new DataBoundField();
  742. //_printBoundField.IsDataBound = false;
  743. //_printBoundField.BoundFieldIndex = -1;
  744. //_defaultItemName = LayoutBox.ITEMNAME_FIX + ID;
  745. _textItemName = _defaultItemName;
  746. }
  747. #endregion 构造函数
  748. #region 函数
  749. /// <summary>
  750. /// 格式化数据源中的值
  751. /// </summary>
  752. private string FormatDataValue(object value)
  753. {
  754. string result = null;
  755. if (value != null && value != DBNull.Value)
  756. {
  757. if (_itemStyle == ItemStyle.Date)
  758. {
  759. result = value.ToString();
  760. DateTime dateTime;
  761. if (DateTime.TryParse(result, out dateTime))
  762. {
  763. if (string.IsNullOrEmpty(_format))
  764. {
  765. result = dateTime.ToString(LayoutConsts.DATETIME_FRORMAT);
  766. }
  767. else
  768. {
  769. //if (LayoutConsts.CULTUREINFO_JP.Equals(_culture))
  770. //{
  771. // result = dateTime.ToString(_format, LayoutCommon.CultureInfoJP);
  772. //}
  773. //else
  774. //{
  775. // MMM
  776. if (_format.Contains(LayoutConsts.DATETIME_FRORMAT_MMM))
  777. {
  778. if (string.IsNullOrEmpty(_culture))
  779. {
  780. result = dateTime.ToString(_format).ToUpper();
  781. }
  782. else
  783. {
  784. result = dateTime.ToString(_format, CultureInfo.CreateSpecificCulture(_culture)).ToUpper();
  785. }
  786. }
  787. else if (string.IsNullOrEmpty(_culture))
  788. {
  789. result = dateTime.ToString(_format);
  790. }
  791. else
  792. {
  793. result = dateTime.ToString(_format, CultureInfo.CreateSpecificCulture(_culture));
  794. }
  795. //}
  796. }
  797. }
  798. }
  799. else if (_itemStyle == ItemStyle.Sign)
  800. {
  801. if (string.IsNullOrEmpty(_format))
  802. {
  803. result = value.ToString();
  804. }
  805. else
  806. {
  807. result = _format;
  808. }
  809. }
  810. else
  811. {
  812. result = value.ToString();
  813. }
  814. }
  815. return result;
  816. }
  817. /// <summary>
  818. /// 文本内容设置
  819. /// </summary>
  820. public object ResetTextValue(object value, bool isDataBound)
  821. {
  822. //if (Owner == null)
  823. //{
  824. // return _displayValue;
  825. //}
  826. //if (Owner.LayoutMode == LayoutMode.Preview)
  827. {
  828. if (!string.IsNullOrEmpty(_dataMember))
  829. {
  830. if (isDataBound)
  831. {
  832. //object result = value;//Owner.SetItemValueFormattingArgs(this, value, _format);
  833. if (PicColumn)
  834. {
  835. //if (result != null && result is Image)
  836. //{
  837. // return result;
  838. //}
  839. if (value != null && value is byte[])
  840. {
  841. using (MemoryStream ms = new MemoryStream(value as byte[]))
  842. {
  843. return Image.FromStream(ms);
  844. }
  845. }
  846. return value;
  847. }
  848. //if (result != null)
  849. //{
  850. // return result.ToString();
  851. //}
  852. if (value != null && value != DBNull.Value)
  853. {
  854. return FormatDataValue(value);
  855. }
  856. return _defaultValue;
  857. }
  858. //return _defaultValue;
  859. return "";
  860. }
  861. return _displayValue;
  862. }
  863. //else
  864. //{
  865. // //object result = Owner.SetItemValueFormattingArgs(this, value, _format);
  866. // object result = Owner.SetItemValueFormattingArgs(this, _displayValue, _format);
  867. // if (result != null)
  868. // {
  869. // return result.ToString();
  870. // }
  871. // return _displayValue;
  872. //}
  873. }
  874. /// <summary>
  875. /// Item初始化
  876. /// </summary>
  877. /// <param name="box">LayoutBox</param>
  878. /// <param name="isNewID">是否生成新ID</param>
  879. /// <returns>是否成功</returns>
  880. internal override bool Init(/*LayoutBox box, bool isNewID*/)
  881. {
  882. //if (_fontDraw == null)
  883. //{
  884. // if (_font == null)
  885. // {
  886. // _font = LayoutCommon.DefaultItemFont;
  887. // _fontDraw = LayoutCommon.DefaultItemFont;
  888. // }
  889. // else
  890. // {
  891. // _fontDraw = new Font(_font.FontFamily, _font.Size, _font.Style & (FontStyle.Regular | FontStyle.Bold | FontStyle.Italic));
  892. // }
  893. //}
  894. //_boundField.IsDataBound = false;
  895. //_boundField.BoundFieldIndex = -1;
  896. //_printBoundField.IsDataBound = false;
  897. //_printBoundField.BoundFieldIndex = -1;
  898. bool isInit = base.Init(/*box, isNewID*/);
  899. //if (isNewID)
  900. //{
  901. // _defaultItemName = LayoutBox.ITEMNAME_FIX + ID;
  902. // _textItemName = _defaultItemName;
  903. //}
  904. return isInit;
  905. }
  906. ///// <summary>
  907. ///// Item图形上的OnPaint事件
  908. ///// </summary>
  909. ///// <param name="sender">指定的对象</param>
  910. ///// <param name="e">提供的事件数据</param>
  911. //protected override void OnPaint(object sender, PaintEventArgs e)
  912. //{
  913. // DrawTextItem(_dataValue, _boundField, e.Graphics, RectangleFDraw, _marginRectangleFDraw);
  914. //}
  915. //private void DrawTextItem(object date, DataBoundField dataBoundField, Graphics graphics, RectangleF rectangleM, RectangleF rectangle)
  916. //{
  917. // object value = ResetTextValue(date, dataBoundField.IsDataBound);
  918. // if (value == null)
  919. // {
  920. // return;
  921. // }
  922. // if (PicColumn && value is Image)
  923. // {
  924. // Image image = value as Image;
  925. // if (image != null)
  926. // {
  927. // RectangleF rfp = PicMargin ? rectangle : rectangleM;
  928. // if (FixedRatio)
  929. // {
  930. // // 背景图片的高宽比例
  931. // float imgWHRate = (float)image.Width / (float)image.Height;
  932. // // 打印纸张的高宽比例
  933. // float paperWHRate = rfp.Width / rfp.Height;
  934. // // 背景图片打印范围
  935. // RectangleF newImgRectangleF = RectangleF.Empty;
  936. // // 缩放背景图片,但保持图片高宽比例
  937. // if (imgWHRate > paperWHRate)
  938. // {
  939. // newImgRectangleF.Width = rfp.Width;
  940. // newImgRectangleF.Height = rfp.Width / imgWHRate;
  941. // newImgRectangleF.X = rfp.X;
  942. // newImgRectangleF.Y = (rfp.Height - newImgRectangleF.Height) / 2 + rfp.Y;
  943. // }
  944. // else
  945. // {
  946. // newImgRectangleF.Width = rfp.Height * imgWHRate;
  947. // newImgRectangleF.Height = rfp.Height;
  948. // newImgRectangleF.X = (rfp.Width - newImgRectangleF.Width) / 2 + rfp.X;
  949. // newImgRectangleF.Y = rfp.Y;
  950. // }
  951. // rfp = newImgRectangleF;
  952. // }
  953. // // 在指定的范围绘制背景图片
  954. // LayoutUtility.DrawImage(graphics, GraphicsUnit.Millimeter, rfp, image);
  955. // }
  956. // return;
  957. // }
  958. // LayoutUtility.DrawText(graphics,
  959. // GraphicsUnit.Millimeter,
  960. // rectangleM,
  961. // rectangle,
  962. // value.ToString(),
  963. // _fontDraw,
  964. // _textColor,
  965. // _lineSpaceDraw,
  966. // _characterSpaceDraw,
  967. // _characterCount,
  968. // _textAlign,
  969. // _textAlignVertical,
  970. // _wrap,
  971. // Clip,
  972. // Selected);
  973. //}
  974. public void PrintTextItem(object date, Graphics graphics, RectangleF rectangleM, RectangleF rectangle)
  975. {
  976. object value = ResetTextValue(date, true);
  977. if (value == null)
  978. {
  979. return;
  980. }
  981. //bool isPrint = true;
  982. try
  983. {
  984. if (this._itemStyle == ItemStyle.Barcode)
  985. {
  986. OneDDrawingOptions ops = new OneDDrawingOptions();
  987. //ops.DrawGraphics = graphics;
  988. ops.ShowType = (this.Wrap ? BarcodeShowType.Zoom: BarcodeShowType.Show);
  989. //ops.ImageRect = new Rectangle(
  990. // LayoutCommon.MillimeterToPixel(rectangleM.Left, graphics.DpiX),
  991. // LayoutCommon.MillimeterToPixel(rectangleM.Top, graphics.DpiX),
  992. // LayoutCommon.MillimeterToPixel(rectangleM.Width, graphics.DpiX),
  993. // LayoutCommon.MillimeterToPixel(rectangleM.Height, graphics.DpiX));
  994. //BarcodeDraw.DrawQRCodeImageOnGraphics(value.ToString(), imageRect, ops);
  995. ops.ImageWidth = LayoutCommon.MillimeterToPixelDpi(rectangleM.Width, graphics.DpiX);
  996. ops.ImageHeight = LayoutCommon.MillimeterToPixelDpi(rectangleM.Height, graphics.DpiY);
  997. ops.DpiX = graphics.DpiX;
  998. ops.DpiY = graphics.DpiY;
  999. using (Image pic1 = OneDHelper.GetOneDImage(value.ToString(), ops))
  1000. {
  1001. graphics.DrawImage(pic1, rectangleM, ops.ImageRect, GraphicsUnit.Pixel);
  1002. }
  1003. return;
  1004. }
  1005. if (this._itemStyle == ItemStyle.QRCode)
  1006. {
  1007. QRCodeDrawingOptions ops = new QRCodeDrawingOptions();
  1008. //ops.DrawGraphics = graphics;
  1009. ops.ShowType = (this.Wrap ? BarcodeShowType.Zoom : BarcodeShowType.Show);
  1010. //ops.ImageRect = new Rectangle(
  1011. // LayoutCommon.MillimeterToPixel(rectangleM.Left, graphics.DpiX),
  1012. // LayoutCommon.MillimeterToPixel(rectangleM.Top, graphics.DpiX),
  1013. // LayoutCommon.MillimeterToPixel(rectangleM.Width, graphics.DpiX),
  1014. // LayoutCommon.MillimeterToPixel(rectangleM.Height, graphics.DpiX));
  1015. //BarcodeDraw.DrawQRCodeImageOnGraphics(value.ToString(), imageRect, ops);
  1016. ops.ImageWidth = LayoutCommon.MillimeterToPixelDpi(rectangleM.Width, graphics.DpiX);
  1017. ops.ImageHeight = LayoutCommon.MillimeterToPixelDpi(rectangleM.Height, graphics.DpiY);
  1018. ops.DpiX = graphics.DpiX;
  1019. ops.DpiY = graphics.DpiY;
  1020. using (Image pic1 = QRCodeHelper.GetQRCodeImage(value.ToString(), ops))
  1021. {
  1022. graphics.DrawImage(pic1, rectangleM, ops.ImageRect, GraphicsUnit.Pixel);
  1023. }
  1024. return;
  1025. }
  1026. }
  1027. catch (Exception ex)
  1028. {
  1029. throw ex;
  1030. }
  1031. if (PicColumn && value is Image)
  1032. {
  1033. Image image = value as Image;
  1034. if (image != null)
  1035. {
  1036. RectangleF rfp = PicMargin ? rectangle : rectangleM;
  1037. if (FixedRatio)
  1038. {
  1039. // 背景图片的高宽比例
  1040. float imgWHRate = (float)image.Width / (float)image.Height;
  1041. // 打印纸张的高宽比例
  1042. float paperWHRate = rfp.Width / rfp.Height;
  1043. // 背景图片打印范围
  1044. RectangleF newImgRectangleF = RectangleF.Empty;
  1045. // 缩放背景图片,但保持图片高宽比例
  1046. if (imgWHRate > paperWHRate)
  1047. {
  1048. newImgRectangleF.Width = rfp.Width;
  1049. newImgRectangleF.Height = rfp.Width / imgWHRate;
  1050. newImgRectangleF.X = rfp.X;
  1051. newImgRectangleF.Y = (rfp.Height - newImgRectangleF.Height) / 2 + rfp.Y;
  1052. }
  1053. else
  1054. {
  1055. newImgRectangleF.Width = rfp.Height * imgWHRate;
  1056. newImgRectangleF.Height = rfp.Height;
  1057. newImgRectangleF.X = (rfp.Width - newImgRectangleF.Width) / 2 + rfp.X;
  1058. newImgRectangleF.Y = rfp.Y;
  1059. }
  1060. rfp = newImgRectangleF;
  1061. }
  1062. // 在指定的范围绘制背景图片
  1063. LayoutUtility.DrawImage(graphics, GraphicsUnit.Millimeter, rfp, image);
  1064. }
  1065. return;
  1066. }
  1067. LayoutUtility.DrawText(graphics,
  1068. GraphicsUnit.Millimeter,
  1069. rectangleM,
  1070. rectangle,
  1071. value.ToString(),
  1072. _font,
  1073. _textColor,
  1074. _lineSpace,
  1075. _characterSpace,
  1076. _characterCount,
  1077. _textAlign,
  1078. _textAlignVertical,
  1079. _wrap,
  1080. Clip,
  1081. false);
  1082. }
  1083. #endregion 函数
  1084. public Image pictureProcess(Image sourceImage, int targetWidth, int targetHeight)
  1085. {
  1086. int width;//图片最终的宽
  1087. int height;//图片最终的高
  1088. try
  1089. {
  1090. System.Drawing.Imaging.ImageFormat format = sourceImage.RawFormat;
  1091. Bitmap targetPicture = new Bitmap(targetWidth, targetHeight);
  1092. Graphics g = Graphics.FromImage(targetPicture);
  1093. g.Clear(Color.White);
  1094. //计算缩放图片的大小
  1095. if (sourceImage.Width > targetWidth && sourceImage.Height <= targetHeight)
  1096. {
  1097. width = targetWidth;
  1098. height = (width * sourceImage.Height) / sourceImage.Width;
  1099. }
  1100. else if (sourceImage.Width <= targetWidth && sourceImage.Height > targetHeight)
  1101. {
  1102. height = targetHeight;
  1103. width = (height * sourceImage.Width) / sourceImage.Height;
  1104. }
  1105. else if (sourceImage.Width <= targetWidth && sourceImage.Height <= targetHeight)
  1106. {
  1107. width = sourceImage.Width;
  1108. height = sourceImage.Height;
  1109. }
  1110. else
  1111. {
  1112. width = targetWidth;
  1113. height = (width * sourceImage.Height) / sourceImage.Width;
  1114. if (height > targetHeight)
  1115. {
  1116. height = targetHeight;
  1117. width = (height * sourceImage.Width) / sourceImage.Height;
  1118. }
  1119. }
  1120. g.DrawImage(sourceImage, (targetWidth - width) / 2, (targetHeight - height) / 2, width, height);
  1121. sourceImage.Dispose();
  1122. return targetPicture;
  1123. }
  1124. catch// (Exception ex)
  1125. {
  1126. }
  1127. return null;
  1128. }
  1129. }
  1130. public class QRCodeWriterMy : Writer
  1131. {
  1132. private const int QUIET_ZONE_SIZE = 4;
  1133. /// <summary>
  1134. /// Encode a barcode using the default settings.
  1135. /// </summary>
  1136. /// <param name="contents">The contents to encode in the barcode</param>
  1137. /// <param name="format">The barcode format to generate</param>
  1138. /// <param name="width">The preferred width in pixels</param>
  1139. /// <param name="height">The preferred height in pixels</param>
  1140. /// <returns>
  1141. /// The generated barcode as a Matrix of unsigned bytes (0 == black, 255 == white)
  1142. /// </returns>
  1143. public BitMatrix encode(string contents, BarcodeFormat format, int width, int height)
  1144. {
  1145. return encode(contents, format, width, height, null);
  1146. }
  1147. /// <summary>
  1148. /// </summary>
  1149. /// <param name="contents">The contents to encode in the barcode</param>
  1150. /// <param name="format">The barcode format to generate</param>
  1151. /// <param name="width">The preferred width in pixels</param>
  1152. /// <param name="height">The preferred height in pixels</param>
  1153. /// <param name="hints">Additional parameters to supply to the encoder</param>
  1154. /// <returns>
  1155. /// The generated barcode as a Matrix of unsigned bytes (0 == black, 255 == white)
  1156. /// </returns>
  1157. public BitMatrix encode(string contents,
  1158. BarcodeFormat format,
  1159. int width,
  1160. int height,
  1161. IDictionary<EncodeHintType, object> hints)
  1162. {
  1163. if (String.IsNullOrEmpty(contents))
  1164. {
  1165. throw new ArgumentException("Found empty contents");
  1166. }
  1167. if (format != BarcodeFormat.QR_CODE)
  1168. {
  1169. throw new ArgumentException("Can only encode QR_CODE, but got " + format);
  1170. }
  1171. if (width < 0 || height < 0)
  1172. {
  1173. throw new ArgumentException("Requested dimensions are too small: " + width + 'x' + height);
  1174. }
  1175. var errorCorrectionLevel = ErrorCorrectionLevel.L;
  1176. int quietZone = QUIET_ZONE_SIZE;
  1177. if (hints != null)
  1178. {
  1179. if (hints.ContainsKey(EncodeHintType.ERROR_CORRECTION))
  1180. {
  1181. var requestedECLevel = hints[EncodeHintType.ERROR_CORRECTION];
  1182. if (requestedECLevel != null)
  1183. {
  1184. errorCorrectionLevel = requestedECLevel as ErrorCorrectionLevel;
  1185. if (errorCorrectionLevel == null)
  1186. {
  1187. switch (requestedECLevel.ToString().ToUpper())
  1188. {
  1189. case "L":
  1190. errorCorrectionLevel = ErrorCorrectionLevel.L;
  1191. break;
  1192. case "M":
  1193. errorCorrectionLevel = ErrorCorrectionLevel.M;
  1194. break;
  1195. case "Q":
  1196. errorCorrectionLevel = ErrorCorrectionLevel.Q;
  1197. break;
  1198. case "H":
  1199. errorCorrectionLevel = ErrorCorrectionLevel.H;
  1200. break;
  1201. default:
  1202. errorCorrectionLevel = ErrorCorrectionLevel.L;
  1203. break;
  1204. }
  1205. }
  1206. }
  1207. }
  1208. if (hints.ContainsKey(EncodeHintType.MARGIN))
  1209. {
  1210. var quietZoneInt = hints[EncodeHintType.MARGIN];
  1211. if (quietZoneInt != null)
  1212. {
  1213. quietZone = Convert.ToInt32(quietZoneInt.ToString());
  1214. }
  1215. }
  1216. }
  1217. var code = Encoder.encode(contents, errorCorrectionLevel, hints);
  1218. BitMatrix bm = renderResult(code, width, height, quietZone);
  1219. hints[EncodeHintType.WIDTH] = bm.Width;
  1220. hints[EncodeHintType.HEIGHT] = bm.Height;
  1221. return bm;
  1222. }
  1223. // Note that the input matrix uses 0 == white, 1 == black, while the output matrix uses
  1224. // 0 == black, 255 == white (i.e. an 8 bit greyscale bitmap).
  1225. private static BitMatrix renderResult(QRCode code, int width, int height, int quietZone)
  1226. {
  1227. var input = code.Matrix;
  1228. if (input == null)
  1229. {
  1230. throw new InvalidOperationException();
  1231. }
  1232. int inputWidth = input.Width;
  1233. int inputHeight = input.Height;
  1234. int qrWidth = inputWidth + (quietZone << 1);
  1235. int qrHeight = inputHeight + (quietZone << 1);
  1236. int outputWidth = Math.Max(width, qrWidth);
  1237. int outputHeight = Math.Max(height, qrHeight);
  1238. int multiple = Math.Min(outputWidth / qrWidth, outputHeight / qrHeight);
  1239. // Padding includes both the quiet zone and the extra white pixels to accommodate the requested
  1240. // dimensions. For example, if input is 25x25 the QR will be 33x33 including the quiet zone.
  1241. // If the requested size is 200x160, the multiple will be 4, for a QR of 132x132. These will
  1242. // handle all the padding from 100x100 (the actual QR) up to 200x160.
  1243. int leftPadding = (outputWidth - (inputWidth * multiple)) / 2;
  1244. int topPadding = (outputHeight - (inputHeight * multiple)) / 2;
  1245. //int leftPadding = 0;//(outputWidth - (inputWidth * multiple)) / 2;
  1246. //int topPadding = 0;//(outputHeight - (inputHeight * multiple)) / 2;
  1247. //outputWidth = (inputWidth * multiple);
  1248. //outputHeight = (inputHeight * multiple);
  1249. var output = new BitMatrix(outputWidth, outputHeight);
  1250. for (int inputY = 0, outputY = topPadding; inputY < inputHeight; inputY++, outputY += multiple)
  1251. {
  1252. // Write the contents of this row of the barcode
  1253. for (int inputX = 0, outputX = leftPadding; inputX < inputWidth; inputX++, outputX += multiple)
  1254. {
  1255. if (input[inputX, inputY] == 1)
  1256. {
  1257. output.setRegion(outputX, outputY, multiple, multiple);
  1258. //int mX = outputWidth - outputX;
  1259. //int mY = outputHeight - outputY;
  1260. //mX = Math.Min(mX, multiple);
  1261. //mY = Math.Min(mY, multiple);
  1262. //output.setRegion(outputX, outputY, mX, mY);
  1263. }
  1264. }
  1265. }
  1266. return output;
  1267. }
  1268. }
  1269. }