TextItem.cs 70 KB

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