重新格式化自由文本,以固定格式的文本(C#) [英] Re-formatting free-text to fixed format text (C#)

查看:151
本文介绍了重新格式化自由文本,以固定格式的文本(C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来很简单的一个问题,但我不能找到一个清洁,简单的解决方案。

I have a problem that seems quite straight forward, but I cannot find a clean and simple solution.

  • 我有一些自由格式的文本。本文可能很长,并且包含各种线路长度(> 120个字符),段落和空行。

  • I have some freely formatted text. This text can be quite long and contains lines of various length (> 120 characters), paragraphs and empty lines.

我需要present在一个固定的格式这段文字(比如120个字符的公关,线和25线的PR。页)。但保持在段落和空行原来的格式。

I need to present this text in a fixed format (say 120 characters pr. line and 25 lines pr. page). But keeping the original formatting in paragraphs and empty lines.

一个分页符不应该在一个字的中间。理想的情况是一个分页符应该被放置成使得我们避免单线条一个新段的上一个页面的底部,而将整个段到下一个页面等

A page break should not be in the middle of a word. Ideally a page break should be placed so that we avoid single lines of a new paragraph on the bottom of a page and rather move the whole paragraph to the next page etc.

简体样品(输入文本):

Simplified sample (input text):


Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec at magna at tellus vehicula eleifend. Vivamus at est erat. Phasellus eget tincidunt tellus. Integer ultrices dolor a magna congue imperdiet. 

Duis est sem, aliquet id fermentum sed, mollis nec metus. Phasellus porttitor porttitor sodales. Aliquam tincidunt convallis massa, sed tempus erat ornare in. Sed scelerisque, lorem accumsan imperdiet accumsan, mauris turpis molestie augue, vehicula egestas tellus quam ac nulla. 

In porta augue ac dolor imperdiet semper. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Proin lacus neque, tempor nec feugiat sed, posuere sed lorem. 

Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Nulla metus neque, volutpat vitae pharetra rutrum, malesuada in dolor. 

固定的宽度格式化分页符(节目输出):

"Fixed" width formatted with page breaks (output of program):


Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
Donec at magna at tellus vehicula eleifend. Vivamus at 
est erat. Phasellus eget tincidunt tellus. Integer 
ultrices dolor a magna congue imperdiet. 

Duis est sem, aliquet id fermentum sed, mollis nec metus. 
Phasellus porttitor porttitor sodales. Aliquam tincidunt 
convallis massa, sed tempus erat ornare in. Sed scelerisque, 
lorem accumsan imperdiet accumsan, mauris turpis molestie 
augue, vehicula egestas tellus quam ac nulla. 
[pagebreak]
In porta augue ac dolor imperdiet semper. Vestibulum ante 
ipsum primis in faucibus orci luctus et ultrices posuere 
cubilia Curae; Proin lacus neque, tempor nec feugiat sed, 
posuere sed lorem. 

Class aptent taciti sociosqu ad litora torquent per conubia 
nostra, per inceptos himenaeos. Nulla metus neque, volutpat 
vitae pharetra rutrum, malesuada in dolor. 

任何人有什么想法?

Anyone have any ideas?

推荐答案

  1. 读课文成一个字符串。
  2. 拆分行到一个数组(行[])上的换行符(\ n)的。

第2期

  1. 在初始化一个StringBuilder。
  2. 循环遍历行集合,分割每行成词上的空格字符数组。然后通过话数组并追加每一个字符串生成器。当线路长度超过您门槛插入一个换行符。当你在该行阵结束后,检查StringBuilder的!的endsWith换行符(你的最后一行是完全门槛长度,然后添加两个换行符的段落休息。

这篇关于重新格式化自由文本,以固定格式的文本(C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