Esempio n. 1
0
        /// <summary>
        /// Render a hyperlink.
        /// </summary>
        /// <param name="hyperlink"></param>
        /// <param name="parent"></param>
        /// <param name="context"></param>
        /// <param name="documentPart"></param>
        /// <param name="formatProvider"></param>
        /// <returns></returns>
        public static OpenXmlElement Render(this Hyperlink hyperlink,
                                            OpenXmlElement parent,
                                            ContextModel context,
                                            OpenXmlPart documentPart,
                                            IFormatProvider formatProvider)
        {
            context.ReplaceItem(hyperlink, formatProvider);

            if (!hyperlink.Show)
            {
                return(null);
            }

            var fieldCodeXmlelement = new DocumentFormat.OpenXml.Wordprocessing.Hyperlink();

            if (!string.IsNullOrWhiteSpace(hyperlink.Anchor))
            {
                fieldCodeXmlelement.Anchor = hyperlink.Anchor;
            }
            else if (!string.IsNullOrWhiteSpace(hyperlink.WebSiteUri))
            {
                HyperlinkRelationship hyperlinkPart = documentPart.AddHyperlinkRelationship(new Uri(hyperlink.WebSiteUri), true);
                fieldCodeXmlelement.Id = hyperlinkPart.Id;
            }

            parent.AppendChild(fieldCodeXmlelement);

            hyperlink.Text.Render(fieldCodeXmlelement, context, documentPart, formatProvider);

            return(fieldCodeXmlelement);
        }
 private static void AddRelationships(OpenXmlPart oldPart, OpenXmlPart newPart, IEnumerable<XElement> newContent)
 {
     var relevantElements = newContent.DescendantsAndSelf()
         .Where(d => RelationshipMarkup.ContainsKey(d.Name) &&
             d.Attributes().Any(a => RelationshipMarkup[d.Name].Contains(a.Name)))
         .ToList();
     foreach (var e in relevantElements)
     {
         if (e.Name == W.hyperlink)
         {
             string relId = (string)e.Attribute(R.id);
             if (string.IsNullOrEmpty(relId))
                 continue;
             var tempHyperlink = newPart.HyperlinkRelationships.FirstOrDefault(h => h.Id == relId);
             if (tempHyperlink != null)
                 continue;
             Guid g = Guid.NewGuid();
             string newRid = "R" + g.ToString().Replace("-", "");
             var oldHyperlink = oldPart.HyperlinkRelationships.FirstOrDefault(h => h.Id == relId);
             if (oldHyperlink == null)
                 continue;
             //throw new DocumentBuilderInternalException("Internal Error 0002");
             newPart.AddHyperlinkRelationship(oldHyperlink.Uri, oldHyperlink.IsExternal, newRid);
             UpdateContent(newContent, e.Name, relId, newRid);
         }
         if (e.Name == W.attachedTemplate || e.Name == W.saveThroughXslt)
         {
             string relId = (string)e.Attribute(R.id);
             if (string.IsNullOrEmpty(relId))
                 continue;
             var tempExternalRelationship = newPart.ExternalRelationships.FirstOrDefault(h => h.Id == relId);
             if (tempExternalRelationship != null)
                 continue;
             Guid g = Guid.NewGuid();
             string newRid = "R" + g.ToString().Replace("-", "");
             var oldRel = oldPart.ExternalRelationships.FirstOrDefault(h => h.Id == relId);
             if (oldRel == null)
                 throw new DocumentBuilderInternalException("Source {0} is invalid document - hyperlink contains invalid references");
             newPart.AddExternalRelationship(oldRel.RelationshipType, oldRel.Uri, newRid);
             UpdateContent(newContent, e.Name, relId, newRid);
         }
         if (e.Name == A.hlinkClick)
         {
             string relId = (string)e.Attribute(R.id);
             if (string.IsNullOrEmpty(relId))
                 continue;
             var tempHyperlink = newPart.HyperlinkRelationships.FirstOrDefault(h => h.Id == relId);
             if (tempHyperlink != null)
                 continue;
             Guid g = Guid.NewGuid();
             string newRid = "R" + g.ToString().Replace("-", "");
             var oldHyperlink = oldPart.HyperlinkRelationships.FirstOrDefault(h => h.Id == relId);
             if (oldHyperlink == null)
                 continue;
             newPart.AddHyperlinkRelationship(oldHyperlink.Uri, oldHyperlink.IsExternal, newRid);
             UpdateContent(newContent, e.Name, relId, newRid);
         }
         if (e.Name == VML.imagedata)
         {
             string relId = (string)e.Attribute(R.href);
             if (string.IsNullOrEmpty(relId))
                 continue;
             var tempExternalRelationship = newPart.ExternalRelationships.FirstOrDefault(h => h.Id == relId);
             if (tempExternalRelationship != null)
                 continue;
             Guid g = Guid.NewGuid();
             string newRid = "R" + g.ToString().Replace("-", "");
             var oldRel = oldPart.ExternalRelationships.FirstOrDefault(h => h.Id == relId);
             if (oldRel == null)
                 throw new DocumentBuilderInternalException("Internal Error 0006");
             newPart.AddExternalRelationship(oldRel.RelationshipType, oldRel.Uri, newRid);
             UpdateContent(newContent, e.Name, relId, newRid);
         }
         if (e.Name == A.blip)
         {
             string relId = (string)e.Attribute(R.link);
             if (string.IsNullOrEmpty(relId))
                 continue;
             var tempExternalRelationship = newPart.ExternalRelationships.FirstOrDefault(h => h.Id == relId);
             if (tempExternalRelationship != null)
                 continue;
             Guid g = Guid.NewGuid();
             string newRid = "R" + g.ToString().Replace("-", "");
             var oldRel = oldPart.ExternalRelationships.FirstOrDefault(h => h.Id == relId);
             if (oldRel == null)
                 continue;
             newPart.AddExternalRelationship(oldRel.RelationshipType, oldRel.Uri, newRid);
             UpdateContent(newContent, e.Name, relId, newRid);
         }
     }
 }