当前位置: 首页>>代码示例>>C#>>正文


C# OpenXmlPart.AddExternalRelationship方法代码示例

本文整理汇总了C#中OpenXmlPart.AddExternalRelationship方法的典型用法代码示例。如果您正苦于以下问题:C# OpenXmlPart.AddExternalRelationship方法的具体用法?C# OpenXmlPart.AddExternalRelationship怎么用?C# OpenXmlPart.AddExternalRelationship使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在OpenXmlPart的用法示例。


在下文中一共展示了OpenXmlPart.AddExternalRelationship方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: AddRelationships

 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);
         }
     }
 }
开发者ID:jecabana,项目名称:Portal-Vanity-Daniel-en-stand-by,代码行数:91,代码来源:DocumentBuilder.cs

示例2: CopyRelatedImage

        private static void CopyRelatedImage(OpenXmlPart oldContentPart, OpenXmlPart newContentPart, XElement imageReference, XName attributeName,
            List<ImageData> images)
        {
            string relId = (string)imageReference.Attribute(attributeName);
            if (string.IsNullOrEmpty(relId))
                return;
            try
            {
                // First look to see if this relId has already been added to the new document.
                // This is necessary for those parts that get processed with both old and new ids, such as the comments
                // part.  This is not necessary for parts such as the main document part, but this code won't malfunction
                // in that case.
                try
                {
                    OpenXmlPart tempPart = newContentPart.GetPartById(relId);
                    return;
                }
                catch (ArgumentOutOfRangeException)
                {
                    try
                    {
                        ExternalRelationship tempEr = newContentPart.GetExternalRelationship(relId);
                        return;
                    }
                    catch (KeyNotFoundException)
                    {
                        // nothing to do
                    }
                }

                ImagePart oldPart = (ImagePart)oldContentPart.GetPartById(relId);
                ImageData temp = ManageImageCopy(oldPart, newContentPart, images);
                if (temp.ResourceID == null)
                {
                    ImagePart newPart = null;
                    if (newContentPart is MainDocumentPart)
                        newPart = ((MainDocumentPart)newContentPart).AddImagePart(oldPart.ContentType);
                    if (newContentPart is HeaderPart)
                        newPart = ((HeaderPart)newContentPart).AddImagePart(oldPart.ContentType);
                    if (newContentPart is FooterPart)
                        newPart = ((FooterPart)newContentPart).AddImagePart(oldPart.ContentType);
                    if (newContentPart is EndnotesPart)
                        newPart = ((EndnotesPart)newContentPart).AddImagePart(oldPart.ContentType);
                    if (newContentPart is FootnotesPart)
                        newPart = ((FootnotesPart)newContentPart).AddImagePart(oldPart.ContentType);
                    if (newContentPart is ThemePart)
                        newPart = ((ThemePart)newContentPart).AddImagePart(oldPart.ContentType);
                    if (newContentPart is WordprocessingCommentsPart)
                        newPart = ((WordprocessingCommentsPart)newContentPart).AddImagePart(oldPart.ContentType);
                    if (newContentPart is DocumentSettingsPart)
                        newPart = ((DocumentSettingsPart)newContentPart).AddImagePart(oldPart.ContentType);
                    if (newContentPart is ChartPart)
                        newPart = ((ChartPart)newContentPart).AddImagePart(oldPart.ContentType);
                    if (newContentPart is NumberingDefinitionsPart)
                        newPart = ((NumberingDefinitionsPart)newContentPart).AddImagePart(oldPart.ContentType);
                    if (newContentPart is DiagramDataPart)
                        newPart = ((DiagramDataPart)newContentPart).AddImagePart(oldPart.ContentType);
                    if (newContentPart is ChartDrawingPart)
                        newPart = ((ChartDrawingPart)newContentPart).AddImagePart(oldPart.ContentType);
                    temp.ResourceID = newContentPart.GetIdOfPart(newPart);
                    temp.WriteImage(newPart);
                    imageReference.Attribute(attributeName).Value = temp.ResourceID;
                }
                else
                    imageReference.Attribute(attributeName).Value = temp.ResourceID;
            }
            catch (ArgumentOutOfRangeException)
            {
                try
                {
                    ExternalRelationship er = oldContentPart.GetExternalRelationship(relId);
                    ExternalRelationship newEr = newContentPart.AddExternalRelationship(er.RelationshipType, er.Uri);
                    imageReference.Attribute(R.id).Value = newEr.Id;
                }
                catch (KeyNotFoundException)
                {
                    throw new DocumentBuilderInternalException("Source {0} is unsupported document - contains reference to NULL image");
                }
            }
        }
