copying openXML image from one document to another

前提是你 提交于 2019-12-05 18:54:10

Late reaction but this thread helped me a lot to got it working. Here my solution for copying a document with images

private static void CopyDocumentWithImages(string path)
{
    if (!Path.GetFileName(path).StartsWith("~$"))
    {
        using (var source = WordprocessingDocument.Open(path, false))
        {
            using (var newDoc = source.CreateNew(path.Replace(".docx", "-images.docx")))
            {
                foreach (var e in source.MainDocumentPart.Document.Body.Elements())
                {
                    var clonedElement = e.CloneNode(true);
                    clonedElement.Descendants<DocumentFormat.OpenXml.Drawing.Blip>()
                        .ToList().ForEach(blip =>
                        {
                            var newRelation = newDoc.CopyImage(blip.Embed, source);
                            blip.Embed = newRelation;
                        });
                    clonedElement.Descendants<DocumentFormat.OpenXml.Vml.ImageData>().ToList().ForEach(imageData =>
                    {
                        var newRelation = newDoc.CopyImage(imageData.RelationshipId, source);
                        imageData.RelationshipId = newRelation;
                    });
                    newDoc.MainDocumentPart.Document.Body.AppendChild(clonedElement);
                }
                newDoc.Save();
            }
        }
    }
}

CopyImage:

public static string CopyImage(this WordprocessingDocument newDoc, string relId, WordprocessingDocument org)
{
    var p = org.MainDocumentPart.GetPartById(relId) as ImagePart;
    var newPart = newDoc.MainDocumentPart.AddPart(p);
    newPart.FeedData(p.GetStream());
    return newDoc.MainDocumentPart.GetIdOfPart(newPart);
}

CreateNew:

public static WordprocessingDocument CreateNew(this WordprocessingDocument org, string name)
{
    var doc = WordprocessingDocument.Create(name, WordprocessingDocumentType.Document);
    doc.AddMainDocumentPart();
    doc.MainDocumentPart.Document = new Document(new Body());
    using (var streamReader = new StreamReader(org.MainDocumentPart.ThemePart.GetStream()))
    using (var streamWriter = new StreamWriter(doc.MainDocumentPart.AddNewPart<ThemePart>().GetStream(FileMode.Create)))
    {
        streamWriter.Write(streamReader.ReadToEnd());
    }
    using (var streamReader = new StreamReader(org.MainDocumentPart.StyleDefinitionsPart.GetStream()))
    using (var streamWriter = new StreamWriter(doc.MainDocumentPart.AddNewPart<StyleDefinitionsPart>().GetStream(FileMode.Create)))
    {
        streamWriter.Write(streamReader.ReadToEnd());
    }
    return doc;
}

Stuart,

I had faced the same problem when I was trying to copy the numbering styles from one document to the other.

I think what Word does internally is, whenever an object is copied from one document to the other the ID for that object is not copied over to the new document and instead what happens is a new ID is assigned to it.

You'll have to get the ID after the image has been copied and then replace it everywhere your image has been used.

I hope this helps, this is what I to use copy numbering styles.

Cheers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!