0

I am a newbie to C#, I have a "Save to File" option in my program which saves the output of a richtextbox in a word document and when the user chooses this option, I have used saveFileDialogue box for the user to chose the filename and the location.

What I want is that every time when the user chooses this option the word document in which the output is saved has a pre-defined header and footer images...

Thanks a lot for your help in advance!

below is my 'Save to File" code.

    private void menuItem7_Click(object sender, EventArgs e)
    {
        // Create a SaveFileDialog to request a path and file name to save to.
        SaveFileDialog saveFile1 = new SaveFileDialog();

        // Initialize the SaveFileDialog to specify the RTF extension for the file.
        saveFile1.DefaultExt = "*.rtf";
        saveFile1.Filter = "RTF Files|*.rtf";

        // Determine if the user selected a file name from the saveFileDialog.
        if (saveFile1.ShowDialog() == System.Windows.Forms.DialogResult.OK &&
                saveFile1.FileName.Length > 0)
        {
            // Save the contents of the RichTextBox into the file.
            richTextBox1.SaveFile(saveFile1.FileName,
            RichTextBoxStreamType.PlainText);
        }
    }
Chris
  • 4,661
  • 1
  • 23
  • 25
user1563401
  • 3
  • 1
  • 3
  • 1
    RichTextBox is no substitute for a word processor, the kind of program that supports headers and footers. You'll need to consider automating, say, Microsoft Word instead. Well supported in C# with the classes in the Microsoft.Office.Interop.Word namespace. – Hans Passant Jul 30 '12 at 16:01
  • What format are your "header and footer images"? Images like JPEGs or BMPs? Saving those to a text file is unlikely to give anything useful; what format do you want to save the file? – Dour High Arch Jul 30 '12 at 16:03
  • If I save the file as a docx file then what would be the changes that I need?? Really appreciate your help! – user1563401 Jul 30 '12 at 16:04
  • My images are in .JPEG format... And I can save my output in any format. i randomly chose .rtf format. it can be in any format. i just need the header and footer along with the output – user1563401 Jul 30 '12 at 16:05
  • If you're looking to insert images into the output file, you really need to be working with something a lot more complicated than just a `RichTextBox` to do the work - [you need to be outputting RTF code at the very least](http://en.wikipedia.org/wiki/Rich_Text_Format) - you can't just stick some text in there - you also *certainly* cannot use `RichTextBoxStreamType.PlainText` - which literally avoids writing out *any* RTF formatting. – Andras Zoltan Jul 30 '12 at 16:11
  • Any help on what I could use and how?? I'm an maturer and really need to get this done somehow! – user1563401 Jul 30 '12 at 16:14
  • @user1563401 - this question is in danger of getting closed because it's what we call a 'gimme teh codez' question: you've not said what you've tried, & it doesn't appear that you really understand RTF - that in itself isn't an issue (lack of knowledge on SO is a pre-requisite for a question!) - but there's no perceivable effort from you. [Here's another SO question that I think might help you](http://stackoverflow.com/questions/1490734/programatically-adding-images-to-rtf-document) - but I'm afraid I do not have the time to write a whole load of code for you to copy... – Andras Zoltan Jul 30 '12 at 16:18

3 Answers3

0

Here is an example how to use Open XML SDK ...

Source Code

you will have to plug-in your text where it says "Original Text Here".

Darek
  • 4,687
  • 31
  • 47
  • Could you please explain with an example. I would really appreciate it! – user1563401 Jul 30 '12 at 15:58
  • But judging from your answers so far, this might be a tad to complicated for your current skill level (honestly). – Darek Jul 30 '12 at 16:19
  • Yes @Darek - I applaud your efforts, especially in keeping with the [summer of love](http://blog.stackoverflow.com/2012/07/kicking-off-the-summer-of-love/), but I'm not sure how much we can actually help here... – Andras Zoltan Jul 30 '12 at 16:25
  • Thanks @AndrasZoltan ... The Open XML SDK 2.0 Productivity Tool was a great help in generating the sample. – Darek Jul 30 '12 at 17:06
0

You have picked a very difficult task for a "newbie". To mix images and text you will need a complex format like PostScript, PDF, DocX, or RTF. Controlling pagination, for example to specify your header and footer images only once and have them automatically show up on the top and bottom of each page, is an even more difficult task.

You have not given us enough information to tell you where to start. For example, what is "my program"? Is it like a word processor? You will have to use the System.Drawing.Printing.PrintDocument classes to define a print document, draw headers and footers when you reach the appropriate place on each page, perform line layout, breaks and pagination. This is a large job for a professional programmer.

Or do you just want to produce a file that another program can output, with headers and footers? You could output RTF; the specification is here. This is an easier task; you may be able to leverage exisiting RTF interpreters.

Or do you want to display these documents on-screen? Han's suggestion to use an existing application via automation is a good one.

Break your task into smaller requirements and investigate each requirement.

Dour High Arch
  • 21,513
  • 29
  • 75
  • 90
  • I think you've made up my mind - because there so many variables in this answer - all valid, too - I feel I have to vote to close... – Andras Zoltan Jul 30 '12 at 16:27
  • It is a XML parser. The user chooses the file they want to parse and the tool parses and represents each and every line. I want the parsed data to be saved in a file with my template on it, which the user can access anytime – user1563401 Jul 30 '12 at 16:34
  • @user1563401 what have you *tried* in order to achieve your goal? Equally, there really is quite a lot of information up here now for you to go off and learn the subjects you're being pointed towards. I'm sorry, but if you're hoping for someone to post you a piece of magic code that'll do what you want; it just won't happen. – Andras Zoltan Jul 30 '12 at 16:43
  • I never asked for a piece of code! And thanks for the information given. It actually helped me! I am going through all the links posted and will post my efforts for you to see! – user1563401 Jul 30 '12 at 16:49
0

First of all, create a function to take an image, width and height and return the rtf:

This is for a png

    public string GetImage(string path, int width, int height)
    {
        var stream = new MemoryStream();
        var img = Image.FromFile(path);
        img.Save(stream, System.Drawing.Imaging.ImageFormat.Png);

        var bytes = stream.ToArray();

        var str = BitConverter.ToString(bytes, 0).Replace("-", string.Empty);

        var mpic = @"{\pict\pngblip\picw" + img.Width.ToString() + @"\pich" + img.Height.ToString() +
            @"\picwgoa" + width.ToString() + @"\pichgoa" + height.ToString() + 
            @"\hex " + str + "}";
        return mpic;
    }

Now you need to insert this 'image' into the right place in the rtf. If you open your rtf file in notepad you should see something like this:

{\rtf1\ansi\ansicpg1252\deff0\deflang2057{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}} \viewkind4\uc1\pard\f0\fs17 MYTEXT\par }

If you wanted a quick and dirty method then get the rtf from the richTextBox into a string, and insert your header image string after the deflang2057 followed by a '/par' to make a new line. Then insert your footer image string just before the closing '}'

something like this:

    // Determine if the user selected a file name from the saveFileDialog.
    if (saveFile1.ShowDialog() == System.Windows.Forms.DialogResult.OK &&
            saveFile1.FileName.Length > 0)
    {
        var rtf = richTextBox1.Rtf.Insert(richTextBox1.Rtf.IndexOf("deflang2057") + 11, GetImage(@"c:\a.png", 5, 5) + @"\par");

        using (var rtfFile = new StreamWriter(saveFile1.FileName))
        {
            rtfFile.Write(rtf);
        }
    }

I hope that gets you started.

Keith Cassidy
  • 70
  • 1
  • 8