locked
Reading / Modifying RTF files RRS feed

  • Question

  • I am looking for a library or means using C# to read and modifiy rtf files.  Mostly I want to modify the text within a rt file and save to another file.
    Tuesday, June 27, 2017 2:17 PM

All replies

  • You can just use a RichTextBox
    Tuesday, June 27, 2017 2:26 PM
  • Hi Noidfactor,

    Thank you for posting here.

    According to your description, you’d like to modify the text within a RTF file and save to another file..

    As we know The Rich Text Format (often abbreviated RTF) is a proprietary document file format with published specification developed by Microsoft Corporation for cross-platform document interchange with Microsoft products. So, RTF files can be convert to plain text. Then we can print it and save it as a rtf file.

    Following codes show how to read and write a RTF file:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main()
            {
                string path = @"C:\test\Readlines.rtf";
                System.Windows.Forms.RichTextBox rtBox = new System.Windows.Forms.RichTextBox();
                string rtfText = System.IO.File.ReadAllText(path);
                rtBox.Rtf = rtfText;
                string plainText = rtBox.Text;
                Console.WriteLine(plainText);
    
                using (System.IO.StreamWriter file =
                    new System.IO.StreamWriter(@"C:\test\WriteLines.rtf"))
                {
                    file.WriteLine(plainText);
                }
                Console.ReadLine();       
            }
    
        }
    }
    

    Here is my output.

    Before run

    After

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, June 30, 2017 2:12 AM