Exemple #1
0
 public Paragraph InsertHyperlink(int index, Hyperlink h) { return InsertHyperlink(h, index); }
Exemple #2
0
        /// <summary>
        /// This function inserts a hyperlink into a Paragraph at a specified character index.
        /// </summary>
        /// <param name="index">The index to insert at.</param>
        /// <param name="h">The hyperlink to insert.</param>
        /// <returns>The Paragraph with the Hyperlink inserted at the specified index.</returns>
        /// <!-- 
        /// This function was added by Brian Campbell aka chickendelicious on Jun 16 2010
        /// Thank you Brian.
        /// -->
        public Paragraph InsertHyperlink(Hyperlink h, int index = 0)
        {
            // Convert the path of this mainPart to its equilivant rels file path.
            string path = mainPart.Uri.OriginalString.Replace("/word/", "");
            Uri rels_path = new Uri("/word/_rels/" + path + ".rels", UriKind.Relative);

            // Check to see if the rels file exists and create it if not.
            if (!Document.package.PartExists(rels_path))
                HelperFunctions.CreateRelsPackagePart(Document, rels_path);

            // Check to see if a rel for this Picture exists, create it if not.
            var Id = GetOrGenerateRel(h);

            XElement h_xml;
            if (index == 0)
            {
                // Add this hyperlink as the last element.
                Xml.AddFirst(h.Xml);

                // Extract the picture back out of the DOM.
                h_xml = (XElement)Xml.FirstNode;
            }

            else
            {
                // Get the first run effected by this Insert
                Run run = GetFirstRunEffectedByEdit(index);

                if (run == null)
                {
                    // Add this hyperlink as the last element.
                    Xml.Add(h.Xml);

                    // Extract the picture back out of the DOM.
                    h_xml = (XElement)Xml.LastNode;
                }

                else
                {
                    // Split this run at the point you want to insert
                    XElement[] splitRun = Run.SplitRun(run, index);

                    // Replace the origional run.
                    run.Xml.ReplaceWith
                    (
                        splitRun[0],
                        h.Xml,
                        splitRun[1]
                    );

                    // Get the first run effected by this Insert
                    run = GetFirstRunEffectedByEdit(index);

                    // The picture has to be the next element, extract it back out of the DOM.
                    h_xml = (XElement)run.Xml.NextNode;
                }

                h_xml.SetAttributeValue(DocX.r + "id", Id);
            }

            return this;
        }
Exemple #3
0
        internal string GetOrGenerateRel(Hyperlink h)
        {
            string image_uri_string = h.Uri.OriginalString;

            // Search for a relationship with a TargetUri that points at this Image.
            var Id =
            (
                from r in mainPart.GetRelationshipsByType("http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink")
                where r.TargetUri.OriginalString == image_uri_string
                select r.Id
            ).SingleOrDefault();

            // If such a relation dosen't exist, create one.
            if (Id == null)
            {
                // Check to see if a relationship for this Picture exists and create it if not.
                PackageRelationship pr = mainPart.CreateRelationship(h.Uri, TargetMode.External, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink");
                Id = pr.Id;
            }
            return Id;
        }
Exemple #4
0
        /// <summary>
        /// Append a hyperlink to a Paragraph.
        /// </summary>
        /// <param name="h">The hyperlink to append.</param>
        /// <returns>The Paragraph with the hyperlink appended.</returns>
        /// <example>
        /// Creates a Paragraph with some text and a hyperlink.
        /// <code>
        /// // Create a document.
        /// using (DocX document = DocX.Create(@"Test.docx"))
        /// {
        ///    // Add a hyperlink to this document.
        ///    Hyperlink h = document.AddHyperlink("Google", new Uri("http://www.google.com"));
        ///    
        ///    // Add a new Paragraph to this document.
        ///    Paragraph p = document.InsertParagraph();
        ///    p.Append("My favourite search engine is ");
        ///    p.AppendHyperlink(h);
        ///    p.Append(", I think it's great.");
        ///
        ///    // Save all changes made to this document.
        ///    document.Save();
        /// }
        /// </code>
        /// </example>
        public Paragraph AppendHyperlink(Hyperlink h)
        {
            // Convert the path of this mainPart to its equilivant rels file path.
            string path = mainPart.Uri.OriginalString.Replace("/word/", "");
            Uri rels_path = new Uri("/word/_rels/" + path + ".rels", UriKind.Relative);

            // Check to see if the rels file exists and create it if not.
            if (!Document.package.PartExists(rels_path))
                HelperFunctions.CreateRelsPackagePart(Document, rels_path);

            // Check to see if a rel for this Hyperlink exists, create it if not.
            var Id = GetOrGenerateRel(h);

            Xml.Add(h.Xml);
            Xml.Elements().Last().SetAttributeValue(DocX.r + "id", Id);

            this.runs = Xml.Elements().Last().Elements(XName.Get("r", DocX.w.NamespaceName)).ToList();

            return this;
        }
Exemple #5
0
        /// <summary>
        /// Adds a hyperlink to a document and creates a Paragraph which uses it.
        /// </summary>
        /// <param name="text">The text as displayed by the hyperlink.</param>
        /// <param name="uri">The hyperlink itself.</param>
        /// <returns>Returns a hyperlink that can be inserted into a Paragraph.</returns>
        /// <example>
        /// Adds a hyperlink to a document and creates a Paragraph which uses it.
        /// <code>
        /// // Create a document.
        /// using (DocX document = DocX.Create(@"Test.docx"))
        /// {
        ///    // Add a hyperlink to this document.
        ///    Hyperlink h = document.AddHyperlink("Google", new Uri("http://www.google.com"));
        ///    
        ///    // Add a new Paragraph to this document.
        ///    Paragraph p = document.InsertParagraph();
        ///    p.Append("My favourite search engine is ");
        ///    p.AppendHyperlink(h);
        ///    p.Append(", I think it's great.");
        ///
        ///    // Save all changes made to this document.
        ///    document.Save();
        /// }
        /// </code>
        /// </example>
        public Hyperlink AddHyperlink(string text, Uri uri)
        {
            XElement i = new XElement
            (
                XName.Get("hyperlink", DocX.w.NamespaceName),
                new XAttribute(r + "id", string.Empty),
                new XAttribute(w + "history", "1"),
                new XElement(XName.Get("r", DocX.w.NamespaceName),
                new XElement(XName.Get("rPr", DocX.w.NamespaceName),
                new XElement(XName.Get("rStyle", DocX.w.NamespaceName),
                new XAttribute(w + "val", "Hyperlink"))),
                new XElement(XName.Get("t", DocX.w.NamespaceName), text))
            );

            Hyperlink h = new Hyperlink(this, mainPart, i);

            h.text = text;
            h.uri = uri;

            AddHyperlinkStyleIfNotPresent();

            return h;
        }
Exemple #6
0
 private static IEnumerable<TemplMatchHyperlink> Find(TemplRegex rxp, Paragraph p, Hyperlink hl) 
 {
     return Find<TemplMatchHyperlink>(rxp, UrlString(hl)).Select(m => {
         m.Hyperlink= hl;
         m.Paragraph = p;
         return m;
     });
 }
Exemple #7
0
 private static string UrlString(Hyperlink hl) => WebUtility.UrlDecode(hl?.Uri?.OriginalString ?? "");