开发者ID:jecabana,项目名称:Portal-Vanity-Daniel-en-stand-by,代码行数:80,代码来源:DocumentBuilder.cs

示例3: CopyRelatedPartsForContentParts


//.........这里部分代码省略.........
                        if (newContentPart is WordprocessingCommentsPart)
                            newPart = ((WordprocessingCommentsPart)newContentPart).AddEmbeddedObjectPart(oldPart.ContentType);
                    }
                    else if (oldPart is EmbeddedPackagePart)
                    {
                        if (newContentPart is HeaderPart)
                            newPart = ((HeaderPart)newContentPart).AddEmbeddedPackagePart(oldPart.ContentType);
                        if (newContentPart is FooterPart)
                            newPart = ((FooterPart)newContentPart).AddEmbeddedPackagePart(oldPart.ContentType);
                        if (newContentPart is MainDocumentPart)
                            newPart = ((MainDocumentPart)newContentPart).AddEmbeddedPackagePart(oldPart.ContentType);
                        if (newContentPart is FootnotesPart)
                            newPart = ((FootnotesPart)newContentPart).AddEmbeddedPackagePart(oldPart.ContentType);
                        if (newContentPart is EndnotesPart)
                            newPart = ((EndnotesPart)newContentPart).AddEmbeddedPackagePart(oldPart.ContentType);
                        if (newContentPart is WordprocessingCommentsPart)
                            newPart = ((WordprocessingCommentsPart)newContentPart).AddEmbeddedPackagePart(oldPart.ContentType);
                        if (newContentPart is ChartPart)
                            newPart = ((ChartPart)newContentPart).AddEmbeddedPackagePart(oldPart.ContentType);
                    }
                    using (Stream oldObject = oldPart.GetStream(FileMode.Open, FileAccess.Read))
                    using (Stream newObject = newPart.GetStream(FileMode.Create, FileAccess.ReadWrite))
                    {
                        int byteCount;
                        byte[] buffer = new byte[65536];
                        while ((byteCount = oldObject.Read(buffer, 0, 65536)) != 0)
                            newObject.Write(buffer, 0, byteCount);
                    }
                    oleReference.Attribute(R.id).Value = newContentPart.GetIdOfPart(newPart);
                }
                catch (ArgumentOutOfRangeException)
                {
                    ExternalRelationship er = oldContentPart.GetExternalRelationship(relId);
                    ExternalRelationship newEr = newContentPart.AddExternalRelationship(er.RelationshipType, er.Uri);
                    oleReference.Attribute(R.id).Value = newEr.Id;
                }
            }

            foreach (XElement chartReference in newContent.DescendantsAndSelf(C.chart))
            {
                try
                {
                    string relId = (string)chartReference.Attribute(R.id);
                    if (string.IsNullOrEmpty(relId))
                        continue;
                    try
                    {
                        OpenXmlPart tempPart = newContentPart.GetPartById(relId);
                        continue;
                    }
                    catch (ArgumentOutOfRangeException)
                    {
                        try
                        {
                            ExternalRelationship tempEr = newContentPart.GetExternalRelationship(relId);
                            continue;
                        }
                        catch (KeyNotFoundException)
                        {
                        }
                    }
                    ChartPart oldPart = (ChartPart)oldContentPart.GetPartById(relId);
                    XDocument oldChart = oldPart.GetXDocument();
                    ChartPart newPart = newContentPart.AddNewPart<ChartPart>();
                    XDocument newChart = newPart.GetXDocument();
                    newChart.Add(oldChart.Root);
开发者ID:jecabana,项目名称:Portal-Vanity-Daniel-en-stand-by,代码行数:67,代码来源:DocumentBuilder.cs

示例4: CopyRelatedImage

        private static void CopyRelatedImage(OpenXmlPart oldContentPart, OpenXmlPart newContentPart, XElement imageReference, XName attributeName,
            List<ImageData> images)
        {
            string relId = (string)imageReference.Attribute(attributeName);
            if (string.IsNullOrEmpty(relId))
                return;

            // First look to see if this relId has already been added to the new document.
            // This is necessary for those parts that get processed with both old and new ids, such as the comments
            // part.  This is not necessary for parts such as the main document part, but this code won't malfunction
            // in that case.
            var tempPartIdPair5 = newContentPart.Parts.FirstOrDefault(p => p.RelationshipId == relId);
            if (tempPartIdPair5 != null)
                return;

            ExternalRelationship tempEr5 = newContentPart.ExternalRelationships.FirstOrDefault(er => er.Id == relId);
            if (tempEr5 != null)
                return;

            var ipp2 = oldContentPart.Parts.FirstOrDefault(ipp => ipp.RelationshipId == relId);
            if (ipp2 != null)
            {
                ImagePart oldPart = (ImagePart)ipp2.OpenXmlPart;
                ImageData temp = ManageImageCopy(oldPart, newContentPart, images);
                if (temp.ImagePart == null)
                {
                    ImagePart newPart = null;
                    if (newContentPart is MainDocumentPart)
                        newPart = ((MainDocumentPart)newContentPart).AddImagePart(oldPart.ContentType);
                    if (newContentPart is HeaderPart)
                        newPart = ((HeaderPart)newContentPart).AddImagePart(oldPart.ContentType);
                    if (newContentPart is FooterPart)
                        newPart = ((FooterPart)newContentPart).AddImagePart(oldPart.ContentType);
                    if (newContentPart is EndnotesPart)
                        newPart = ((EndnotesPart)newContentPart).AddImagePart(oldPart.ContentType);
                    if (newContentPart is FootnotesPart)
                        newPart = ((FootnotesPart)newContentPart).AddImagePart(oldPart.ContentType);
                    if (newContentPart is ThemePart)
                        newPart = ((ThemePart)newContentPart).AddImagePart(oldPart.ContentType);
                    if (newContentPart is WordprocessingCommentsPart)
                        newPart = ((WordprocessingCommentsPart)newContentPart).AddImagePart(oldPart.ContentType);
                    if (newContentPart is DocumentSettingsPart)
                        newPart = ((DocumentSettingsPart)newContentPart).AddImagePart(oldPart.ContentType);
                    if (newContentPart is ChartPart)
                        newPart = ((ChartPart)newContentPart).AddImagePart(oldPart.ContentType);
                    if (newContentPart is NumberingDefinitionsPart)
                        newPart = ((NumberingDefinitionsPart)newContentPart).AddImagePart(oldPart.ContentType);
                    if (newContentPart is DiagramDataPart)
                        newPart = ((DiagramDataPart)newContentPart).AddImagePart(oldPart.ContentType);
                    if (newContentPart is ChartDrawingPart)
                        newPart = ((ChartDrawingPart)newContentPart).AddImagePart(oldPart.ContentType);
                    temp.ImagePart = newPart;
                    var id = newContentPart.GetIdOfPart(newPart);
                    temp.AddContentPartRelTypeResourceIdTupple(newContentPart, newPart.RelationshipType, id);
                    imageReference.Attribute(attributeName).Value = id;
                    temp.WriteImage(newPart);
                }
                else
                {
                    var refRel = newContentPart.Parts.FirstOrDefault(pip =>
                    {
                        var rel = temp.ContentPartRelTypeIdList.FirstOrDefault(cpr =>
                        {
                            var found = cpr.ContentPart == newContentPart;
                            return found;
                        });
                        return rel != null;
                    });
                    if (refRel != null)
                    {
                        imageReference.Attribute(attributeName).Value = temp.ContentPartRelTypeIdList.First(cpr =>
                        {
                            var found = cpr.ContentPart == newContentPart;
                            return found;
                        }).RelationshipId;
                        return;
                    }
                    var newId = "R" + Guid.NewGuid().ToString().Replace("-", "").Substring(0, 16);
                    newContentPart.CreateRelationshipToPart(temp.ImagePart, newId);
                    imageReference.Attribute(R.id).Value = newId;
                }
            }
            else
            {
                ExternalRelationship er = oldContentPart.ExternalRelationships.FirstOrDefault(er1 => er1.Id == relId);
                if (er != null)
                {
                    ExternalRelationship newEr = newContentPart.AddExternalRelationship(er.RelationshipType, er.Uri);
                    imageReference.Attribute(R.id).Value = newEr.Id;
                }
                throw new DocumentBuilderInternalException("Source {0} is unsupported document - contains reference to NULL image");
            }
        }
开发者ID:bbqchickenrobot,项目名称:OpenXmlPowerTools,代码行数:93,代码来源:DocumentBuilder.cs


注:本文中的OpenXmlPart.AddExternalRelationship方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。