Word OpenXML更改imagePart的关系ID和URI?

时间:2019-03-26 20:31:27

标签: c# ms-word openxml openxml-sdk

嗨,我该如何更改房地产和uri。 我想合并来自不同文件的页脚和页眉,但在这种情况下,文件和rId1中的uri和关系ID都是/word/media/image1.png。

我想要的是在合并文档之前,我要使用唯一的GUID更改所有关系。 uri应该是

  

/word/media/rIddb654056-f112-4695-b38b-0f4f5f81a1ed.png

现在发生的是只有1张图像被传输到目标文档。

    public void ReplaceBlipRealtionShip(HeaderPart part)
    {
        foreach (var imgPart in part.GetPartsOfType<ImagePart>().Take(1).ToList())
        {
            var existingId = part.GetIdOfPart(imgPart);
            var blip = part.Header.Descendants<A.Blip>()
                .Where(x => x.Embed.Value == existingId).FirstOrDefault();



            if (blip != null)
            {
                var newId = OpenXmlGenericHelper.GenerateUniqueRId();
                var extension = Path.GetExtension(imgPart.Uri.ToString());
                Uri imageUri = new Uri($"/word/media/{newId}.{extension}", UriKind.Relative);
                // Create "image" part in /word/media
                // Change content type for other image types.
                var packageImagePart =
                  WordDoc.Package.CreatePart(imageUri, imgPart.ContentType);

                imgPart.GetStream().CopyTo(packageImagePart.GetStream());


                var packagePart = part.OpenXmlPackage.Package.GetPart(part.Uri);


                var imageReleationshipPart = packagePart.CreateRelationship(
                      new Uri($"/word/media/{newId}.{extension}", UriKind.Relative),
                      motpackaging::System.IO.Packaging.TargetMode.Internal, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image");

                //part.ChangeIdOfPart(imgPart, newId);
                this.WordDoc.Save();
            }
        }
    }

其他代码:

private List<KeyValuePair<Type, Stream>> CopyParts(OpenXmlPart srcPart, OpenXmlPart destPart)
    {
        if(srcPart is HeaderPart)
        {
            ReplaceBlipRealtionShip((HeaderPart)srcPart);
        }

        var listToAdd = new List<KeyValuePair<Type, Stream>>();
        foreach (var idPart in srcPart.Parts)
        {
            var part = srcPart.GetPartById(idPart.RelationshipId);
            if (part is ImagePart)
            {
                var newPart = destPart.AddNewPart<ImagePart>(part.ContentType, idPart.RelationshipId);
                listToAdd.Add(new KeyValuePair<Type, Stream>(typeof(ImagePart), part.GetStream()));
            }
            else if (part is DiagramStylePart)
            {
                destPart.AddNewPart<DiagramStylePart>(idPart.RelationshipId);
                listToAdd.Add(new KeyValuePair<Type, Stream>(typeof(DiagramStylePart), part.GetStream()));
            }
            else if (part is DiagramColorsPart)
            {
                destPart.AddNewPart<DiagramColorsPart>(idPart.RelationshipId);
                listToAdd.Add(new KeyValuePair<Type, Stream>(typeof(DiagramColorsPart),
                    part.GetStream()));
            }
            else if (part is DiagramDataPart)
            {
                destPart.AddNewPart<DiagramDataPart>(idPart.RelationshipId);
                listToAdd.Add(new KeyValuePair<Type, Stream>(typeof(DiagramDataPart), part.GetStream()));
            }
            else if (part is DiagramPersistLayoutPart)
            {
                destPart.AddNewPart<DiagramPersistLayoutPart>(idPart.RelationshipId);
                listToAdd.Add(new KeyValuePair<Type, Stream>(typeof(DiagramPersistLayoutPart),
                    part.GetStream()));
            }
            else if (part is StyleDefinitionsPart)
            {
                destPart.AddNewPart<StyleDefinitionsPart>(idPart.RelationshipId);
                listToAdd.Add(new KeyValuePair<Type, Stream>(typeof(StyleDefinitionsPart),
                    part.GetStream()));
            }
            else if (part is StylesWithEffectsPart)
            {
                destPart.AddNewPart<StylesWithEffectsPart>(idPart.RelationshipId);
                listToAdd.Add(new KeyValuePair<Type, Stream>(typeof(StylesWithEffectsPart),
                    part.GetStream()));
            }
            else if (part is ThemePart)
            {
                destPart.AddNewPart<ThemePart>(idPart.RelationshipId);
                listToAdd.Add(new KeyValuePair<Type, Stream>(typeof(ThemePart),
                    part.GetStream()));
            }
            else if (part is PivotTablePart)
            {
                destPart.AddNewPart<PivotTablePart>(idPart.RelationshipId);
                listToAdd.Add(new KeyValuePair<Type, Stream>(typeof(PivotTablePart),
                    part.GetStream()));
            }
            else if (part is TableDefinitionPart)
            {
                destPart.AddNewPart<TableDefinitionPart>(idPart.RelationshipId);
                listToAdd.Add(new KeyValuePair<Type, Stream>(typeof(TableDefinitionPart),
                    part.GetStream()));
            }
            else if (part is TableStylesPart)
            {
                destPart.AddNewPart<TableStylesPart>(idPart.RelationshipId);
                listToAdd.Add(new KeyValuePair<Type, Stream>(typeof(TableStylesPart),
                    part.GetStream()));
            }
        }

        return listToAdd;
    }

    private void _PrependHeader(HeaderPart srcHeaderPart, HeaderPart destHeaderPart, MainDocumentPart destMainPart)
    {
        if (srcHeaderPart != null)
        {
            // Copy content of header to new header
            destHeaderPart.FeedData(srcHeaderPart.GetStream());

            if (destHeaderPart.Header != null)
            {
                // Keep formatting
                KeepParagraphStyle(destHeaderPart.Header);
            }

            // Get all ids of every 'Parts'
            var listToAdd = CopyParts(srcHeaderPart, destHeaderPart);
            // Foreach Part, copy stream to new header
            var i = 0;
            foreach (var idPart in destHeaderPart.Parts)
            {
                var part = destHeaderPart.GetPartById(idPart.RelationshipId);
                if (part.GetType() == listToAdd[i].Key)
                {
                    part.FeedData(listToAdd[i].Value);
                }
                i++;
            }
        }
        else
        {
            destMainPart.DeleteParts(destMainPart.HeaderParts);
            var sectToRemovePrs = destMainPart.Document.Body.Descendants<SectionProperties>();
            foreach (var sectPr in sectToRemovePrs)
            {
                // Delete reference of old header
                sectPr.RemoveAllChildren<HeaderReference>();
            }
        }
    }

0 个答案:

没有答案