C# 两个或多个word合并,读取word,word转PDF(调用Spire dll)

通过Spire动态库可以   两个或多个word合并,读取word,word转PDF,这里只是例出了这几个功能,还有很多功能可以找相关的文档查看

源码实例: https://pan.baidu.com/s/1Y38KrYK3NdPnkc9NJ9BZRw 提取码: 8etb

核心代码

/// <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 = string.Format("Mergesection{0}.docx", DateTime.Now.ToString("yyyyMMddHHmmssff"));
    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.ToString());
}

评论