#region MigraDoc - Creating Documents on the Fly
//
// Authors:
// Stefan Lange
// Klaus Potzesny
// David Stephensen
//
// Copyright (c) 2001-2017 empira Software GmbH, Cologne Area (Germany)
//
// http://www.pdfsharp.com
// http://www.migradoc.com
// http://sourceforge.net/projects/pdfsharp
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
#endregion
using MigraDoc.DocumentObjectModel.publics;
using MigraDoc.DocumentObjectModel.Visitors;
using MigraDoc.DocumentObjectModel.Tables;
using MigraDoc.DocumentObjectModel.Shapes.Charts;
using MigraDoc.DocumentObjectModel.Shapes;
namespace MigraDoc.DocumentObjectModel
{
///
/// Represents a collection of document elements.
///
public class DocumentElements : DocumentObjectCollection, IVisitable
{
///
/// Initializes a new instance of the DocumentElements class.
///
public DocumentElements()
{ }
///
/// Initializes a new instance of the DocumentElements class with the specified parent.
///
public DocumentElements(DocumentObject parent) : base(parent) { }
///
/// Gets a document object by its index.
///
public new DocumentObject this[int index]
{
get { return base[index]; }
}
#region Methods
///
/// Creates a deep copy of this object.
///
public new DocumentElements Clone()
{
return (DocumentElements)DeepCopy();
}
///
/// Adds a new paragraph to the collection.
///
public Paragraph AddParagraph()
{
Paragraph paragraph = new Paragraph();
Add(paragraph);
return paragraph;
}
///
/// Adds a new paragraph with the specified text to the collection.
///
public Paragraph AddParagraph(string text)
{
Paragraph paragraph = new Paragraph();
paragraph.AddText(text);
Add(paragraph);
return paragraph;
}
///
/// Adds a new paragraph with the specified text and style to the collection.
///
public Paragraph AddParagraph(string text, string style)
{
Paragraph paragraph = new Paragraph();
paragraph.AddText(text);
paragraph.Style = style;
Add(paragraph);
return paragraph;
}
///
/// Adds a new table to the collection.
///
public Table AddTable()
{
Table tbl = new Table();
Add(tbl);
return tbl;
}
///
/// Adds a new legend to the collection.
///
public Legend AddLegend()
{
Legend legend = new Legend();
Add(legend);
return legend;
}
///
/// Add a manual page break.
///
public void AddPageBreak()
{
PageBreak pageBreak = new PageBreak();
Add(pageBreak);
}
///
/// Adds a new barcode to the collection.
///
public Barcode AddBarcode()
{
Barcode barcode = new Barcode();
Add(barcode);
return barcode;
}
///
/// Adds a new chart with the specified type to the collection.
///
public Chart AddChart(ChartType type)
{
Chart chart = AddChart();
chart.Type = type;
return chart;
}
///
/// Adds a new chart with the specified type to the collection.
///
public Chart AddChart()
{
Chart chart = new Chart();
chart.Type = ChartType.Line;
Add(chart);
return chart;
}
///
/// Adds a new image to the collection.
///
public Image AddImage(string name)
{
Image image = new Image();
image.Name = name;
Add(image);
return image;
}
///
/// Adds a new text frame to the collection.
///
public TextFrame AddTextFrame()
{
TextFrame textFrame = new TextFrame();
Add(textFrame);
return textFrame;
}
#endregion
#region public
///
/// Converts DocumentElements into DDL.
///
public override void Serialize(Serializer serializer)
{
int count = Count;
if (count == 1 && this[0] is Paragraph)
{
// Omit keyword if paragraph has no attributes set.
Paragraph paragraph = (Paragraph)this[0];
if (paragraph.Style == "" && paragraph.IsNull("Format"))
{
paragraph.SerializeContentOnly = true;
paragraph.Serialize(serializer);
paragraph.SerializeContentOnly = false;
return;
}
}
for (int index = 0; index < count; index++)
{
DocumentObject documentElement = this[index];
documentElement.Serialize(serializer);
}
}
///
/// Allows the visitor object to visit the document object and its child objects.
///
void IVisitable.AcceptVisitor(DocumentObjectVisitor visitor, bool visitChildren)
{
visitor.VisitDocumentElements(this);
foreach (DocumentObject docObject in this)
{
if (docObject is IVisitable)
((IVisitable)docObject).AcceptVisitor(visitor, visitChildren);
}
}
///
/// Returns the meta object of this instance.
///
public override Meta Meta
{
get { return _meta ?? (_meta = new Meta(typeof(DocumentElements))); }
}
static Meta _meta;
#endregion
}
}