#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.Fields;
using MigraDoc.DocumentObjectModel.Shapes;
namespace MigraDoc.DocumentObjectModel
{
///
/// A ParagraphElements collection contains the individual objects of a paragraph.
///
public class ParagraphElements : DocumentObjectCollection
{
///
/// Initializes a new instance of the ParagraphElements class.
///
public ParagraphElements()
{ }
///
/// Initializes a new instance of the ParagraphElements class with the specified parent.
///
public ParagraphElements(DocumentObject parent) : base(parent) { }
///
/// Gets a ParagraphElement by its index.
///
public new DocumentObject this[int index]
{
get { return base[index] as DocumentObject; }
}
#region Methods
///
/// Creates a deep copy of this object.
///
public new ParagraphElements Clone()
{
return (ParagraphElements)DeepCopy();
}
///
/// Adds a Text object.
///
/// Content of the new Text object.
/// Returns a new Text object.
public Text AddText(string text)
{
if (text == null)
throw new ArgumentNullException("text");
#if true
Text txt = null;
string[] lines = text.Split('\n');
int lineCount = lines.Length;
for (int line = 0; line < lineCount; line++)
{
string[] tabParts = lines[line].Split('\t');
int count = tabParts.Length;
for (int idx = 0; idx < count; idx++)
{
if (tabParts[idx].Length != 0)
{
txt = new Text(tabParts[idx]);
Add(txt);
}
if (idx < count - 1)
AddTab();
}
if (line < lineCount - 1)
AddLineBreak();
}
return txt;
#else
Text txt = new Text();
txt.Content = text;
Add(txt);
return txt;
#endif
}
///
/// Adds a single character repeated the specified number of times to the paragraph.
///
public Text AddChar(char ch, int count)
{
return AddText(new string(ch, count));
}
///
/// Adds a single character to the paragraph.
///
public Text AddChar(char ch)
{
return AddText(new string(ch, 1));
}
///
/// Adds a Character object.
///
public Character AddCharacter(SymbolName symbolType)
{
return AddCharacter(symbolType, 1);
}
///
/// Adds one or more Character objects.
///
public Character AddCharacter(SymbolName symbolType, int count)
{
Character character = new Character();
Add(character);
character.SymbolName = symbolType;
character.Count = count;
return character;
}
///
/// Adds a Character object defined by a character.
///
public Character AddCharacter(char ch)
{
return AddCharacter((SymbolName)ch, 1);
}
///
/// Adds one or more Character objects defined by a character.
///
public Character AddCharacter(char ch, int count)
{
return AddCharacter((SymbolName)ch, count);
}
///
/// Adds a space character as many as count.
///
public Character AddSpace(int count)
{
return AddCharacter(SymbolName.Blank, count);
}
///
/// Adds a horizontal tab.
///
public Character AddTab()
{
return AddCharacter(SymbolName.Tab, 1);
}
///
/// Adds a line break.
///
public Character AddLineBreak()
{
return AddCharacter(SymbolName.LineBreak, 1);
}
///
/// Adds a new FormattedText.
///
public FormattedText AddFormattedText()
{
FormattedText formattedText = new FormattedText();
Add(formattedText);
return formattedText;
}
///
/// Adds a new FormattedText object with the given format.
///
public FormattedText AddFormattedText(TextFormat textFormat)
{
FormattedText formattedText = AddFormattedText();
if ((textFormat & TextFormat.Bold) == TextFormat.Bold)
formattedText.Bold = true;
if ((textFormat & TextFormat.NotBold) == TextFormat.NotBold)
formattedText.Bold = false;
if ((textFormat & TextFormat.Italic) == TextFormat.Italic)
formattedText.Italic = true;
if ((textFormat & TextFormat.NotItalic) == TextFormat.NotItalic)
formattedText.Italic = false;
if ((textFormat & TextFormat.Underline) == TextFormat.Underline)
formattedText.Underline = Underline.Single;
if ((textFormat & TextFormat.NoUnderline) == TextFormat.NoUnderline)
formattedText.Underline = Underline.None;
return formattedText;
}
///
/// Adds a new FormattedText with the given Font.
///
public FormattedText AddFormattedText(Font font)
{
FormattedText formattedText = new FormattedText();
formattedText.Font.ApplyFont(font);
Add(formattedText);
return formattedText;
}
///
/// Adds a new FormattedText with the given text.
///
public FormattedText AddFormattedText(string text)
{
FormattedText formattedText = new FormattedText();
formattedText.AddText(text);
Add(formattedText);
return formattedText;
}
///
/// Adds a new FormattedText object with the given text and format.
///
public FormattedText AddFormattedText(string text, TextFormat textFormat)
{
FormattedText formattedText = AddFormattedText(textFormat);
formattedText.AddText(text);
return formattedText;
}
///
/// Adds a new FormattedText object with the given text and font.
///
public FormattedText AddFormattedText(string text, Font font)
{
FormattedText formattedText = AddFormattedText(font);
formattedText.AddText(text);
return formattedText;
}
///
/// Adds a new FormattedText object with the given text and style.
///
public FormattedText AddFormattedText(string text, string style)
{
FormattedText formattedText = AddFormattedText(text);
formattedText.Style = style;
return formattedText;
}
///
/// Adds a new Hyperlink of Type "Local", i.e. the Target is a Bookmark within the Document
///
public Hyperlink AddHyperlink(string name)
{
Hyperlink hyperlink = new Hyperlink();
hyperlink.Name = name;
Add(hyperlink);
return hyperlink;
}
///
/// Adds a new Hyperlink
///
public Hyperlink AddHyperlink(string name, HyperlinkType type)
{
Hyperlink hyperlink = new Hyperlink();
hyperlink.Name = name;
hyperlink.Type = type;
Add(hyperlink);
return hyperlink;
}
///
/// Adds a new Bookmark.
///
public BookmarkField AddBookmark(string name)
{
BookmarkField fieldBookmark = new BookmarkField();
fieldBookmark.Name = name;
Add(fieldBookmark);
return fieldBookmark;
}
///
/// Adds a new PageField.
///
public PageField AddPageField()
{
PageField fieldPage = new PageField();
Add(fieldPage);
return fieldPage;
}
///
/// Adds a new RefFieldPage.
///
public PageRefField AddPageRefField(string name)
{
PageRefField fieldPageRef = new PageRefField();
fieldPageRef.Name = name;
Add(fieldPageRef);
return fieldPageRef;
}
///
/// Adds a new NumPagesField.
///
public NumPagesField AddNumPagesField()
{
NumPagesField fieldNumPages = new NumPagesField();
Add(fieldNumPages);
return fieldNumPages;
}
///
/// Adds a new SectionField.
///
public SectionField AddSectionField()
{
SectionField fieldSection = new SectionField();
Add(fieldSection);
return fieldSection;
}
///
/// Adds a new SectionPagesField.
///
public SectionPagesField AddSectionPagesField()
{
SectionPagesField fieldSectionPages = new SectionPagesField();
Add(fieldSectionPages);
return fieldSectionPages;
}
///
/// Adds a new DateField.
///
///
public DateField AddDateField()
{
DateField fieldDate = new DateField();
Add(fieldDate);
return fieldDate;
}
///
/// Adds a new DateField with the given format.
///
public DateField AddDateField(string format)
{
DateField fieldDate = new DateField();
fieldDate.Format = format;
Add(fieldDate);
return fieldDate;
}
///
/// Adds a new InfoField with the given type.
///
public InfoField AddInfoField(InfoFieldType iType)
{
InfoField fieldInfo = new InfoField();
fieldInfo.Name = iType.ToString();
Add(fieldInfo);
return fieldInfo;
}
///
/// Adds a new Footnote with the specified Text.
///
public Footnote AddFootnote(string text)
{
Footnote footnote = new Footnote();
Paragraph par = footnote.Elements.AddParagraph();
par.AddText(text);
Add(footnote);
return footnote;
}
///
/// Adds a new Footnote.
///
public Footnote AddFootnote()
{
Footnote footnote = new Footnote();
Add(footnote);
return footnote;
}
///
/// Adds a new Image.
///
public Image AddImage(string name)
{
Image image = new Image();
image.Name = name;
Add(image);
return image;
}
///
///
///
public override void Add(DocumentObject docObj)
{
base.Add(docObj);
}
#endregion
#region public
///
/// Converts ParagraphElements into DDL.
///
public override void Serialize(Serializer serializer)
{
int count = Count;
for (int index = 0; index < count; ++index)
{
DocumentObject element = this[index];
element.Serialize(serializer);
}
}
///
/// Returns the meta object of this instance.
///
public override Meta Meta
{
get { return _meta ?? (_meta = new Meta(typeof(ParagraphElements))); }
}
static Meta _meta;
#endregion
}
}