本文主要是介绍合并多个rtf文件,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
合并多个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;
}
这篇关于合并多个rtf文件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!