#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.Fields; using MigraDoc.DocumentObjectModel.Shapes; namespace MigraDoc.DocumentObjectModel { /// /// A Hyperlink is used to reference targets in the document (Local), on a drive (File) or a network (Web). /// public class Hyperlink : DocumentObject, IVisitable { /// /// Initializes a new instance of the Hyperlink class. /// public Hyperlink() { } /// /// Initializes a new instance of the Hyperlink class with the specified parent. /// public Hyperlink(DocumentObject parent) : base(parent) { } /// /// Initializes a new instance of the Hyperlink class with the text the hyperlink shall content. /// The type will be treated as Local by default. /// public Hyperlink(string name, string text) : this() { Name = name; Elements.AddText(text); } /// /// Initializes a new instance of the Hyperlink class with the type and text the hyperlink shall /// represent. /// public Hyperlink(string name, HyperlinkType type, string text) : this() { Name = name; Type = type; Elements.AddText(text); } #region Methods /// /// Creates a deep copy of this object. /// public new Hyperlink Clone() { return (Hyperlink)DeepCopy(); } /// /// Implements the deep copy of the object. /// protected override object DeepCopy() { Hyperlink hyperlink = (Hyperlink)base.DeepCopy(); if (hyperlink._elements != null) { hyperlink._elements = hyperlink._elements.Clone(); hyperlink._elements._parent = hyperlink; } return hyperlink; } /// /// Adds a text phrase to the hyperlink. /// public Text AddText(String text) { return Elements.AddText(text); } /// /// Adds a single character repeated the specified number of times to the hyperlink. /// public Text AddChar(char ch, int count) { return Elements.AddChar(ch, count); } /// /// Adds a single character to the hyperlink. /// 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 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 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 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 font object. /// public Font Font { get { return _font ?? (_font = new Font(this)); } set { SetParent(value); _font = value; } } [DV] public Font _font; /// /// Gets or sets the target name of the Hyperlink, e.g. an URL or a bookmark's name. /// public string Name { get { return _name.Value; } set { _name.Value = value; } } [DV] public NString _name = NString.NullValue; /// /// Gets or sets the target type of the Hyperlink. /// public HyperlinkType Type { get { return (HyperlinkType)_type.Value; } set { _type.Value = (int)value; } } [DV(Type = typeof(HyperlinkType))] public NEnum _type = NEnum.NullValue(typeof(HyperlinkType)); /// /// Gets the ParagraphElements of the Hyperlink specifying its 'clickable area'. /// public ParagraphElements Elements { get { return _elements ?? (_elements = new ParagraphElements(this)); } set { SetParent(value); _elements = value; } } [DV(ItemType = typeof(DocumentObject))] public ParagraphElements _elements; #endregion #region public /// /// Converts Hyperlink into DDL. /// public override void Serialize(Serializer serializer) { if (_name.Value == string.Empty) throw new InvalidOperationException(DomSR.MissingObligatoryProperty("Name", "Hyperlink")); serializer.Write("\\hyperlink"); string str = "[Name = \"" + Name.Replace("\\", "\\\\").Replace("\"", "\\\"") + "\""; if (!_type.IsNull) str += " Type = " + Type; str += "]"; serializer.Write(str); serializer.Write("{"); if (_elements != null) _elements.Serialize(serializer); serializer.Write("}"); } /// /// Returns the meta object of this instance. /// public override Meta Meta { get { return _meta ?? (_meta = new Meta(typeof(Hyperlink))); } } static Meta _meta; #endregion #region IDomVisitable Members /// /// Allows the visitor object to visit the document object and its child objects. /// public void AcceptVisitor(DocumentObjectVisitor visitor, bool visitChildren) { visitor.VisitHyperlink(this); if (visitChildren && _elements != null) { ((IVisitable)_elements).AcceptVisitor(visitor, true); } } #endregion } }