#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 System.Collections.Generic; using MigraDoc.DocumentObjectModel.publics; using MigraDoc.DocumentObjectModel.Visitors; using MigraDoc.DocumentObjectModel.Fields; using MigraDoc.DocumentObjectModel.Shapes; namespace MigraDoc.DocumentObjectModel { /// /// Represents a paragraph which is used to build up a document with text. /// public class Paragraph : DocumentObject, IVisitable { /// /// Initializes a new instance of the Paragraph class. /// public Paragraph() { } /// /// Initializes a new instance of the Paragraph class with the specified parent. /// public Paragraph(DocumentObject parent) : base(parent) { } #region Methods /// /// Creates a deep copy of this object. /// public new Paragraph Clone() { return (Paragraph)DeepCopy(); } /// /// Implements the deep copy of the object. /// protected override object DeepCopy() { Paragraph paragraph = (Paragraph)base.DeepCopy(); if (paragraph._format != null) { paragraph._format = paragraph._format.Clone(); paragraph._format._parent = paragraph; } if (paragraph._elements != null) { paragraph._elements = paragraph._elements.Clone(); paragraph._elements._parent = paragraph; } return paragraph; } /// /// Adds a text phrase to the paragraph. /// public Text AddText(String text) { return Elements.AddText(text); } /// /// Adds a single character repeated the specified number of times to the paragraph. /// public Text AddChar(char ch, int count) { return Elements.AddChar(ch, count); } /// /// Adds a single character to the paragraph. /// public Text AddChar(char ch) { return Elements.AddChar(ch); } /// /// Adds one or more Symbol objects. /// public Character AddCharacter(SymbolName symbolType, int count) { return Elements.AddCharacter(symbolType, count); } /// /// Adds a Symbol object. /// public Character AddCharacter(SymbolName symbolType) { return Elements.AddCharacter(symbolType); } /// /// Adds one or more Symbol objects defined by a character. /// public Character AddCharacter(char ch, int count) { return Elements.AddCharacter(ch, count); } /// /// Adds a Symbol object defined by a character. /// public Character AddCharacter(char ch) { return Elements.AddCharacter(ch); } /// /// Adds a space character as many as count. /// public Character AddSpace(int count) { return Elements.AddSpace(count); } /// /// Adds a horizontal tab. /// public void AddTab() { Elements.AddTab(); } /// /// Adds a line break. /// public void AddLineBreak() { Elements.AddLineBreak(); } /// /// Adds a new FormattedText. /// public FormattedText AddFormattedText() { return Elements.AddFormattedText(); } /// /// Adds a new FormattedText object with the given format. /// public FormattedText AddFormattedText(TextFormat textFormat) { return Elements.AddFormattedText(textFormat); } /// /// Adds a new FormattedText with the given Font. /// public FormattedText AddFormattedText(Font font) { return Elements.AddFormattedText(font); } /// /// Adds a new FormattedText with the given text. /// public FormattedText AddFormattedText(string text) { return Elements.AddFormattedText(text); } /// /// Adds a new FormattedText object with the given text and format. /// public FormattedText AddFormattedText(string text, TextFormat textFormat) { return Elements.AddFormattedText(text, textFormat); } /// /// Adds a new FormattedText object with the given text and font. /// public FormattedText AddFormattedText(string text, Font font) { return Elements.AddFormattedText(text, font); } /// /// Adds a new FormattedText object with the given text and style. /// public FormattedText AddFormattedText(string text, string style) { return Elements.AddFormattedText(text, style); } /// /// Adds a new Hyperlink of Type "Local", /// i.e. the Target is a Bookmark within the Document /// public Hyperlink AddHyperlink(string name) { return Elements.AddHyperlink(name); } /// /// Adds a new Hyperlink /// public Hyperlink AddHyperlink(string name, HyperlinkType type) { return Elements.AddHyperlink(name, type); } /// /// Adds a new Bookmark. /// public BookmarkField AddBookmark(string name) { return Elements.AddBookmark(name); } /// /// Adds a new PageField. /// public PageField AddPageField() { return Elements.AddPageField(); } /// /// Adds a new PageRefField. /// public PageRefField AddPageRefField(string name) { return Elements.AddPageRefField(name); } /// /// Adds a new NumPagesField. /// public NumPagesField AddNumPagesField() { return Elements.AddNumPagesField(); } /// /// Adds a new SectionField. /// public SectionField AddSectionField() { return Elements.AddSectionField(); } /// /// Adds a new SectionPagesField. /// public SectionPagesField AddSectionPagesField() { return Elements.AddSectionPagesField(); } /// /// Adds a new DateField. /// public DateField AddDateField() { return Elements.AddDateField(); } /// /// Adds a new DateField. /// public DateField AddDateField(string format) { return Elements.AddDateField(format); } /// /// Adds a new InfoField. /// public InfoField AddInfoField(InfoFieldType iType) { return Elements.AddInfoField(iType); } /// /// Adds a new Footnote with the specified text. /// public Footnote AddFootnote(string text) { return Elements.AddFootnote(text); } /// /// Adds a new Footnote. /// public Footnote AddFootnote() { return Elements.AddFootnote(); } /// /// Adds a new Image object /// public Image AddImage(string fileName) { return Elements.AddImage(fileName); } /// /// Adds a new Bookmark /// public void Add(BookmarkField bookmark) { Elements.Add(bookmark); } /// /// Adds a new PageField /// public void Add(PageField pageField) { Elements.Add(pageField); } /// /// Adds a new PageRefField /// public void Add(PageRefField pageRefField) { Elements.Add(pageRefField); } /// /// Adds a new NumPagesField /// public void Add(NumPagesField numPagesField) { Elements.Add(numPagesField); } /// /// Adds a new SectionField /// public void Add(SectionField sectionField) { Elements.Add(sectionField); } /// /// Adds a new SectionPagesField /// public void Add(SectionPagesField sectionPagesField) { Elements.Add(sectionPagesField); } /// /// Adds a new DateField /// public void Add(DateField dateField) { Elements.Add(dateField); } /// /// Adds a new InfoField /// public void Add(InfoField infoField) { Elements.Add(infoField); } /// /// Adds a new Footnote /// public void Add(Footnote footnote) { Elements.Add(footnote); } /// /// Adds a new Text /// public void Add(Text text) { Elements.Add(text); } /// /// Adds a new FormattedText /// public void Add(FormattedText formattedText) { Elements.Add(formattedText); } /// /// Adds a new Hyperlink /// public void Add(Hyperlink hyperlink) { Elements.Add(hyperlink); } /// /// Adds a new Image /// public void Add(Image image) { Elements.Add(image); } /// /// Adds a new Character /// public void Add(Character character) { Elements.Add(character); } #endregion #region Properties /// /// Gets or sets the style name. /// public string Style { get { return _style.Value; } set { _style.Value = value; } } [DV] public NString _style = NString.NullValue; /// /// Gets or sets the ParagraphFormat object of the paragraph. /// 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 paragraph. /// public ParagraphElements Elements { get { return _elements ?? (_elements = new ParagraphElements(this)); } set { SetParent(value); _elements = value; } } [DV] public ParagraphElements _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 /// /// Allows the visitor object to visit the document object and its child objects. /// void IVisitable.AcceptVisitor(DocumentObjectVisitor visitor, bool visitChildren) { visitor.VisitParagraph(this); if (visitChildren && _elements != null) ((IVisitable)_elements).AcceptVisitor(visitor, true); } /// /// For public use only. /// public bool SerializeContentOnly { get { return _serializeContentOnly; } set { _serializeContentOnly = value; } } bool _serializeContentOnly; /// /// Converts Paragraph into DDL. /// public override void Serialize(Serializer serializer) { if (!_serializeContentOnly) { serializer.WriteComment(_comment.Value); serializer.WriteLine("\\paragraph"); int pos = serializer.BeginAttributes(); if (_style.Value != "") serializer.WriteLine("Style = \"" + _style.Value + "\""); if (!IsNull("Format")) _format.Serialize(serializer, "Format", null); serializer.EndAttributes(pos); serializer.BeginContent(); if (!IsNull("Elements")) Elements.Serialize(serializer); serializer.CloseUpLine(); serializer.EndContent(); } else { Elements.Serialize(serializer); serializer.CloseUpLine(); } } /// /// Returns the meta object of this instance. /// public override Meta Meta { get { return _meta ?? (_meta = new Meta(typeof(Paragraph))); } } /// /// Returns an array of Paragraphs that are separated by parabreaks. Null if no parabreak is found. /// public Paragraph[] SplitOnParaBreak() { if (_elements == null) return null; int startIdx = 0; List paragraphs = new List(); for (int idx = 0; idx < Elements.Count; ++idx) { DocumentObject element = Elements[idx]; if (element is Character) { Character character = (Character)element; if (character.SymbolName == SymbolName.ParaBreak) { Paragraph paragraph = new Paragraph(); paragraph.Format = Format.Clone(); paragraph.Style = Style; paragraph.Elements = SubsetElements(startIdx, idx - 1); startIdx = idx + 1; paragraphs.Add(paragraph); } } } if (startIdx == 0) //No paragraph breaks given. return null; else { Paragraph paragraph = new Paragraph(); paragraph.Format = Format.Clone(); paragraph.Style = Style; paragraph.Elements = SubsetElements(startIdx, _elements.Count - 1); paragraphs.Add(paragraph); return paragraphs.ToArray(); } } /// /// Gets a subset of the paragraphs elements. /// /// Start index of the required subset. /// End index of the required subset. /// A ParagraphElements object with cloned elements. private ParagraphElements SubsetElements(int startIdx, int endIdx) { ParagraphElements paragraphElements = new ParagraphElements(); for (int idx = startIdx; idx <= endIdx; ++idx) paragraphElements.Add((DocumentObject)_elements[idx].Clone()); return paragraphElements; } static Meta _meta; #endregion } }