合并多个rtf文件,有2个细节:
1、不同文件之间加分页
2、编号的处理
选择一个目录
private void BtnImportingDirectoryBrowse_Click(object sender, EventArgs e)
{
if (FbdlgDirectory.ShowDialog() == DialogResult.OK)
{
textBox1.Text = FbdlgDirectory.SelectedPath;
string dir = textBox1.Text.Trim();
if (System.IO.Directory.Exists(dir))
{
string rtf_fn = dir + ".rtf";
merge_RTF(dir, rtf_fn);
MessageBox.Show("Save to " + rtf_fn);
}
}
}
合并
private void merge_RTF(string dir,string rtf_fn)
{
string[] ss = System.IO.Directory.GetFiles(dir, "*.rtf");
List<string> files = new List<string>();
files.AddRange(ss);
files.Sort();
int p_Count = 0;
for (int idx=0; idx < files.Count; idx++)
{
if (idx == 0)
{
richEditControl1.LoadDocument(files[idx]);
p_Count = richEditControl1.Document.Paragraphs.Count;
}
else
{
string rtf = System.IO.File.ReadAllText(files[idx]);
char pageBreakChar = (char)12;
richEditControl1.Document.BeginUpdate();
richEditControl1.Document.AppendText( pageBreakChar.ToString());
DocumentRange range= richEditControl1.Document.AppendRtfText(rtf) ;
richEditControl1.Document.EndUpdate();
richEditControl1.Document.BeginUpdate();
ParagraphCollection paragraphs = richEditControl1.Document.Paragraphs;
int c= paragraphs.Count;
if (c >= p_Count)
{
NumberingList numberingList = null;
for (int i = p_Count - 1; i < c; i++)
{
Paragraph pgf = paragraphs[i];
if (pgf.IsInList)
{
if (numberingList == null)
{
NumberingList old = richEditControl1.Document.NumberingLists[pgf.ListIndex];
numberingList = richEditControl1.Document.NumberingLists.Add(old.AbstractNumberingListIndex);
numberingList.NumberingType = old.NumberingType;
//numberingList.Levels[0].NewStart = 0;
numberingList.Levels[0].SetOverrideStart(true);
numberingList.Levels[0].NewStart = 1;
pgf.ListIndex = numberingList.Index;
}
else
{
pgf.ListIndex = numberingList.Index;
}
}
else
{
numberingList = null;
}
}
p_Count = c;
}
richEditControl1.Document.EndUpdate();
}
}
NumberingListCollection nl = richEditControl1.Document.NumberingLists;
foreach (NumberingList n in nl)
{
}
richEditControl1.Document.SaveDocument(rtf_fn, DocumentFormat.Rtf);
}
插入分页
char pageBreakChar = (char)12;
richEditControl1.Document.BeginUpdate();
richEditControl1.Document.AppendText( pageBreakChar.ToString());
DocumentRange range= richEditControl1.Document.AppendRtfText(rtf) ;
richEditControl1.Document.EndUpdate();
编号的处理,遍历所有 numberingList 判断位置 ,处理编号 SetOverrideStart , NewStart = 1
ParagraphCollection paragraphs = richEditControl1.Document.Paragraphs;
int c= paragraphs.Count;
if (c >= p_Count)
{
NumberingList numberingList = null;
for (int i = p_Count - 1; i < c; i++)
{
Paragraph pgf = paragraphs[i];
if (pgf.IsInList)
{
if (numberingList == null)
{
NumberingList old = richEditControl1.Document.NumberingLists[pgf.ListIndex];
numberingList = richEditControl1.Document.NumberingLists.Add(old.AbstractNumberingListIndex);
numberingList.NumberingType = old.NumberingType;
//numberingList.Levels[0].NewStart = 0;
numberingList.Levels[0].SetOverrideStart(true);
numberingList.Levels[0].NewStart = 1;
pgf.ListIndex = numberingList.Index;
}
else
{
pgf.ListIndex = numberingList.Index;
}
}
else
{
numberingList = null;
}
}
p_Count = c;
}