本文整理汇总了C#中OpenXmlPart.GetIdOfPart方法的典型用法代码示例。如果您正苦于以下问题:C# OpenXmlPart.GetIdOfPart方法的具体用法?C# OpenXmlPart.GetIdOfPart怎么用?C# OpenXmlPart.GetIdOfPart使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类OpenXmlPart
的用法示例。
在下文中一共展示了OpenXmlPart.GetIdOfPart方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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");
}
}
}
示例2: CopyRelatedPartsForContentParts
private static void CopyRelatedPartsForContentParts(OpenXmlPart oldContentPart, OpenXmlPart newContentPart,
IEnumerable<XElement> newContent, List<ImageData> images)
{
var relevantElements = newContent.DescendantsAndSelf()
.Where(d => d.Name == VML.imagedata || d.Name == VML.fill || d.Name == VML.stroke || d.Name == A.blip)
.ToList();
foreach (XElement imageReference in relevantElements)
{
CopyRelatedImage(oldContentPart, newContentPart, imageReference, R.embed, images);
CopyRelatedImage(oldContentPart, newContentPart, imageReference, R.pict, images);
CopyRelatedImage(oldContentPart, newContentPart, imageReference, R.id, images);
}
foreach (XElement diagramReference in newContent.DescendantsAndSelf().Where(d => d.Name == DGM.relIds || d.Name == A.relIds))
{
// dm attribute
string relId = diagramReference.Attribute(R.dm).Value;
try
{
OpenXmlPart tempPart = newContentPart.GetPartById(relId);
continue;
}
catch (ArgumentOutOfRangeException)
{
try
{
ExternalRelationship tempEr = newContentPart.GetExternalRelationship(relId);
continue;
}
catch (KeyNotFoundException)
{
}
}
OpenXmlPart oldPart = oldContentPart.GetPartById(relId);
OpenXmlPart newPart = newContentPart.AddNewPart<DiagramDataPart>();
newPart.GetXDocument().Add(oldPart.GetXDocument().Root);
diagramReference.Attribute(R.dm).Value = newContentPart.GetIdOfPart(newPart);
AddRelationships(oldPart, newPart, new[] { newPart.GetXDocument().Root });
CopyRelatedPartsForContentParts(oldPart, newPart, new[] { newPart.GetXDocument().Root }, images);
// lo attribute
relId = diagramReference.Attribute(R.lo).Value;
try
{
OpenXmlPart tempPart = newContentPart.GetPartById(relId);
continue;
}
catch (ArgumentOutOfRangeException)
{
try
{
ExternalRelationship tempEr = newContentPart.GetExternalRelationship(relId);
continue;
}
catch (KeyNotFoundException)
{
}
}
oldPart = oldContentPart.GetPartById(relId);
newPart = newContentPart.AddNewPart<DiagramLayoutDefinitionPart>();
newPart.GetXDocument().Add(oldPart.GetXDocument().Root);
diagramReference.Attribute(R.lo).Value = newContentPart.GetIdOfPart(newPart);
AddRelationships(oldPart, newPart, new[] { newPart.GetXDocument().Root });
CopyRelatedPartsForContentParts(oldPart, newPart, new[] { newPart.GetXDocument().Root }, images);
// qs attribute
relId = diagramReference.Attribute(R.qs).Value;
try
{
OpenXmlPart tempPart = newContentPart.GetPartById(relId);
continue;
}
catch (ArgumentOutOfRangeException)
{
try
{
ExternalRelationship tempEr = newContentPart.GetExternalRelationship(relId);
continue;
}
catch (KeyNotFoundException)
{
}
}
oldPart = oldContentPart.GetPartById(relId);
newPart = newContentPart.AddNewPart<DiagramStylePart>();
newPart.GetXDocument().Add(oldPart.GetXDocument().Root);
diagramReference.Attribute(R.qs).Value = newContentPart.GetIdOfPart(newPart);
AddRelationships(oldPart, newPart, new[] { newPart.GetXDocument().Root });
CopyRelatedPartsForContentParts(oldPart, newPart, new[] { newPart.GetXDocument().Root }, images);
// cs attribute
relId = diagramReference.Attribute(R.cs).Value;
try
{
OpenXmlPart tempPart = newContentPart.GetPartById(relId);
continue;
}
catch (ArgumentOutOfRangeException)
{
try
//.........这里部分代码省略.........
示例3: 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");
}
}