Click here to Skip to main content
15,897,891 members
Articles / Programming Languages / Visual Basic

Printing Reports in .NET

Rate me:
Please Sign up or sign in to vote.
4.85/5 (70 votes)
26 Aug 2008CPOL11 min read 441.8K   15.6K   257  
Using the library presented, you can print reports from C# and other .NET languages
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Reflection;
using System.IO;
using ReportPrinting;

namespace ReportDocumentTesting
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
    public class MainForm : System.Windows.Forms.Form
    {
        private System.Windows.Forms.ListBox listBox1;
        private System.ComponentModel.IContainer components;
        private System.Windows.Forms.ToolTip toolTip1;

		/// <summary>
		/// Lengthy string from funny.txt
		/// </summary>
        public static readonly string Text2;

        public static readonly Bitmap Watermark;
        private ReportPrinting.ReportDocument reportDocument1;
        private ReportPrinting.PrintControlToolBar printControlToolBar1;
        private System.Windows.Forms.Button sample2Button;
        private System.Windows.Forms.Button xmlSampleButton;
        private System.Windows.Forms.Button sample1Button;
        private System.Windows.Forms.Button richTextButton;
        private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.Button btnTableTest;
        public static readonly Stream GradesXml;

        static MainForm()
        {
            Assembly a = Assembly.GetExecutingAssembly();
            Stream stream = a.GetManifestResourceStream("ReportDocumentTesting.funny.txt");
            using (StreamReader sr = new StreamReader(stream))
            {
                Text2 = sr.ReadToEnd();
            }

            Stream stream3= a.GetManifestResourceStream("ReportDocumentTesting.mag37_soft.png");
            Watermark = new Bitmap (stream3);

            GradesXml = a.GetManifestResourceStream("ReportDocumentTesting.grades.xml");
        }


        public MainForm()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();
    
            SetupList();

        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if(components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

		#region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
			this.components = new System.ComponentModel.Container();
			this.sample2Button = new System.Windows.Forms.Button();
			this.printControlToolBar1 = new ReportPrinting.PrintControlToolBar();
			this.reportDocument1 = new ReportPrinting.ReportDocument();
			this.listBox1 = new System.Windows.Forms.ListBox();
			this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
			this.xmlSampleButton = new System.Windows.Forms.Button();
			this.sample1Button = new System.Windows.Forms.Button();
			this.richTextButton = new System.Windows.Forms.Button();
			this.btnTableTest = new System.Windows.Forms.Button();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.groupBox1.SuspendLayout();
			this.SuspendLayout();
			// 
			// sample2Button
			// 
			this.sample2Button.Location = new System.Drawing.Point(128, 24);
			this.sample2Button.Name = "sample2Button";
			this.sample2Button.Size = new System.Drawing.Size(96, 23);
			this.sample2Button.TabIndex = 5;
			this.sample2Button.Text = "Sample2";
			this.toolTip1.SetToolTip(this.sample2Button, "Run the second sample");
			this.sample2Button.Click += new System.EventHandler(this.sample2Button_Click);
			// 
			// printControlToolBar1
			// 
			this.printControlToolBar1.Dock = System.Windows.Forms.DockStyle.Top;
			this.printControlToolBar1.Document = this.reportDocument1;
			this.printControlToolBar1.Location = new System.Drawing.Point(0, 0);
			this.printControlToolBar1.Name = "printControlToolBar1";
			this.printControlToolBar1.Size = new System.Drawing.Size(368, 40);
			this.printControlToolBar1.TabIndex = 2;
			this.printControlToolBar1.Printing += new System.EventHandler(this.printControlToolBar1_Printing);
			// 
			// reportDocument1
			// 
			this.reportDocument1.Body = null;
			this.reportDocument1.PageFooter = null;
			this.reportDocument1.PageHeader = null;
			// 
			// listBox1
			// 
			this.listBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.listBox1.Location = new System.Drawing.Point(0, 40);
			this.listBox1.Name = "listBox1";
			this.listBox1.Size = new System.Drawing.Size(368, 277);
			this.listBox1.TabIndex = 1;
			this.toolTip1.SetToolTip(this.listBox1, "Select a test, then click preview to see it.");
			this.listBox1.DoubleClick += new System.EventHandler(this.listBox1_DoubleClick);
			// 
			// xmlSampleButton
			// 
			this.xmlSampleButton.Location = new System.Drawing.Point(16, 64);
			this.xmlSampleButton.Name = "xmlSampleButton";
			this.xmlSampleButton.Size = new System.Drawing.Size(96, 23);
			this.xmlSampleButton.TabIndex = 8;
			this.xmlSampleButton.Text = "XML Sample";
			this.toolTip1.SetToolTip(this.xmlSampleButton, "Run the second sample");
			this.xmlSampleButton.Click += new System.EventHandler(this.xmlSampleButton_Click);
			// 
			// sample1Button
			// 
			this.sample1Button.Location = new System.Drawing.Point(16, 24);
			this.sample1Button.Name = "sample1Button";
			this.sample1Button.Size = new System.Drawing.Size(96, 23);
			this.sample1Button.TabIndex = 4;
			this.sample1Button.Text = "Sample1";
			this.toolTip1.SetToolTip(this.sample1Button, "Run the first sample");
			this.sample1Button.Click += new System.EventHandler(this.sample1Button_Click);
			// 
			// richTextButton
			// 
			this.richTextButton.Location = new System.Drawing.Point(128, 64);
			this.richTextButton.Name = "richTextButton";
			this.richTextButton.Size = new System.Drawing.Size(96, 23);
			this.richTextButton.TabIndex = 9;
			this.richTextButton.Text = "Rich Text";
			this.toolTip1.SetToolTip(this.richTextButton, "Run the second sample");
			this.richTextButton.Click += new System.EventHandler(this.richTextButton_Click);
			// 
			// btnTableTest
			// 
			this.btnTableTest.Location = new System.Drawing.Point(240, 64);
			this.btnTableTest.Name = "btnTableTest";
			this.btnTableTest.Size = new System.Drawing.Size(96, 23);
			this.btnTableTest.TabIndex = 10;
			this.btnTableTest.Text = "Table Test";
			this.toolTip1.SetToolTip(this.btnTableTest, "Run the second sample");
			this.btnTableTest.Click += new System.EventHandler(this.btnTableTest_Click);
			// 
			// groupBox1
			// 
			this.groupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.groupBox1.Controls.Add(this.btnTableTest);
			this.groupBox1.Controls.Add(this.sample1Button);
			this.groupBox1.Controls.Add(this.sample2Button);
			this.groupBox1.Controls.Add(this.xmlSampleButton);
			this.groupBox1.Controls.Add(this.richTextButton);
			this.groupBox1.Location = new System.Drawing.Point(8, 328);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(352, 100);
			this.groupBox1.TabIndex = 10;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "Other Samples";
			// 
			// MainForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(368, 446);
			this.Controls.Add(this.groupBox1);
			this.Controls.Add(this.printControlToolBar1);
			this.Controls.Add(this.listBox1);
			this.MinimumSize = new System.Drawing.Size(376, 320);
			this.Name = "MainForm";
			this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Show;
			this.Text = "Report Printing Test";
			this.groupBox1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion



        void SetupList()
        {
            ArrayList tests = new ArrayList();
            tests.Add(new ComboObject("Test 1 - Linear layout", new ReportTest1()));
            tests.Add(new ComboObject("Test 2 - Layered layout", new ReportTest2()));
            tests.Add(new ComboObject("Test 3 - Header and Footer", new ReportTest3()));
            tests.Add(new ComboObject("Test 4 - Data Table and Text", new ReportTest4()));
            tests.Add(new ComboObject("Test 6 - Various lines", new ReportTest6()));
            tests.Add(new ComboObject("Test 8 - Data table with margins", new ReportTest8()));
            tests.Add(new ComboObject("Test 9 - Two columns of text", new ReportTest9()));
            tests.Add(new ComboObject("Test 11 - Table with grid lines", new ReportTest11()));
            tests.Add(new ComboObject("Test 12 - Various boxes", new ReportTest12()));
            tests.Add(new ComboObject("Test 13 - Table with only horizontal lines", new ReportTest13()));
            tests.Add(new ComboObject("Test 14 - Background watermark", new ReportTest14()));
            tests.Add(new ComboObject("Test 15 - Metric example", new ReportTest15()));
            tests.Add(new ComboObject("Test 16 - DocumentUnit example", new ReportTest16()));
            tests.Add(new ComboObject("Test 17 - DocumentUnit example 2", new ReportTest17()));
			tests.Add(new ComboObject("Test 18 - Different styles on columns", new ReportTest18()));
			tests.Add(new ComboObject("Test 19 - Summary row on table", new ReportTest19()));
            tests.Add(new ComboObject("Test 20 - Summary row on table (without using event handler)", new ReportTest20()));
            tests.Add(new ComboObject("Test 21 - Changing page orientation", new ReportTest21()));
            this.listBox1.DisplayMember = "Display";
            this.listBox1.ValueMember = "Value";
            this.listBox1.DataSource = tests;
            this.listBox1.SelectedIndex = 0;
        }


        IReportMaker GetReportMaker()
        {
            if (this.listBox1.SelectedIndex >= 0)
            {
                return this.listBox1.SelectedValue as IReportMaker;
            }
            return null;
        }

        private void printControlToolBar1_Printing(object sender, System.EventArgs e)
        {
            this.reportDocument1.ReportMaker = GetReportMaker();
        }

        private void listBox1_DoubleClick(object sender, System.EventArgs e)
        {
            this.reportDocument1.ReportMaker = GetReportMaker();
            this.printControlToolBar1.Preview (null, null);
        }


        // Create an instance of each print form at start.
        // This is so that the page settings, preview, & print
        // all have somewhere to go.  Otherwise, you have
        // to create a singleton pattern on the dialogs.
        SamplePrintDialog1 form1 = new SamplePrintDialog1();
        SamplePrintDialog2 form2 = new SamplePrintDialog2();
		SamplePrintDialogXML formXML = new SamplePrintDialogXML();
        RichTextEdit rteForm = new RichTextEdit();


        private void sample1Button_Click(object sender, System.EventArgs e)
        {
            this.form1.ShowDialog();
        }

        private void sample2Button_Click(object sender, System.EventArgs e)
        {
            this.form2.ShowDialog();
        }
		private void xmlSampleButton_Click(object sender, System.EventArgs e)
		{
			this.formXML.ShowDialog();
		}
        private void richTextButton_Click(object sender, System.EventArgs e)
        {
            rteForm.ShowDialog (this);
        }

		private void btnTableTest_Click(object sender, System.EventArgs e)
		{
			using (frmTableTest frm = new frmTableTest())
			{
				frm.ShowDialog();
			}
		}

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            Form form = new MainForm();
            Application.Run (form);
        }


		/// <summary>
		/// Imponderables text
		/// </summary>
        public const string Text1 = 
@"Imponderables:
Why do we say something is out of whack? What's a whack? If a pig loses its voice, is it disgruntled?
If love is blind, why is lingerie so popular?
When someone asks you, 'A penny for your thoughts' and you put your two cents in... what happens to the other penny?
Why is the man who invests all your money called a broker?
Why do croutons come in airtight packages? Aren't they just stale bread?
When cheese gets its picture taken, what does it say?
Why is a person who plays the piano called a pianist but a person who drives a race car not called a racist?
Why are a wise man and a wise guy opposites?  Why do overlook and oversee mean opposite things?  
Why isn't the number 11 pronounced onety one?
";
	} // end MainForm
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions