RichTextBox getting literal text - c#

I'm trying to save the text from my RichTextBox to a text file. However, upon doing so, the new lines in the RTB aren't considered "new lines" and hence a \n isn't appended in the text file, so if I have:
This is a test
line of content
It will write to the text file as:
This is a testline of content
I'm saving the text the following way:
File.WriteAllText(currentFile, richTextBox1.Text);
I'm just wondering if there's any solution for this. Thanks.

Try this:
StreamWriter sw = File.CreateText(currentFile);
for (int i = 0; i < richTextBox1.Lines.Length; i++)
{
sw.WriteLine(richTextBox1.Lines[i]);
}
sw.Flush();
sw.Close();
or use the rtb.SaveFile() method: http://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox.savefile%28VS.71%29.aspx

Use the built it SaveFile method instead.
richTextBox1.SaveFile(currentFile, RichTextBoxStreamType.PlainText);

Related

WPF RichTextBox: AppendText, TextRange and the trailing newline

I am reading a text file's contents into a RichTextBox like this:
string contents = File.ReadAllText("MyFile.txt");
myRichTextBox.Document.Blocks.Clear();
myRichTextBox.AppendText(contents);
I am using the RichTextBox to automatically apply some syntax highlighting of sorts. When I try reading the unformatted text as described here to save it back to the file, things happen:
A newline (\r\n) is added to the back of the file, which I don't want unless the user explicitly adds this newline.
When I load the file again, the newline is not displayed in the RichTextEdit, even if it is present in the file.
How can I change this, so that the RichTextBox displays and returns exactly the contents of the text file?
The newline \r\n (CR/LF) is part of the text formatting in the RichTextBox control. Each paragraph while converting to the text will be appended by the \r\n.
This is means when a user press the ENTER button a new paragraph with \r\n is adding to the RichTextBox control. And when StringFromRichTextBox() method, described in the Microsoft documentation is used to extract the text content from a RichTextBox it will return a string in which all paragraphs are separated by the \r\n.
The explanations regarding the comments above:
A newline (\r\n) is added to the back of the file, which I don't want unless the user explicitly adds this newline.
A newline \r\n is adding to the end of the file only as a part of the each paragraph ending.
NOTE: If it is necessary to save and thereafter to load the saved document the TextRange.Save() and TextRange.Load() methods can be used:
public void SaveRtf(RichTextBox rtb, string file)
{
var range = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);
using (var stream = new StreamWriter(file))
{
range.Load(stream.BaseStream, DataFormats.Rtf);
}
}
public void LoadRtf(RichTextBox rtb, string file)
{
var range = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);
using (var stream = new StreamWriter(file))
{
range.Save(stream.BaseStream, DataFormats.Rtf);
}
}
If to save the whole RuchTextBox content the new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd).Text will be used than any text formatting after restoring will be lost.
Could this work? contents.Replace("\r\n", "\n");

iTextSharp How to read Table in PDF file

