Skip to content

Commit 8830e02

Browse files
committed
支持自动保持最后一次盖章时的选项,下次打开默认加载最后一次的配置
1 parent 64b28a8 commit 8830e02

File tree

4 files changed

+246
-5
lines changed

4 files changed

+246
-5
lines changed

PDFQFZ/Form1.Designer.cs

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

PDFQFZ/Form1.cs

Lines changed: 126 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using iTextSharp.text;
1313
using iTextSharp.text.exceptions;
1414
using Org.BouncyCastle.Crypto.Generators;
15+
using PDFQFZ.Library;
1516

1617
namespace PDFQFZ
1718
{
@@ -24,11 +25,11 @@ public partial class Form1 : Form
2425
DataTable dtPos = new DataTable();//PDF各文件印章位置表
2526
DataTable dtYz = new DataTable();//PDF列表
2627
string sourcePath = "",outputPath = "",imgPath = "",previewPath = "",signText = "", password="",pdfpassword="";
27-
int wjType = 1, qfzType = 0, yzType = 0, djType = 0, qmType = 0, wzType = 3, qbflag = 0, size = 40, rotation = 0, opacity = 100, wz = 50, yzr = 10, maximg = 250, maxfgs = 20;
28+
int wjType = 1, qfzType = 0, yzType = 0, djType = 0, qmType = 0, wzType = 3, yzIndex = -1, qbflag = 0, size = 40, rotation = 0, opacity = 100, wz = 50, yzr = 10, maximg = 250, maxfgs = 20;
2829
Bitmap imgYz = null;
2930
X509Certificate2 cert = null;//证书
3031
float xzbl = 1f;
31-
32+
private string strIniFilePath = $@"{Application.StartupPath}\config.ini";//获取INI文件路径
3233

3334

3435
System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)//把DLL打包到EXE需要用到
@@ -52,22 +53,123 @@ public Form1()
5253
//程序加载
5354
private void Form1_Load(object sender, EventArgs e)
5455
{
56+
if (File.Exists(strIniFilePath))//读取时先要判读INI文件是否存在
57+
{
58+
IniFileHelper iniFileHelper = new IniFileHelper(strIniFilePath);
59+
string section = "config";
60+
string WjType = iniFileHelper.ContentValue(section, "wjType");//文件类型
61+
string QfzType = iniFileHelper.ContentValue(section, "qfzType");//骑缝章类型
62+
string YzType = iniFileHelper.ContentValue(section, "yzType");//印章类型
63+
string DjType = iniFileHelper.ContentValue(section, "djType");//叠加类型
64+
string QmType = iniFileHelper.ContentValue(section, "qmType");//签名类型
65+
string WzType = iniFileHelper.ContentValue(section, "wzType");//骑缝章位置类型
66+
string Qbflag = iniFileHelper.ContentValue(section, "qbflag");//是否切边标记
67+
string Size = iniFileHelper.ContentValue(section, "size");//印章尺寸
68+
string Rotation = iniFileHelper.ContentValue(section, "rotation");//旋转角度
69+
string Opacity = iniFileHelper.ContentValue(section, "opacity");//不透明度
70+
string Wz = iniFileHelper.ContentValue(section, "wz");//骑缝章位置
71+
string Maxfgs = iniFileHelper.ContentValue(section, "maxfgs");//骑缝章最大分割数
72+
string YzIndex = iniFileHelper.ContentValue(section, "yzIndex");//选择的印章索引
73+
signText = iniFileHelper.ContentValue(section, "signText");//签名
74+
wjType = Int32.Parse(WjType);
75+
qfzType = Int32.Parse(QfzType);
76+
yzType = Int32.Parse(YzType);
77+
djType = Int32.Parse(DjType);
78+
qmType = Int32.Parse(QmType);
79+
wzType = Int32.Parse(WzType);
80+
qbflag = Int32.Parse(Qbflag);
81+
size = Int32.Parse(Size);
82+
rotation = Int32.Parse(Rotation);
83+
opacity = Int32.Parse(Opacity);
84+
wz = Int32.Parse(Wz);
85+
maxfgs = Int32.Parse(Maxfgs);
86+
yzIndex = Int32.Parse(YzIndex);
87+
}
88+
fw = this.Width;
89+
fh = this.Height;
90+
if (yzType != 0 || qfzType == 4)
91+
{
92+
this.Size = new Size(fw + 267, fh);
93+
}
94+
else
95+
{
96+
this.Size = new Size(fw, fh);
97+
}
5598
comboType.SelectedIndex = wjType;
5699
comboQfz.SelectedIndex = qfzType;
57100
comboYz.SelectedIndex = yzType;
58101
comboDJ.SelectedIndex = djType;
59102
comboQmtype.SelectedIndex = qmType;
60103
comboBoxWZ.SelectedIndex = wzType;
61104
comboBoxQB.SelectedIndex = qbflag;
62-
fw = this.Width;
63-
fh = this.Height;
105+
textCC.Text = size.ToString();
106+
textRotation.Text = rotation.ToString();
107+
textOpacity.Text = opacity.ToString();
108+
textWzbl.Text = wz.ToString();
109+
textMaxFgs.Text = maxfgs.ToString();
110+
textname.Text = signText;
111+
if (qmType == 0)
112+
{
113+
labelname.Text = "签名";
114+
textname.Text = "";
115+
textpass.Text = "";
116+
textname.ReadOnly = true;
117+
textpass.ReadOnly = true;
118+
}
119+
else if (qmType == 1)
120+
{
121+
122+
textpass.Text = "";
123+
if (!File.Exists(certDefaultPath))
124+
{
125+
labelname.Text = "签名";
126+
textname.Text = "";
127+
textname.ReadOnly = false;
128+
}
129+
else
130+
{
131+
labelname.Text = "证书";
132+
textname.Text = certDefaultPath;
133+
textname.ReadOnly = true;
134+
}
135+
136+
textpass.ReadOnly = false;
137+
}
138+
else
139+
{
140+
labelname.Text = "证书";
141+
textname.Text = "";
142+
textpass.Text = "";
143+
textname.ReadOnly = true;
144+
textpass.ReadOnly = false;
145+
146+
OpenFileDialog file = new OpenFileDialog();
147+
file.Filter = "证书文件|*.pfx";
148+
if (file.ShowDialog() == DialogResult.OK)
149+
{
150+
textname.Text = file.FileName;
151+
}
152+
else
153+
{
154+
comboQmtype.SelectedIndex = 0;
155+
labelname.Text = "签名";
156+
textname.ReadOnly = true;
157+
textpass.ReadOnly = true;
158+
}
159+
}
64160
dtYz.Columns.Add("Name", typeof(string));
65161
dtYz.Columns.Add("Value", typeof(string));
66162
comboBoxYz.DisplayMember = "Name";
67163
comboBoxYz.ValueMember = "Value";
68164
comboBoxYz.DataSource = dtYz;
69165
if (!File.Exists(yzLog))
70166
{
167+
yzIndex = -1;
168+
//为了避免误删印章记录,然后下次打开又不盖章,再打开可能的报错,这里先重置下印章索引
169+
IniFileHelper iniFileHelper = new IniFileHelper(strIniFilePath);
170+
string section = "config";
171+
iniFileHelper.WriteIniString(section, "yzIndex", yzIndex.ToString());
172+
71173
File.Create(yzLog).Close();
72174
}
73175
else
@@ -84,6 +186,7 @@ private void Form1_Load(object sender, EventArgs e)
84186
sr.Close();
85187
sr.Dispose();
86188
}
189+
comboBoxYz.SelectedIndex = yzIndex;
87190
dt.Columns.Add("Name", typeof(string));
88191
dt.Columns.Add("Value", typeof(string));
89192
comboPDFlist.DisplayMember = "Name";
@@ -105,6 +208,7 @@ private void button1_Click(object sender, EventArgs e)
105208
qmType = comboQmtype.SelectedIndex;
106209
wzType = comboBoxWZ.SelectedIndex;
107210
qbflag = comboBoxQB.SelectedIndex;
211+
yzIndex = comboBoxYz.SelectedIndex;
108212

