Click here to Skip to main content
15,897,187 members
Articles / Programming Languages / C#

EasiReports

Rate me:
Please Sign up or sign in to vote.
4.87/5 (64 votes)
13 Feb 2006CPOL6 min read 482.6K   9.7K   219  
A library to add reports to your application.
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Printing;

namespace Puzzle.SourceCode
{
	/// <summary>
	/// Printer document class.
	/// </summary>
	/// <example >
	/// 
	/// 
	/// <b>Print the content of a SyntaxDocument:</b>
	/// <code>
	/// SourceCodePrintDocument PrintDoc=new SourceCodePrintDocument(MySyntaxDocument);
	///
	///	PrintDialog1.Document =PrintDoc;
	///	if (PrintDialog1.ShowDialog ()==DialogResult.OK)
	///		PrintDoc.Print ();
	/// </code>
	/// <hr/>
	/// <b>Print Preview the content of a SyntaxDocument</b>
	/// <code>
	/// SourceCodePrintDocument PrintDoc=new SourceCodePrintDocument(MySyntaxDocument);
	/// PrintPreviewDialog1.Document = PrintDoc
	/// PrintPreviewDialog1.ShowDialog ();
	/// </code>
	/// </example>
	[ToolboxItem(true)]
	public class SourceCodePrintDocument : PrintDocument
	{
		private Font fontNormal = null;
		private Font fontBreak = null;
		private int RowIndex = 0;


		private SyntaxDocument _Document = null;
		private RowCollection rc = null;

		public SyntaxDocument Document
		{
			get { return _Document; }
			set { _Document = value; }

		}

		public SourceCodePrintDocument() : base()
		{
		}

		public SourceCodePrintDocument(SyntaxDocument document) : base()
		{
			this.Document = document;
		}

		//Override OnBeginPrint to set up the font we are going to use
		protected override void OnBeginPrint(PrintEventArgs ev)
		{
			base.OnBeginPrint(ev);
			fontNormal = new Font("Courier new", 8, FontStyle.Regular);
			fontBreak = new Font("Symbol", 8, FontStyle.Bold);
//			fontBold						= new Font("Arial", 10,FontStyle.Bold);
//			fontItalic						= new Font("Arial", 10,FontStyle.Italic);
//			fontBoldItalic					= new Font("Arial", 10,FontStyle.Bold | FontStyle.Italic);
//			fontUnderline					= new Font("Arial", 10,FontStyle.Underline);
//			fontBoldUnderline				= new Font("Arial", 10,FontStyle.Bold | FontStyle.Underline);
//			fontItalicUnderline				= new Font("Arial", 10,FontStyle.Italic | FontStyle.Underline);
//			fontBoldItalicUnderline			= new Font("Arial", 10,FontStyle.Bold | FontStyle.Italic | FontStyle.Underline);
			RowIndex = 0;


		}

		//Override the OnPrintPage to provide the printing logic for the document
		protected override void OnPrintPage(PrintPageEventArgs ev)
		{
			float lpp = 0;
			float yPos = 0;
			int count = 0;
			float leftMargin = ev.MarginBounds.Left;
			float rightMargin = ev.MarginBounds.Right;
			float topMargin = ev.MarginBounds.Top;
			//ev.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;

			if (rc == null)
			{
				Document.ParseAll();
				Document.ParseAll(true);


				rc = new RowCollection();
				foreach (Row r in Document)
				{
					bool hasbreak = false;
					float x = leftMargin;
					Row newRow = new Row();
					rc.Add(newRow);
					foreach (Word w in r)
					{
						Font f = fontNormal;
						if (w.Style != null)
						{
							FontStyle fs = 0;

							if (w.Style.Bold)
								fs |= FontStyle.Bold;

							if (w.Style.Italic)
								fs |= FontStyle.Italic;

							if (w.Style.Underline)
								fs |= FontStyle.Underline;

							f = new Font("Courier new", 8, fs);
						}
						SizeF sf = ev.Graphics.MeasureString(w.Text, f);
						if (x + sf.Width > rightMargin)
						{
							char chr = (char) 0xbf;
							Word br = new Word();
							br.Text = chr + "";
							br.InfoTip = "break char";
							newRow.Add(br);
							hasbreak = true;


							newRow = new Row();
							rc.Add(newRow);
							x = leftMargin;
						}
						x += sf.Width;
						newRow.Add(w);


					}
					if (hasbreak)
					{
						rc.Add(new Row());
					}
				}
			}
			//------------------------------------------------------

			base.OnPrintPage(ev);


			lpp = ev.MarginBounds.Height/fontNormal.GetHeight(ev.Graphics);


			while (count < lpp && (RowIndex < rc.Count))
			{
				float x = leftMargin;
				yPos = topMargin + (count*fontNormal.GetHeight(ev.Graphics));

				Row r = rc[RowIndex];

				foreach (Word w in r)
				{
					if (w.InfoTip != null && w.InfoTip == "break char")
					{
						ev.Graphics.DrawString(w.Text, fontBreak, Brushes.Black, x, yPos, new StringFormat());
					}
					else
					{
						SizeF sf = ev.Graphics.MeasureString(w.Text, fontNormal);

						if (w.Text != null && (".,:;".IndexOf(w.Text) >= 0))
						{
							sf.Width = 6;
							x -= 4;
						}
						if (w.Text == "\t")
						{
							sf.Width = ev.Graphics.MeasureString("...", fontNormal).Width;
						}


						Color c = Color.Black;
						Font f = fontNormal;
						if (w.Style != null)
						{
							c = w.Style.ForeColor;
							FontStyle fs = 0;

							if (w.Style.Bold)
								fs |= FontStyle.Bold;

							if (w.Style.Italic)
								fs |= FontStyle.Italic;

							if (w.Style.Underline)
								fs |= FontStyle.Underline;

							f = new Font("Courier new", 8, fs);

							if (!w.Style.Transparent)
							{
								Color bg = w.Style.BackColor;
								ev.Graphics.FillRectangle(new SolidBrush(bg), x, yPos, sf.Width, fontNormal.GetHeight(ev.Graphics));


							}

						}

						c = Color.FromArgb(c.R, c.G, c.B);


						ev.Graphics.DrawString(w.Text, f, new SolidBrush(c), x, yPos, new StringFormat());
						x += sf.Width;
					}
				}

				count++;
				RowIndex++;
			}

			//If we have more lines then print another page
			if (RowIndex < rc.Count)
				ev.HasMorePages = true;
			else
				ev.HasMorePages = false;
		}

	}

}

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 Kingdom United Kingdom
I discovered C# and .NET 1.0 Beta 1 in late 2000 and loved them immediately.
I have been writing software professionally in C# ever since

In real life, I have spent 3 years travelling abroad,
I have held a UK Private Pilots Licence for 20 years,
and I am a PADI Divemaster.

I now live near idyllic Bournemouth in England.

I can work 'virtually' anywhere!

Comments and Discussions