TextItem.cs 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937
  1. 
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. using System.Globalization;
  6. using System.IO;
  7. using System.Windows.Forms;
  8. using Curtain.Framework.Barcode;
  9. using Curtain.Framework.Barcode.OneD;
  10. using Curtain.Framework.Barcode.QRCode;
  11. using ZXing;
  12. using ZXing.Common;
  13. using ZXing.QrCode.Internal;
  14. namespace Dongke.WinForm.Controls.InvoiceLayout
  15. {
  16. /// <summary>
  17. ///
  18. /// </summary>
  19. [Serializable]
  20. public class TextItem : LayoutItem, IBoundItem
  21. {
  22. #region 成员变量
  23. private string _dataMember; // 对应数据源中的字段名
  24. private string _displayValue; // 编辑模式显示的值
  25. private object _formatType; // 格式类别
  26. private string _format; // 文本格式
  27. private string _barcodeFormat; // 文本格式
  28. private string _qrCodeLevel;
  29. private Font _font; // 文本字体
  30. private bool _isUnderline; // 下划线
  31. private bool _isStrikeout; // 删除线
  32. private bool _wrap; // 是否自动换行
  33. private bool _clip; // 是否剪裁延伸到边框外侧的文本
  34. private Color _textColor; // 文本颜色
  35. private TextAlignment _textAlign; // 文本对齐方式
  36. private TextAlignment _textAlignVertical; // 文本垂直对齐
  37. private float _characterSpace; // 文字间隔
  38. private float _lineSpace; // 文字行间隔
  39. private int _characterCount; // 行文字数
  40. private string _defaultValue; // 文本默认值
  41. private string _culture; // culture
  42. private string _defaultItemName; // 默认项目名
  43. private string _textItemName; // 文本项目名
  44. private bool _isDataBinding; // 是否数据绑定
  45. [NonSerialized]
  46. private string _text; // 文本
  47. [NonSerialized]
  48. private object _dataValue; // 数据源中的值
  49. [NonSerialized]
  50. private object _printDataValue; // 打印数据源中的值
  51. [NonSerialized]
  52. private DataBoundField _boundField; // 数据源字段
  53. [NonSerialized]
  54. private DataBoundField _printBoundField; // 打印数据源字段
  55. [NonSerialized]
  56. private Font _fontDraw; // 绘制字体
  57. [NonSerialized]
  58. private float _lineSpaceDraw; // 绘制行间隔
  59. [NonSerialized]
  60. private float _characterSpaceDraw; // 绘制文字间隔
  61. /// <summary>
  62. ///
  63. /// </summary>
  64. [NonSerialized]
  65. protected RectangleF _marginRectangleFDraw; // 绘制范围
  66. private string _itemCode; // 项目ID
  67. private int _itemRefClass; // 分类
  68. private ItemStyle _itemStyle; // 项目类别
  69. private string _itemSample; // 项目示例
  70. /// <summary>
  71. /// 条码打印用
  72. /// </summary>
  73. [NonSerialized]
  74. private string _picCodeValue = null;
  75. [NonSerialized]
  76. private int _picCodeWidth = -1;
  77. [NonSerialized]
  78. private int _picCodeHeight = -1;
  79. [NonSerialized]
  80. private Image _picCode = null;
  81. //[NonSerialized]
  82. //BarcodeWriter _writer = null;
  83. //[NonSerialized]
  84. //EncodingOptions _options = null;
  85. #endregion 成员变量
  86. #region 属性
  87. /// <summary>
  88. /// 对应数据源中的字段名
  89. /// </summary>
  90. public string DataMember
  91. {
  92. get
  93. {
  94. return _dataMember;
  95. }
  96. set
  97. {
  98. if (string.IsNullOrEmpty(value))
  99. {
  100. value = string.Empty;
  101. }
  102. if (!value.Equals(_dataMember))
  103. {
  104. _dataMember = value;
  105. if (Owner != null)
  106. {
  107. if (Owner.LayoutMode == LayoutMode.Preview)
  108. {
  109. Owner.RefreshTextItemBound(this);
  110. }
  111. Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  112. }
  113. }
  114. }
  115. }
  116. /// <summary>
  117. /// 编辑模式显示的值
  118. /// </summary>
  119. public string DisplayValue
  120. {
  121. get
  122. {
  123. return _displayValue;
  124. }
  125. set
  126. {
  127. if (string.IsNullOrEmpty(value))
  128. {
  129. value = string.Empty;
  130. }
  131. if (!value.Equals(_displayValue))
  132. {
  133. _displayValue = value;
  134. if (Owner != null)
  135. {
  136. Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  137. }
  138. }
  139. }
  140. }
  141. /// <summary>
  142. /// 格式类别
  143. /// </summary>
  144. public object FormatType
  145. {
  146. get
  147. {
  148. return _formatType;
  149. }
  150. set
  151. {
  152. _formatType = value;
  153. if (Owner != null)
  154. {
  155. Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  156. }
  157. }
  158. }
  159. /// <summary>
  160. /// 文本格式
  161. /// </summary>
  162. public string Format
  163. {
  164. get
  165. {
  166. return _format;
  167. }
  168. set
  169. {
  170. if (string.IsNullOrEmpty(value))
  171. {
  172. value = string.Empty;
  173. }
  174. if (!value.Equals(_format))
  175. {
  176. _format = value;
  177. if (Owner != null)
  178. {
  179. Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  180. }
  181. }
  182. }
  183. }
  184. /// <summary>
  185. /// 一维码格式
  186. /// </summary>
  187. public string BarcodeFormat
  188. {
  189. get
  190. {
  191. return _barcodeFormat;
  192. }
  193. set
  194. {
  195. if (string.IsNullOrEmpty(value))
  196. {
  197. value = string.Empty;
  198. }
  199. if (!value.Equals(_barcodeFormat))
  200. {
  201. _barcodeFormat = value;
  202. if (Owner != null)
  203. {
  204. Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  205. }
  206. }
  207. }
  208. }
  209. /// <summary>
  210. /// 二维码级别
  211. /// </summary>
  212. public string QRCodeLevel
  213. {
  214. get
  215. {
  216. return _qrCodeLevel;
  217. }
  218. set
  219. {
  220. if (string.IsNullOrEmpty(value))
  221. {
  222. value = string.Empty;
  223. }
  224. if (!value.Equals(_qrCodeLevel))
  225. {
  226. _qrCodeLevel = value;
  227. if (Owner != null)
  228. {
  229. Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  230. }
  231. }
  232. }
  233. }
  234. /// <summary>
  235. /// 文本字体
  236. /// </summary>
  237. public Font Font
  238. {
  239. get
  240. {
  241. return _font;
  242. }
  243. set
  244. {
  245. if (!_font.Equals(value))
  246. {
  247. _font = value;
  248. _isUnderline = _font.Underline;
  249. _isStrikeout = _font.Strikeout;
  250. float zoom = base.GetZoom();
  251. _fontDraw = new Font(_font.FontFamily, _font.Size * zoom, _font.Style & (FontStyle.Regular | FontStyle.Bold | FontStyle.Italic));
  252. if (Owner != null)
  253. {
  254. Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  255. }
  256. }
  257. }
  258. }
  259. /// <summary>
  260. /// 是否自动文本换行
  261. /// </summary>
  262. public bool Wrap
  263. {
  264. get
  265. {
  266. return _wrap;
  267. }
  268. set
  269. {
  270. if (_wrap != value)
  271. {
  272. _wrap = value;
  273. _clip = !_wrap;
  274. if (Owner != null)
  275. {
  276. Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  277. }
  278. }
  279. }
  280. }
  281. /// <summary>
  282. /// 是否剪裁延伸到边框外侧的文本
  283. /// </summary>
  284. internal bool Clip
  285. {
  286. get
  287. {
  288. _clip = !_wrap;
  289. return _clip;
  290. }
  291. }
  292. /// <summary>
  293. /// 文本颜色
  294. /// </summary>
  295. public Color TextColor
  296. {
  297. get
  298. {
  299. return _textColor;
  300. }
  301. set
  302. {
  303. if (!_textColor.Equals(value))
  304. {
  305. _textColor = value;
  306. if (Owner != null)
  307. {
  308. Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  309. }
  310. }
  311. }
  312. }
  313. /// <summary>
  314. /// 文本对齐方式
  315. /// </summary>
  316. public TextAlignment TextAlign
  317. {
  318. get
  319. {
  320. return _textAlign;
  321. }
  322. set
  323. {
  324. if (_textAlign != value)
  325. {
  326. _textAlign = value;
  327. if (Owner != null)
  328. {
  329. Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  330. }
  331. }
  332. }
  333. }
  334. /// <summary>
  335. /// 文本垂直对齐方式
  336. /// </summary>
  337. public TextAlignment TextAlignVertical
  338. {
  339. get
  340. {
  341. return _textAlignVertical;
  342. }
  343. set
  344. {
  345. if (_textAlignVertical != value)
  346. {
  347. _textAlignVertical = value;
  348. if (Owner != null)
  349. {
  350. Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  351. }
  352. }
  353. }
  354. }
  355. /// <summary>
  356. /// 文字间隔
  357. /// </summary>
  358. public float CharacterSpace
  359. {
  360. get
  361. {
  362. return _characterSpace;
  363. }
  364. set
  365. {
  366. if (_characterSpace != value)
  367. {
  368. _characterSpace = value;
  369. if (Owner != null)
  370. {
  371. Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  372. }
  373. }
  374. }
  375. }
  376. /// <summary>
  377. /// 文字行间隔
  378. /// </summary>
  379. public float LineSpace
  380. {
  381. get
  382. {
  383. return _lineSpace;
  384. }
  385. set
  386. {
  387. if (_lineSpace != value)
  388. {
  389. _lineSpace = value;
  390. if (Owner != null)
  391. {
  392. Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  393. }
  394. }
  395. }
  396. }
  397. /// <summary>
  398. /// 行文字数
  399. /// </summary>
  400. public int CharacterCount
  401. {
  402. get
  403. {
  404. return _characterCount;
  405. }
  406. set
  407. {
  408. if (_characterCount != value)
  409. {
  410. _characterCount = value;
  411. if (Owner != null)
  412. {
  413. Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  414. }
  415. }
  416. }
  417. }
  418. /// <summary>
  419. /// 文本默认值
  420. /// </summary>
  421. public string DefaultValue
  422. {
  423. get
  424. {
  425. return _defaultValue;
  426. }
  427. set
  428. {
  429. if (string.IsNullOrEmpty(value))
  430. {
  431. value = string.Empty;
  432. }
  433. if (!value.Equals(_defaultValue))
  434. {
  435. _defaultValue = value;
  436. if (Owner != null)
  437. {
  438. Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  439. }
  440. }
  441. }
  442. }
  443. /// <summary>
  444. /// culture
  445. /// </summary>
  446. public string Culture
  447. {
  448. get
  449. {
  450. return _culture;
  451. }
  452. set
  453. {
  454. if (string.IsNullOrEmpty(value))
  455. {
  456. value = string.Empty;
  457. }
  458. if (!value.Equals(_culture))
  459. {
  460. _culture = value;
  461. if (Owner != null)
  462. {
  463. Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  464. }
  465. }
  466. }
  467. }
  468. /// <summary>
  469. /// 文本项目名
  470. /// </summary>
  471. public string TextItemName
  472. {
  473. get
  474. {
  475. return _textItemName;
  476. }
  477. set
  478. {
  479. if (string.IsNullOrEmpty(value))
  480. {
  481. value = string.Empty;
  482. }
  483. if (!value.Equals(_textItemName))
  484. {
  485. _textItemName = value;
  486. if (Owner != null)
  487. {
  488. Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  489. }
  490. }
  491. }
  492. }
  493. /// <summary>
  494. /// 默认项目名
  495. /// </summary>
  496. public string DefaultItemName
  497. {
  498. get
  499. {
  500. return _defaultItemName;
  501. }
  502. set
  503. {
  504. if (string.IsNullOrEmpty(value))
  505. {
  506. value = string.Empty;
  507. }
  508. if (!value.Equals(_defaultItemName))
  509. {
  510. _defaultItemName = value;
  511. if (Owner != null)
  512. {
  513. Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  514. }
  515. }
  516. }
  517. }
  518. /// <summary>
  519. /// 数据源中的值
  520. /// </summary>
  521. internal object DataValue
  522. {
  523. get
  524. {
  525. return _dataValue;
  526. }
  527. set
  528. {
  529. if (value != _dataValue
  530. || (value != null && !value.Equals(_dataValue)))
  531. {
  532. _dataValue = value;
  533. }
  534. }
  535. }
  536. /// <summary>
  537. /// 打印数据源中的值
  538. /// </summary>
  539. internal object PrintDataValue
  540. {
  541. get
  542. {
  543. return _printDataValue;
  544. }
  545. set
  546. {
  547. if (value != _printDataValue
  548. || (value != null && !value.Equals(_printDataValue)))
  549. {
  550. _printDataValue = value;
  551. }
  552. }
  553. }
  554. /// <summary>
  555. /// 是否数据绑定
  556. /// </summary>
  557. internal bool IsDataBinding
  558. {
  559. get
  560. {
  561. return _isDataBinding;
  562. }
  563. set
  564. {
  565. _isDataBinding = value;
  566. }
  567. }
  568. /// <summary>
  569. /// 数据源字段
  570. /// </summary>
  571. internal DataBoundField BoundField
  572. {
  573. get
  574. {
  575. return _boundField;
  576. }
  577. set
  578. {
  579. _boundField = value;
  580. }
  581. }
  582. /// <summary>
  583. /// 打印数据源字段
  584. /// </summary>
  585. internal DataBoundField PrintBoundField
  586. {
  587. get
  588. {
  589. return _printBoundField;
  590. }
  591. set
  592. {
  593. _printBoundField = value;
  594. }
  595. }
  596. /// <summary>
  597. /// 绘制字体
  598. /// </summary>
  599. internal Font FontDraw
  600. {
  601. get
  602. {
  603. return _fontDraw;
  604. }
  605. }
  606. /// <summary>
  607. /// 绘制文字间隔
  608. /// </summary>
  609. internal float CharacterSpaceDraw
  610. {
  611. get
  612. {
  613. return _characterSpaceDraw;
  614. }
  615. }
  616. /// <summary>
  617. /// 绘制行间隔
  618. /// </summary>
  619. internal float LineSpaceDraw
  620. {
  621. get
  622. {
  623. return _lineSpaceDraw;
  624. }
  625. }
  626. /// <summary>
  627. /// 项目编码
  628. /// </summary>
  629. public string ItemCode
  630. {
  631. get
  632. {
  633. return this._itemCode;
  634. }
  635. set
  636. {
  637. if (this._itemCode != value)
  638. {
  639. this._itemCode = value;
  640. if (Owner != null)
  641. {
  642. Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  643. }
  644. }
  645. if (string.IsNullOrEmpty(this._itemCode))
  646. {
  647. this._isDataBinding = false;
  648. }
  649. else
  650. {
  651. this._isDataBinding = true;
  652. }
  653. }
  654. }
  655. /// <summary>
  656. /// 分类
  657. /// </summary>
  658. public int ItemRefClass
  659. {
  660. get
  661. {
  662. return _itemRefClass;
  663. }
  664. set
  665. {
  666. if (value != _itemRefClass)
  667. {
  668. _itemRefClass = value;
  669. if (Owner != null)
  670. {
  671. Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  672. }
  673. }
  674. }
  675. }
  676. /// <summary>
  677. /// 项目类别
  678. /// </summary>
  679. public ItemStyle ItemStyle
  680. {
  681. get
  682. {
  683. return _itemStyle;
  684. }
  685. set
  686. {
  687. if (value != _itemStyle)
  688. {
  689. _itemStyle = value;
  690. this._picCode = null;
  691. if (Owner != null)
  692. {
  693. Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  694. }
  695. }
  696. }
  697. }
  698. /// <summary>
  699. /// 项目示例
  700. /// </summary>
  701. public string ItemSample
  702. {
  703. get
  704. {
  705. return _itemSample;
  706. }
  707. set
  708. {
  709. if (string.IsNullOrEmpty(value))
  710. {
  711. value = string.Empty;
  712. }
  713. if (!value.Equals(_itemSample))
  714. {
  715. _itemSample = value;
  716. if (Owner != null)
  717. {
  718. Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  719. }
  720. }
  721. }
  722. }
  723. /// <summary>
  724. ///
  725. /// </summary>
  726. public bool PicColumn
  727. {
  728. get;
  729. set;
  730. }
  731. /// <summary>
  732. ///
  733. /// </summary>
  734. public bool PicMargin
  735. {
  736. get;
  737. set;
  738. }
  739. /// <summary>
  740. ///
  741. /// </summary>
  742. public bool FixedRatio
  743. {
  744. get;
  745. set;
  746. }
  747. #endregion 属性
  748. #region 构造函数
  749. /// <summary>
  750. /// 构造函数
  751. /// </summary>
  752. internal TextItem()
  753. : this(null)
  754. {
  755. }
  756. /// <summary>
  757. /// 构造函数
  758. /// </summary>
  759. internal TextItem(LayoutBox box)
  760. : this(box, ItemType.Text)
  761. {
  762. }
  763. /// <summary>
  764. /// 构造函数
  765. /// </summary>
  766. internal TextItem(LayoutBox box, ItemType itemType)
  767. : base(box, itemType)
  768. {
  769. _width = LayoutCommon.DefaultTextItemSizeWidth;
  770. _height = LayoutCommon.DefaultTextItemSizeHeight;
  771. _displayValue = string.Empty;
  772. _format = string.Empty;
  773. _defaultValue = string.Empty;
  774. _font = LayoutCommon.DefaultItemFont;
  775. _fontDraw = LayoutCommon.DefaultItemFont;
  776. _textColor = Color.Black;
  777. _textAlign = TextAlignment.LeftOrTop;
  778. _characterSpace = 0f;
  779. _lineSpace = 0f;
  780. _characterCount = 0;
  781. _dataValue = null;
  782. _text = string.Empty;
  783. _itemRefClass = 0;
  784. _itemStyle = ItemStyle.Other;
  785. _isDataBinding = false;
  786. _boundField = new DataBoundField();
  787. _boundField.IsDataBound = false;
  788. _boundField.BoundFieldIndex = -1;
  789. _printBoundField = new DataBoundField();
  790. _printBoundField.IsDataBound = false;
  791. _printBoundField.BoundFieldIndex = -1;
  792. _defaultItemName = LayoutBox.ITEMNAME_FIX + ID;
  793. _textItemName = _defaultItemName;
  794. }
  795. #endregion 构造函数
  796. #region 函数
  797. /// <summary>
  798. /// 格式化数据源中的值
  799. /// </summary>
  800. private string FormatDataValue(object value)
  801. {
  802. string result = null;
  803. if (value != null && value != DBNull.Value)
  804. {
  805. if (_itemStyle == ItemStyle.Date)
  806. {
  807. result = value.ToString();
  808. DateTime dateTime;
  809. if (DateTime.TryParse(result, out dateTime))
  810. {
  811. if (string.IsNullOrEmpty(_format))
  812. {
  813. result = dateTime.ToString(LayoutConsts.DATETIME_FRORMAT);
  814. }
  815. else
  816. {
  817. if (LayoutConsts.CULTUREINFO_JP.Equals(_culture))
  818. {
  819. result = dateTime.ToString(_format, LayoutCommon.CultureInfoJP);
  820. }
  821. else
  822. {
  823. // MMM
  824. if (_format.Contains(LayoutConsts.DATETIME_FRORMAT_MMM))
  825. {
  826. if (string.IsNullOrEmpty(_culture))
  827. {
  828. result = dateTime.ToString(_format).ToUpper();
  829. }
  830. else
  831. {
  832. result = dateTime.ToString(_format, CultureInfo.CreateSpecificCulture(_culture)).ToUpper();
  833. }
  834. }
  835. else if (string.IsNullOrEmpty(_culture))
  836. {
  837. result = dateTime.ToString(_format);
  838. }
  839. else
  840. {
  841. result = dateTime.ToString(_format, CultureInfo.CreateSpecificCulture(_culture));
  842. }
  843. }
  844. }
  845. }
  846. }
  847. else if (_itemStyle == ItemStyle.Sign)
  848. {
  849. if (string.IsNullOrEmpty(_format))
  850. {
  851. result = value.ToString();
  852. }
  853. else
  854. {
  855. result = _format;
  856. }
  857. }
  858. else
  859. {
  860. result = value.ToString();
  861. }
  862. }
  863. return result;
  864. }
  865. /// <summary>
  866. /// 文本内容设置
  867. /// </summary>
  868. internal object ResetTextValue(object value, bool isDataBound)
  869. {
  870. if (Owner == null)
  871. {
  872. return _displayValue;
  873. }
  874. if (Owner.LayoutMode == LayoutMode.Preview)
  875. {
  876. if (!string.IsNullOrEmpty(_dataMember))
  877. {
  878. if (isDataBound)
  879. {
  880. object result = Owner.SetItemValueFormattingArgs(this, value, _format);
  881. if (this.ItemStyle == ItemStyle.LinearGraph || PicColumn)
  882. {
  883. if (result != null && result is Image)
  884. {
  885. return result;
  886. }
  887. if (value != null && value is byte[])
  888. {
  889. System.IO.MemoryStream ms = new System.IO.MemoryStream(value as byte[]);
  890. return Image.FromStream(ms);
  891. }
  892. return value;
  893. }
  894. if (result != null)
  895. {
  896. return result.ToString();
  897. }
  898. if (value != null && value != DBNull.Value)
  899. {
  900. return FormatDataValue(value);
  901. }
  902. return _defaultValue;
  903. }
  904. //return _defaultValue;
  905. return "";
  906. }
  907. return _displayValue;
  908. }
  909. else
  910. {
  911. //object result = Owner.SetItemValueFormattingArgs(this, value, _format);
  912. object result = Owner.SetItemValueFormattingArgs(this, _displayValue, _format);
  913. if (result != null)
  914. {
  915. return result.ToString();
  916. }
  917. return _displayValue;
  918. }
  919. }
  920. /// <summary>
  921. /// 显示Item属性设置画面
  922. /// </summary>
  923. /// <returns>
  924. /// DialogResult.OK:选中的Item有效,设置成功<br/>
  925. /// DialogResult.Cancel:选中的Item有效,取消设置<br/>
  926. /// DialogResult.None:LayoutBox不是编辑模式,或选中的Item不是一个
  927. /// </returns>
  928. protected override DialogResult ShowItemPropertyDialogInner()
  929. {
  930. //if (this.PicColumn)
  931. if (this.ItemStyle == ItemStyle.LinearGraph)
  932. {
  933. using (ImageItemSetting itemPropertySetting = new ImageItemSetting())
  934. {
  935. itemPropertySetting.ImageLocationX = Left;
  936. itemPropertySetting.ImageLocationY = Top;
  937. itemPropertySetting.ImageWidth = Width;
  938. itemPropertySetting.ImageHeight = Height;
  939. itemPropertySetting.SideRatioFixed = SideRatioFixed;
  940. //itemPropertySetting.FileName = FileName;
  941. //itemPropertySetting.Image = Image;
  942. DialogResult result = itemPropertySetting.ShowDialog();
  943. if (result == DialogResult.OK)
  944. {
  945. Left = itemPropertySetting.ImageLocationX;
  946. Top = itemPropertySetting.ImageLocationY;
  947. Width = itemPropertySetting.ImageWidth;
  948. Height = itemPropertySetting.ImageHeight;
  949. SideRatioFixed = itemPropertySetting.SideRatioFixed;
  950. ResetItemFrame();
  951. SetDrawProperty();
  952. Owner.Dirty = true;
  953. }
  954. return result;
  955. }
  956. }
  957. using (TextItemSetting itemPropertySetting = new TextItemSetting())
  958. {
  959. itemPropertySetting.IsNew = false;
  960. itemPropertySetting.IsDataBinding = IsDataBinding;
  961. itemPropertySetting.TextLocationX = Left;
  962. itemPropertySetting.TextLocationY = Top;
  963. itemPropertySetting.TextWidth = Width;
  964. itemPropertySetting.TextHeight = Height;
  965. itemPropertySetting.Culture = Culture;
  966. itemPropertySetting.LineSpace = LineSpace;
  967. itemPropertySetting.CharacterSpace = CharacterSpace;
  968. itemPropertySetting.CharacterCount = CharacterCount;
  969. itemPropertySetting.TextAlign = TextAlign;
  970. itemPropertySetting.TextAlignVertical = TextAlignVertical;
  971. itemPropertySetting.TextPreview = DisplayValue;
  972. itemPropertySetting.TextColor = TextColor;
  973. itemPropertySetting.ItemStyle = ItemStyle;
  974. itemPropertySetting.ItemName = TextItemName;
  975. itemPropertySetting.TextFormat = Format;
  976. itemPropertySetting.BarcodeFormat = BarcodeFormat;
  977. itemPropertySetting.QRCodeLevel = QRCodeLevel;
  978. itemPropertySetting.TextFont = Font;
  979. itemPropertySetting.Wrap = Wrap;
  980. itemPropertySetting.DefaultValue = DefaultValue;
  981. itemPropertySetting.chkFixedRatio.Checked = FixedRatio;
  982. itemPropertySetting.chkM.Checked = PicMargin;
  983. itemPropertySetting.chkPic.Checked = PicColumn;
  984. itemPropertySetting.chkFixedRatio.Enabled = PicColumn;
  985. itemPropertySetting.chkM.Enabled = PicColumn;
  986. DialogResult result = itemPropertySetting.ShowDialog();
  987. if (result == DialogResult.OK)
  988. {
  989. Left = itemPropertySetting.TextLocationX;
  990. Top = itemPropertySetting.TextLocationY;
  991. Width = itemPropertySetting.TextWidth;
  992. Height = itemPropertySetting.TextHeight;
  993. LineSpace = itemPropertySetting.LineSpace;
  994. CharacterSpace = itemPropertySetting.CharacterSpace;
  995. CharacterCount = itemPropertySetting.CharacterCount;
  996. Font = itemPropertySetting.TextFont;
  997. Wrap = itemPropertySetting.Wrap;
  998. TextAlign = itemPropertySetting.TextAlign;
  999. TextAlignVertical = itemPropertySetting.TextAlignVertical;
  1000. TextColor = itemPropertySetting.TextColor;
  1001. DisplayValue = itemPropertySetting.TextPreview;
  1002. ItemStyle = itemPropertySetting.ItemStyle;
  1003. TextItemName = itemPropertySetting.ItemName;
  1004. Format = itemPropertySetting.TextFormat;
  1005. BarcodeFormat = itemPropertySetting.BarcodeFormat;
  1006. QRCodeLevel = itemPropertySetting.QRCodeLevel;
  1007. Culture = itemPropertySetting.Culture;
  1008. DefaultValue = itemPropertySetting.DefaultValue;
  1009. FixedRatio = itemPropertySetting.chkFixedRatio.Checked;
  1010. PicMargin = itemPropertySetting.chkM.Checked;
  1011. PicColumn = itemPropertySetting.chkPic.Checked;
  1012. ResetItemFrame();
  1013. SetDrawProperty();
  1014. _picCode = null;
  1015. Owner.Dirty = true;
  1016. }
  1017. return result;
  1018. }
  1019. }
  1020. /// <summary>
  1021. /// Item初始化
  1022. /// </summary>
  1023. /// <param name="box">LayoutBox</param>
  1024. /// <param name="isNewID">是否生成新ID</param>
  1025. /// <returns>是否成功</returns>
  1026. internal override bool Init(LayoutBox box, bool isNewID)
  1027. {
  1028. if (_fontDraw == null)
  1029. {
  1030. if (_font == null)
  1031. {
  1032. _font = LayoutCommon.DefaultItemFont;
  1033. _fontDraw = LayoutCommon.DefaultItemFont;
  1034. }
  1035. else
  1036. {
  1037. _fontDraw = new Font(_font.FontFamily, _font.Size, _font.Style & (FontStyle.Regular | FontStyle.Bold | FontStyle.Italic));
  1038. }
  1039. }
  1040. _boundField.IsDataBound = false;
  1041. _boundField.BoundFieldIndex = -1;
  1042. _printBoundField.IsDataBound = false;
  1043. _printBoundField.BoundFieldIndex = -1;
  1044. bool isInit = base.Init(box, isNewID);
  1045. if (isNewID)
  1046. {
  1047. _defaultItemName = LayoutBox.ITEMNAME_FIX + ID;
  1048. _textItemName = _defaultItemName;
  1049. }
  1050. return isInit;
  1051. }
  1052. /// <summary>
  1053. /// 初始化Item绘制用属性
  1054. /// </summary>
  1055. internal override float SetDrawProperty()
  1056. {
  1057. float zoom = base.SetDrawProperty();
  1058. _fontDraw = new Font(_font.FontFamily, _font.Size * zoom, _fontDraw.Style);
  1059. _lineSpaceDraw = _lineSpace * zoom;
  1060. if (_characterCount < 1)
  1061. {
  1062. _characterSpaceDraw = _characterSpace * zoom;
  1063. }
  1064. _marginRectangleFDraw = new RectangleF();
  1065. _marginRectangleFDraw.X = (Left + LayoutConsts.TEXT_MARGIN) * zoom;
  1066. _marginRectangleFDraw.Y = (Top + LayoutConsts.TEXT_MARGIN) * zoom;
  1067. _marginRectangleFDraw.Width = (Width - LayoutConsts.TEXT_MARGIN - LayoutConsts.TEXT_MARGIN) * zoom;
  1068. _marginRectangleFDraw.Height = (Height - LayoutConsts.TEXT_MARGIN - LayoutConsts.TEXT_MARGIN) * zoom;
  1069. return zoom;
  1070. }
  1071. /// <summary>
  1072. /// 初始化Item绘制用属性
  1073. /// </summary>
  1074. internal override float SetRectangleF()
  1075. {
  1076. float zoom = base.SetRectangleF();
  1077. if (_characterCount < 1)
  1078. {
  1079. _characterSpaceDraw = _characterSpace * zoom;
  1080. }
  1081. _marginRectangleFDraw = new RectangleF();
  1082. _marginRectangleFDraw.X = (Left + LayoutConsts.TEXT_MARGIN) * zoom;
  1083. _marginRectangleFDraw.Y = (Top + LayoutConsts.TEXT_MARGIN) * zoom;
  1084. _marginRectangleFDraw.Width = (Width - LayoutConsts.TEXT_MARGIN - LayoutConsts.TEXT_MARGIN) * zoom;
  1085. _marginRectangleFDraw.Height = (Height - LayoutConsts.TEXT_MARGIN - LayoutConsts.TEXT_MARGIN) * zoom;
  1086. return zoom;
  1087. }
  1088. /// <summary>
  1089. /// Item图形上的OnPaint事件
  1090. /// </summary>
  1091. /// <param name="sender">指定的对象</param>
  1092. /// <param name="e">提供的事件数据</param>
  1093. protected override void OnPaint(object sender, PaintEventArgs e)
  1094. {
  1095. DrawTextItem(_dataValue, _boundField, e.Graphics, RectangleFDraw, _marginRectangleFDraw, false);
  1096. }
  1097. internal void DrawTextItem(object data, DataBoundField dataBoundField, Graphics graphics, RectangleF rectangleM, RectangleF rectangle, bool isPrint)
  1098. {
  1099. object value = ResetTextValue(data, dataBoundField.IsDataBound);
  1100. if (value == null)
  1101. {
  1102. return;
  1103. }
  1104. try
  1105. {
  1106. if (this._itemStyle == ItemStyle.Barcode)
  1107. {
  1108. // TODO 切换zxing生成69码图片时将EAN_13=》EAN13
  1109. if (this._barcodeFormat == "EAN_13")
  1110. {
  1111. string vv = EAN13((value + "").PadRight(12, '0'));
  1112. //graphics.DrawString(vv, _fontDraw, Brushes.Black, this.SimpleShape.Bounds);
  1113. Rectangle rr = new Rectangle();
  1114. rr.X = LayoutCommon.MillimeterToPixelDpi(rectangleM.X, graphics.DpiX);
  1115. rr.Y = LayoutCommon.MillimeterToPixelDpi(rectangleM.Y, graphics.DpiY);
  1116. rr.Width = LayoutCommon.MillimeterToPixelDpi(rectangleM.Width, graphics.DpiX);
  1117. rr.Height = LayoutCommon.MillimeterToPixelDpi(rectangleM.Height, graphics.DpiY);
  1118. graphics.DrawString(vv, _fontDraw, Brushes.Black, rr);
  1119. return;
  1120. }
  1121. else
  1122. {
  1123. if (isPrint)
  1124. {
  1125. OneDDrawingOptions ops = new OneDDrawingOptions();
  1126. //ops.DrawGraphics = graphics;
  1127. ops.ShowType = (this.Wrap ? BarcodeShowType.Zoom : BarcodeShowType.Show);
  1128. //ops.ImageWidth = this.SimpleShape.Width;
  1129. //ops.ImageHeight = this.SimpleShape.Height;
  1130. ops.ImageWidth = LayoutCommon.MillimeterToPixelDpi(rectangleM.Width, graphics.DpiX);
  1131. ops.ImageHeight = LayoutCommon.MillimeterToPixelDpi(rectangleM.Height, graphics.DpiY);
  1132. ops.DpiX = graphics.DpiX;
  1133. ops.DpiY = graphics.DpiY;
  1134. //string vv = value + "";
  1135. //if (vv.StartsWith("69"))
  1136. if (this._barcodeFormat == "EAN_13")
  1137. {
  1138. ops.ShowText = true;
  1139. ops.TextFont = _fontDraw;
  1140. string vv = value + "";
  1141. int len = vv.Length;
  1142. if (len < 12)
  1143. {
  1144. vv = vv.PadRight(12, '0');
  1145. }
  1146. else if (len > 13)
  1147. {
  1148. vv = vv.Substring(0, 12);
  1149. }
  1150. value = vv;
  1151. }
  1152. try
  1153. {
  1154. //if (this.ItemCode == "ZWLJC")
  1155. //{
  1156. // using (Image pic1 = OneDHelper.GetOneDImage(value.ToString(), ops, OneDFormat.EAN_13))
  1157. // {
  1158. // graphics.DrawImage(pic1, rectangleM, ops.ImageRect, GraphicsUnit.Pixel);
  1159. // }
  1160. //}
  1161. //else
  1162. //{
  1163. // using (Image pic1 = OneDHelper.GetOneDImage(value.ToString(), ops))
  1164. // {
  1165. // graphics.DrawImage(pic1, rectangleM, ops.ImageRect, GraphicsUnit.Pixel);
  1166. // }
  1167. //}
  1168. using (Image pic1 = OneDHelper.GetOneDImage(value.ToString(), ops, GetOneDFormat(this._barcodeFormat)))
  1169. {
  1170. graphics.DrawImage(pic1, rectangleM, ops.ImageRect, GraphicsUnit.Pixel);
  1171. }
  1172. }
  1173. catch
  1174. {
  1175. }
  1176. return;
  1177. }
  1178. Image pic = null;
  1179. if (!isPrint && this.SimpleShape.Width == this._picCodeWidth &&
  1180. this.SimpleShape.Height == this._picCodeHeight &&
  1181. value.ToString() == _picCodeValue)
  1182. {
  1183. pic = this._picCode;
  1184. }
  1185. if (pic == null)
  1186. {
  1187. OneDDrawingOptions ops = new OneDDrawingOptions();
  1188. //ops.DrawGraphics = graphics;
  1189. ops.ShowType = (this.Wrap ? BarcodeShowType.Zoom : BarcodeShowType.Show);
  1190. //ops.ImageWidth = this.SimpleShape.Width;
  1191. //ops.ImageHeight = this.SimpleShape.Height;
  1192. ops.ImageWidth = LayoutCommon.MillimeterToPixelDpi(rectangleM.Width, graphics.DpiX);
  1193. ops.ImageHeight = LayoutCommon.MillimeterToPixelDpi(rectangleM.Height, graphics.DpiX);
  1194. ops.DpiX = graphics.DpiX;
  1195. ops.DpiY = graphics.DpiY;
  1196. //string vv = value + "";
  1197. //if (vv.StartsWith("69"))
  1198. if (this._barcodeFormat == "EAN_13")
  1199. {
  1200. ops.ShowText = true;
  1201. ops.TextFont = _fontDraw;
  1202. string vv = value + "";
  1203. int len = vv.Length;
  1204. if (len < 12)
  1205. {
  1206. vv = vv.PadRight(12, '0');
  1207. }
  1208. else if (len > 13)
  1209. {
  1210. vv = vv.Substring(0, 12);
  1211. }
  1212. value = vv;
  1213. }
  1214. try
  1215. {
  1216. //if (this.ItemCode == "ZWLJC")
  1217. //{
  1218. // pic = OneDHelper.GetOneDImage(value.ToString(), ops, OneDFormat.EAN_13);
  1219. //}
  1220. //else
  1221. //{
  1222. // pic = OneDHelper.GetOneDImage(value.ToString(), ops);
  1223. //}
  1224. pic = OneDHelper.GetOneDImage(value.ToString(), ops, GetOneDFormat(this._barcodeFormat));
  1225. }
  1226. catch
  1227. {
  1228. pic = null;
  1229. }
  1230. if (!isPrint)
  1231. {
  1232. try
  1233. {
  1234. this._picCode?.Dispose();
  1235. }
  1236. catch
  1237. {
  1238. }
  1239. this._picCode = pic;
  1240. _picCodeValue = value.ToString();
  1241. _picCodeWidth = this.SimpleShape.Width;
  1242. _picCodeHeight = this.SimpleShape.Height;
  1243. }
  1244. }
  1245. try
  1246. {
  1247. if (pic != null)
  1248. {
  1249. //graphics.DrawImage(pic, this.SimpleShape.Bounds, new Rectangle(0, 0, pic.Width, pic.Height), GraphicsUnit.Pixel);
  1250. graphics.DrawImage(pic, this.SimpleShape.Bounds);
  1251. }
  1252. }
  1253. catch
  1254. {
  1255. }
  1256. finally
  1257. {
  1258. if (pic != null && isPrint)
  1259. {
  1260. pic.Dispose();
  1261. }
  1262. }
  1263. return;
  1264. }
  1265. }
  1266. if (this._itemStyle == ItemStyle.QRCode)
  1267. {
  1268. if (isPrint)
  1269. {
  1270. QRCodeDrawingOptions ops = new QRCodeDrawingOptions();
  1271. //ops.DrawGraphics = graphics;
  1272. ops.ShowType = (this.Wrap ? BarcodeShowType.Zoom : BarcodeShowType.Show);
  1273. //ops.ImageWidth = this.SimpleShape.Width;
  1274. //ops.ImageHeight = this.SimpleShape.Height;
  1275. ops.ImageWidth = LayoutCommon.MillimeterToPixelDpi(rectangleM.Width, graphics.DpiX);
  1276. ops.ImageHeight = LayoutCommon.MillimeterToPixelDpi(rectangleM.Height, graphics.DpiX);
  1277. ops.DpiX = graphics.DpiX;
  1278. ops.DpiY = graphics.DpiY;
  1279. QRCodeEncodingOptions qre = new QRCodeEncodingOptions();
  1280. qre.ECLevel = GetQRCodeLevel(this.QRCodeLevel);
  1281. using (Image pic1 = QRCodeHelper.GetQRCodeImage(value.ToString(), ops, qre))
  1282. {
  1283. graphics.DrawImage(pic1, rectangleM, ops.ImageRect, GraphicsUnit.Pixel);
  1284. }
  1285. return;
  1286. }
  1287. Image pic = null;
  1288. if (!isPrint && this.SimpleShape.Width == this._picCodeWidth &&
  1289. this.SimpleShape.Height == this._picCodeHeight &&
  1290. value.ToString() == _picCodeValue)
  1291. {
  1292. pic = this._picCode;
  1293. }
  1294. if (pic == null)
  1295. {
  1296. QRCodeDrawingOptions ops = new QRCodeDrawingOptions();
  1297. //ops.DrawGraphics = graphics;
  1298. ops.ShowType = (this.Wrap ? BarcodeShowType.Zoom : BarcodeShowType.Show);
  1299. //ops.ImageWidth = this.SimpleShape.Width;
  1300. //ops.ImageHeight = this.SimpleShape.Height;
  1301. ops.ImageWidth = LayoutCommon.MillimeterToPixelDpi(rectangleM.Width, graphics.DpiX);
  1302. ops.ImageHeight = LayoutCommon.MillimeterToPixelDpi(rectangleM.Height, graphics.DpiX);
  1303. ops.DpiX = graphics.DpiX;
  1304. ops.DpiY = graphics.DpiY;
  1305. QRCodeEncodingOptions qre = new QRCodeEncodingOptions();
  1306. qre.ECLevel = GetQRCodeLevel(this.QRCodeLevel);
  1307. pic = QRCodeHelper.GetQRCodeImage(value.ToString(), ops, qre);
  1308. //pic.Save("C://1.png");
  1309. if (!isPrint)
  1310. {
  1311. try
  1312. {
  1313. this._picCode?.Dispose();
  1314. }
  1315. catch
  1316. {
  1317. }
  1318. this._picCode = pic;
  1319. _picCodeValue = value.ToString();
  1320. _picCodeWidth = this.SimpleShape.Width;
  1321. _picCodeHeight = this.SimpleShape.Height;
  1322. }
  1323. }
  1324. try
  1325. {
  1326. if (pic != null)
  1327. {
  1328. //graphics.DrawImage(pic, this.SimpleShape.Bounds, new Rectangle(0, 0, pic.Width, pic.Height), GraphicsUnit.Pixel);
  1329. graphics.DrawImage(pic, this.SimpleShape.Bounds);
  1330. }
  1331. }
  1332. catch
  1333. {
  1334. }
  1335. finally
  1336. {
  1337. if (pic != null && isPrint)
  1338. {
  1339. pic.Dispose();
  1340. }
  1341. }
  1342. return;
  1343. }
  1344. //xuwei add 2024-04-12
  1345. /*
  1346. if (this._itemStyle == ItemStyle.LinearGraph)
  1347. {
  1348. if (isPrint)
  1349. {
  1350. OneDDrawingOptions ops = new OneDDrawingOptions();
  1351. //ops.DrawGraphics = graphics;
  1352. ops.ShowType = (this.Wrap ? BarcodeShowType.Zoom : BarcodeShowType.Show);
  1353. //ops.ImageWidth = this.SimpleShape.Width;
  1354. //ops.ImageHeight = this.SimpleShape.Height;
  1355. ops.ImageWidth = LayoutCommon.MillimeterToPixelDpi(rectangleM.Width, graphics.DpiX);
  1356. ops.ImageHeight = LayoutCommon.MillimeterToPixelDpi(rectangleM.Height, graphics.DpiX);
  1357. ops.DpiX = graphics.DpiX;
  1358. ops.DpiY = graphics.DpiY;
  1359. //xuwei to do ===========================
  1360. using (Image pic1 = Image.FromFile(value.ToString()))
  1361. {
  1362. graphics.DrawImage(pic1, rectangleM, ops.ImageRect, GraphicsUnit.Pixel);
  1363. }
  1364. //xuwei end
  1365. return;
  1366. }
  1367. Image pic = null;
  1368. if (!isPrint && this.SimpleShape.Width == this._picCodeWidth &&
  1369. this.SimpleShape.Height == this._picCodeHeight &&
  1370. value.ToString() == _picCodeValue)
  1371. {
  1372. pic = this._picCode;
  1373. }
  1374. if (pic == null)
  1375. {
  1376. OneDDrawingOptions ops = new OneDDrawingOptions();
  1377. //ops.DrawGraphics = graphics;
  1378. ops.ShowType = (this.Wrap ? BarcodeShowType.Zoom : BarcodeShowType.Show);
  1379. //ops.ImageWidth = this.SimpleShape.Width;
  1380. //ops.ImageHeight = this.SimpleShape.Height;
  1381. ops.ImageWidth = LayoutCommon.MillimeterToPixelDpi(rectangleM.Width, graphics.DpiX);
  1382. ops.ImageHeight = LayoutCommon.MillimeterToPixelDpi(rectangleM.Height, graphics.DpiX);
  1383. ops.DpiX = graphics.DpiX;
  1384. ops.DpiY = graphics.DpiY;
  1385. //pic = OneDHelper.GetOneDImage(value.ToString(), ops);
  1386. pic = Image.FromFile(value.ToString());
  1387. if (!isPrint)
  1388. {
  1389. try
  1390. {
  1391. this._picCode?.Dispose();
  1392. }
  1393. catch
  1394. { }
  1395. this._picCode = pic;
  1396. _picCodeValue = value.ToString();
  1397. _picCodeWidth = this.SimpleShape.Width;
  1398. _picCodeHeight = this.SimpleShape.Height;
  1399. }
  1400. }
  1401. try
  1402. {
  1403. if (pic != null)
  1404. {
  1405. //graphics.DrawImage(pic, this.SimpleShape.Bounds, new Rectangle(0, 0, pic.Width, pic.Height), GraphicsUnit.Pixel);
  1406. graphics.DrawImage(pic, this.SimpleShape.Bounds);
  1407. }
  1408. }
  1409. catch
  1410. {
  1411. }
  1412. finally
  1413. {
  1414. if (pic != null && isPrint)
  1415. {
  1416. pic.Dispose();
  1417. }
  1418. }
  1419. return;
  1420. }
  1421. */
  1422. //xuwei end
  1423. if (this._itemStyle == ItemStyle.LinearGraph)
  1424. {
  1425. if (value is null)
  1426. {
  1427. return;
  1428. }
  1429. System.Drawing.Image image = null;
  1430. if (value is string)
  1431. {
  1432. string ff = value + "";
  1433. if (ff == "" || !File.Exists(ff))
  1434. {
  1435. return;
  1436. }
  1437. FileInfo fileInfo = new FileInfo(ff);
  1438. Stream imageStream = null;
  1439. try
  1440. {
  1441. imageStream = fileInfo.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete);
  1442. image = System.Drawing.Image.FromStream(imageStream);
  1443. MemoryStream mstream = new MemoryStream();
  1444. image.Save(mstream, image.RawFormat);
  1445. image = System.Drawing.Image.FromStream(mstream);
  1446. }
  1447. finally
  1448. {
  1449. if (imageStream != null)
  1450. {
  1451. imageStream.Dispose();
  1452. }
  1453. }
  1454. }
  1455. if (value is System.Drawing.Image)
  1456. {
  1457. image = value as System.Drawing.Image;
  1458. }
  1459. if (image != null)
  1460. {
  1461. RectangleF rfp = PicMargin ? rectangle : rectangleM;
  1462. if (FixedRatio)
  1463. {
  1464. // 背景图片的高宽比例
  1465. float imgWHRate = (float)image.Width / (float)image.Height;
  1466. // 打印纸张的高宽比例
  1467. float paperWHRate = rfp.Width / rfp.Height;
  1468. // 背景图片打印范围
  1469. RectangleF newImgRectangleF = RectangleF.Empty;
  1470. // 缩放背景图片,但保持图片高宽比例
  1471. if (imgWHRate > paperWHRate)
  1472. {
  1473. newImgRectangleF.Width = rfp.Width;
  1474. newImgRectangleF.Height = rfp.Width / imgWHRate;
  1475. newImgRectangleF.X = rfp.X;
  1476. newImgRectangleF.Y = (rfp.Height - newImgRectangleF.Height) / 2 + rfp.Y;
  1477. }
  1478. else
  1479. {
  1480. newImgRectangleF.Width = rfp.Height * imgWHRate;
  1481. newImgRectangleF.Height = rfp.Height;
  1482. newImgRectangleF.X = (rfp.Width - newImgRectangleF.Width) / 2 + rfp.X;
  1483. newImgRectangleF.Y = rfp.Y;
  1484. }
  1485. rfp = newImgRectangleF;
  1486. }
  1487. // 在指定的范围绘制背景图片
  1488. LayoutUtility.DrawImage(graphics, GraphicsUnit.Millimeter, rfp, image);
  1489. }
  1490. return;
  1491. }
  1492. }
  1493. catch (Exception ex)
  1494. {
  1495. throw ex;
  1496. }
  1497. LayoutUtility.DrawText(graphics,
  1498. GraphicsUnit.Millimeter,
  1499. rectangleM,
  1500. rectangle,
  1501. value.ToString(),
  1502. _fontDraw,
  1503. _textColor,
  1504. _lineSpaceDraw,
  1505. _characterSpaceDraw,
  1506. _characterCount,
  1507. _textAlign,
  1508. _textAlignVertical,
  1509. _wrap,
  1510. Clip,
  1511. Selected);
  1512. }
  1513. #endregion 函数
  1514. public Image pictureProcess(Image sourceImage, int targetWidth, int targetHeight)
  1515. {
  1516. int width;//图片最终的宽
  1517. int height;//图片最终的高
  1518. try
  1519. {
  1520. System.Drawing.Imaging.ImageFormat format = sourceImage.RawFormat;
  1521. Bitmap targetPicture = new Bitmap(targetWidth, targetHeight);
  1522. Graphics g = Graphics.FromImage(targetPicture);
  1523. g.Clear(Color.White);
  1524. //计算缩放图片的大小
  1525. if (sourceImage.Width > targetWidth && sourceImage.Height <= targetHeight)
  1526. {
  1527. width = targetWidth;
  1528. height = (width * sourceImage.Height) / sourceImage.Width;
  1529. }
  1530. else if (sourceImage.Width <= targetWidth && sourceImage.Height > targetHeight)
  1531. {
  1532. height = targetHeight;
  1533. width = (height * sourceImage.Width) / sourceImage.Height;
  1534. }
  1535. else if (sourceImage.Width <= targetWidth && sourceImage.Height <= targetHeight)
  1536. {
  1537. width = sourceImage.Width;
  1538. height = sourceImage.Height;
  1539. }
  1540. else
  1541. {
  1542. width = targetWidth;
  1543. height = (width * sourceImage.Height) / sourceImage.Width;
  1544. if (height > targetHeight)
  1545. {
  1546. height = targetHeight;
  1547. width = (height * sourceImage.Width) / sourceImage.Height;
  1548. }
  1549. }
  1550. g.DrawImage(sourceImage, (targetWidth - width) / 2, (targetHeight - height) / 2, width, height);
  1551. sourceImage.Dispose();
  1552. return targetPicture;
  1553. }
  1554. catch// (Exception ex)
  1555. {
  1556. }
  1557. return null;
  1558. }
  1559. public OneDFormat GetOneDFormat(string value)
  1560. {
  1561. if (string.IsNullOrWhiteSpace(value))
  1562. {
  1563. return OneDFormat.CODE_128;
  1564. }
  1565. switch (value)
  1566. {
  1567. case "CODE_128":
  1568. return OneDFormat.CODE_128;
  1569. case "EAN_13":
  1570. return OneDFormat.EAN_13;
  1571. case "EAN_8":
  1572. return OneDFormat.EAN_8;
  1573. case "CODABAR":
  1574. return OneDFormat.CODABAR;
  1575. case "CODE_39":
  1576. return OneDFormat.CODE_39;
  1577. case "CODE_93":
  1578. return OneDFormat.CODE_93;
  1579. case "ITF":
  1580. return OneDFormat.ITF;
  1581. case "MSI":
  1582. return OneDFormat.MSI;
  1583. case "PLESSEY":
  1584. return OneDFormat.PLESSEY;
  1585. case "UPC_E":
  1586. return OneDFormat.UPC_E;
  1587. default:
  1588. return OneDFormat.CODE_128;
  1589. }
  1590. }
  1591. public QRECLevel GetQRCodeLevel(string value)
  1592. {
  1593. if (string.IsNullOrWhiteSpace(value))
  1594. {
  1595. return QRECLevel.H;
  1596. }
  1597. switch (value)
  1598. {
  1599. case "H":
  1600. return QRECLevel.H;
  1601. case "Q":
  1602. return QRECLevel.Q;
  1603. case "M":
  1604. return QRECLevel.M;
  1605. case "L":
  1606. return QRECLevel.L;
  1607. default:
  1608. return QRECLevel.H;
  1609. }
  1610. }
  1611. /// <summary>
  1612. /// 12位条码补齐校验位生成13位的条码
  1613. /// </summary>
  1614. /// <param name="chaine"></param>
  1615. /// <returns></returns>
  1616. public static string EAN13(string chaine)
  1617. {
  1618. int i;
  1619. int first;
  1620. int checkSum = 0;
  1621. string Barcode = "";
  1622. bool tableA;
  1623. //if (Regex.IsMatch(chaine, "^\\d{12}$"))
  1624. {
  1625. for (i = 1; i < 12; i += 2)
  1626. {
  1627. //System.Diagnostics.Debug.WriteLine(chaine.Substring(i, 1));
  1628. checkSum += Convert.ToInt32(chaine.Substring(i, 1));
  1629. }
  1630. checkSum *= 3;
  1631. for (i = 0; i < 12; i += 2)
  1632. {
  1633. checkSum += Convert.ToInt32(chaine.Substring(i, 1));
  1634. }
  1635. chaine += (10 - checkSum % 10) % 10;
  1636. string Barcode13Digits = chaine.ToString();
  1637. Barcode = chaine.Substring(0, 1) + (char)(65 + Convert.ToInt32(chaine.Substring(1, 1)));
  1638. first = Convert.ToInt32(chaine.Substring(0, 1));
  1639. for (i = 2; i <= 6; i++)
  1640. {
  1641. tableA = false;
  1642. switch (i)
  1643. {
  1644. case 2:
  1645. if (first >= 0 && first <= 3)
  1646. {
  1647. tableA = true;
  1648. }
  1649. break;
  1650. case 3:
  1651. if (first == 0 || first == 4 || first == 7 || first == 8)
  1652. {
  1653. tableA = true;
  1654. }
  1655. break;
  1656. case 4:
  1657. if (first == 0 || first == 1 || first == 4 || first == 5 || first == 9)
  1658. {
  1659. tableA = true;
  1660. }
  1661. break;
  1662. case 5:
  1663. if (first == 0 || first == 2 || first == 5 || first == 6 || first == 7)
  1664. {
  1665. tableA = true;
  1666. }
  1667. break;
  1668. case 6:
  1669. if (first == 0 || first == 3 || first == 6 || first == 8 || first == 9)
  1670. {
  1671. tableA = true;
  1672. }
  1673. break;
  1674. }
  1675. if (tableA)
  1676. {
  1677. Barcode += (char)(65 + Convert.ToInt32(chaine.Substring(i, 1)));
  1678. }
  1679. else
  1680. {
  1681. Barcode += (char)(75 + Convert.ToInt32(chaine.Substring(i, 1)));
  1682. }
  1683. }
  1684. Barcode += "*";
  1685. for (i = 7; i <= 12; i++)
  1686. {
  1687. Barcode += (char)(97 + Convert.ToInt32(chaine.Substring(i, 1)));
  1688. }
  1689. Barcode += "+";
  1690. }
  1691. return Barcode;
  1692. }
  1693. }
  1694. public class QRCodeWriterMy : Writer
  1695. {
  1696. private const int QUIET_ZONE_SIZE = 4;
  1697. /// <summary>
  1698. /// Encode a barcode using the default settings.
  1699. /// </summary>
  1700. /// <param name="contents">The contents to encode in the barcode</param>
  1701. /// <param name="format">The barcode format to generate</param>
  1702. /// <param name="width">The preferred width in pixels</param>
  1703. /// <param name="height">The preferred height in pixels</param>
  1704. /// <returns>
  1705. /// The generated barcode as a Matrix of unsigned bytes (0 == black, 255 == white)
  1706. /// </returns>
  1707. public BitMatrix encode(string contents, BarcodeFormat format, int width, int height)
  1708. {
  1709. return encode(contents, format, width, height, null);
  1710. }
  1711. /// <summary>
  1712. /// </summary>
  1713. /// <param name="contents">The contents to encode in the barcode</param>
  1714. /// <param name="format">The barcode format to generate</param>
  1715. /// <param name="width">The preferred width in pixels</param>
  1716. /// <param name="height">The preferred height in pixels</param>
  1717. /// <param name="hints">Additional parameters to supply to the encoder</param>
  1718. /// <returns>
  1719. /// The generated barcode as a Matrix of unsigned bytes (0 == black, 255 == white)
  1720. /// </returns>
  1721. public BitMatrix encode(string contents,
  1722. BarcodeFormat format,
  1723. int width,
  1724. int height,
  1725. IDictionary<EncodeHintType, object> hints)
  1726. {
  1727. if (String.IsNullOrEmpty(contents))
  1728. {
  1729. throw new ArgumentException("Found empty contents");
  1730. }
  1731. if (format != BarcodeFormat.QR_CODE)
  1732. {
  1733. throw new ArgumentException("Can only encode QR_CODE, but got " + format);
  1734. }
  1735. if (width < 0 || height < 0)
  1736. {
  1737. throw new ArgumentException("Requested dimensions are too small: " + width + 'x' + height);
  1738. }
  1739. var errorCorrectionLevel = ErrorCorrectionLevel.L;
  1740. int quietZone = QUIET_ZONE_SIZE;
  1741. if (hints != null)
  1742. {
  1743. if (hints.ContainsKey(EncodeHintType.ERROR_CORRECTION))
  1744. {
  1745. var requestedECLevel = hints[EncodeHintType.ERROR_CORRECTION];
  1746. if (requestedECLevel != null)
  1747. {
  1748. errorCorrectionLevel = requestedECLevel as ErrorCorrectionLevel;
  1749. if (errorCorrectionLevel == null)
  1750. {
  1751. switch (requestedECLevel.ToString().ToUpper())
  1752. {
  1753. case "L":
  1754. errorCorrectionLevel = ErrorCorrectionLevel.L;
  1755. break;
  1756. case "M":
  1757. errorCorrectionLevel = ErrorCorrectionLevel.M;
  1758. break;
  1759. case "Q":
  1760. errorCorrectionLevel = ErrorCorrectionLevel.Q;
  1761. break;
  1762. case "H":
  1763. errorCorrectionLevel = ErrorCorrectionLevel.H;
  1764. break;
  1765. default:
  1766. errorCorrectionLevel = ErrorCorrectionLevel.L;
  1767. break;
  1768. }
  1769. }
  1770. }
  1771. }
  1772. if (hints.ContainsKey(EncodeHintType.MARGIN))
  1773. {
  1774. var quietZoneInt = hints[EncodeHintType.MARGIN];
  1775. if (quietZoneInt != null)
  1776. {
  1777. quietZone = Convert.ToInt32(quietZoneInt.ToString());
  1778. }
  1779. }
  1780. }
  1781. var code = Encoder.encode(contents, errorCorrectionLevel, hints);
  1782. BitMatrix bm = renderResult(code, width, height, quietZone);
  1783. hints[EncodeHintType.WIDTH] = bm.Width;
  1784. hints[EncodeHintType.HEIGHT] = bm.Height;
  1785. return bm;
  1786. }
  1787. // Note that the input matrix uses 0 == white, 1 == black, while the output matrix uses
  1788. // 0 == black, 255 == white (i.e. an 8 bit greyscale bitmap).
  1789. private static BitMatrix renderResult(QRCode code, int width, int height, int quietZone)
  1790. {
  1791. var input = code.Matrix;
  1792. if (input == null)
  1793. {
  1794. throw new InvalidOperationException();
  1795. }
  1796. int inputWidth = input.Width;
  1797. int inputHeight = input.Height;
  1798. int qrWidth = inputWidth + (quietZone << 1);
  1799. int qrHeight = inputHeight + (quietZone << 1);
  1800. int outputWidth = Math.Max(width, qrWidth);
  1801. int outputHeight = Math.Max(height, qrHeight);
  1802. int multiple = Math.Min(outputWidth / qrWidth, outputHeight / qrHeight);
  1803. // Padding includes both the quiet zone and the extra white pixels to accommodate the requested
  1804. // dimensions. For example, if input is 25x25 the QR will be 33x33 including the quiet zone.
  1805. // If the requested size is 200x160, the multiple will be 4, for a QR of 132x132. These will
  1806. // handle all the padding from 100x100 (the actual QR) up to 200x160.
  1807. int leftPadding = (outputWidth - (inputWidth * multiple)) / 2;
  1808. int topPadding = (outputHeight - (inputHeight * multiple)) / 2;
  1809. //int leftPadding = 0;//(outputWidth - (inputWidth * multiple)) / 2;
  1810. //int topPadding = 0;//(outputHeight - (inputHeight * multiple)) / 2;
  1811. //outputWidth = (inputWidth * multiple);
  1812. //outputHeight = (inputHeight * multiple);
  1813. var output = new BitMatrix(outputWidth, outputHeight);
  1814. for (int inputY = 0, outputY = topPadding; inputY < inputHeight; inputY++, outputY += multiple)
  1815. {
  1816. // Write the contents of this row of the barcode
  1817. for (int inputX = 0, outputX = leftPadding; inputX < inputWidth; inputX++, outputX += multiple)
  1818. {
  1819. if (input[inputX, inputY] == 1)
  1820. {
  1821. output.setRegion(outputX, outputY, multiple, multiple);
  1822. //int mX = outputWidth - outputX;
  1823. //int mY = outputHeight - outputY;
  1824. //mX = Math.Min(mX, multiple);
  1825. //mY = Math.Min(mY, multiple);
  1826. //output.setRegion(outputX, outputY, mX, mY);
  1827. }
  1828. }
  1829. }
  1830. return output;
  1831. }
  1832. }
  1833. }