I am working on convert PDF to text. I can get text from PDF correctly but it is being complicated in table structure. I know PDF doesn't support table structure but I think there is a way get cells correctly. Well, for example:
I want to convert to text like this:
> This is first example.
> This is second example.
But, when I convert PDF to text, theese datas looking like this:
> This is This is
> first example. second example.
How can I get values correctly?
--EDIT:
Here is how did I convert PDF to Text:
OpenFileDialog ofd = new OpenFileDialog();
string filepath;
ofd.Filter = "PDF Files(*.PDF)|*.PDF|All Files(*.*)|*.*";
if (ofd.ShowDialog() == DialogResult.OK)
{
filepath = ofd.FileName.ToString();
string strText = string.Empty;
try
{
PdfReader reader = new PdfReader(filepath);
for (int page = 1; page < reader.NumberOfPages; page++)
{
ITextExtractionStrategy its = new iTextSharp.text.pdf.parser.LocationTextExtractionStrategy();
string s = PdfTextExtractor.GetTextFromPage(reader, page, its);
s = Encoding.UTF8.GetString(ASCIIEncoding.Convert(Encoding.Default, Encoding.UTF8, Encoding.Default.GetBytes(s)));
strText += s;
}
reader.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
To make my comment an actual answer...
You use the LocationTextExtractionStrategy for text extraction:
ITextExtractionStrategy its = new iTextSharp.text.pdf.parser.LocationTextExtractionStrategy();
string s = PdfTextExtractor.GetTextFromPage(reader, page, its);
This strategy arranges all text it finds in left-to-right lines from top to bottom (actually also taking the text line angle into account). Thus, it clearly is not what you need to extract text from tables with cells with multi-line content.
Depending on the document in question there are different approaches one can take:
Use the iText SimpleTextExtractionStrategy if the text drawing operations in the document in question already are in the order one wants for text extraction.
Use a custom text extraction strategy which makes use of tagging information if the document tables are properly tagged.
Use a complex custom text extraction strategy which tries to get hints from text arrangements, line paths, or background colors to guess the table cell structure and extract text cell by cell.
In this case, the OP commented that he changed LocationTextExtractionStrategy with SimpleTextExtractionStrategy, then it worked.

How to merge multiple RTF files into one RTF in C#

I'm trying to merge multiple rtf documents into one. the size of the merged is increased (size of all the documents) but when i open, i could see only the first RTF file content only.
string srcpath = #"C:\CSI\RTFtest\src\";
string despath = #"C:\CSI\RTFtest\dest\single.rtf";
string content = "";
List<string> files = new List<string>(Directory.GetFiles(srcpath, "*.rtf"));
StreamReader read;
if (files.Count > 1)
{
for (int i = 0; i < files.Count; i++)
{
String filename = files[i];
content = File.ReadAllText(filename);
//content = content + read.ReadToEnd();
File.AppendAllText(despath, content.ToString());
File.AppendAllText(despath, System.Environment.NewLine);
}
RTF Files are not text files. You can't jsut concatenate the text as there are headers and other structures involved. You can read all about this spec here http://support.microsoft.com/kb/86999 (yuck).
You can use a TextRange object if you're using wpf, load the first file into it then append each additional file's content. Or you could read files into richtextbox objects in winforms and append the content (how to load: http://msdn.microsoft.com/en-us/library/1z7hy77a.aspx).
I guess you could use TextBox1.Rtf = TextBox1.Rtf + textBox2.rtf untill all are loaded since the rtf property is the string with rtf encoding in it.
Use StringBuilder. You can append RTF to RTF.
Example:
StringBuilder sb = new StringBuilder();
sb.Append(#"{\rtf1\ansi");
sb.Append(#"...
HPIrichTextBox.Rtf = sb.ToString();

Replace found strings with new strings?

I have a open file dialog that open XML file. The regex expression find every string between > and <, and write every string in new line to the rich text box.
private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
StreamReader sr = new StreamReader(openFileDialog1.FileName);
string s = sr.ReadToEnd();
richTextBox1.Text = s;
}
string txt = richTextBox1.Text;
var foundWords = Regex.Matches(txt, #"(?<=>)([\w ]+?)(?=<)");
richTextBox1.Text = string.Join("\n", foundWords.Cast<Match>().Select(x => x.Value).ToArray());
}
Then I can change those strings. But how can I import those changed strings back to original XML file on its same place?
You could try to replace these strings inside a file, but once you replace something with a different length, it would be simpler to just write the entire file instead.
It looks like the user is able to modify these strings - that's your challenge there: you will have to keep track of which word was where in the original file to replace them back into the data. Furthermore the user is able to remove or add lines to the textbox, what would your application do in that case?
It would be easier to process the xml file using XDocument and store the XElements that contain the original values. XDocument allows you to replace these values and store the file.
Note that since you're not explicitly closing the StreamReader, the file may still be in use when you try to write it. Simply put the StreamReader in a using block to prevent this.

How to write a text file after the previous line in C#?

My below code creates a txt file and writes something to that file. But I need to write a new line after the previous lines when I run the script several times. Code:
string filePath = "D:\\DOT_NET\\C#\\abc.txt";
FileInfo t = new FileInfo(filePath);
StreamWriter Tex = t.CreateText();
Tex.WriteLine("Hi freinds");
Tex.WriteLine("csharpfriends is the new url for c-sharp");
Tex.Write(Tex.NewLine);
Tex.Close();
Current output on the abc.txt file:
Hi friends
csharpfriends is the new url for c-sharp
But I need the output if I run the script several times to be this:
Hi friends
csharpfriends is the new url for c-sharp
Hi friends
csharpfriends is the new url for c-sharp
Hi friends
csharpfriends is the new url for c-sharp
How can I do that? Please help.
StreamWriter has a constructor which lets you append text instead of just writing into the file. The constructor is
new StreamWriter(string filepath, bool append)
If you set that bool to "true", then all writing will be at the end of the document. In your example...
StreamWriter Tex = new StreamWriter(#"D:\DOT_NET\C#\abc.txt", true);
using (StreamWriter sw = File.AppendText(path))
{
sw.WriteLine("...");
}
Try this:
System.IO.File.WriteAllText(#"file_location", System.IO.File.ReadAllText(#"file_location") + System.Environment.NewLine + "The_new_text");
That will add a new text to the next line.

Categories