I have an application that accepts around 20 fields of data (string/int) from the user. Then allows the user to edit a dataset in a datagrid, and sort this dataset to display the updated dataset.

I now need to take the fields of user input (textboxes) and the applicable dataset (dataset or datatable), and print them. After the document is printed, I need to be able to save this information in some accessible format. This saved document needs to be able to be located by a primary key and printed/viewed again in the future.

I need to be able to organize this data and add a logo to it. My current thought is to either use a richtextbox or a listbox for the printable page, and save the file as a text file from this box. This seems like it works logically, but I'm not sure it's the correct way to do it.

In essence my plan is to use the code I have already; then add a logo to the top of the box, add the user input fields to the box, then copy my datatable and add each datarow to the box. I then wish to be able to print this document and store it in a form that can be located and converted back to the box it was in before.

I have found some good printing tutorials, and am confident that I can print from a textbox, listbox, rtextbox. I also feel like I will be successfully able to print my box to a text file. Areas where I'm hazy on are.

1) what should I use for my printable region? Data will be an image, textbox data, and an array of datarows.

2) should I use a text file, or do I need to use xml or some other sort of file I'm not thinking of? Any help with the event of converting to a file type, then converting back to the printable format.

3) any starting point on how to combine these two events and make them run efficiently. Also, any heads up I should have when starting to print my first documents.

Recommended Answers

All 7 Replies

You generally print using a Graphics object. With it you can print text and images. (I do)
I am not into xml (yet) so I cannot tell you anything about that.
Among the tutorials you found this is perhaps one you missed : http://www.devarticles.com/c/a/C-Sharp/Printing-Using-C-sharp/

I've been playing with this for a few days now and have run into a new problem. I am using richtextboxes to print and saving as .rtf, using a technique/class library found here...

http://support.microsoft.com/default.aspx?scid=kb;en-us;812425

In setting this up I have encountered a problem. My final output needs to be formatted, with a couple of tables and some other markup stuff. In order to do so, I have to use the richtextbox.Rtf() method. Working with this stuff is a pain in the ass, but provides a bunch of freedom for formatting and such.

my problem is this... I am entering data from an array, where I have to run through a number of iterations to enter all of the data. When using the .rtf method, if I attempt to make a series of edits only the last edit appears in the final document. It is like I was using the richtextbox.text property and changing the property each time. I can't figure a way to make .rtf function like .appendtext, so that I can make a series of edits without erasing the prior edits.

I noticed the same, but what I do in essence is :

private void PrintLn(RichTextBox RT)
        {
            RT.AppendText("\n");
        }

        private void AddLine(RichTextBox RT)
        {
            //50 equal signs
            RT.AppendText("==================================================");
            PrintLn(RT);
        }

        private void PrintTitle(RichTextBox RT, string Title)
        {
            SetCourierStyle(RT);
            AddLine(RT);
            RT.SelectionFont = new Font("Arial", 12F, FontStyle.Bold);
            RT.SelectionColor = Color.Blue;
            RT.AppendText(Title);
            PrintLn(RT);
            SetCourierStyle(RT);
            AddLine(RT);
        }

        private void SetCourierStyle(RichTextBox RT)
        {
            RT.SelectionFont = new Font("Courier New", 10F, FontStyle.Bold);
            RT.SelectionAlignment = HorizontalAlignment.Left;
        }

        private void PrintMainTitle(RichTextBox RT, string Title)
        {
            RT.Font = new Font("Arial", 20F, FontStyle.Bold);
            RT.SelectionAlignment = HorizontalAlignment.Center;
            RT.SelectionColor = Color.Red;
            RT.AppendText(Title);
            PrintLn(RT);
        }

        private void ShowtextBtn_Click(object sender, EventArgs e)
        {
            //this.resultTbx is RichTextBox on my Form
            PrintMainTitle(this.resultTbx, "Main title");

            PrintTitle(this.resultTbx, "SubTitle A");
            SetCourierStyle(this.resultTbx);
            this.resultTbx.AppendText(TextStringA);

            PrintTitle(this.resultTbx, "SubTitle B");
            SetCourierStyle(this.resultTbx);
            this.resultTbx.AppendText(TextStringB);
        }

Just playing around with RichTextBox, but notice that I have to set a style more than needed. If I don't do that it gets messed up.
I have a feeling that after every newline the style get's lost.
Have not really worke it out, but if anyone out there has a better answer you may let me know also.

I can not seem to figure a way out to dynamically add entries using the .rtf property. Please help me.

What I need is this; I need to be able to get a table into a richtextbox and be able to format it. I know I can print the table without getting it into a richtextbox, but in reality I not only need to be able to print, but also create a file that can be sent via email and printed in the exact same format. I feel like using the richtextbox --> .rtf is the best way to achieve this.

Any method that will result in me having a formatted table (from a dataset, datatable, datagridview or datarow array) in my richtextbox would be extremely helpful.

You could try and copy a table into a RichTextBox and look at the Rtf property to see wich codes are generated and do the same in your code.
This might help: http://latex2rtf.sourceforge.net/rtfspec.html

But I think you really need to serialize your dataset etc.
Serializing puts objects into a file which you can send via email. You can deserialize them again. You can use a BinaryFormatter object to accompish this.

ddanbe thankyou for taking the time to respond to this thread multiple times, I really appreciate it.

I have now created a solution, using the rtf property and a string builder. In case anyone is interested in a similar solution, here is the basic steps to creating a dynamic table using the rtf property.

1) Design the table to your exact specifications in word or open office (I preferred open office because it created more concise rtf from my tests). During design, you can save yourself a bunch of time, by putting something recognizable in each field you will want to edit dynamically.

save this file as .rtf

2) Open the .rtf you just made in notepad or some other .txt editor. The code will be extremely difficult to decipher with no experience. Here is how I did it. Each document will have a similar heading starting with \rtf1 and continuing until your table starts. I used my first row as a header row, so the first row that would be dynamically edited directly followed this row. all rows end in \cell\row\pard.

After I located this I separated the new row with a couple of spaces, and isolated it from the others. you will need everything from \trowd --> \pard for new dynamic rows.

So, now I have the static top portion of the document and the repeatable row code separate.

3) Next I created a stringbuilder. Then I added my static top to my stringbuilder (ie sb.append(@"{\rft1...\part}")).

4) Now I need to create my table dynamically. To do so, i simply iterate through my dataset and create a datarow array, then store the array data as variables (doesnt matter how you get your data, so long as its stored a re-usable format).

Now I iterate through a loop (i used foreach(datarow dr...)), and during the iteration I again use the sb.append method. This time the string I enter is the re-usable table that I separated earlier from the static form. I simply replace those recognizable words with my variable (make sure to put @ after you concatenate your variable to string to avoid escape sequences).

5) Finally, after my iteration is complete I update my richtextbox with the rtf property (ie rtbbox.rtf = sb.tostring())

Thats pretty much it. I found it easiest to perfect my table in a text editor and then transfer it, but you could also actually manipulate the rtf manually.

I had to do this because my columns HAD to be different sizes. If you simply want an easy way to edit rtf w/o learning it (and dont need different column sizes in the tables you'll make), this class library can get you up and running in about 20 minutes...

http://www.codeproject.com/KB/string/RTFBuilder.aspx

We are here to help if we can. Btw. thanks for the tip on RTFBuilder.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.