109213
if (qfzType == 1&& yzType == 0 && qmType == 0)
110214
{
@@ -152,6 +256,24 @@ private void button1_Click(object sender, EventArgs e)
152256
else
153257
{
154258
pdfGz();
259+
260+
//自动保持最后一次盖章的配置信息到配置文件
261+
IniFileHelper iniFileHelper = new IniFileHelper(strIniFilePath);
262+
string section = "config";
263+
iniFileHelper.WriteIniString(section, "wjType", wjType.ToString());
264+
iniFileHelper.WriteIniString(section, "qfzType", qfzType.ToString());
265+
iniFileHelper.WriteIniString(section, "yzType", yzType.ToString());
266+
iniFileHelper.WriteIniString(section, "djType", djType.ToString());
267+
iniFileHelper.WriteIniString(section, "qmType", qmType.ToString());
268+
iniFileHelper.WriteIniString(section, "wzType", wzType.ToString());
269+
iniFileHelper.WriteIniString(section, "qbflag", qbflag.ToString());
270+
iniFileHelper.WriteIniString(section, "size", size.ToString());
271+
iniFileHelper.WriteIniString(section, "rotation", rotation.ToString());
272+
iniFileHelper.WriteIniString(section, "opacity", opacity.ToString());
273+
iniFileHelper.WriteIniString(section, "wz", wz.ToString());
274+
iniFileHelper.WriteIniString(section, "maxfgs", maxfgs.ToString());
275+
iniFileHelper.WriteIniString(section, "yzIndex", yzIndex.ToString());
276+
iniFileHelper.WriteIniString(section, "signText", signText);
155277
}
156278
}
157279
else

