⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bar128.cs

📁 水晶报表使用例子
💻 CS
字号:
using System;
using System.Text;

namespace PrintApp
{
	/// <summary>
	/// 此类实现了BARCODE128码的转化。
	/// 具体的实现是:
	/// 1. 选择相应的字体(如Bar Sample 128AB HR)
	/// 2. 把要打印的是字符串经过函数的转化,
	///    分别由两个函数Bar128AB,Bar128SubsetC来实现
	/// 3. 把转化后的结果赋值给显示控件
	/// 4. OK
	/// </summary>
	internal class Bar128
	{
		private string BarTextOut;
		private string Tempstring;  
		private string BarTempOut;  
		private string BarCodeOut;  
		private long   Sum; 
		private string ThisChar;  
		private long   CharValue;  
		private long   CheckSumValue;  
		private string CheckSum;  
		private string StartChar;  
		private int    Weighting;  
		private int    charASCII;

		public Bar128()
		{
			// 
			// TODO: Add constructor logic here
			//
		}

		//-----------------------------------------------------------------------------
		//Convert input string to bar code 128 A or B format, Ps Subset 0 = A, 1 = B
		//-----------------------------------------------------------------------------
		public string Bar128AB(string BarTextIn, int Subset)
		{
			//Initialize input and output strings
			BarTextOut = "";
			BarTextIn = BarTextIn.Trim();

			// Set up for the subset we are in
			if(Subset == 0)
			{
				Sum = 103;
				StartChar = "{";
			}
			else
			{
				Sum = 104;
				StartChar = "|";
			}

			// Calculate the checksum, mod 103 and build output string
			for(int i = 0; i < BarTextIn.Length; i++)
			{			
				//Find the ASCII value of the current character
				ThisChar = BarTextIn.Substring(i, 1);
				charASCII = (int)ThisChar[0];
				//Calculate the bar code 128 value
				if( charASCII < 127 )
					CharValue = charASCII - 32;
				else
					CharValue = charASCII - 103;
				
				//add this value to sum for checksum work
				Sum = Sum + (CharValue * (i+1));

				//Now work on output string, no spaces in TrueType fonts, quotes replaced for Word mailmerge bug
				string tmpStr = BarTextIn.Substring(i, 1);
				if(tmpStr == " " )
					BarTextOut = BarTextOut + (char)228;
				else if(((int)ThisChar[0]) == 34)
					BarTextOut = BarTextOut + (char)226;
				else
					BarTextOut = BarTextOut + BarTextIn.Substring(i, 1);
			}

			// Find the remainder when Sum is divided by 103
			CheckSumValue = (Sum % 103);
			// Translate that value to an CII character
			if( CheckSumValue > 90 )
				CheckSum = Convert.ToString(Convert.ToChar(CheckSumValue + 103));
			else if( CheckSumValue > 0 )
				CheckSum = Convert.ToString(Convert.ToChar(CheckSumValue + 32));
			else
				CheckSum = Convert.ToString(Convert.ToChar(228));
			
			//Build ouput string, trailing space is for Windows rterization bug
			//注意此处最后连接的是"~ "而非"~",后面有空格
			BarTempOut = StartChar + BarTextOut + CheckSum + "~ ";

			//Return the string
			return  BarTempOut;
		}

		public string Bar128A(string BarTextInA)
		{
			return Bar128AB(BarTextInA, 0);
		}

		public string Bar128Aucc(string BarTextInA)
		{													 
			Tempstring = (char)172 + BarTextInA;

			return Bar128AB(Tempstring, 0);
		}

		public string Bar128B(string BarTextInB)
		{
			return Bar128AB(BarTextInB, 1);
		}

		
		// This string converts a string into a format compatible with Elfring
		// Fonts Inc bar codes. This conversion is for UCC/EAN, subset B only.
		// It adds the two start characters, scans and converts data, adds a checksum
		// and a stop character.
		//---------------------------------------------------------
		public string Bar128Bucc(string BarTextInB)
		{
			// Add FNC1 to beginning of string
			Tempstring = (char)172 + BarTextInB;

			return Bar128AB(Tempstring, 1);
		}
		
