本文整理汇总了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);
}
}
}
示例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");
}
}
}
示例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);
示例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");
}
}