#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 System; using MigraDoc.DocumentObjectModel.publics; using MigraDoc.DocumentObjectModel.Visitors; using MigraDoc.DocumentObjectModel.Shapes.Charts; using MigraDoc.DocumentObjectModel.Tables; using MigraDoc.DocumentObjectModel.Shapes; namespace MigraDoc.DocumentObjectModel { /// /// Represents a header or footer object in a section. /// public class HeaderFooter : DocumentObject, IVisitable { /// /// Initializes a new instance of the HeaderFooter class. /// public HeaderFooter() { } /// /// Initializes a new instance of the HeaderFooter class with the specified parent. /// public HeaderFooter(DocumentObject parent) : base(parent) { } #region Methods /// /// Creates a deep copy of this object. /// public new HeaderFooter Clone() { return (HeaderFooter)DeepCopy(); } /// /// Implements the deep copy of the object. /// protected override object DeepCopy() { HeaderFooter headerFooter = (HeaderFooter)base.DeepCopy(); if (headerFooter._format != null) { headerFooter._format = headerFooter._format.Clone(); headerFooter._format._parent = headerFooter; } if (headerFooter._elements != null) { headerFooter._elements = headerFooter._elements.Clone(); headerFooter._elements._parent = headerFooter; } return headerFooter; } /// /// Adds a new paragraph to the header or footer. /// public Paragraph AddParagraph() { return Elements.AddParagraph(); } /// /// Adds a new paragraph with the specified text to the header or footer. /// public Paragraph AddParagraph(string paragraphText) { return Elements.AddParagraph(paragraphText); } /// /// Adds a new chart with the specified type to the header or footer. /// public Chart AddChart(ChartType type) { return Elements.AddChart(type); } /// /// Adds a new chart to the header or footer. /// public Chart AddChart() { return Elements.AddChart(); } /// /// Adds a new table to the header or footer. /// public Table AddTable() { return Elements.AddTable(); } /// /// Adds a new Image to the header or footer. /// public Image AddImage(string fileName) { return Elements.AddImage(fileName); } /// /// Adds a new textframe to the header or footer. /// public TextFrame AddTextFrame() { return Elements.AddTextFrame(); } /// /// Adds a new paragraph to the header or footer. /// public void Add(Paragraph paragraph) { Elements.Add(paragraph); } /// /// Adds a new chart to the header or footer. /// public void Add(Chart chart) { Elements.Add(chart); } /// /// Adds a new table to the header or footer. /// public void Add(Table table) { Elements.Add(table); } /// /// Adds a new image to the header or footer. /// public void Add(Image image) { Elements.Add(image); } /// /// Adds a new text frame to the header or footer. /// public void Add(TextFrame textFrame) { Elements.Add(textFrame); } #endregion #region Properties /// /// Returns true if this is a headers, false otherwise. /// public bool IsHeader { get { return ((HeadersFooters)_parent).IsHeader; } } /// /// Returns true if this is a footer, false otherwise. /// public bool IsFooter { get { return ((HeadersFooters)_parent).IsFooter; } } /// /// Returns true if this is a first page header or footer, false otherwise. /// public bool IsFirstPage { get { return ((HeadersFooters)_parent)._firstPage == this; } } /// /// Returns true if this is an even page header or footer, false otherwise. /// public bool IsEvenPage { get { return ((HeadersFooters)_parent)._evenPage == this; } } /// /// Returns true if this is a primary header or footer, false otherwise. /// public bool IsPrimary { get { return ((HeadersFooters)_parent)._primary == this; } } /// /// Gets or sets the style name. /// public string Style { get { return _style.Value; } set { // Just save style name. Style style = Document.Styles[value]; if (style != null) _style.Value = value; else throw new ArgumentException("Invalid style name '" + value + "'."); } } [DV] public NString _style = NString.NullValue; /// /// Gets or sets the paragraph format. /// public ParagraphFormat Format { get { return _format ?? (_format = new ParagraphFormat(this)); } set { SetParent(value); _format = value; } } [DV] public ParagraphFormat _format; /// /// Gets the collection of document objects that defines the header or footer. /// public DocumentElements Elements { get { return _elements ?? (_elements = new DocumentElements(this)); } set { SetParent(value); _elements = value; } } [DV(ItemType = typeof(DocumentObject))] public DocumentElements _elements; /// /// Gets or sets a comment associated with this object. /// public string Comment { get { return _comment.Value; } set { _comment.Value = value; } } [DV] public NString _comment = NString.NullValue; #endregion #region public /// /// Converts HeaderFooter into DDL. /// public override void Serialize(Serializer serializer) { HeadersFooters headersfooters = (HeadersFooters)_parent; if (headersfooters.Primary == this) Serialize(serializer, "primary"); else if (headersfooters.EvenPage == this) Serialize(serializer, "evenpage"); else if (headersfooters.FirstPage == this) Serialize(serializer, "firstpage"); } /// /// Converts HeaderFooter into DDL. /// public void Serialize(Serializer serializer, string prefix) { serializer.WriteComment(_comment.Value); serializer.WriteLine("\\" + prefix + (IsHeader ? "header" : "footer")); int pos = serializer.BeginAttributes(); if (!IsNull("Format")) _format.Serialize(serializer, "Format", null); serializer.EndAttributes(pos); serializer.BeginContent(); if (!IsNull("Elements")) _elements.Serialize(serializer); serializer.EndContent(); } /// /// Allows the visitor object to visit the document object and its child objects. /// void IVisitable.AcceptVisitor(DocumentObjectVisitor visitor, bool visitChildren) { visitor.VisitHeaderFooter(this); if (visitChildren && _elements != null) ((IVisitable)_elements).AcceptVisitor(visitor, true); } /// /// Determines whether this instance is null (not set). /// public override bool IsNull() { return false; } /// /// Returns the meta object of this instance. /// public override Meta Meta { get { return _meta ?? (_meta = new Meta(typeof(HeaderFooter))); } } static Meta _meta; #endregion } }