You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
DS7/ToolProject/TestForm/TestForm/Form1.cs

495 lines
19 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using DSWeb.Service.Output;
using iText.IO.Font;
using iText.Kernel.Colors;
using iText.Kernel.Font;
using iText.Layout.Element;
using iText.Layout.Properties;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.qrcode;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Net.Mime.MediaTypeNames;
using Rectangle = System.Drawing.Rectangle;
//using D7MqClient;
using Topshelf;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
namespace TestForm
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// DJYtoD7_OPSTATUS dj = new DJYtoD7_OPSTATUS();
}
private void button2_Click(object sender, EventArgs e)
{
var str = textBox1.Text;
var newint = 0;
try
{
newint = Convert.ToInt32(Convert.ToDecimal(str));
}catch(Exception ex)
{
}
label1.Text = newint.ToString();
}
private void button3_Click(object sender, EventArgs e)
{
//触发荣圣达读取泛微审核结果逻辑
JobFanWeiToDS7 jd = new JobFanWeiToDS7();
jd.DoWork();
}
private void button4_Click(object sender, EventArgs e)
{
try {
var width = 842;
var height = 595;
Document document = new Document(new iTextSharp.text.Rectangle( width, height), 50, 50, 20, 20);
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(@"G:\aa.pdf", FileMode.Create));
//先打开文档,往里写一段内容,最后关闭文档
document.Open();
// step 4: we add content to the document
//楷体字
//BaseFont bfComic = BaseFont.createFont("c://windows//fonts//simkai.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//方正舒体
//BaseFont bfComic = BaseFont.createFont("c://windows//fonts//FZSTK.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//方正姚体
//BaseFont bfComic = BaseFont.createFont("c://windows//fonts//FZYTK.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//仿宋体
//BaseFont bfComic = BaseFont.createFont("c://windows//fonts//SIMFANG.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//黑体
//BaseFont bfComic = BaseFont.createFont("c://windows//fonts//SIMHEI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//华文彩云
//BaseFont bfComic = BaseFont.createFont("c://windows//fonts//STCAIYUN.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//华文仿宋
//BaseFont bfComic = BaseFont.createFont("c://windows//fonts//STFANGSO.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//华文细黑
//BaseFont bfComic = BaseFont.createFont("c://windows//fonts//STXIHEI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//华文新魏
//BaseFont bfComic = BaseFont.createFont("c://windows//fonts//STXINWEI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//华文行楷
//BaseFont bfComic = BaseFont.createFont("c://windows//fonts//STXINGKA.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//华文中宋
BaseFont bfComic = BaseFont.CreateFont("c://windows//fonts//STZHONGS.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//隶书
//BaseFont bfComic = BaseFont.createFont("c://windows//fonts//SIMLI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//宋体&新宋体 (这种字体的输出不了.有问题)
//BaseFont bfComic = BaseFont.createFont("c://windows//fonts//SIMSUN.TTC", BaseFont.NOT_EMBEDDED, BaseFont.NOT_EMBEDDED);
//宋体-方正超大字符集
//BaseFont bfComic = BaseFont.createFont("c://windows//fonts//SURSONG.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//幼圆
//BaseFont bfComic = BaseFont.CreateFont("c://windows//fonts//SIMYOU.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//BaseFont baseFont = BaseFont.CreateFont("C:/WINDOWS/Fonts/SIMYOU.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
iTextSharp.text.Font title = new iTextSharp.text.Font(bfComic, 24, iTextSharp.text.Font.BOLD, iTextSharp.text.BaseColor.BLACK);
iTextSharp.text.Font infos = new iTextSharp.text.Font(bfComic, 11, 0, iTextSharp.text.BaseColor.BLACK);
var = new iTextSharp.text.Paragraph("总标题", title);
//paragraph.setSpacingAfter(50);
//paragraph.setSpacingBefore(50);
//paragraph.setAlignment(Element.ALIGN_LEFT);
//paragraph.setAlignment(Element.ALIGN_CENTER);
//paragraph.setAlignment(Element.ALIGN_RIGHT);
.Alignment=iTextSharp.text.Element.ALIGN_CENTER;
.Leading = 50;
BarCodeDrawing _Code39 = new BarCodeDrawing();
_Code39.Height = 50;
_Code39.Magnify = 1;
_Code39.ViewFont = new System.Drawing.Font("宋体", 13);
var = "2013121542";
System.Drawing.Image _codeImage = _Code39.GetCodeImage(, BarCodeDrawing.BarCodeDrawingModel.BarCodeDrawingNormal, true);
System.IO.MemoryStream _Stream = new System.IO.MemoryStream();
_codeImage.Save(_Stream, System.Drawing.Imaging.ImageFormat.Jpeg);
string markPath = System.Environment.CurrentDirectory + $"\\temp{条码号}.jpg";
_codeImage.Save(markPath);
iTextSharp.text.Image im = iTextSharp.text.Image.GetInstance(markPath);
im.SetAbsolutePosition(width-280, height-60);
im.ScaleAbsolute(260, 40);
document.Add();
document.Add(im);
for (var _i = 0; _i < 5; _i++)
{
var newinfo = new iTextSharp.text.Paragraph("行信息" + _i.ToString(), infos);
newinfo.FirstLineIndent= 100;
newinfo.Leading= 30;
document.Add(newinfo);
}
var footer = new iTextSharp.text.Paragraph("结尾落款" , infos);
//footer.FirstLineIndent = 150;
footer.Leading = 60;
footer.Alignment = iTextSharp.text.Element.ALIGN_RIGHT;
document.Add(footer);
document.Close();
File.Delete(markPath);
//System.out.println(">>> Export : " + "D://ChinesePDF005__.pdf");
}
catch(Exception ex)
{
}
}
public class BarCodeDrawing
{
private Hashtable m_BarCodeDrawing = new Hashtable();
private byte m_Magnify = 0;
/// <summary>
/// 放大倍数
/// </summary>
public byte Magnify { get { return m_Magnify; } set { m_Magnify = value; } }
private int m_Height = 40;
/// <summary>
/// 图形高
/// </summary>
public int Height { get { return m_Height; } set { m_Height = value; } }
private System.Drawing.Font m_ViewFont = null;
/// <summary>
/// 字体大小
/// </summary>
public System.Drawing.Font ViewFont { get { return m_ViewFont; } set { m_ViewFont = value; } }
public BarCodeDrawing()
{
m_BarCodeDrawing.Add("A", "1101010010110");
m_BarCodeDrawing.Add("B", "1011010010110");
m_BarCodeDrawing.Add("C", "1101101001010");
m_BarCodeDrawing.Add("D", "1010110010110");
m_BarCodeDrawing.Add("E", "1101011001010");
m_BarCodeDrawing.Add("F", "1011011001010");
m_BarCodeDrawing.Add("G", "1010100110110");
m_BarCodeDrawing.Add("H", "1101010011010");
m_BarCodeDrawing.Add("I", "1011010011010");
m_BarCodeDrawing.Add("J", "1010110011010");
m_BarCodeDrawing.Add("K", "1101010100110");
m_BarCodeDrawing.Add("L", "1011010100110");
m_BarCodeDrawing.Add("M", "1101101010010");
m_BarCodeDrawing.Add("N", "1010110100110");
m_BarCodeDrawing.Add("O", "1101011010010");
m_BarCodeDrawing.Add("P", "1011011010010");
m_BarCodeDrawing.Add("Q", "1010101100110");
m_BarCodeDrawing.Add("R", "1101010110010");
m_BarCodeDrawing.Add("S", "1011010110010");
m_BarCodeDrawing.Add("T", "1010110110010");
m_BarCodeDrawing.Add("U", "1100101010110");
m_BarCodeDrawing.Add("V", "1001101010110");
m_BarCodeDrawing.Add("W", "1100110101010");
m_BarCodeDrawing.Add("X", "1001011010110");
m_BarCodeDrawing.Add("Y", "1100101101010");
m_BarCodeDrawing.Add("Z", "1001101101010");
m_BarCodeDrawing.Add("0", "1010011011010");
m_BarCodeDrawing.Add("1", "1101001010110");
m_BarCodeDrawing.Add("2", "1011001010110");
m_BarCodeDrawing.Add("3", "1101100101010");
m_BarCodeDrawing.Add("4", "1010011010110");
m_BarCodeDrawing.Add("5", "1101001101010");
m_BarCodeDrawing.Add("6", "1011001101010");
m_BarCodeDrawing.Add("7", "1010010110110");
m_BarCodeDrawing.Add("8", "1101001011010");
m_BarCodeDrawing.Add("9", "1011001011010");
m_BarCodeDrawing.Add("+", "1001010010010");
m_BarCodeDrawing.Add("-", "1001010110110");
m_BarCodeDrawing.Add("*", "1001011011010");
m_BarCodeDrawing.Add("/", "1001001010010");
m_BarCodeDrawing.Add("%", "1010010010010");
m_BarCodeDrawing.Add("contentquot", "1001001001010");
m_BarCodeDrawing.Add(".", "1100101011010");
m_BarCodeDrawing.Add(" ", "1001101011010");
}
public enum BarCodeDrawingModel
{
/// <summary>
/// 基本类别 1234567890ABC
/// </summary>
BarCodeDrawingNormal,
/// <summary>
/// 全ASCII方式 +A+B 来表示小写
/// </summary>
BarCodeDrawingFullAscII
}
/// <summary>
/// 获得条码图形
/// </summary>
/// <param name="p_Text">文字信息</param>
/// <param name="p_Model">类别</param>
/// <param name="p_StarChar">是否增加前后*号</param>
/// <returns>图形</returns>
public Bitmap GetCodeImage(string p_Text, BarCodeDrawingModel p_Model, bool p_StarChar)
{
string _ValueText = "";
string _CodeText = "";
char[] _ValueChar = null;
switch (p_Model)
{
case BarCodeDrawingModel.BarCodeDrawingNormal:
_ValueText = p_Text.ToUpper();
break;
default:
_ValueChar = p_Text.ToCharArray();
for (int i = 0; i != _ValueChar.Length; i++)
{
if ((int)_ValueChar[i] >= 97 && (int)_ValueChar[i] <= 122)
{
_ValueText += "+" + _ValueChar[i].ToString().ToUpper();
}
else
{
_ValueText += _ValueChar[i].ToString();
}
}
break;
}
_ValueChar = _ValueText.ToCharArray();
if (p_StarChar == true) _CodeText += m_BarCodeDrawing["*"];
for (int i = 0; i != _ValueChar.Length; i++)
{
if (p_StarChar == true && _ValueChar[i] == '*') throw new Exception("带有起始符号不能出现*");
object _CharCode = m_BarCodeDrawing[_ValueChar[i].ToString()];
if (_CharCode == null) throw new Exception("不可用的字符" + _ValueChar[i].ToString());
_CodeText += _CharCode.ToString();
}
if (p_StarChar == true) _CodeText += m_BarCodeDrawing["*"];
Bitmap _CodeBmp = GetImage(_CodeText);
GetViewImage(_CodeBmp, p_Text);
return _CodeBmp;
}
/// <summary>
/// 绘制编码图形
/// </summary>
/// <param name="p_Text">编码</param>
/// <returns>图形</returns>
private Bitmap GetImage(string p_Text)
{
char[] _Value = p_Text.ToCharArray();
//宽 == 需要绘制的数量*放大倍数 + 两个字的宽
Bitmap _CodeImage = new Bitmap(_Value.Length * ((int)m_Magnify + 1), (int)m_Height);
Graphics _Garphics = Graphics.FromImage(_CodeImage);
_Garphics.FillRectangle(Brushes.White, new Rectangle(0, 0, _CodeImage.Width, _CodeImage.Height));
int _LenEx = 0;
for (int i = 0; i != _Value.Length; i++)
{
int _DrawWidth = m_Magnify + 1;
if (_Value[i] == '1')
{
_Garphics.FillRectangle(Brushes.Black, new Rectangle(_LenEx, 0, _DrawWidth, m_Height));
}
else
{
_Garphics.FillRectangle(Brushes.White, new Rectangle(_LenEx, 0, _DrawWidth, m_Height));
}
_LenEx += _DrawWidth;
}
_Garphics.Dispose();
return _CodeImage;
}
/// <summary>
/// 绘制文字
/// </summary>
/// <param name="p_CodeImage">图形</param>
/// <param name="p_Text">文字</param>
private void GetViewImage(Bitmap p_CodeImage, string p_Text)
{
if (m_ViewFont == null) return;
Graphics _Graphics = Graphics.FromImage(p_CodeImage);
SizeF _FontSize = _Graphics.MeasureString(p_Text, m_ViewFont);
if (_FontSize.Width > p_CodeImage.Width || _FontSize.Height > p_CodeImage.Height - 20)
{
_Graphics.Dispose();
return;
}
int _StarHeight = p_CodeImage.Height - (int)_FontSize.Height;
_Graphics.FillRectangle(Brushes.White, new Rectangle(0, _StarHeight, p_CodeImage.Width, (int)_FontSize.Height));
int _StarWidth = (p_CodeImage.Width - (int)_FontSize.Width) / 2;
_Graphics.DrawString(p_Text, m_ViewFont, Brushes.Black, _StarWidth, _StarHeight);
_Graphics.Dispose();
}
}
private void button5_Click(object sender, EventArgs e)
{
string strClass = "TestForm.Test"; // 命名空间+类名
string strMethod = "Method"; // 方法名
Type type; // 存储类
Object obj; // 存储类的实例
type = Type.GetType(strClass); // 通过类名获取同名类
obj = System.Activator.CreateInstance(type); // 创建实例
MethodInfo method = type.GetMethod(strMethod, new Type[] { }); // 获取方法信息
object[] parameters = null;
method.Invoke(obj, parameters); // 调用方法,参数为空
// 注意获取重载方法,需要指定参数类型
method = type.GetMethod(strMethod, new Type[] { typeof(string) }); // 获取方法信息
parameters = new object[] { "hello" };
method.Invoke(obj, parameters); // 调用方法,有参数
method = type.GetMethod(strMethod, new Type[] { typeof(string), typeof(string) }); // 获取方法信息
parameters = new object[] { "hello", "你好" };
string result = (string)method.Invoke(obj, parameters); // 调用方法,有参数,有返回值
Console.WriteLine("Method 返回值:" + result); // 输出返回值
// 获取静态方法类名
string className = MethodBase.GetCurrentMethod().ReflectedType.FullName;
Console.WriteLine("当前静态方法类名:" + className);
//Console.ReadKey();
}
private IConnection mqConn;
private void button6_Click(object sender, EventArgs e)
{
textBox2.Text= SetLength(textBox2.Text, 20);
}
public static string SetLength(string oldstr, int length)
{
var str = string.IsNullOrWhiteSpace(oldstr) ? "" : (len(oldstr) > length) ? SubString2(oldstr, 0, length) : oldstr;
return str;
}
public static int len(string str)
{
System.Text.ASCIIEncoding n = new System.Text.ASCIIEncoding();
byte[] b = n.GetBytes(str);
int length = 0; // l 为字符串的实际长度
for (int i = 0; i <= b.Length - 1; i++)
{
if (b[i] == 63) //判断是否为汉字或全脚符号
{
length++;
}
length++;
}
return length;
}
public static string SubString2(string str, int startIndex, int length)
{
byte[] b = System.Text.Encoding.Default.GetBytes(str);
return System.Text.Encoding.Default.GetString(b, startIndex, length);
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
}
class Test
{
// 无参数,无返回值方法
public void Method()
{
Console.WriteLine("Method无参数 调用成功!");
}
// 有参数,无返回值方法
public void Method(string str)
{
Console.WriteLine("Method有参数 调用成功!参数 " + str);
}
// 有参数,有返回值方法
public string Method(string str1, string str2)
{
Console.WriteLine("Method有参数有返回值 调用成功!参数 " + str1 + ", " + str2);
string className = this.GetType().FullName; // 非静态方法获取类名
return className;
}
}
}