| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580 |
- /*******************************************************************************
- * Copyright(c) 2012 dongke All rights reserved. / Confidential
- * 类的信息:
- * 1.程序名称:TextItem.cs
- * 2.功能描述: 文本Item
- * 编辑履历:
- * 作者 日期 版本 修改内容
- * 欧阳涛 2012/09/14 1.00 新建
- *******************************************************************************/
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Globalization;
- using System.IO;
- using Curtain.Framework.Barcode;
- using Curtain.Framework.Barcode.OneD;
- using Curtain.Framework.Barcode.QRCode;
- using ZXing;
- using ZXing.Common;
- using ZXing.QrCode;
- using ZXing.QrCode.Internal;
- namespace Dongke.WinForm.Controls.InvoiceLayout
- {
- [Serializable]
- public class TextItem : LayoutItem, IBoundItem
- {
- #region 成员变量
- private string _dataMember; // 对应数据源中的字段名
- private string _displayValue; // 编辑模式显示的值
- private object _formatType; // 格式类别
- private string _format; // 文本格式
- private string _barcodeFormat; // 文本格式
- private string _qrCodeLevel; // 文本格式
- private Font _font; // 文本字体
- private bool _isUnderline; // 下划线
- private bool _isStrikeout; // 删除线
- private bool _wrap; // 是否自动换行
- private bool _clip; // 是否剪裁延伸到边框外侧的文本
- private Color _textColor; // 文本颜色
- private TextAlignment _textAlign; // 文本对齐方式
- private TextAlignment _textAlignVertical; // 文本垂直对齐
- private float _characterSpace; // 文字间隔
- private float _lineSpace; // 文字行间隔
- private int _characterCount; // 行文字数
- private string _defaultValue; // 文本默认值
- private string _culture; // culture
- private string _defaultItemName; // 默认项目名
- private string _textItemName; // 文本项目名
- private bool _isDataBinding; // 是否数据绑定
- //[NonSerialized]
- //private string _text; // 文本
- //[NonSerialized]
- //private object _dataValue; // 数据源中的值
- //[NonSerialized]
- //private object _printDataValue; // 打印数据源中的值
- //[NonSerialized]
- //private DataBoundField _boundField; // 数据源字段
- //[NonSerialized]
- //private DataBoundField _printBoundField; // 打印数据源字段
- //[NonSerialized]
- //private Font _fontDraw; // 绘制字体
- //[NonSerialized]
- //private float _lineSpaceDraw; // 绘制行间隔
- //[NonSerialized]
- //private float _characterSpaceDraw; // 绘制文字间隔
- //[NonSerialized]
- //protected RectangleF _marginRectangleFDraw; // 绘制范围
- private int _travelDocItemID; // 项目ID
- private string _itemCode; // 项目ID
- private int _itemRefClass; // 分类
- private ItemStyle _itemStyle; // 项目类别
- private string _itemSample; // 项目示例
- #endregion 成员变量
- #region 属性
- /// <summary>
- /// 对应数据源中的字段名
- /// </summary>
- public string DataMember
- {
- get
- {
- return _dataMember;
- }
- set
- {
- if (string.IsNullOrEmpty(value))
- {
- value = string.Empty;
- }
- if (!value.Equals(_dataMember))
- {
- _dataMember = value;
- //if (Owner != null)
- //{
- // if (Owner.LayoutMode == LayoutMode.Preview)
- // {
- // Owner.RefreshTextItemBound(this);
- // }
- // Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
- //}
- }
- }
- }
- /// <summary>
- /// 编辑模式显示的值
- /// </summary>
- public string DisplayValue
- {
- get
- {
- return _displayValue;
- }
- set
- {
- if (string.IsNullOrEmpty(value))
- {
- value = string.Empty;
- }
- if (!value.Equals(_displayValue))
- {
- _displayValue = value;
- //if (Owner != null)
- //{
- // Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
- //}
- }
- }
- }
- /// <summary>
- /// 格式类别
- /// </summary>
- public object FormatType
- {
- get
- {
- return _formatType;
- }
- set
- {
- _formatType = value;
- //if (Owner != null)
- //{
- // Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
- //}
- }
- }
- /// <summary>
- /// 文本格式
- /// </summary>
- public string Format
- {
- get
- {
- return _format;
- }
- set
- {
- if (string.IsNullOrEmpty(value))
- {
- value = string.Empty;
- }
- if (!value.Equals(_format))
- {
- _format = value;
- //if (Owner != null)
- //{
- // Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
- //}
- }
- }
- }
- /// <summary>
- /// 一维码格式
- /// </summary>
- public string BarcodeFormat
- {
- get
- {
- return _barcodeFormat;
- }
- set
- {
- if (string.IsNullOrEmpty(value))
- {
- value = string.Empty;
- }
- if (!value.Equals(_barcodeFormat))
- {
- _barcodeFormat = value;
- }
- }
- }
- /// <summary>
- /// 二维码级别
- /// </summary>
- public string QRCodeLevel
- {
- get
- {
- return _qrCodeLevel;
- }
- set
- {
- if (string.IsNullOrEmpty(value))
- {
- value = string.Empty;
- }
- if (!value.Equals(_qrCodeLevel))
- {
- _qrCodeLevel = value;
- }
- }
- }
- /// <summary>
- /// 文本字体
- /// </summary>
- public Font Font
- {
- get
- {
- return _font;
- }
- set
- {
- if (!_font.Equals(value))
- {
- _font = value;
- _isUnderline = _font.Underline;
- _isStrikeout = _font.Strikeout;
- //float zoom = base.GetZoom();
- //_fontDraw = new Font(_font.FontFamily, _font.Size * zoom, _font.Style & (FontStyle.Regular | FontStyle.Bold | FontStyle.Italic));
- //if (Owner != null)
- //{
- // Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
- //}
- }
- }
- }
- /// <summary>
- /// 是否自动文本换行
- /// </summary>
- public bool Wrap
- {
- get
- {
- return _wrap;
- }
- set
- {
- if (_wrap != value)
- {
- _wrap = value;
- _clip = !_wrap;
- //if (Owner != null)
- //{
- // Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
- //}
- }
- }
- }
- /// <summary>
- /// 是否剪裁延伸到边框外侧的文本
- /// </summary>
- public bool Clip
- {
- get
- {
- _clip = !_wrap;
- return _clip;
- }
- }
- /// <summary>
- /// 文本颜色
- /// </summary>
- public Color TextColor
- {
- get
- {
- return _textColor;
- }
- set
- {
- if (!_textColor.Equals(value))
- {
- _textColor = value;
- //if (Owner != null)
- //{
- // Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
- //}
- }
- }
- }
- /// <summary>
- /// 文本对齐方式
- /// </summary>
- public TextAlignment TextAlign
- {
- get
- {
- return _textAlign;
- }
- set
- {
- if (_textAlign != value)
- {
- _textAlign = value;
- //if (Owner != null)
- //{
- // Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
- //}
- }
- }
- }
- /// <summary>
- /// 文本垂直对齐方式
- /// </summary>
- public TextAlignment TextAlignVertical
- {
- get
- {
- return _textAlignVertical;
- }
- set
- {
- if (_textAlignVertical != value)
- {
- _textAlignVertical = value;
- //if (Owner != null)
- //{
- // Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
- //}
- }
- }
- }
- /// <summary>
- /// 文字间隔
- /// </summary>
- public float CharacterSpace
- {
- get
- {
- return _characterSpace;
- }
- set
- {
- if (_characterSpace != value)
- {
- _characterSpace = value;
- //if (Owner != null)
- //{
- // Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
- //}
- }
- }
- }
- /// <summary>
- /// 文字行间隔
- /// </summary>
- public float LineSpace
- {
- get
- {
- return _lineSpace;
- }
- set
- {
- if (_lineSpace != value)
- {
- _lineSpace = value;
- //if (Owner != null)
- //{
- // Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
- //}
- }
- }
- }
- /// <summary>
- /// 行文字数
- /// </summary>
- public int CharacterCount
- {
- get
- {
- return _characterCount;
- }
- set
- {
- if (_characterCount != value)
- {
- _characterCount = value;
- //if (Owner != null)
- //{
- // Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
- //}
- }
- }
- }
- /// <summary>
- /// 文本默认值
- /// </summary>
- public string DefaultValue
- {
- get
- {
- return _defaultValue;
- }
- set
- {
- if (string.IsNullOrEmpty(value))
- {
- value = string.Empty;
- }
- if (!value.Equals(_defaultValue))
- {
- _defaultValue = value;
- //if (Owner != null)
- //{
- // Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
- //}
- }
- }
- }
- /// <summary>
- /// culture
- /// </summary>
- public string Culture
- {
- get
- {
- return _culture;
- }
- set
- {
- if (string.IsNullOrEmpty(value))
- {
- value = string.Empty;
- }
- if (!value.Equals(_culture))
- {
- _culture = value;
- //if (Owner != null)
- //{
- // Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
- //}
- }
- }
- }
- /// <summary>
- /// 文本项目名
- /// </summary>
- public string TextItemName
- {
- get
- {
- return _textItemName;
- }
- set
- {
- if (string.IsNullOrEmpty(value))
- {
- value = string.Empty;
- }
- if (!value.Equals(_textItemName))
- {
- _textItemName = value;
- //if (Owner != null)
- //{
- // Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
- //}
- }
- }
- }
- /// <summary>
- /// 默认项目名
- /// </summary>
- public string DefaultItemName
- {
- get
- {
- return _defaultItemName;
- }
- set
- {
- if (string.IsNullOrEmpty(value))
- {
- value = string.Empty;
- }
- if (!value.Equals(_defaultItemName))
- {
- _defaultItemName = value;
- //if (Owner != null)
- //{
- // Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
- //}
- }
- }
- }
- ///// <summary>
- ///// 数据源中的值
- ///// </summary>
- //internal object DataValue
- //{
- // get
- // {
- // return _dataValue;
- // }
- // set
- // {
- // if (value != _dataValue
- // || (value != null && !value.Equals(_dataValue)))
- // {
- // _dataValue = value;
- // }
- // }
- //}
- ///// <summary>
- ///// 打印数据源中的值
- ///// </summary>
- //internal object PrintDataValue
- //{
- // get
- // {
- // return _printDataValue;
- // }
- // set
- // {
- // if (value != _printDataValue
- // || (value != null && !value.Equals(_printDataValue)))
- // {
- // _printDataValue = value;
- // }
- // }
- //}
- /// <summary>
- /// 是否数据绑定
- /// </summary>
- internal bool IsDataBinding
- {
- get
- {
- return _isDataBinding;
- }
- set
- {
- _isDataBinding = value;
- }
- }
- ///// <summary>
- ///// 数据源字段
- ///// </summary>
- //internal DataBoundField BoundField
- //{
- // get
- // {
- // return _boundField;
- // }
- // set
- // {
- // _boundField = value;
- // }
- //}
- ///// <summary>
- ///// 打印数据源字段
- ///// </summary>
- //internal DataBoundField PrintBoundField
- //{
- // get
- // {
- // return _printBoundField;
- // }
- // set
- // {
- // _printBoundField = value;
- // }
- //}
- ///// <summary>
- ///// 绘制字体
- ///// </summary>
- //internal Font FontDraw
- //{
- // get
- // {
- // return _fontDraw;
- // }
- //}
- ///// <summary>
- ///// 绘制文字间隔
- ///// </summary>
- //internal float CharacterSpaceDraw
- //{
- // get
- // {
- // return _characterSpaceDraw;
- // }
- //}
- ///// <summary>
- ///// 绘制行间隔
- ///// </summary>
- //internal float LineSpaceDraw
- //{
- // get
- // {
- // return _lineSpaceDraw;
- // }
- //}
- /// <summary>
- /// 项目ID
- /// </summary>
- public int TravelDocItemID
- {
- get
- {
- return _travelDocItemID;
- }
- set
- {
- if (_travelDocItemID != value)
- {
- _travelDocItemID = value;
- //if (Owner != null)
- //{
- // Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
- //}
- }
- if (0 < _travelDocItemID)
- {
- _isDataBinding = true;
- }
- else
- {
- _isDataBinding = false;
- }
- }
- }
- /// <summary>
- /// 项目编码
- /// </summary>
- public string ItemCode
- {
- get
- {
- return this._itemCode;
- }
- set
- {
- if (this._itemCode != value)
- {
- this._itemCode = value;
-
- }
- if (string.IsNullOrEmpty(this._itemCode))
- {
- this._isDataBinding = false;
- }
- else
- {
- this._isDataBinding = true;
- }
- }
- }
-
- /// <summary>
- /// 分类
- /// </summary>
- public int ItemRefClass
- {
- get
- {
- return _itemRefClass;
- }
- set
- {
- if (value != _itemRefClass)
- {
- _itemRefClass = value;
- //if (Owner != null)
- //{
- // Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
- //}
- }
- }
- }
- /// <summary>
- /// 项目类别
- /// </summary>
- public ItemStyle ItemStyle
- {
- get
- {
- return _itemStyle;
- }
- set
- {
- if (value != _itemStyle)
- {
- _itemStyle = value;
- //this._picCode = null;
- //if (Owner != null)
- //{
- // Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
- //}
- }
- }
- }
- /// <summary>
- /// 项目示例
- /// </summary>
- public string ItemSample
- {
- get
- {
- return _itemSample;
- }
- set
- {
- if (string.IsNullOrEmpty(value))
- {
- value = string.Empty;
- }
- if (!value.Equals(_itemSample))
- {
- _itemSample = value;
- //if (Owner != null)
- //{
- // Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
- //}
- }
- }
- }
- public bool PicColumn
- {
- get;
- set;
- }
- public bool PicMargin
- {
- get;
- set;
- }
- public bool FixedRatio
- {
- get;
- set;
- }
- #endregion 属性
- #region 构造函数
- /// <summary>
- /// 构造函数
- /// </summary>
- internal TextItem()
- : this(ItemType.Text)
- {
- }
- ///// <summary>
- ///// 构造函数
- ///// </summary>
- //internal TextItem(LayoutBox box)
- // : this(box, ItemType.Text)
- //{
- //}
- /// <summary>
- /// 构造函数
- /// </summary>
- internal TextItem(/*LayoutBox box,*/ ItemType itemType)
- : base(/*box, */itemType)
- {
- //_width = LayoutCommon.DefaultTextItemSizeWidth;
- //_height = LayoutCommon.DefaultTextItemSizeHeight;
- _displayValue = string.Empty;
- _format = string.Empty;
- _defaultValue = string.Empty;
- //_font = LayoutCommon.DefaultItemFont;
- //_fontDraw = LayoutCommon.DefaultItemFont;
- _textColor = Color.Black;
- _textAlign = TextAlignment.LeftOrTop;
- _characterSpace = 0f;
- _lineSpace = 0f;
- _characterCount = 0;
- //_dataValue = null;
- //_text = string.Empty;
- _itemRefClass = 0;
- _itemStyle = ItemStyle.Other;
- _isDataBinding = false;
- //_boundField = new DataBoundField();
- //_boundField.IsDataBound = false;
- //_boundField.BoundFieldIndex = -1;
- //_printBoundField = new DataBoundField();
- //_printBoundField.IsDataBound = false;
- //_printBoundField.BoundFieldIndex = -1;
- //_defaultItemName = LayoutBox.ITEMNAME_FIX + ID;
- _textItemName = _defaultItemName;
- }
- #endregion 构造函数
- #region 函数
- /// <summary>
- /// 格式化数据源中的值
- /// </summary>
- private string FormatDataValue(object value)
- {
- string result = null;
- if (value != null && value != DBNull.Value)
- {
- if (_itemStyle == ItemStyle.Date)
- {
- result = value.ToString();
- DateTime dateTime;
- if (DateTime.TryParse(result, out dateTime))
- {
- if (string.IsNullOrEmpty(_format))
- {
- result = dateTime.ToString(LayoutConsts.DATETIME_FRORMAT);
- }
- else
- {
- //if (LayoutConsts.CULTUREINFO_JP.Equals(_culture))
- //{
- // result = dateTime.ToString(_format, LayoutCommon.CultureInfoJP);
- //}
- //else
- //{
- // MMM
- if (_format.Contains(LayoutConsts.DATETIME_FRORMAT_MMM))
- {
- if (string.IsNullOrEmpty(_culture))
- {
- result = dateTime.ToString(_format).ToUpper();
- }
- else
- {
- result = dateTime.ToString(_format, CultureInfo.CreateSpecificCulture(_culture)).ToUpper();
- }
- }
- else if (string.IsNullOrEmpty(_culture))
- {
- result = dateTime.ToString(_format);
- }
- else
- {
- result = dateTime.ToString(_format, CultureInfo.CreateSpecificCulture(_culture));
- }
- //}
- }
- }
- }
- else if (_itemStyle == ItemStyle.Sign)
- {
- if (string.IsNullOrEmpty(_format))
- {
- result = value.ToString();
- }
- else
- {
- result = _format;
- }
- }
- else
- {
- result = value.ToString();
- }
- }
- return result;
- }
- /// <summary>
- /// 文本内容设置
- /// </summary>
- public object ResetTextValue(object value, bool isDataBound)
- {
- //if (Owner == null)
- //{
- // return _displayValue;
- //}
- //if (Owner.LayoutMode == LayoutMode.Preview)
- {
- if (!string.IsNullOrEmpty(_dataMember))
- {
- if (isDataBound)
- {
- //object result = value;//Owner.SetItemValueFormattingArgs(this, value, _format);
- if (PicColumn)
- {
- //if (result != null && result is Image)
- //{
- // return result;
- //}
- if (value != null && value is byte[])
- {
- using (MemoryStream ms = new MemoryStream(value as byte[]))
- {
- return Image.FromStream(ms);
- }
- }
- return value;
- }
- //if (result != null)
- //{
- // return result.ToString();
- //}
- if (value != null && value != DBNull.Value)
- {
- return FormatDataValue(value);
- }
- return _defaultValue;
- }
- //return _defaultValue;
- return "";
- }
- return _displayValue;
- }
- //else
- //{
- // //object result = Owner.SetItemValueFormattingArgs(this, value, _format);
- // object result = Owner.SetItemValueFormattingArgs(this, _displayValue, _format);
- // if (result != null)
- // {
- // return result.ToString();
- // }
- // return _displayValue;
- //}
- }
- /// <summary>
- /// Item初始化
- /// </summary>
- /// <param name="box">LayoutBox</param>
- /// <param name="isNewID">是否生成新ID</param>
- /// <returns>是否成功</returns>
- internal override bool Init(/*LayoutBox box, bool isNewID*/)
- {
- //if (_fontDraw == null)
- //{
- // if (_font == null)
- // {
- // _font = LayoutCommon.DefaultItemFont;
- // _fontDraw = LayoutCommon.DefaultItemFont;
- // }
- // else
- // {
- // _fontDraw = new Font(_font.FontFamily, _font.Size, _font.Style & (FontStyle.Regular | FontStyle.Bold | FontStyle.Italic));
- // }
- //}
- //_boundField.IsDataBound = false;
- //_boundField.BoundFieldIndex = -1;
- //_printBoundField.IsDataBound = false;
- //_printBoundField.BoundFieldIndex = -1;
- bool isInit = base.Init(/*box, isNewID*/);
- //if (isNewID)
- //{
- // _defaultItemName = LayoutBox.ITEMNAME_FIX + ID;
- // _textItemName = _defaultItemName;
- //}
- return isInit;
- }
- ///// <summary>
- ///// Item图形上的OnPaint事件
- ///// </summary>
- ///// <param name="sender">指定的对象</param>
- ///// <param name="e">提供的事件数据</param>
- //protected override void OnPaint(object sender, PaintEventArgs e)
- //{
- // DrawTextItem(_dataValue, _boundField, e.Graphics, RectangleFDraw, _marginRectangleFDraw);
- //}
- //private void DrawTextItem(object date, DataBoundField dataBoundField, Graphics graphics, RectangleF rectangleM, RectangleF rectangle)
- //{
- // object value = ResetTextValue(date, dataBoundField.IsDataBound);
- // if (value == null)
- // {
- // return;
- // }
- // if (PicColumn && value is Image)
- // {
- // Image image = value as Image;
- // if (image != null)
- // {
- // RectangleF rfp = PicMargin ? rectangle : rectangleM;
- // if (FixedRatio)
- // {
- // // 背景图片的高宽比例
- // float imgWHRate = (float)image.Width / (float)image.Height;
- // // 打印纸张的高宽比例
- // float paperWHRate = rfp.Width / rfp.Height;
- // // 背景图片打印范围
- // RectangleF newImgRectangleF = RectangleF.Empty;
- // // 缩放背景图片,但保持图片高宽比例
- // if (imgWHRate > paperWHRate)
- // {
- // newImgRectangleF.Width = rfp.Width;
- // newImgRectangleF.Height = rfp.Width / imgWHRate;
- // newImgRectangleF.X = rfp.X;
- // newImgRectangleF.Y = (rfp.Height - newImgRectangleF.Height) / 2 + rfp.Y;
- // }
- // else
- // {
- // newImgRectangleF.Width = rfp.Height * imgWHRate;
- // newImgRectangleF.Height = rfp.Height;
- // newImgRectangleF.X = (rfp.Width - newImgRectangleF.Width) / 2 + rfp.X;
- // newImgRectangleF.Y = rfp.Y;
- // }
- // rfp = newImgRectangleF;
- // }
- // // 在指定的范围绘制背景图片
- // LayoutUtility.DrawImage(graphics, GraphicsUnit.Millimeter, rfp, image);
- // }
- // return;
- // }
- // LayoutUtility.DrawText(graphics,
- // GraphicsUnit.Millimeter,
- // rectangleM,
- // rectangle,
- // value.ToString(),
- // _fontDraw,
- // _textColor,
- // _lineSpaceDraw,
- // _characterSpaceDraw,
- // _characterCount,
- // _textAlign,
- // _textAlignVertical,
- // _wrap,
- // Clip,
- // Selected);
- //}
- public void PrintTextItem(object date, Graphics graphics, RectangleF rectangleM, RectangleF rectangle)
- {
- object value = ResetTextValue(date, true);
- if (value == null)
- {
- return;
- }
- //bool isPrint = true;
- try
- {
- if (this._itemStyle == ItemStyle.Barcode)
- {
- // TODO 切换zxing生成69码图片时将EAN_13=》EAN13
- if (this._barcodeFormat == "EAN_13")
- {
- string vv = EAN13((value + "").PadRight(12, '0'));
- //Rectangle rr = new Rectangle();
- //rr.X = LayoutCommon.MillimeterToPixelDpi(rectangleM.X, graphics.DpiX);
- //rr.Y = LayoutCommon.MillimeterToPixelDpi(rectangleM.Y, graphics.DpiY);
- //rr.Width = LayoutCommon.MillimeterToPixelDpi(rectangleM.Width, graphics.DpiX);
- //rr.Height = LayoutCommon.MillimeterToPixelDpi(rectangleM.Height, graphics.DpiY);
- //graphics.DrawString(vv, _font, Brushes.Black, rr);
- graphics.DrawString(vv, _font, Brushes.Black, rectangleM);
- return;
- }
- else
- {
- OneDDrawingOptions ops = new OneDDrawingOptions();
- //ops.DrawGraphics = graphics;
- ops.ShowType = (this.Wrap ? BarcodeShowType.Zoom : BarcodeShowType.Show);
- ops.ImageWidth = LayoutCommon.MillimeterToPixelDpi(rectangleM.Width, graphics.DpiX);
- ops.ImageHeight = LayoutCommon.MillimeterToPixelDpi(rectangleM.Height, graphics.DpiY);
- ops.DpiX = graphics.DpiX;
- ops.DpiY = graphics.DpiY;
- if (this._barcodeFormat == "EAN_13")
- {
- ops.ShowText = true;
- ops.TextFont = _font;
- // zxing AlternateBitmapRenderer 创建图片没有设置分辨率
- //ops.TextFont = new Font(_font.FontFamily, _font.Size * graphics.DpiX / 96, _font.Style);
- //ops.ImageWidth = LayoutCommon.MillimeterToPixelDpi(rectangleM.Width, 96);
- //ops.ImageHeight = LayoutCommon.MillimeterToPixelDpi(rectangleM.Height, 96);
- //ops.DpiX = 96;
- //ops.DpiY = 96;
- //string vv = value + "6970322941388";
- string vv = value + "";
- int len = vv.Length;
- if (len < 12)
- {
- vv = vv.PadRight(12, '0');
- }
- else if (len > 13)
- {
- vv = vv.Substring(0, 12);
- }
- value = vv;
- }
- try
- {
- //if (this.ItemCode == "ZWLJC")
- //{
- // using (Image pic1 = OneDHelper.GetOneDImage(value.ToString(), ops, OneDFormat.EAN_13))
- // {
- // graphics.DrawImage(pic1, rectangleM, ops.ImageRect, GraphicsUnit.Pixel);
- // }
- //}
- //else
- //{
- // using (Image pic1 = OneDHelper.GetOneDImage(value.ToString(), ops))
- // {
- // graphics.DrawImage(pic1, rectangleM, ops.ImageRect, GraphicsUnit.Pixel);
- // }
- //}
- using (Image pic1 = OneDHelper.GetOneDImage(value.ToString(), ops, GetOneDFormat(this._barcodeFormat)))
- {
- //pic1.Save("D://1.png");
- graphics.DrawImage(pic1, rectangleM, ops.ImageRect, GraphicsUnit.Pixel);
- }
- }
- catch { }
- return;
- }
- }
- if (this._itemStyle == ItemStyle.QRCode)
- {
- QRCodeDrawingOptions ops = new QRCodeDrawingOptions();
- //ops.DrawGraphics = graphics;
- ops.ShowType = (this.Wrap ? BarcodeShowType.Zoom : BarcodeShowType.Show);
- //ops.ImageRect = new Rectangle(
- // LayoutCommon.MillimeterToPixel(rectangleM.Left, graphics.DpiX),
- // LayoutCommon.MillimeterToPixel(rectangleM.Top, graphics.DpiX),
- // LayoutCommon.MillimeterToPixel(rectangleM.Width, graphics.DpiX),
- // LayoutCommon.MillimeterToPixel(rectangleM.Height, graphics.DpiX));
- //BarcodeDraw.DrawQRCodeImageOnGraphics(value.ToString(), imageRect, ops);
- ops.ImageWidth = LayoutCommon.MillimeterToPixelDpi(rectangleM.Width, graphics.DpiX);
- ops.ImageHeight = LayoutCommon.MillimeterToPixelDpi(rectangleM.Height, graphics.DpiY);
- ops.DpiX = graphics.DpiX;
- ops.DpiY = graphics.DpiY;
- QRCodeEncodingOptions qre = new QRCodeEncodingOptions();
- qre.ECLevel = GetQRCodeLevel(this.QRCodeLevel);
- using (Image pic1 = QRCodeHelper.GetQRCodeImage(value.ToString(), ops, qre))
- {
- graphics.DrawImage(pic1, rectangleM, ops.ImageRect, GraphicsUnit.Pixel);
- }
- return;
- }
- }
- catch (Exception ex)
- {
- throw ex;
- }
- if (PicColumn && value is Image)
- {
- Image image = value as Image;
- if (image != null)
- {
- RectangleF rfp = PicMargin ? rectangle : rectangleM;
- if (FixedRatio)
- {
- // 背景图片的高宽比例
- float imgWHRate = (float)image.Width / (float)image.Height;
- // 打印纸张的高宽比例
- float paperWHRate = rfp.Width / rfp.Height;
- // 背景图片打印范围
- RectangleF newImgRectangleF = RectangleF.Empty;
- // 缩放背景图片,但保持图片高宽比例
- if (imgWHRate > paperWHRate)
- {
- newImgRectangleF.Width = rfp.Width;
- newImgRectangleF.Height = rfp.Width / imgWHRate;
- newImgRectangleF.X = rfp.X;
- newImgRectangleF.Y = (rfp.Height - newImgRectangleF.Height) / 2 + rfp.Y;
- }
- else
- {
- newImgRectangleF.Width = rfp.Height * imgWHRate;
- newImgRectangleF.Height = rfp.Height;
- newImgRectangleF.X = (rfp.Width - newImgRectangleF.Width) / 2 + rfp.X;
- newImgRectangleF.Y = rfp.Y;
- }
- rfp = newImgRectangleF;
- }
- // 在指定的范围绘制背景图片
- LayoutUtility.DrawImage(graphics, GraphicsUnit.Millimeter, rfp, image);
- }
- return;
- }
- LayoutUtility.DrawText(graphics,
- GraphicsUnit.Millimeter,
- rectangleM,
- rectangle,
- value.ToString(),
- _font,
- _textColor,
- _lineSpace,
- _characterSpace,
- _characterCount,
- _textAlign,
- _textAlignVertical,
- _wrap,
- Clip,
- false);
- }
- #endregion 函数
- public Image pictureProcess(Image sourceImage, int targetWidth, int targetHeight)
- {
- int width;//图片最终的宽
- int height;//图片最终的高
- try
- {
- System.Drawing.Imaging.ImageFormat format = sourceImage.RawFormat;
- Bitmap targetPicture = new Bitmap(targetWidth, targetHeight);
- Graphics g = Graphics.FromImage(targetPicture);
- g.Clear(Color.White);
- //计算缩放图片的大小
- if (sourceImage.Width > targetWidth && sourceImage.Height <= targetHeight)
- {
- width = targetWidth;
- height = (width * sourceImage.Height) / sourceImage.Width;
- }
- else if (sourceImage.Width <= targetWidth && sourceImage.Height > targetHeight)
- {
- height = targetHeight;
- width = (height * sourceImage.Width) / sourceImage.Height;
- }
- else if (sourceImage.Width <= targetWidth && sourceImage.Height <= targetHeight)
- {
- width = sourceImage.Width;
- height = sourceImage.Height;
- }
- else
- {
- width = targetWidth;
- height = (width * sourceImage.Height) / sourceImage.Width;
- if (height > targetHeight)
- {
- height = targetHeight;
- width = (height * sourceImage.Width) / sourceImage.Height;
- }
- }
- g.DrawImage(sourceImage, (targetWidth - width) / 2, (targetHeight - height) / 2, width, height);
- sourceImage.Dispose();
- return targetPicture;
- }
- catch// (Exception ex)
- {
- }
- return null;
- }
- public OneDFormat GetOneDFormat(string value)
- {
- if (string.IsNullOrWhiteSpace(value))
- {
- return OneDFormat.CODE_128;
- }
- switch (value)
- {
- case "CODE_128":
- return OneDFormat.CODE_128;
- case "EAN_13":
- return OneDFormat.EAN_13;
- case "EAN_8":
- return OneDFormat.EAN_8;
- case "CODABAR":
- return OneDFormat.CODABAR;
- case "CODE_39":
- return OneDFormat.CODE_39;
- case "CODE_93":
- return OneDFormat.CODE_93;
- case "ITF":
- return OneDFormat.ITF;
- case "MSI":
- return OneDFormat.MSI;
- case "PLESSEY":
- return OneDFormat.PLESSEY;
- case "UPC_E":
- return OneDFormat.UPC_E;
- default:
- return OneDFormat.CODE_128;
- }
- }
- public QRECLevel GetQRCodeLevel(string value)
- {
- if (string.IsNullOrWhiteSpace(value))
- {
- return QRECLevel.H;
- }
- switch (value)
- {
- case "H":
- return QRECLevel.H;
- case "Q":
- return QRECLevel.Q;
- case "M":
- return QRECLevel.M;
- case "L":
- return QRECLevel.L;
- default:
- return QRECLevel.H;
- }
- }
- /// <summary>
- /// 12位条码补齐校验位生成13位的条码
- /// </summary>
- /// <param name="chaine"></param>
- /// <returns></returns>
- public static string EAN13(string chaine)
- {
- int i;
- int first;
- int checkSum = 0;
- string Barcode = "";
- bool tableA;
- //if (Regex.IsMatch(chaine, "^\\d{12}$"))
- {
- for (i = 1; i < 12; i += 2)
- {
- //System.Diagnostics.Debug.WriteLine(chaine.Substring(i, 1));
- checkSum += Convert.ToInt32(chaine.Substring(i, 1));
- }
- checkSum *= 3;
- for (i = 0; i < 12; i += 2)
- {
- checkSum += Convert.ToInt32(chaine.Substring(i, 1));
- }
- chaine += (10 - checkSum % 10) % 10;
- string Barcode13Digits = chaine.ToString();
- Barcode = chaine.Substring(0, 1) + (char)(65 + Convert.ToInt32(chaine.Substring(1, 1)));
- first = Convert.ToInt32(chaine.Substring(0, 1));
- for (i = 2; i <= 6; i++)
- {
- tableA = false;
- switch (i)
- {
- case 2:
- if (first >= 0 && first <= 3)
- {
- tableA = true;
- }
- break;
- case 3:
- if (first == 0 || first == 4 || first == 7 || first == 8)
- {
- tableA = true;
- }
- break;
- case 4:
- if (first == 0 || first == 1 || first == 4 || first == 5 || first == 9)
- {
- tableA = true;
- }
- break;
- case 5:
- if (first == 0 || first == 2 || first == 5 || first == 6 || first == 7)
- {
- tableA = true;
- }
- break;
- case 6:
- if (first == 0 || first == 3 || first == 6 || first == 8 || first == 9)
- {
- tableA = true;
- }
- break;
- }
- if (tableA)
- {
- Barcode += (char)(65 + Convert.ToInt32(chaine.Substring(i, 1)));
- }
- else
- {
- Barcode += (char)(75 + Convert.ToInt32(chaine.Substring(i, 1)));
- }
- }
- Barcode += "*";
- for (i = 7; i <= 12; i++)
- {
- Barcode += (char)(97 + Convert.ToInt32(chaine.Substring(i, 1)));
- }
- Barcode += "+";
- }
- return Barcode;
- }
- }
- public class QRCodeWriterMy : Writer
- {
- private const int QUIET_ZONE_SIZE = 4;
- /// <summary>
- /// Encode a barcode using the default settings.
- /// </summary>
- /// <param name="contents">The contents to encode in the barcode</param>
- /// <param name="format">The barcode format to generate</param>
- /// <param name="width">The preferred width in pixels</param>
- /// <param name="height">The preferred height in pixels</param>
- /// <returns>
- /// The generated barcode as a Matrix of unsigned bytes (0 == black, 255 == white)
- /// </returns>
- public BitMatrix encode(string contents, BarcodeFormat format, int width, int height)
- {
- return encode(contents, format, width, height, null);
- }
- /// <summary>
- /// </summary>
- /// <param name="contents">The contents to encode in the barcode</param>
- /// <param name="format">The barcode format to generate</param>
- /// <param name="width">The preferred width in pixels</param>
- /// <param name="height">The preferred height in pixels</param>
- /// <param name="hints">Additional parameters to supply to the encoder</param>
- /// <returns>
- /// The generated barcode as a Matrix of unsigned bytes (0 == black, 255 == white)
- /// </returns>
- public BitMatrix encode(string contents,
- BarcodeFormat format,
- int width,
- int height,
- IDictionary<EncodeHintType, object> hints)
- {
- if (String.IsNullOrEmpty(contents))
- {
- throw new ArgumentException("Found empty contents");
- }
- if (format != BarcodeFormat.QR_CODE)
- {
- throw new ArgumentException("Can only encode QR_CODE, but got " + format);
- }
- if (width < 0 || height < 0)
- {
- throw new ArgumentException("Requested dimensions are too small: " + width + 'x' + height);
- }
- var errorCorrectionLevel = ErrorCorrectionLevel.L;
- int quietZone = QUIET_ZONE_SIZE;
- if (hints != null)
- {
- if (hints.ContainsKey(EncodeHintType.ERROR_CORRECTION))
- {
- var requestedECLevel = hints[EncodeHintType.ERROR_CORRECTION];
- if (requestedECLevel != null)
- {
- errorCorrectionLevel = requestedECLevel as ErrorCorrectionLevel;
- if (errorCorrectionLevel == null)
- {
- switch (requestedECLevel.ToString().ToUpper())
- {
- case "L":
- errorCorrectionLevel = ErrorCorrectionLevel.L;
- break;
- case "M":
- errorCorrectionLevel = ErrorCorrectionLevel.M;
- break;
- case "Q":
- errorCorrectionLevel = ErrorCorrectionLevel.Q;
- break;
- case "H":
- errorCorrectionLevel = ErrorCorrectionLevel.H;
- break;
- default:
- errorCorrectionLevel = ErrorCorrectionLevel.L;
- break;
- }
- }
- }
- }
- if (hints.ContainsKey(EncodeHintType.MARGIN))
- {
- var quietZoneInt = hints[EncodeHintType.MARGIN];
- if (quietZoneInt != null)
- {
- quietZone = Convert.ToInt32(quietZoneInt.ToString());
- }
- }
- }
- var code = Encoder.encode(contents, errorCorrectionLevel, hints);
- BitMatrix bm = renderResult(code, width, height, quietZone);
- hints[EncodeHintType.WIDTH] = bm.Width;
- hints[EncodeHintType.HEIGHT] = bm.Height;
- return bm;
- }
- // Note that the input matrix uses 0 == white, 1 == black, while the output matrix uses
- // 0 == black, 255 == white (i.e. an 8 bit greyscale bitmap).
- private static BitMatrix renderResult(QRCode code, int width, int height, int quietZone)
- {
- var input = code.Matrix;
- if (input == null)
- {
- throw new InvalidOperationException();
- }
- int inputWidth = input.Width;
- int inputHeight = input.Height;
- int qrWidth = inputWidth + (quietZone << 1);
- int qrHeight = inputHeight + (quietZone << 1);
- int outputWidth = Math.Max(width, qrWidth);
- int outputHeight = Math.Max(height, qrHeight);
- int multiple = Math.Min(outputWidth / qrWidth, outputHeight / qrHeight);
- // Padding includes both the quiet zone and the extra white pixels to accommodate the requested
- // dimensions. For example, if input is 25x25 the QR will be 33x33 including the quiet zone.
- // If the requested size is 200x160, the multiple will be 4, for a QR of 132x132. These will
- // handle all the padding from 100x100 (the actual QR) up to 200x160.
- int leftPadding = (outputWidth - (inputWidth * multiple)) / 2;
- int topPadding = (outputHeight - (inputHeight * multiple)) / 2;
- //int leftPadding = 0;//(outputWidth - (inputWidth * multiple)) / 2;
- //int topPadding = 0;//(outputHeight - (inputHeight * multiple)) / 2;
- //outputWidth = (inputWidth * multiple);
- //outputHeight = (inputHeight * multiple);
- var output = new BitMatrix(outputWidth, outputHeight);
- for (int inputY = 0, outputY = topPadding; inputY < inputHeight; inputY++, outputY += multiple)
- {
- // Write the contents of this row of the barcode
- for (int inputX = 0, outputX = leftPadding; inputX < inputWidth; inputX++, outputX += multiple)
- {
- if (input[inputX, inputY] == 1)
- {
- output.setRegion(outputX, outputY, multiple, multiple);
- //int mX = outputWidth - outputX;
- //int mY = outputHeight - outputY;
- //mX = Math.Min(mX, multiple);
- //mY = Math.Min(mY, multiple);
- //output.setRegion(outputX, outputY, mX, mY);
- }
- }
- }
- return output;
- }
- }
- }
|