📄 daprintdocument.cs
字号:
#region License
/*
This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General
Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option)
any later version.
This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to
the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#endregion
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Printing;
using System.Data;
using System.Xml;
using System.Windows.Forms;
using System.IO;
namespace daReport
{
#region Event Delegate Declarations
/// <summary>
/// Represents the method that will handle the MarginsChanged event.
/// </summary>
/// <param name="sender">The source of the event. </param>
/// <remarks>
/// When you create a MarginsChangedHandler delegate, you identify the method that will handle the event.
/// To associate the event with your event handler, add an instance of the delegate to the event. The event
/// handler is called whenever the event occurs, until you remove the delegate.
/// Note: The declaration of your event handler must have the same parameters as the
/// MarginsChangedHandler delegate declaration.
/// </remarks>
public delegate void MarginsChangedHandler(object sender);
/// <summary>
/// Represents the method that will handle the PageSizeChanged event.
/// </summary>
/// <param name="sender">The source of the event. </param>
/// <remarks>
/// When you create a PageSizeChangedHandler delegate, you identify the method that will handle the event.
/// To associate the event with your event handler, add an instance of the delegate to the event. The event
/// handler is called whenever the event occurs, until you remove the delegate.
/// Note: The declaration of your event handler must have the same parameters as the
/// PageSizeChangedHandler delegate declaration.
/// </remarks>
public delegate void PageSizeChangedHandler(object sender);
/// <summary>
/// Represents the method that will handle the PageLayoutChanged event.
/// </summary>
/// <param name="sender">The source of the event. </param>
/// <remarks>
/// When you create a PageLayoutChangedHandler delegate, you identify the method that will handle the event.
/// To associate the event with your event handler, add an instance of the delegate to the event. The event
/// handler is called whenever the event occurs, until you remove the delegate.
/// Note: The declaration of your event handler must have the same parameters as the
/// PageLayoutChangedHandler delegate declaration.
/// </remarks>
public delegate void PageLayoutChangedHandler(object sender);
#endregion
/// <summary>
/// Class representing the DaPrintDocument object.
/// </summary>
/// <remarks>The DaPrintDocument parses the XML template files and produces the report. The designer
/// application also uses it to parse all the document objects and place them on the designer pane</remarks>
public class DaPrintDocument : System.Drawing.Printing.PrintDocument
{
#region Declarations
private System.ComponentModel.Container components = null;
private License designLicense;
/// <summary>
/// Event declaration for the delegate MarginsChangedHandler
/// </summary>
public event MarginsChangedHandler OnMarginsChanged;
/// <summary>
/// Event declaration for the delegate PageSizeChangedHandler
/// </summary>
public event PageSizeChangedHandler OnPageSizeChanged;
/// <summary>
/// Event declaration for the delegate PageLayoutChangedHandler
/// </summary>
public event PageLayoutChangedHandler OnPageLayoutChanged;
private bool designMode = false;
private Paper.Type paperType = Paper.Type.A4;
private string mDocRoot = "";
private XmlDocument xmlDoc;
private bool isXmlOK = true;
private string theErrorMessage;
private XmlNodeList xmlStaticElements;
private XmlNodeList xmlDynamicElements;
private Parameters declaredParameters;
private Hashtable parameterValues;
private Hashtable systemValues;
private Hashtable mDataTables;
private Hashtable rowsPrintedSoFar;
private Hashtable theColumns;
private Hashtable theCharts;
private ICustomPaint[] staticObjects;
private ICustomPaint[] dynamicObjects;
/// <summary>
/// Specifies the layout of the report.
/// </summary>
public enum LayoutType
{
/// <summary>Portrait Layout</summary>
Portrait=0,
/// <summary>Landscape Layout</summary>
Landscape
};
#endregion
#region Public Methods
/// <summary>
/// A serie is used to populate the ChartBox.
/// </summary>
/// <param name="chartName">The name of the chart to add the serie to</param>
/// <param name="serieName">Name of the serie. Displayed in the Legend</param>
/// <param name="Values">A array of Double values</param>
/// <param name="serieColor">Color of the bar/pie</param>
/// <remarks>
/// This method is used to a serie to a specific chart in the current DaPrintDocument object.
/// See <see cref="daReport.ChartBox.AddSerie">daReport.ChartBox.AddSerie</see> for an example
/// of using the method</remarks>
public void AddChartSerie(string chartName,string serieName,double[] Values,Color serieColor)
{
if ( !theCharts.Contains(chartName) ) return;
int theIndex = (int)theCharts[chartName];
ChartBox chartBox = staticObjects[theIndex] as ChartBox;
chartBox.AddSerie(serieName,Values,serieColor);
}
/// <summary>
/// Adds a DataTable to the collection
/// </summary>
/// <remarks>The DataTable.Name must match the dataSource property in the report definition for a
/// dynamic table</remarks>
/// <param name="newTable">System.Data.DataTable: source data for a dynamic table</param>
public void AddData(DataTable newTable)
{
try
{
if (mDataTables.Contains(newTable.TableName))
{
mDataTables.Remove(newTable.TableName);
rowsPrintedSoFar.Remove(newTable.TableName);
}
mDataTables.Add(newTable.TableName,newTable);
rowsPrintedSoFar.Add(newTable.TableName,0);
}
catch (Exception){}
}
/// <summary>
/// Function to load the Dynamic Document objects in the XML template file
/// </summary>
public void InitDynamicObjects()
{
if ( xmlDynamicElements == null )
{
dynamicObjects = new ICustomPaint[0];
return;
}
dynamicObjects = new ICustomPaint[xmlDynamicElements.Count];
for (int i=0;i<xmlDynamicElements.Count;i++)
{
switch( xmlDynamicElements[i].Name )
{
case "textField":
TextField textField = new TextField(0, 0, 0, 0, this);
textField.LoadFromXMLNode(xmlDynamicElements[i]);
dynamicObjects[i] = textField;
break;
case "table":
StyledTable styledTable = new StyledTable(0, 0, 0, 0, this);
styledTable.LoadFromXMLNode(xmlDynamicElements[i]);
dynamicObjects[i] = styledTable;
break;
default:
dynamicObjects[i] = null;
break;
}
}
}
/// <summary>
/// Function to load the Static Document objects in the XML template file
/// </summary>
public void InitStaticObjects()
{
if ( xmlStaticElements == null )
{
staticObjects = new ICustomPaint[0];
return;
}
staticObjects = new ICustomPaint[xmlStaticElements.Count];
for (int i=0;i<xmlStaticElements.Count;i++)
{
switch( xmlStaticElements[i].Name )
{
case "textField":
TextField textField = new TextField(0, 0, 0, 0, this);
textField.LoadFromXMLNode(xmlStaticElements[i]);
staticObjects[i] = textField;
break;
case "pictureBox":
PictureBox pictureBox = new PictureBox(0,0,0,0,this);
pictureBox.LoadFromXMLNode(xmlStaticElements[i]);
staticObjects[i] = pictureBox;
break;
case "chartBox":
ChartBox chartBox = new ChartBox(0, 0, 0, 0,this);
chartBox.LoadFromXMLNode(xmlStaticElements[i]);
if (!theCharts.Contains(chartBox.Name))
theCharts.Add(chartBox.Name,i);
staticObjects[i] = chartBox;
break;
case "table":
StyledTable styledTable = new StyledTable(0, 0, 0, 0, this);
styledTable.LoadFromXMLNode(xmlStaticElements[i]);
staticObjects[i] = styledTable;
break;
case "line":
Line line = new Line(0, 0, 0, 0, this);
line.LoadFromXMLNode(xmlStaticElements[i]);
staticObjects[i] = line;
break;
default:
staticObjects[i] = null;
break;
}
}
}
/// <summary>
/// Public function to reapply alignments to all static and dynamic document objects.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <remarks>This function is called when margins or page layout has been changed, and the alignment
/// of the objects needs to be reset.</remarks>
public void RepeatAlignments(object sender)
{
for (int i=0;i<this.staticObjects.Length;i++)
{
staticObjects[i].HorizontalAlignment = staticObjects[i].HorizontalAlignment;
staticObjects[i].VerticalAlignment = staticObjects[i].VerticalAlignment;
}
for (int i=0;i<this.dynamicObjects.Length;i++)
{
dynamicObjects[i].HorizontalAlignment = dynamicObjects[i].HorizontalAlignment;
dynamicObjects[i].VerticalAlignment = dynamicObjects[i].VerticalAlignment;
}
}
/// <summary>
/// Function to load a Hashtable of parameter values
/// </summary>
/// <param name="parameters">Hashtable of parameter values</param>
/// <remarks>
/// c#
/// <code language="c#">
/// // fill in with some parameters
/// // (parameter names are case sensitive)
/// Hashtable parameters = new Hashtable();
/// parameters.Add("author","Predrag Dukanac");
/// daPrintDocument.SetParameters(parameters);
/// </code>
/// vb.net
/// <code language="Visual Basic">
/// ' fill in with some parameters
/// ' (parameter names are case sensitive)
/// Dim parameters as Hashtable = new Hashtable()
/// parameters.Add("author","Predrag Dukanac")
/// daPrintDocument.SetParameters(parameters)
/// </code>
/// </remarks>
public void SetParameters(Hashtable parameters)
{
parameterValues = parameters;
}
/// <summary>
/// Function to load XML template from a file
/// </summary>
/// <param name="FileName">File location of XML template file</param>
public void setXML(string FileName)
{
xmlDoc = new XmlDocument();
xmlDoc.Load(FileName);
DocRoot = (new FileInfo(FileName)).DirectoryName;
setXML();
}
/// <summary>
/// Function to load XML template from a file
/// </summary>
/// <param name="XmlDoc">XMLDoc variable.</param>
/// <param name="XMLDocRoot">XMLDocument Root</param>
/// <remarks>
/// This function can be used to load an XML template file, make some changes to the
/// XML and then pass through. The XMLDocRoot is used as a starting point for
/// PictureBox image locations
/// </remarks>
public void setXML(System.Xml.XmlDocument XmlDoc, string XMLDocRoot)
{
this.xmlDoc = xmlDoc;
DocRoot = XMLDocRoot;
setXML();
}
/// <summary>
/// Sets the categories for the specified chart
/// </summary>
/// <param name="chartName"><see cref="daReport.ChartBox">daReport.ChartBox</see> to set categories for</param>
/// <param name="theCategories">string array of categories</param>
/// <remarks>
/// This sets grouping for the data being passed into the ChartBox.
/// c#
/// <code language="c#">
/// daPrintDocument.SetChartCategories("chart0",new string[] {"New York","Shangai","Mexico City"});
/// </code>
/// vb.net
/// <code language="c#">
/// daPrintDocument.SetChartCategories("chart0",new string() {"New York","Shangai","Mexico City"})
/// </code>
/// </remarks>
public void SetChartCategories(string chartName,string[] theCategories)
{
if ( !theCharts.Contains(chartName) ) return;
int theIndex = (int)theCharts[chartName];
ChartBox chartBox = staticObjects[theIndex] as ChartBox;
chartBox.XCategories = theCategories;
}
#endregion
#region Public Properties
/// <summary>
/// Gets the current collection of DataTables for this report
/// </summary>
[Browsable(false)]
public Hashtable DataTables
{
get { return this.mDataTables; }
}
/// <summary>
/// Gets/Sets the DocRoot for the current XML document
/// </summary>
/// <remarks>This is used to parse for locations of referenced images.</remarks>
public string DocRoot
{
get { return this.mDocRoot; }
set {this.mDocRoot=value; }
}
/// <summary>
/// Gets/Sets the
/// <see cref="daReport.DaPrintDocument.LayoutType">daReport.DaPrintDocument.LayoutType</see>
/// for the current DaPrintDocument
/// </summary>
public LayoutType Layout
{
get
{
if ( this.DefaultPageSettings.Landscape )
return LayoutType.Landscape;
else
return LayoutType.Portrait;
}
set
{
if ( value == LayoutType.Landscape )
this.DefaultPageSettings.Landscape = true;
else
this.DefaultPageSettings.Landscape = false;
if(OnPageLayoutChanged!=null)
OnPageLayoutChanged(this);
}
}
/// <summary>
/// Gets/Sets the margins for this page.
/// </summary>
/// <remarks>When handling the PrintDocument.PrintPage event, you can use this property along with the
/// Bounds property to calculate the printing area for the page.</remarks>
public Margins Margins
{
get {return this.DefaultPageSettings.Margins;}
set
{
this.DefaultPageSettings.Margins = value;
if(OnMarginsChanged!=null)
OnMarginsChanged(this);
}
}
/// <summary>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -