Form1.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. using Microsoft.Office.Interop.Word;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Data;
  7. using System.Diagnostics;
  8. using System.Drawing;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows.Forms;
  14. using MsWord = Microsoft.Office.Interop.Word;
  15. using Spire.Doc;
  16. namespace XWYN_BG2023V1._0
  17. {
  18. public partial class Form1 : Form
  19. {
  20. public Form1()
  21. {
  22. InitializeComponent();
  23. }
  24. //定义变量
  25. object modulePath;//文本模板路径和名称
  26. object bgbzrPath;//报告编制人照片路径
  27. object shrPath;//区县公司审核人照片路径
  28. object opinionPath;//审核意见照片路径
  29. object datePath;//报告编制日期照片路径
  30. object sjyPath;//设计院签章路径
  31. object jsdwPath;//建设单位签章路径
  32. string strDName;//模板所在文件夹路径
  33. string newDName;//已完成文件存放路径
  34. //勘察报告模板选择
  35. private void button1_Click(object sender, EventArgs e)
  36. {
  37. if (openFileDialog1.ShowDialog() == DialogResult.OK)
  38. {
  39. modulePath = openFileDialog1.FileName;
  40. //将路径显示到文本框
  41. textBox1.Text = (string)modulePath;
  42. FileInfo fileInfo = new FileInfo(textBox1.Text);
  43. strDName = fileInfo.DirectoryName;
  44. }
  45. }
  46. //选择报告编制人签名照片
  47. private void button2_Click(object sender, EventArgs e)
  48. {
  49. if (openFileDialog1.ShowDialog() == DialogResult.OK)
  50. {
  51. bgbzrPath = openFileDialog1.FileName;
  52. textBox2.Text = (string)bgbzrPath;
  53. }
  54. }
  55. //选择区县公司审核人签名照片
  56. private void button3_Click(object sender, EventArgs e)
  57. {
  58. if (openFileDialog1.ShowDialog() == DialogResult.OK)
  59. {
  60. shrPath = openFileDialog1.FileName;
  61. textBox3.Text = (string)shrPath;
  62. }
  63. }
  64. //选择审核意见照片
  65. private void button4_Click(object sender, EventArgs e)
  66. {
  67. if (openFileDialog1.ShowDialog() == DialogResult.OK)
  68. {
  69. opinionPath = openFileDialog1.FileName;
  70. textBox4.Text = (string)opinionPath;
  71. }
  72. }
  73. //选择报告编制日期照片
  74. private void button5_Click(object sender, EventArgs e)
  75. {
  76. if (openFileDialog1.ShowDialog() == DialogResult.OK)
  77. {
  78. datePath = openFileDialog1.FileName;
  79. textBox5.Text = (string)datePath;
  80. }
  81. }
  82. //选择设计院签章照片
  83. private void button6_Click(object sender, EventArgs e)
  84. {
  85. if (openFileDialog1.ShowDialog() == DialogResult.OK)
  86. {
  87. sjyPath = openFileDialog1.FileName;
  88. textBox6.Text = (string)sjyPath;
  89. }
  90. }
  91. //选择建设单位签章照片
  92. private void button7_Click(object sender, EventArgs e)
  93. {
  94. if (openFileDialog1.ShowDialog() == DialogResult.OK)
  95. {
  96. jsdwPath = openFileDialog1.FileName;
  97. textBox7.Text = (string)jsdwPath;
  98. }
  99. }
  100. //定义集合用来存储文件夹名
  101. ArrayList nameList = new ArrayList();
  102. object Nothing = System.Reflection.Missing.Value;
  103. //添加签名按钮功能
  104. private void button8_Click(object sender, EventArgs e)
  105. {
  106. //实例化DirectoryInfo对象
  107. DirectoryInfo dinfo = new DirectoryInfo(strDName);
  108. //获取指定目录下的所有子目录及文件类型
  109. FileSystemInfo[] fsinfos = dinfo.GetFileSystemInfos();
  110. FileInfo fileInfo = new FileInfo((string)modulePath);
  111. //创建已完成文件夹
  112. newDName = strDName + "\\" + "已完成";
  113. Directory.CreateDirectory(newDName);
  114. richTextBox1.Text += "开始处理...\n";
  115. //创建一个名为wordApp的组件对象并初始化
  116. MsWord.Application wordApp = new MsWord.Application();//初始化
  117. foreach (FileSystemInfo fsinfo in fsinfos)
  118. {
  119. //判断是否文件夹
  120. if (fsinfo is DirectoryInfo)
  121. {
  122. //使用获取的文件夹名称实例化DirectoryInfo对象
  123. DirectoryInfo dirinfo = new DirectoryInfo(fsinfo.FullName);
  124. //复制报告、改名后存到总文件夹下
  125. //fileInfo.CopyTo(strDName+ "\\"+ dirinfo.Name + "勘察报告.doc", true);
  126. //复制报告、改名后存到各站点对应文件夹下
  127. //fileInfo.CopyTo(fsinfo.FullName + "\\" + dirinfo.Name + "勘察报告.doc", true);
  128. //复制并保存文件
  129. string newFileName = newDName + "\\" + dirinfo.Name + "勘察报告.doc";
  130. fileInfo.CopyTo(newFileName, true);
  131. //将文件夹名添加到集合
  132. nameList.Add(dirinfo.Name);
  133. //richTextBox1.Text += dirinfo.Name + "已完成\n";
  134. //richTextBox1.Text += dirinfo.FullName+"\n";
  135. object filePath = newFileName;
  136. //定义插入图片是否为外部链接
  137. object LinkToFile = false;
  138. //定义插入图片是否随word文档一起保存
  139. object SaveWithDocument = true;
  140. //定义集合存放机房照片路径
  141. ArrayList hosePath = new ArrayList();
  142. //定义集合存放馈窗照片路径
  143. ArrayList windowPath = new ArrayList();
  144. //定义集合存放勘察路由照片路径
  145. ArrayList routerPath = new ArrayList();
  146. //定义存放草图照片路径
  147. ArrayList sketchPath = new ArrayList();
  148. //打开word文档
  149. Microsoft.Office.Interop.Word.Document doc = wordApp.Documents.Open(ref filePath, ref Nothing, ref Nothing, ref Nothing,
  150. ref Nothing, ref Nothing, ref Nothing, ref Nothing,
  151. ref Nothing, ref Nothing, ref Nothing, ref Nothing,
  152. ref Nothing, ref Nothing, ref Nothing, ref Nothing);
  153. try
  154. {
  155. //标签
  156. object bookMark1 = "机房";
  157. if (doc.Bookmarks.Exists(Convert.ToString(bookMark1)) == true)
  158. {
  159. //查找书签
  160. doc.Bookmarks.get_Item(ref bookMark1).Select();
  161. //设置图片位置
  162. wordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
  163. //遍历子文件夹,获取照片明细
  164. FileSystemInfo[] filesinfos = dirinfo.GetFileSystemInfos();
  165. foreach (FileSystemInfo fsi in filesinfos)
  166. {
  167. if (fsi.Name.Contains("机房"))
  168. {
  169. hosePath.Add(Convert.ToString(fsi.FullName));
  170. }
  171. }
  172. for (int i = 0; i < hosePath.Count; i++)
  173. {
  174. //在书签的位置添加图片
  175. InlineShape inlineShape = wordApp.Selection.InlineShapes.AddPicture((string)hosePath[i], ref LinkToFile, ref SaveWithDocument, ref Nothing);
  176. //设置图片大小
  177. inlineShape.Width = 180;
  178. inlineShape.Height = 230;
  179. //richTextBox1.Text += hosePath[i] + "\n";
  180. //每张照片后添加两个空格进行分割
  181. wordApp.Selection.TypeText(" ");
  182. }
  183. }
  184. object bookMark2 = "路由";
  185. if (doc.Bookmarks.Exists(Convert.ToString(bookMark2)) == true)
  186. {
  187. //查找书签
  188. doc.Bookmarks.get_Item(ref bookMark2).Select();
  189. //设置图片位置
  190. wordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
  191. //遍历子文件夹,获取照片明细
  192. FileSystemInfo[] filesinfos = dirinfo.GetFileSystemInfos();
  193. foreach (FileSystemInfo fsi in filesinfos)
  194. {
  195. if (fsi.Name.Contains("路由"))
  196. {
  197. routerPath.Add(Convert.ToString(fsi.FullName));
  198. }
  199. }
  200. for (int i = 0; i < routerPath.Count; i++)
  201. {
  202. //在书签的位置添加图片
  203. InlineShape inlineShape = wordApp.Selection.InlineShapes.AddPicture((string)routerPath[i], ref LinkToFile, ref SaveWithDocument, ref Nothing);
  204. //设置图片大小
  205. inlineShape.Width = 180;
  206. inlineShape.Height = 230;
  207. //richTextBox1.Text += hosePath[i] + "\n";
  208. //每张照片后添加两个空格进行分割
  209. wordApp.Selection.TypeText(" ");
  210. }
  211. }
  212. object bookMark3 = "馈窗";
  213. if (doc.Bookmarks.Exists(Convert.ToString(bookMark3)) == true)
  214. {
  215. //查找书签
  216. doc.Bookmarks.get_Item(ref bookMark3).Select();
  217. //设置图片位置
  218. wordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
  219. //遍历子文件夹,获取照片明细
  220. FileSystemInfo[] filesinfos = dirinfo.GetFileSystemInfos();
  221. foreach (FileSystemInfo fsi in filesinfos)
  222. {
  223. if (fsi.Name.Contains("馈"))
  224. {
  225. windowPath.Add(Convert.ToString(fsi.FullName));
  226. }
  227. }
  228. for (int i = 0; i < windowPath.Count; i++)
  229. {
  230. //在书签的位置添加图片
  231. InlineShape inlineShape = wordApp.Selection.InlineShapes.AddPicture((string)windowPath[i], ref LinkToFile, ref SaveWithDocument, ref Nothing);
  232. //设置图片大小
  233. inlineShape.Width = 180;
  234. inlineShape.Height = 230;
  235. //richTextBox1.Text += hosePath[i] + "\n";
  236. //每张照片后添加两个空格进行分割
  237. wordApp.Selection.TypeText(" ");
  238. }
  239. }
  240. object bookMark4 = "草图";
  241. if (doc.Bookmarks.Exists(Convert.ToString(bookMark4)) == true)
  242. {
  243. //查找书签
  244. doc.Bookmarks.get_Item(ref bookMark4).Select();
  245. //设置图片位置
  246. wordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
  247. //遍历子文件夹,获取照片明细
  248. FileSystemInfo[] filesinfos = dirinfo.GetFileSystemInfos();
  249. foreach (FileSystemInfo fsi in filesinfos)
  250. {
  251. if (fsi.Name.Contains("草图"))
  252. {
  253. sketchPath.Add(fsi.FullName);
  254. }
  255. }
  256. for (int i = 0; i < sketchPath.Count; i++)
  257. {
  258. //在书签的位置添加图片
  259. InlineShape inlineShape = wordApp.Selection.InlineShapes.AddPicture((string)sketchPath[i], ref LinkToFile, ref SaveWithDocument, ref Nothing);
  260. //设置图片大小
  261. inlineShape.Width = 400;
  262. inlineShape.Height = 560;
  263. //richTextBox1.Text += hosePath[i] + "\n";
  264. }
  265. }
  266. object bookMark5 = "报告编制人";
  267. if (doc.Bookmarks.Exists(Convert.ToString(bookMark5)) == true)
  268. {
  269. //查找书签
  270. doc.Bookmarks.get_Item(ref bookMark5).Select();//wdAlignParagraphRight
  271. //设置图片位置
  272. wordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
  273. //在书签的位置添加图片
  274. InlineShape inlineShape = wordApp.Selection.InlineShapes.AddPicture((string)bgbzrPath, ref LinkToFile, ref SaveWithDocument, ref Nothing);
  275. //设置图片大小
  276. inlineShape.Width = 50;
  277. inlineShape.Height = 30;
  278. inlineShape.Select();
  279. //inlineShape.ConvertToShape().IncrementLeft(-60.0f);
  280. inlineShape.ConvertToShape().IncrementTop(-10f);
  281. //将图片设置浮动在文字上方
  282. //inlineShape.ConvertToShape().WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapBehind;
  283. //richTextBox1.Text += nameList[i] + "勘察报告编制人已完成" + "\n";
  284. }
  285. object bookMark6 = "区县审核人";
  286. if (doc.Bookmarks.Exists(Convert.ToString(bookMark6)) == true)
  287. {
  288. //查找书签
  289. doc.Bookmarks.get_Item(ref bookMark6).Select();//wdAlignParagraphRight
  290. //设置图片位置
  291. wordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
  292. //在书签的位置添加图片
  293. InlineShape inlineShape = wordApp.Selection.InlineShapes.AddPicture((string)shrPath, ref LinkToFile, ref SaveWithDocument, ref Nothing);
  294. //设置图片大小
  295. inlineShape.Width = 50;
  296. inlineShape.Height = 30;
  297. inlineShape.Select();
  298. inlineShape.ConvertToShape().IncrementTop(-10f);
  299. //将图片设置浮动在文字上方
  300. //inlineShape.ConvertToShape().WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapBehind;
  301. //richTextBox1.Text += nameList[i] + "勘察报告审核人已完成" + "\n";
  302. }
  303. object bookMark7 = "审核意见";
  304. if (doc.Bookmarks.Exists(Convert.ToString(bookMark7)) == true)
  305. {
  306. //查找书签
  307. doc.Bookmarks.get_Item(ref bookMark7).Select();
  308. //设置图片位置
  309. wordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
  310. //在书签的位置添加图片
  311. InlineShape inlineShape = wordApp.Selection.InlineShapes.AddPicture((string)opinionPath, ref LinkToFile, ref SaveWithDocument, ref Nothing);
  312. //设置图片大小
  313. inlineShape.Width = 50;
  314. inlineShape.Height = 30;
  315. inlineShape.Select();
  316. inlineShape.ConvertToShape().IncrementTop(-10f);
  317. //将图片设置浮动在文字上方
  318. //inlineShape.ConvertToShape().WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapBehind;
  319. //richTextBox1.Text += nameList[i] + "勘察报告审核意见已完成" + "\n";
  320. }
  321. object bookMark8 = "编制日期";
  322. if (doc.Bookmarks.Exists(Convert.ToString(bookMark8)) == true)
  323. {
  324. //查找书签
  325. doc.Bookmarks.get_Item(ref bookMark8).Select();
  326. //设置图片位置
  327. wordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
  328. //在书签的位置添加图片
  329. InlineShape inlineShape = wordApp.Selection.InlineShapes.AddPicture((string)datePath, ref LinkToFile, ref SaveWithDocument, ref Nothing);
  330. //设置图片大小
  331. inlineShape.Width = 60;
  332. inlineShape.Height = 30;
  333. inlineShape.Select();
  334. inlineShape.ConvertToShape().IncrementTop(-10f);
  335. //将图片设置浮动在文字上方
  336. //inlineShape.ConvertToShape().WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapBehind;
  337. //richTextBox1.Text += nameList[i] + "勘察报告审核日期已完成" + "\n";
  338. }
  339. object bookMark9 = "设计单位";
  340. if (doc.Bookmarks.Exists(Convert.ToString(bookMark9)) == true)
  341. {
  342. //查找书签
  343. doc.Bookmarks.get_Item(ref bookMark9).Select();
  344. //设置图片位置
  345. wordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
  346. //在书签的位置添加图片
  347. InlineShape inlineShape = wordApp.Selection.InlineShapes.AddPicture((string)sjyPath, ref LinkToFile, ref SaveWithDocument, ref Nothing);
  348. //设置图片大小
  349. inlineShape.Width = 125;
  350. inlineShape.Height = 120;
  351. inlineShape.Select();
  352. inlineShape.ConvertToShape().IncrementTop(-10f);
  353. //richTextBox1.Text += nameList[i] + "勘察报告设计院签章已完成" + "\n";
  354. }
  355. object bookMark10 = "建设单位";
  356. if (doc.Bookmarks.Exists(Convert.ToString(bookMark10)) == true)
  357. {
  358. //查找书签
  359. doc.Bookmarks.get_Item(ref bookMark10).Select();
  360. //设置图片位置
  361. wordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
  362. //在书签的位置添加图片
  363. InlineShape inlineShape = wordApp.Selection.InlineShapes.AddPicture((string)jsdwPath, ref LinkToFile, ref SaveWithDocument, ref Nothing);
  364. //设置图片大小
  365. inlineShape.Width = 140;
  366. inlineShape.Height = 120;
  367. inlineShape.Select();
  368. inlineShape.ConvertToShape().IncrementTop(-10f);
  369. //richTextBox1.Text += nameList[i] + "勘察报告设计院建设单位签章已完成" + "\n";
  370. }
  371. //else
  372. //{
  373. // //word文档中不存在该书签,关闭文档
  374. // doc.Close(ref Nothing, ref Nothing, ref Nothing);
  375. // richTextBox1.Text += nameList[i] + "书签不存在\n";
  376. //}
  377. richTextBox1.Text += dirinfo.Name + "已处理完成\n";
  378. }
  379. catch
  380. {
  381. }
  382. //接收word中所有的修订
  383. doc.AcceptAllRevisions();
  384. //保存
  385. doc.Save();
  386. //关闭word文档
  387. doc.Close(ref Nothing, ref Nothing, ref Nothing);
  388. }
  389. }
  390. richTextBox1.Text += "\n已完成" + nameList.Count + "个文档的签名添加";
  391. //关闭进程
  392. Process myProcess = new Process();
  393. Process[] wordProcess = Process.GetProcessesByName("winword");
  394. for (int i = 0; i < wordProcess.Length; i++)//这里是找到那些没有界面的Word进程
  395. {
  396. //IntPtr ip = wordProcess[i].MainWindowHandle;
  397. //string str = wordProcess[i].MainWindowTitle; //发现程序中打开跟用户自己打开的区别就在这个属性
  398. // //用户打开的str 是文件的名称,程序中打开的就是空字符串
  399. //if (str == (string)nameList[i]) //如果打开的word进程太多,这条语句会报错:超出索引范围
  400. //{
  401. // wordProcess[i].Kill();
  402. //}
  403. wordProcess[i].Kill();
  404. }
  405. }
  406. }
  407. }