2021-07-16 更新
通过Spire动态库可以 两个或多个word合并,读取word,word转PDF,这里只是例出了这几个功能,还有很多功能可以找相关的文档查看
源码实例: https://pan.baidu.com/s/17foJ0s3UPUw1VhX67R7hPg 提取码: s9mb
核心代码
/// <summary> /// 将两个word合并 也可以按最类似多个word合并 /// </summary> /// <param name="path1"></param> /// <param name="path2"></param> public void WordMerge(string path1,string path2) { //加载文档1和文档2 Document doc1 = new Document(); doc1.LoadFromFile(path1, FileFormat.Docx); Document doc2 = new Document(); doc2.LoadFromFile(path2, FileFormat.Docx); //获取文档2的第一个section Section sec = doc2.Sections[0]; //克隆该section并把它添加到文档1 doc1.Sections.Add(sec.Clone()); string fileName = DateTime.Now.ToString("yyyyMMddHHmmssff")+ ".docx"; doc1.SaveToFile(fileName, FileFormat.Docx); } /// <summary> /// word转pdf /// </summary> /// <param name="path"></param> public void Word2PDF(string path) { Document document = new Document(); document.LoadFromFile(path); //Word转PDF 并保存到根目录下 document.SaveToFile("toPDF.PDF", FileFormat.PDF); //打开PDF //System.Diagnostics.Process.Start("toPDF.PDF"); } /// <summary> /// 读word内容 /// </summary> /// <param name="path"></param> public void ReadWord(string path) { Document doc = new Document(); doc.LoadFromFile(path); string s = doc.GetText(); File.WriteAllText("文本1.txt", s); } /// <summary> /// 导出模板 /// </summary> /// <param name="path"></param> public void ExportWord(string path,string outPath) { //1、需引用命名空间using Spire.Doc; Document doc = new Document(); string templatePath = path; //模板路径 doc.LoadFromFile(templatePath); //加载模板路径 //将模板里面的所有$[lqwvje]$ 替换成 罗分明 doc.Replace("$[lqwvje]$", "罗分明", true, true); //第一个参数:模板的占位符;第二个参数:替换的内容;第三个参数:是否区分大小写;第四个参数:是否全字匹配 string savePath = outPath+"\\"+DateTime.Now.ToString("yyyyMMddHHmmss")+".docx"; //导出路径 doc.SaveToFile(savePath, FileFormat.Docx); doc.Close(); }