123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466 |
- using Microsoft.Office.Interop.Word;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Diagnostics;
- using System.Drawing;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using MsWord = Microsoft.Office.Interop.Word;
- using Spire.Doc;
- namespace XWYN_BG2023V1._0
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- //定义变量
- object modulePath;//文本模板路径和名称
- object bgbzrPath;//报告编制人照片路径
- object shrPath;//区县公司审核人照片路径
- object opinionPath;//审核意见照片路径
- object datePath;//报告编制日期照片路径
- object sjyPath;//设计院签章路径
- object jsdwPath;//建设单位签章路径
- string strDName;//模板所在文件夹路径
- string newDName;//已完成文件存放路径
- //勘察报告模板选择
- private void button1_Click(object sender, EventArgs e)
- {
- if (openFileDialog1.ShowDialog() == DialogResult.OK)
- {
- modulePath = openFileDialog1.FileName;
- //将路径显示到文本框
- textBox1.Text = (string)modulePath;
- FileInfo fileInfo = new FileInfo(textBox1.Text);
- strDName = fileInfo.DirectoryName;
- }
- }
- //选择报告编制人签名照片
- private void button2_Click(object sender, EventArgs e)
- {
- if (openFileDialog1.ShowDialog() == DialogResult.OK)
- {
- bgbzrPath = openFileDialog1.FileName;
- textBox2.Text = (string)bgbzrPath;
- }
- }
- //选择区县公司审核人签名照片
- private void button3_Click(object sender, EventArgs e)
- {
- if (openFileDialog1.ShowDialog() == DialogResult.OK)
- {
- shrPath = openFileDialog1.FileName;
- textBox3.Text = (string)shrPath;
- }
- }
- //选择审核意见照片
- private void button4_Click(object sender, EventArgs e)
- {
- if (openFileDialog1.ShowDialog() == DialogResult.OK)
- {
- opinionPath = openFileDialog1.FileName;
- textBox4.Text = (string)opinionPath;
- }
- }
- //选择报告编制日期照片
- private void button5_Click(object sender, EventArgs e)
- {
- if (openFileDialog1.ShowDialog() == DialogResult.OK)
- {
- datePath = openFileDialog1.FileName;
- textBox5.Text = (string)datePath;
- }
- }
- //选择设计院签章照片
- private void button6_Click(object sender, EventArgs e)
- {
- if (openFileDialog1.ShowDialog() == DialogResult.OK)
- {
- sjyPath = openFileDialog1.FileName;
- textBox6.Text = (string)sjyPath;
- }
- }
- //选择建设单位签章照片
- private void button7_Click(object sender, EventArgs e)
- {
- if (openFileDialog1.ShowDialog() == DialogResult.OK)
- {
- jsdwPath = openFileDialog1.FileName;
- textBox7.Text = (string)jsdwPath;
- }
- }
- //定义集合用来存储文件夹名
- ArrayList nameList = new ArrayList();
- object Nothing = System.Reflection.Missing.Value;
- //添加签名按钮功能
- private void button8_Click(object sender, EventArgs e)
- {
- //实例化DirectoryInfo对象
- DirectoryInfo dinfo = new DirectoryInfo(strDName);
- //获取指定目录下的所有子目录及文件类型
- FileSystemInfo[] fsinfos = dinfo.GetFileSystemInfos();
- FileInfo fileInfo = new FileInfo((string)modulePath);
- //创建已完成文件夹
- newDName = strDName + "\\" + "已完成";
- Directory.CreateDirectory(newDName);
- richTextBox1.Text += "开始处理...\n";
- //创建一个名为wordApp的组件对象并初始化
- MsWord.Application wordApp = new MsWord.Application();//初始化
- foreach (FileSystemInfo fsinfo in fsinfos)
- {
- //判断是否文件夹
- if (fsinfo is DirectoryInfo)
- {
- //使用获取的文件夹名称实例化DirectoryInfo对象
- DirectoryInfo dirinfo = new DirectoryInfo(fsinfo.FullName);
- //复制报告、改名后存到总文件夹下
- //fileInfo.CopyTo(strDName+ "\\"+ dirinfo.Name + "勘察报告.doc", true);
- //复制报告、改名后存到各站点对应文件夹下
- //fileInfo.CopyTo(fsinfo.FullName + "\\" + dirinfo.Name + "勘察报告.doc", true);
- //复制并保存文件
- string newFileName = newDName + "\\" + dirinfo.Name + "勘察报告.doc";
- fileInfo.CopyTo(newFileName, true);
- //将文件夹名添加到集合
- nameList.Add(dirinfo.Name);
- //richTextBox1.Text += dirinfo.Name + "已完成\n";
- //richTextBox1.Text += dirinfo.FullName+"\n";
- object filePath = newFileName;
- //定义插入图片是否为外部链接
- object LinkToFile = false;
- //定义插入图片是否随word文档一起保存
- object SaveWithDocument = true;
- //定义集合存放机房照片路径
- ArrayList hosePath = new ArrayList();
- //定义集合存放馈窗照片路径
- ArrayList windowPath = new ArrayList();
- //定义集合存放勘察路由照片路径
- ArrayList routerPath = new ArrayList();
- //定义存放草图照片路径
- ArrayList sketchPath = new ArrayList();
- //打开word文档
- Microsoft.Office.Interop.Word.Document doc = wordApp.Documents.Open(ref filePath, ref Nothing, ref Nothing, ref Nothing,
- ref Nothing, ref Nothing, ref Nothing, ref Nothing,
- ref Nothing, ref Nothing, ref Nothing, ref Nothing,
- ref Nothing, ref Nothing, ref Nothing, ref Nothing);
- try
- {
- //标签
- object bookMark1 = "机房";
- if (doc.Bookmarks.Exists(Convert.ToString(bookMark1)) == true)
- {
- //查找书签
- doc.Bookmarks.get_Item(ref bookMark1).Select();
- //设置图片位置
- wordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
- //遍历子文件夹,获取照片明细
- FileSystemInfo[] filesinfos = dirinfo.GetFileSystemInfos();
- foreach (FileSystemInfo fsi in filesinfos)
- {
- if (fsi.Name.Contains("机房"))
- {
- hosePath.Add(Convert.ToString(fsi.FullName));
- }
- }
- for (int i = 0; i < hosePath.Count; i++)
- {
- //在书签的位置添加图片
- InlineShape inlineShape = wordApp.Selection.InlineShapes.AddPicture((string)hosePath[i], ref LinkToFile, ref SaveWithDocument, ref Nothing);
- //设置图片大小
- inlineShape.Width = 180;
- inlineShape.Height = 230;
- //richTextBox1.Text += hosePath[i] + "\n";
- //每张照片后添加两个空格进行分割
- wordApp.Selection.TypeText(" ");
- }
- }
- object bookMark2 = "路由";
- if (doc.Bookmarks.Exists(Convert.ToString(bookMark2)) == true)
- {
- //查找书签
- doc.Bookmarks.get_Item(ref bookMark2).Select();
- //设置图片位置
- wordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
- //遍历子文件夹,获取照片明细
- FileSystemInfo[] filesinfos = dirinfo.GetFileSystemInfos();
- foreach (FileSystemInfo fsi in filesinfos)
- {
- if (fsi.Name.Contains("路由"))
- {
- routerPath.Add(Convert.ToString(fsi.FullName));
- }
- }
- for (int i = 0; i < routerPath.Count; i++)
- {
- //在书签的位置添加图片
- InlineShape inlineShape = wordApp.Selection.InlineShapes.AddPicture((string)routerPath[i], ref LinkToFile, ref SaveWithDocument, ref Nothing);
- //设置图片大小
- inlineShape.Width = 180;
- inlineShape.Height = 230;
- //richTextBox1.Text += hosePath[i] + "\n";
- //每张照片后添加两个空格进行分割
- wordApp.Selection.TypeText(" ");
- }
- }
- object bookMark3 = "馈窗";
- if (doc.Bookmarks.Exists(Convert.ToString(bookMark3)) == true)
- {
- //查找书签
- doc.Bookmarks.get_Item(ref bookMark3).Select();
- //设置图片位置
- wordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
- //遍历子文件夹,获取照片明细
- FileSystemInfo[] filesinfos = dirinfo.GetFileSystemInfos();
- foreach (FileSystemInfo fsi in filesinfos)
- {
- if (fsi.Name.Contains("馈"))
- {
- windowPath.Add(Convert.ToString(fsi.FullName));
- }
- }
- for (int i = 0; i < windowPath.Count; i++)
- {
- //在书签的位置添加图片
- InlineShape inlineShape = wordApp.Selection.InlineShapes.AddPicture((string)windowPath[i], ref LinkToFile, ref SaveWithDocument, ref Nothing);
- //设置图片大小
- inlineShape.Width = 180;
- inlineShape.Height = 230;
- //richTextBox1.Text += hosePath[i] + "\n";
- //每张照片后添加两个空格进行分割
- wordApp.Selection.TypeText(" ");
- }
- }
- object bookMark4 = "草图";
- if (doc.Bookmarks.Exists(Convert.ToString(bookMark4)) == true)
- {
- //查找书签
- doc.Bookmarks.get_Item(ref bookMark4).Select();
- //设置图片位置
- wordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
- //遍历子文件夹,获取照片明细
- FileSystemInfo[] filesinfos = dirinfo.GetFileSystemInfos();
- foreach (FileSystemInfo fsi in filesinfos)
- {
- if (fsi.Name.Contains("草图"))
- {
- sketchPath.Add(fsi.FullName);
- }
- }
- for (int i = 0; i < sketchPath.Count; i++)
- {
- //在书签的位置添加图片
- InlineShape inlineShape = wordApp.Selection.InlineShapes.AddPicture((string)sketchPath[i], ref LinkToFile, ref SaveWithDocument, ref Nothing);
- //设置图片大小
- inlineShape.Width = 400;
- inlineShape.Height = 560;
- //richTextBox1.Text += hosePath[i] + "\n";
- }
- }
- object bookMark5 = "报告编制人";
- if (doc.Bookmarks.Exists(Convert.ToString(bookMark5)) == true)
- {
- //查找书签
- doc.Bookmarks.get_Item(ref bookMark5).Select();//wdAlignParagraphRight
- //设置图片位置
- wordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
- //在书签的位置添加图片
- InlineShape inlineShape = wordApp.Selection.InlineShapes.AddPicture((string)bgbzrPath, ref LinkToFile, ref SaveWithDocument, ref Nothing);
- //设置图片大小
- inlineShape.Width = 50;
- inlineShape.Height = 30;
- inlineShape.Select();
- //inlineShape.ConvertToShape().IncrementLeft(-60.0f);
- inlineShape.ConvertToShape().IncrementTop(-10f);
- //将图片设置浮动在文字上方
- //inlineShape.ConvertToShape().WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapBehind;
- //richTextBox1.Text += nameList[i] + "勘察报告编制人已完成" + "\n";
- }
- object bookMark6 = "区县审核人";
- if (doc.Bookmarks.Exists(Convert.ToString(bookMark6)) == true)
- {
- //查找书签
- doc.Bookmarks.get_Item(ref bookMark6).Select();//wdAlignParagraphRight
- //设置图片位置
- wordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
- //在书签的位置添加图片
- InlineShape inlineShape = wordApp.Selection.InlineShapes.AddPicture((string)shrPath, ref LinkToFile, ref SaveWithDocument, ref Nothing);
- //设置图片大小
- inlineShape.Width = 50;
- inlineShape.Height = 30;
- inlineShape.Select();
- inlineShape.ConvertToShape().IncrementTop(-10f);
- //将图片设置浮动在文字上方
- //inlineShape.ConvertToShape().WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapBehind;
- //richTextBox1.Text += nameList[i] + "勘察报告审核人已完成" + "\n";
- }
- object bookMark7 = "审核意见";
- if (doc.Bookmarks.Exists(Convert.ToString(bookMark7)) == true)
- {
- //查找书签
- doc.Bookmarks.get_Item(ref bookMark7).Select();
- //设置图片位置
- wordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
- //在书签的位置添加图片
- InlineShape inlineShape = wordApp.Selection.InlineShapes.AddPicture((string)opinionPath, ref LinkToFile, ref SaveWithDocument, ref Nothing);
- //设置图片大小
- inlineShape.Width = 50;
- inlineShape.Height = 30;
- inlineShape.Select();
- inlineShape.ConvertToShape().IncrementTop(-10f);
- //将图片设置浮动在文字上方
- //inlineShape.ConvertToShape().WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapBehind;
- //richTextBox1.Text += nameList[i] + "勘察报告审核意见已完成" + "\n";
- }
- object bookMark8 = "编制日期";
- if (doc.Bookmarks.Exists(Convert.ToString(bookMark8)) == true)
- {
- //查找书签
- doc.Bookmarks.get_Item(ref bookMark8).Select();
- //设置图片位置
- wordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
- //在书签的位置添加图片
- InlineShape inlineShape = wordApp.Selection.InlineShapes.AddPicture((string)datePath, ref LinkToFile, ref SaveWithDocument, ref Nothing);
- //设置图片大小
- inlineShape.Width = 60;
- inlineShape.Height = 30;
- inlineShape.Select();
- inlineShape.ConvertToShape().IncrementTop(-10f);
- //将图片设置浮动在文字上方
- //inlineShape.ConvertToShape().WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapBehind;
- //richTextBox1.Text += nameList[i] + "勘察报告审核日期已完成" + "\n";
- }
- object bookMark9 = "设计单位";
- if (doc.Bookmarks.Exists(Convert.ToString(bookMark9)) == true)
- {
- //查找书签
- doc.Bookmarks.get_Item(ref bookMark9).Select();
- //设置图片位置
- wordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
- //在书签的位置添加图片
- InlineShape inlineShape = wordApp.Selection.InlineShapes.AddPicture((string)sjyPath, ref LinkToFile, ref SaveWithDocument, ref Nothing);
- //设置图片大小
- inlineShape.Width = 125;
- inlineShape.Height = 120;
- inlineShape.Select();
- inlineShape.ConvertToShape().IncrementTop(-10f);
- //richTextBox1.Text += nameList[i] + "勘察报告设计院签章已完成" + "\n";
- }
- object bookMark10 = "建设单位";
- if (doc.Bookmarks.Exists(Convert.ToString(bookMark10)) == true)
- {
- //查找书签
- doc.Bookmarks.get_Item(ref bookMark10).Select();
- //设置图片位置
- wordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
- //在书签的位置添加图片
- InlineShape inlineShape = wordApp.Selection.InlineShapes.AddPicture((string)jsdwPath, ref LinkToFile, ref SaveWithDocument, ref Nothing);
- //设置图片大小
- inlineShape.Width = 140;
- inlineShape.Height = 120;
- inlineShape.Select();
- inlineShape.ConvertToShape().IncrementTop(-10f);
- //richTextBox1.Text += nameList[i] + "勘察报告设计院建设单位签章已完成" + "\n";
- }
- //else
- //{
- // //word文档中不存在该书签,关闭文档
- // doc.Close(ref Nothing, ref Nothing, ref Nothing);
- // richTextBox1.Text += nameList[i] + "书签不存在\n";
- //}
- richTextBox1.Text += dirinfo.Name + "已处理完成\n";
- }
- catch
- {
- }
- //接收word中所有的修订
- doc.AcceptAllRevisions();
- //保存
- doc.Save();
- //关闭word文档
- doc.Close(ref Nothing, ref Nothing, ref Nothing);
- }
- }
- richTextBox1.Text += "\n已完成" + nameList.Count + "个文档的签名添加";
- //关闭进程
- Process myProcess = new Process();
- Process[] wordProcess = Process.GetProcessesByName("winword");
- for (int i = 0; i < wordProcess.Length; i++)//这里是找到那些没有界面的Word进程
- {
- //IntPtr ip = wordProcess[i].MainWindowHandle;
- //string str = wordProcess[i].MainWindowTitle; //发现程序中打开跟用户自己打开的区别就在这个属性
- // //用户打开的str 是文件的名称,程序中打开的就是空字符串
- //if (str == (string)nameList[i]) //如果打开的word进程太多,这条语句会报错:超出索引范围
- //{
- // wordProcess[i].Kill();
- //}
- wordProcess[i].Kill();
- }
- }
- }
- }
|