PDFQFZ/Library/IniFileHelper.cs

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using System.Runtime.InteropServices;
7+
8+
namespace PDFQFZ.Library
9+
{
10+
internal class IniFileHelper
11+
{
12+
string strIniFilePath; // ini配置文件路径
13+
14+
// 返回0表示失败,非0为成功
15+
[DllImport("kernel32", CharSet = CharSet.Auto)]
16+
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
17+
18+
// 返回取得字符串缓冲区的长度
19+
[DllImport("kernel32", CharSet = CharSet.Auto)]
20+
private static extern long GetPrivateProfileString(string section, string key, string strDefault, StringBuilder retVal, int size, string filePath);
21+
22+
[DllImport("Kernel32.dll", CharSet = CharSet.Auto)]
23+
public static extern int GetPrivateProfileInt(string section, string key, int nDefault, string filePath);
24+
25+
/// <summary>
26+
/// 无参构造函数
27+
/// </summary>
28+
/// <returns></returns>
29+
//public IniFileHelper()
30+
//{
31+
// this.strIniFilePath = System.Windows.Forms.Application.StartupPath + "\\config.ini";
32+
//}
33+
34+
35+
/// <summary>
36+
/// 有参构造函数
37+
/// </summary>
38+
/// <param name="strIniFilePath">ini配置文件路径</param>
39+
/// <returns></returns>
40+
public IniFileHelper(string strIniFilePath)
41+
{
42+
if (strIniFilePath != null)
43+
{
44+
this.strIniFilePath = strIniFilePath;
45+
}
46+
}
47+
48+
49+
/// <summary>
50+
/// 获取ini配置文件中的字符串
51+
/// </summary>
52+
/// <param name="section">节名</param>
53+
/// <param name="key">键名</param>
54+
/// <param name="strDefault">默认值</param>
55+
/// <param name="retVal">结果缓冲区</param>
56+
/// <param name="size">结果缓冲区大小</param>
57+
/// <returns>成功true,失败false</returns>
58+
public bool GetIniString(string section, string key, string strDefault, StringBuilder retVal, int size)
59+
{
60+
long liRet = GetPrivateProfileString(section, key, strDefault, retVal, size, strIniFilePath);
61+
return (liRet >= 1);
62+
}
63+
64+
65+
/// <summary>
66+
/// 获取ini配置文件中的整型值
67+
/// </summary>
68+
/// <param name="section">节名</param>
69+
/// <param name="key">键名</param>
70+
/// <param name="nDefault">默认值</param>
71+
/// <returns></returns>
72+
public int GetIniInt(string section, string key, int nDefault)
73+
{
74+
return GetPrivateProfileInt(section, key, nDefault, strIniFilePath);
75+
}
76+
77+
78+
/// <summary>
79+
/// 往ini配置文件写入字符串
80+
/// </summary>
81+
/// <param name="section">节名</param>
82+
/// <param name="key">键名</param>
83+
/// <param name="val">要写入的字符串</param>
84+
/// <returns>成功true,失败false</returns>
85+
public bool WriteIniString(string section, string key, string val)
86+
{
87+
long liRet = WritePrivateProfileString(section, key, val, strIniFilePath);
88+
return (liRet != 0);
89+
}
90+
91+
92+
/// <summary>
93+
/// 往ini配置文件写入整型数据
94+
/// </summary>
95+
/// <param name="section">节名</param>
96+
/// <param name="key">键名</param>
97+
/// <param name="val">要写入的数据</param>
98+
/// <returns>成功true,失败false</returns>
99+
public bool WriteIniInt(string section, string key, int val)
100+
{
101+
return WriteIniString(section, key, val.ToString());
102+
}
103+
104+
/// <summary>
105+
/// 自定义读取INI文件中的内容方法
106+
/// </summary>
107+
/// <param name="Section">键</param>
108+
/// <param name="key">值</param>
109+
/// <returns></returns>
110+
public string ContentValue(string Section, string key)
111+
{
112+
113+
StringBuilder temp = new StringBuilder(1024);
114+
GetPrivateProfileString(Section, key, "", temp, 1024, strIniFilePath);
115+
return temp.ToString();
116+
}
117+
}
118+
}

PDFQFZ/PDFQFZ.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
<Compile Include="Form1.Designer.cs">
9494
<DependentUpon>Form1.cs</DependentUpon>
9595
</Compile>
96+
<Compile Include="Library\IniFileHelper.cs" />
9697
<Compile Include="Program.cs" />
9798
<Compile Include="Properties\AssemblyInfo.cs" />
9899
<None Include="Resources\O2S.Components.PDFRender4NET.dll" />

0 commit comments

Comments
 (0)