		//---------------------------------------------------------------------------
		// Convert input string to bar code 128 C format, Ps UCC 0 = no, 1 = yes
		//---------------------------------------------------------------------------
		public string Bar128SubsetC(string BarTextIn, int UCC)
		{		
			// Initialize input and output strings
			BarTextOut = "";
			BarTextIn =BarTextIn.Trim();

			// Throw away non-numeric data
			Tempstring = "";
			for(int i = 0; i< BarTextIn.Length; i++)
			{
				if(char.IsNumber(BarTextIn, i))
					Tempstring = Tempstring + BarTextIn.Substring(i, 1);
			}

			// if not an even number of digits, add a leading 0
			if((Tempstring.Length % 2) == 1) 
				Tempstring = "0" + Tempstring;
		
			// if UCC = 0,  normal start, otherwise UCC/EAN start
			if( UCC == 0) 
			{
				Sum = 105;
				StartChar = "}";
				Weighting = 1;
			}
			else
			{
				Sum = 207;
				StartChar = "}?";
				Weighting = 2;
			}

			// Calculate the checksum, % 103 and build output string
			for(int i=0; i< Tempstring.Length; i+=2)
			{
				//Break string into pairs of digits and get value
				CharValue = Convert.ToInt64(Tempstring.Substring(i, 2));
				//Multiply value times weighting and add to sum
				Sum = Sum + (CharValue * Weighting);
				Weighting = Weighting + 1;

				//translate value to CII and save in BarTextOut
				if( CharValue < 90 )
					BarTextOut = BarTextOut + (char)(CharValue + 33);
				else
					BarTextOut = BarTextOut + (char)(CharValue + 104);			
			}

			// Find the remainder when Sum is divided by 103
			CheckSumValue = (Sum % 103);
			// Translate that value to an CII character
			if( CheckSumValue < 90 )
				CheckSum = Convert.ToString(CheckSumValue + 33);
			else
				CheckSum = Convert.ToString(CheckSumValue + 104);

			//Build ouput string, trailing space for Windows rterization bug
			BarTempOut = StartChar + BarTextOut + CheckSum + "~ ";

			// Replace all quote characters with duplicate bar code character in slot 226
			// fixing MS Word mailmerge bug
			BarCodeOut = "";
			for( int ii = 0; ii< BarTempOut.Length; ii++)
			{
				//Find the CII value of the current character
				ThisChar = BarTempOut.Substring(ii, 1);
				if( Convert.ToInt32(ThisChar) == 34 )
					BarCodeOut = BarCodeOut + (char)226;
				else
					BarCodeOut = BarCodeOut + BarTempOut.Substring(ii, 1);
			}

			//Return the string
			return BarCodeOut;
		}
		
		// This string converts a string into a format compatible with Elfring
		// Fonts Inc bar codes. This conversion is for subset C only. It adds the
		// start character, throws away any non-numeric data, adds a leading zero if
		// there aren//t an even number of digits, scans and converts data into data
		// pairs, adds a checksum and a stop character.
		//-----------------------------------------------------
		public string Bar128C(string BarTextIn)
		{
			return Bar128SubsetC(BarTextIn, 0);
		}

		// This string converts a string into a format compatible with Elfring
		// Fonts Inc bar codes. This conversion is for UCC/EAN subset C only. It adds
		// the two start characters, throws away any non-numeric data, adds a leading
		// zero if there aren//t an even number of digits, scans and converts data into
		// data pairs, adds a checksum and a stop character.
		//--------------------------------------------------------
		public string Bar128Cucc(string BarTextIn)
		{
			return Bar128SubsetC(BarTextIn, 1);
		}
	}
}

/*************************************************************************************
									CODE 128 国家标准
1. code 128码格式:
  从左起: 空白区域,起始字符,数据区域,校验码,结束字符,空白区域。
  所有字符条纹图像都是以黑色开始,白色结束,只有结束字符例外。

2. 起始字符:
由于128码有三个字符集。所以有三个起始字符。
Start A : 表示后面的码值代码是从字符集A中值。  全部大写字母和标点符号和特殊符号。
        用六个黑白粗细不一表示为:{2,1,1,4,1,2}        
Start B: 表示字符集B,全部大小写字符和标点符号。数据为:{2,1,1,2,1,4}
Start C: 表示字符集C,数字00-99. 数据为:{2,1,1,2,3,2}

3. 数据字符的表示
在128码中所有数据都是有1-4的六位数组表示,总共绘制成11条黑白条纹。

校验码算法:
校验码=(起始字符值 +第一位数据值*1 +第二位数据值*2+ …. + 第 n 位数据*n )%103;

4. 结束字符:
128码结束字符只有一个在编码表中以Stop 来表示,数据为:{2,3,3,1,1,1,2};
*************************************************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -