Writting to a file

I was wondering if it is possible using streamwriter to make some of the text you write to a file bold I have been trying to figure this out, and have no luck in searching various websites if it is possible.

What I am trying to do is, write to an .rtf file and have for example: "Name:" in bold and then the person's name normal style afterwards. If you can't using streamwriter and there is a way, any help would be appreciated.

Thanks in advance.


Answer this question

Writting to a file

  • imranabdulaziz

    I tried just using \b and \b0 and this is what I ended up getting:

    Name:

    Do I have to add the whole {\rtf1\ansi....} line or should \b and \b0 work without it. If I do, how should I go about it Adding the \rtf1 line to a serperate string perhaps

    Thanks in advance, this is my first time really working with writting to files.

  • Sterling Swartwout

    After opening the .rtf file in notepad I saw what the problem was. I didn't add the extra space after "\\b Age: \\b0" so it was reading \b021 like you said might be the problem.

    This solves the last problem I had with writting to the file, so thanks a lot. I would have ended up probably throwing my monitor out the window trying to figure this out on my own.

  • williambeaker

    Thanks a lot, that did the trick. I had went back and added the {\rtf1} header but wasn't using an extra backslash with the tags.

    One extra thing, I've found that when using both the rtfTextBoxes and the normal textBoxes, that if it contains a number, it won't print to the file. If its just a regular word it will print straight to the file, but if it involves a number it doesn't output anything.

    If you know what could be causing this I would love to know, cause this has me floored right now. The one thing I'm guessing it might be is that I might need more of the rtf1 header, but I've copied pretty much the entire header from my test .rtf file with no results. I can't even use: "\\b 123 \\b0", but if it was "\\b ABC \\b0" it would print to the file.

    Any help on this would be appreciated.

  • Nathan R

    Yeah, this might not be the best way to write to an rtf file, but it seems simple enough. I did notice that the header part is required though. To figure out what parts are necessary and what parts aren't, create a new text file in notepad and save it as an rtf instead of a txt, and see what you need to make it work.

    The header string should be the first thing you write to the file, right after you open it for writing.

    Edit:
    Alright, I decided to see what the minimal data required is, and it seems to be the following:

        {\rtf1
        \b some bold text\b0 back to normal text\par This is on a new line.
        }

    So when you open the stream, first write out "{\rtf1", then the text you want with \b and \b0 tags as needed and using \par for line breaks.  Finally, write out the closing } before you close the file. You will have to escape the backslashes with an extra backslash "{\\rtf1", or like @"{\rtf1".  If you want to set the fonts, etc. then you'll want to keep more of the header.

  • carol chen

    I don't know; it's working on my end. I tested the file:
    {\rtf1
    \b 123\b0
    }
    and the numbers showed up in both Word and wordpad.

    Open notepad, and then open the rtf file that you tried to get the numbers to show up in. If the numbers aren't there in the plaintext, then the problem must be with how you are writing the numbers to the file. If the numbers are there, then there is something wrong with the formatting (like if you forgot a space after a \\b or something).

    In either case, post the contents of the rtf file (with the header, etc.) and the code you are using to generate it, and we can get this figured out.


  • Michael Hansen

    Try saving a file with bold text from wordpad, and then open it with notepad. You'll get something like:

    {\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fswiss\fcharset0 Arial;}}
    {\*\generator Msftedit 5.41.21.2500;}\viewkind4\uc1\pard\b\f0\fs20 bold\b0 normal\par
    }

    The \b and \b0 commands are what causes the bold text. Take a look at the RTF Specifications for more information.

    You can also use the RichTextBox control to save and edit the file.

  • Writting to a file