Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Generating formatted file

Generating formatted file

Scheduled Pinned Locked Moved C#
tutorialquestion
10 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Andy Rama
    wrote on last edited by
    #1

    Hi to all, I want to create multiple documents or reports (Word Files or PDF Files) as following format,

    Reference No:

    Name:
    ----------- -------------- ---------------
    Surname First Name Middle Name

    Date Of Birth:

    I have to fill the content in blan spaces. How I can do above formmating while generating files? In one forum, it is written to use StreamReader & StreamWriter classes to create formatted files. How to use StreamWriter using content of a StreamReader (from a fixed file), to generate new formatted file? Regards, Aniket A. Salunkhe

    N 1 Reply Last reply
    0
    • A Andy Rama

      Hi to all, I want to create multiple documents or reports (Word Files or PDF Files) as following format,

      Reference No:

      Name:
      ----------- -------------- ---------------
      Surname First Name Middle Name

      Date Of Birth:

      I have to fill the content in blan spaces. How I can do above formmating while generating files? In one forum, it is written to use StreamReader & StreamWriter classes to create formatted files. How to use StreamWriter using content of a StreamReader (from a fixed file), to generate new formatted file? Regards, Aniket A. Salunkhe

      N Offline
      N Offline
      Natza Mitzi
      wrote on last edited by
      #2

      You can create an rtf file that ms word can read by doing the following: string line; using (StreamWriter writer = File.CreateText(@"C:\file.rtf")) { line = String.Format("Reference No: {0}", 3233); writer.WriteLine(line); writer.WriteLine(); line = String.Format("Name: {0}", "Ron"); writer.WriteLine(line); writer.WriteLine(); //.... writer.Flush(); }

      Natza Mitzi

      A 1 Reply Last reply
      0
      • N Natza Mitzi

        You can create an rtf file that ms word can read by doing the following: string line; using (StreamWriter writer = File.CreateText(@"C:\file.rtf")) { line = String.Format("Reference No: {0}", 3233); writer.WriteLine(line); writer.WriteLine(); line = String.Format("Name: {0}", "Ron"); writer.WriteLine(line); writer.WriteLine(); //.... writer.Flush(); }

        Natza Mitzi

        A Offline
        A Offline
        Andy Rama
        wrote on last edited by
        #3

        Thanks for suggestion. With the help of your code, I have wriiten statements as follows, line = String.Format("Name: {0} {1} {2}", txtLastName.Text, txtMiddleName.Text, txtFirstName.Text); writer.WriteLine(line); writer.WriteLine(); line = String.Format("Date OF Birth: {0}", dtDateOfBirth.Value.ToString("dd-MMM-yyyy")); writer.WriteLine(line); writer.WriteLine(); line = String.Format("Address: {0}", txtAddress.Text); writer.WriteLine(line); writer.WriteLine(); writer.WriteLine(); line = String.Format("Education: {0}", txtEducation.Text); writer.WriteLine(line); writer.WriteLine(); writer.Flush(); Output file is as,

        Name: ASD FGH JKL

        Date OF Birth: 30-Feb-2009

        Address: Block Number,
        Area, City
        PinCode

        Education: Done School in 1999, with % of Marks.
        Done PG in 2004 with % of marsk.</

        But I want output file as with 'Justify' alignement,

        Name: ASD FGH JKL

        Date OF Birth: 30-Feb-2009

        Address:
        Block Number,
        Area, City
        PinCode

        Education:
        Done School in 1999, with % of Marks.
        Done PG in 2004 with % of marsk.

        How to do this? Thanks & Regards, Aniket A. Salunkhe

        N 1 Reply Last reply
        0
        • A Andy Rama

          Thanks for suggestion. With the help of your code, I have wriiten statements as follows, line = String.Format("Name: {0} {1} {2}", txtLastName.Text, txtMiddleName.Text, txtFirstName.Text); writer.WriteLine(line); writer.WriteLine(); line = String.Format("Date OF Birth: {0}", dtDateOfBirth.Value.ToString("dd-MMM-yyyy")); writer.WriteLine(line); writer.WriteLine(); line = String.Format("Address: {0}", txtAddress.Text); writer.WriteLine(line); writer.WriteLine(); writer.WriteLine(); line = String.Format("Education: {0}", txtEducation.Text); writer.WriteLine(line); writer.WriteLine(); writer.Flush(); Output file is as,

          Name: ASD FGH JKL

          Date OF Birth: 30-Feb-2009

          Address: Block Number,
          Area, City
          PinCode

          Education: Done School in 1999, with % of Marks.
          Done PG in 2004 with % of marsk.</

          But I want output file as with 'Justify' alignement,

          Name: ASD FGH JKL

          Date OF Birth: 30-Feb-2009

          Address:
          Block Number,
          Area, City
          PinCode

          Education:
          Done School in 1999, with % of Marks.
          Done PG in 2004 with % of marsk.

          How to do this? Thanks & Regards, Aniket A. Salunkhe

          N Offline
          N Offline
          Natza Mitzi
          wrote on last edited by
          #4

          I am not too familiar with rtf yet I can suggest an alternative (less elegant) solution: You simply add a tab char to your output string: line = String.Format("Address:"); writer.WriteLine(line); line = String.Format("\t{0}", txtAddress1.Text); writer.WriteLine(line); line = String.Format("\t{0}", txtAddress2.Text); writer.WriteLine(line);

          Natza Mitzi

          A 1 Reply Last reply
          0
          • N Natza Mitzi

            I am not too familiar with rtf yet I can suggest an alternative (less elegant) solution: You simply add a tab char to your output string: line = String.Format("Address:"); writer.WriteLine(line); line = String.Format("\t{0}", txtAddress1.Text); writer.WriteLine(line); line = String.Format("\t{0}", txtAddress2.Text); writer.WriteLine(line);

            Natza Mitzi

            A Offline
            A Offline
            Andy Rama
            wrote on last edited by
            #5

            Thanks for the suggestion. I tried it. But it is not going to work properly with a Paragraph (from multiple lines textbox). Is there any formatting function availables with StreamWriter? Else do you know how to use macro ( written by Microsoft Word in VB ) in C#? Thanks & Regards, Aniket A. Salunkhe

            N 1 Reply Last reply
            0
            • A Andy Rama

              Thanks for the suggestion. I tried it. But it is not going to work properly with a Paragraph (from multiple lines textbox). Is there any formatting function availables with StreamWriter? Else do you know how to use macro ( written by Microsoft Word in VB ) in C#? Thanks & Regards, Aniket A. Salunkhe

              N Offline
              N Offline
              Natza Mitzi
              wrote on last edited by
              #6

              I do not have much input on both of your questions. AS far as I know, formatting is done with string.Format and StringBuilder.AppendFormat which formatting wise is the same thing. I can suggest that you check what kind of char to inject to your string (like a paragraph mark) in rtf. I have no experience using office marcos from code. You can look at MS Office Dev Tools or if you are using VS 2008 try an Office add in project. You can also try reposting your question and give the options you are trying (rtf, macro etc.) I am interested in hearing the solution you find. I hope this helps

              Natza Mitzi

              A 1 Reply Last reply
              0
              • N Natza Mitzi

                I do not have much input on both of your questions. AS far as I know, formatting is done with string.Format and StringBuilder.AppendFormat which formatting wise is the same thing. I can suggest that you check what kind of char to inject to your string (like a paragraph mark) in rtf. I have no experience using office marcos from code. You can look at MS Office Dev Tools or if you are using VS 2008 try an Office add in project. You can also try reposting your question and give the options you are trying (rtf, macro etc.) I am interested in hearing the solution you find. I hope this helps

                Natza Mitzi

                A Offline
                A Offline
                Andy Rama
                wrote on last edited by
                #7

                Thanks for suggestion.

                Natza Mitzi wrote:

                You can also try reposting your question and give the options you are trying (rtf, macro etc.) I am interested in hearing the solution you find.

                I will do that. Now I am using StreamWriter to create a rtf file as per your suggestion. I have 2 questions, 1. Can I add image using StreamWriter? & How? 2. If now possible to add image using StreamWriter. Can I creat/append a file using existing file which is having some data (for example image)? I tried to add image using StreamWriter, but didn't get any solution. Thanks & Regards, Aniket A. Salunkhe

                N 1 Reply Last reply
                0
                • A Andy Rama

                  Thanks for suggestion.

                  Natza Mitzi wrote:

                  You can also try reposting your question and give the options you are trying (rtf, macro etc.) I am interested in hearing the solution you find.

                  I will do that. Now I am using StreamWriter to create a rtf file as per your suggestion. I have 2 questions, 1. Can I add image using StreamWriter? & How? 2. If now possible to add image using StreamWriter. Can I creat/append a file using existing file which is having some data (for example image)? I tried to add image using StreamWriter, but didn't get any solution. Thanks & Regards, Aniket A. Salunkhe

                  N Offline
                  N Offline
                  Natza Mitzi
                  wrote on last edited by
                  #8

                  Hey, I found an article that seems to be good for you: A class library for RTF processing in C#[^] Checkout the section "RtfDocument class" it seems to be what you are looking for.

                  Natza Mitzi

                  A 2 Replies Last reply
                  0
                  • N Natza Mitzi

                    Hey, I found an article that seems to be good for you: A class library for RTF processing in C#[^] Checkout the section "RtfDocument class" it seems to be what you are looking for.

                    Natza Mitzi

                    A Offline
                    A Offline
                    Andy Rama
                    wrote on last edited by
                    #9

                    Thank you. I think it is be helpful for me. Provided class has AddImage method too. I will reply to you, as soon as I complete my task. Thanks & Regards, Aniket A. Salunkhe

                    1 Reply Last reply
                    0
                    • N Natza Mitzi

                      Hey, I found an article that seems to be good for you: A class library for RTF processing in C#[^] Checkout the section "RtfDocument class" it seems to be what you are looking for.

                      Natza Mitzi

                      A Offline
                      A Offline
                      Andy Rama
                      wrote on last edited by
                      #10

                      Thank you very much. It is very helpful for me, to generate a file with formatted text & image. Thanks & Regards, Aniket A. Salunkhe

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups