TextItem.cs 37 KB

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