将多个word文档归拢到一个文档含分页(Aspose.words库)

将多个word文档合并到一个文档含分页(Aspose.words库)

   将多个word文档(doc/docx)合并到一个word文档,就像很多人写书,合成一本;包含分页效果和不分页。代码贴上:

WordQuery接口:主要通过路径获取word文档。
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using Aspose.Words;

namespace DocOil.Word.Interface
{
    public interface WordQuery
    {
        Document GetDocment(string path);
        ArrayList GetDocmentList(string[] paths);
    }
}
WordQueryImple实现类:主要实现上面的接口。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DocOil.Word.Interface;
using System.Collections;
using Aspose.Words;

namespace DocOil.Word.Imple
{
    public class WordQueryImple: WordQuery
    {
        public Document GetDocment(string path)
        {
            Document doc=new Document(path);
            return doc;
        }

        public ArrayList GetDocmentList(string[] paths)
        {
            ArrayList docArrayList = null;
            if (0 == paths.Length)
                return docArrayList;
            Document doc;
            docArrayList = new ArrayList();
            for (int i = 0; i < paths.Length; i++)
            {
                doc = new Document(paths[i]);
                docArrayList.Add(doc);
            }
            return docArrayList;
        }
    }
}

 

WordAdd:文档合并接口类。
using System;
using System.Collections.Generic;
using System.Text;
using Aspose.Words;

namespace DocOil.Word.Interface
{
    public interface WordAdd
    {
        Document AddDocs2Doc(string[] paths,string outputPath);
        Document AddDocs2DocByContinuous(string[] paths, string outputPath);
    }
}

 

WordAddImple:对合并接口的实现。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DocOil.Word.Interface;
using System.Collections;
using Aspose.Words;

namespace DocOil.Word.Imple
{
    public class WordAddImple: WordAdd
    {
        WordQuery wordQuery=new WordQueryImple() as WordQuery;

        public Document AddDocs2Doc(string[] paths, string outputPath)
        {
            Document docAll = new Document();
            ArrayList docArrayList = wordQuery.GetDocmentList(paths);
            foreach(Document doc in docArrayList)
            {
                doc.FirstSection.PageSetup.SectionStart = SectionStart.Continuous;
                docAll.AppendDocument(doc, ImportFormatMode.KeepSourceFormatting);
            }
            docAll.Save(outputPath);
            return docAll; 
        }
        public Document AddDocs2DocByContinuous(string[] paths, string outputPath)
        {
            Document docAll = new Document();
            ArrayList docArrayList = wordQuery.GetDocmentList(paths);
            foreach (Document doc in docArrayList)
            {
                doc.FirstSection.PageSetup.SectionStart = SectionStart.NewPage;
                docAll.AppendDocument(doc, ImportFormatMode.KeepSourceFormatting);
            }
            docAll.Save(outputPath);
            return docAll; 
        }
    }
}

 新建一个Winform项目。

设置好界面,如下图:



编写调用代码:

(1)初始化listView:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Windows.Forms;

namespace DocOil.FormList
{
    public class DocListView
    {
        public static void InitListView(ListView listViewDoc, ArrayList wordNameList)
        {
            listViewDoc.Columns.Add("编号").Width = 50;
            listViewDoc.Columns.Add("标题").Width = listViewDoc.Width - 30;
            ListViewItem lvItem;
            ListViewItem.ListViewSubItem lvSubItem;
            for (int i = 0; i < wordNameList.Count; i++)
            {
                lvItem = new ListViewItem();
                lvSubItem = new ListViewItem.ListViewSubItem();
                lvItem.Text = (i + 1).ToString();
                lvSubItem.Text = wordNameList[i].ToString();
                lvItem.SubItems.Add(lvSubItem);
                listViewDoc.Items.Add(lvItem);
            }
            listViewDoc.View = View.Details;
        }
    }
}

 (2)设置进度条,封装一个进度条类

ProgressBarSetValue类
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using DocOil.Until;
using DocOil.Word.Imple;
using Aspose.Words;
using System.Threading;
namespace DocOil.FormList
{
    public class ProgressBarSetValue
    {
        private ProgressBar proBar;
        private int value;
        private int maxNum;

        public ProgressBarSetValue(ProgressBar proBar, int maxNum, int value)
        {
            this.proBar = proBar;
            this.value = value;
            this.maxNum = maxNum;
            proBar.Visible = true;
            proBar.Maximum = maxNum;
        }

        public void SetValue()
        {
            proBar.Value = value;
        }
    }
}

 (3)刚才的窗体是系统的子窗体,因此,会通过父窗体调用word的paths。代码中会有说明,正式调用:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using DocOil.Until;
using DocOil.Word.Imple;
using Aspose.Words;
using System.Threading;

namespace DocOil.FormList
{
    public partial class FWordAdd2One : Form
    {
        private ArrayList wordPaths;//接受从父窗体窗体过来word文档路径集合
        private bool tag = false;//是否对合并时的文档分页
        private int progressValue;//进度条的值

        public FWordAdd2One()
        {
            InitializeComponent();
        }

        public FWordAdd2One(FMain ownerForm)//新增带参数的构造函数,用于窗体通信
        {
            InitializeComponent();
            FMain mainDlg = ownerForm;//确保传进来的是初始化的父窗体
            ArrayList wordNameList = mainDlg.wordNameList;//传过来的word文档名称(不含路径)
            wordPaths = mainDlg.wordList;//传过来的word文档路径
            Control.CheckForIllegalCrossThreadCalls = false;//多线程监测
            this.progressBarSave.Visible = false;
            this.txtSavePath.Enabled = false;
            this.checkBoxFenYe.Checked = true;
            DocListView.InitListView(listViewDoc, wordNameList);//初始化ListView,上面有类
        }

        private void btnSavePath_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Title = "保存word文档";
            dlg.Filter = "word文档|*.doc;*.docx";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                txtSavePath.Text = dlg.FileName;
            }
        }

        private void btnAdd2One_Click(object sender, EventArgs e)//开始合并
        {
            progressValue = 0;
            if (0 == wordPaths.Count)
            {
                MessageBox.Show("列表中没有数据,请导入word文档在执行");
            }
            else
            {
                string[] paths = new string[wordPaths.Count];
                for (int i = 0; i < wordPaths.Count; i++)
                {
                    paths[i] = wordPaths[i].ToString();
                    progressValue++;
                    SetProgressBarValue();
                }
                WordAddImple wordAddImple = new WordAddImple();
                if (!tag)//分页
                    wordAddImple.AddDocs2Doc(paths, txtSavePath.Text);
                else
                {
                    wordAddImple.AddDocs2DocByContinuous(paths, txtSavePath.Text);
                }
                MessageBox.Show(this,"完成!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
        }
        private void SetProgressBarValue()
        {
            ProgressBarSetValue psSetValue = new ProgressBarSetValue(this.progressBarSave, wordPaths.Count, progressValue);
            ThreadStart ts = new ThreadStart(psSetValue.SetValue);
            Thread th = new Thread(ts);
            th.Start();
        }
        private void checkBoxFenYe_Click(object sender, EventArgs e)//更改分页标志
        {
            if (checkBoxFenYe.Checked)
            {
                tag = true;
            }
        }
    }

    

}

 (4)结果,多个文档合并成一个文档;包含分页和不分页效果。

原文链接:http://www.evget.com/article/2012/9/24/17562.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值