对一段古文,进行逆序、断句,简繁转换,标点符号转换,根据设定的行列数输出为仿线装书的竖排格式,纯属个人爱好,也欢迎同好者一起交流。
private void button4_Click(object sender, EventArgs e)
{
richTextBox2.Text = "";
richTextBox2.RightToLeft = RightToLeft.Yes;
string strChange = jfChange(richTextBox1.Text.Trim(), "繁体");
strChange = Regex.Replace(strChange, @"\s", ""); //去除原文中的空格,回车换行,制表符等
//strChange.Replace("“", "﹃");
strChange = Regex.Replace(strChange, ":“", "﹃");
//strChange.Replace("”", "﹄");
strChange = Regex.Replace(strChange, "”", "﹄");
strChange = Regex.Replace(strChange, "《", "︽");
strChange = Regex.Replace(strChange, "》", "︾");
int totalChars = strChange.Length; //总的字符数
int charsPerCol = Convert.ToInt32(numericUpDown1.Value); //charsPerCol为每列字符数(含标点符号)
int cols = Convert.ToInt32(numericUpDown2.Value); //cols为竖排文本的列数
int charsPerPage = charsPerCol * cols;
char fullSpace = Microsoft.VisualBasic.Strings.ChrW(12288); //半角空格是32,全角空格是12288
if (totalChars <= charsPerPage)
{
if (totalChars < charsPerPage)
{
strChange = strChange.PadRight(charsPerPage, fullSpace); //不足每页字数,用全角空格填充不足部分
}
string output = getwordsHtoV(strChange, charsPerCol, cols);
richTextBox2.Text = output;
}
else
{
List<string> outputList = new List<string>();
int outputPages = (totalChars % charsPerPage == 0) ? totalChars / charsPerPage : totalChars / charsPerPage + 1; //判断出分页数
if (totalChars < charsPerPage * outputPages)
{
strChange = strChange.PadRight(charsPerPage * outputPages, fullSpace); //如果字符数不足所有分页的字符数,用全角空格补齐
}
for (int i = 0; i < outputPages; i++)
{
outputList.Add(strChange.Substring(i*charsPerPage, charsPerPage));
}
string output = string.Empty;
foreach (string perPage in outputList)
{
output += getwordsHtoV(perPage, charsPerCol, cols);
}
richTextBox2.Text = output;
}
}
public string getwordsHtoV(string source,int charsPerCol,int cols)
{
List<string> wordList = new List<string>();
//char fullSpace = Microsoft.VisualBasic.Strings.ChrW(12288); //半角空格是32,全角空格是12288
string result = string.Empty;
if (source.Length > charsPerCol * cols)
{
result = string.Format("警告,待转换字符数{0}超出设定的行数{1}和列数{2}!",source.Length,charsPerCol,cols);
}
else
{
for (int i = 1; i <=cols; i++)
{
string cTemp = source.Substring(charsPerCol * (i - 1), charsPerCol);
wordList.Add(cTemp);
}
wordList.Reverse(); //将List倒序,这样文字可以从右往左输出
result = createHead(cols);
result += System.Environment.NewLine;
for (int i = 0; i < charsPerCol; i++)
{
StringBuilder sb = new StringBuilder();
foreach (string cTemp in wordList)
{
sb.Append("│");
sb.Append(cTemp.Substring(i, 1));
}
result+=sb.ToString() + "│"+System.Environment.NewLine;
}
result+= createFoot(cols);
result+= System.Environment.NewLine;
}
return result;
}