Skip to content

how to copy <w:drawing> <wp:inline to other docx? #1966

@billpeace

Description

@billpeace

Can someone provide an example?

thanks

The current approach is to save the original document as a new file, delete all the body content, place the relevant XML into the paragraph, and generate a new document. However, there is some error.

openXmlElement2 is DocumentFormat.OpenXml.Wordprocessing.Run
<w:r> <w:drawing> <wp:inline>

DocumentFormat.OpenXml.Wordprocessing.Paragraph paragraph6 = new DocumentFormat.OpenXml.Wordprocessing.Paragraph();
foreach (OpenXmlElement element in openXmlElement1.Elements<DocumentFormat.OpenXml.Wordprocessing.ParagraphProperties>())
{
    OpenXmlElement newChild6 = element.CloneNode(true);
    newChild6.RemoveAllChildren<SectionProperties>();
    newChild6.RemoveAllChildren<ParagraphBorders>();
    paragraph6.AppendChild<OpenXmlElement>(newChild6);
}
paragraph6.AppendChild<OpenXmlElement>(openXmlElement2.CloneNode(true));
tableXmlElementList.Add(openXmlElement2);
tablebody2.AppendChild<OpenXmlElement>(paragraph6.CloneNode(true));

using (WordprocessingDocument wordprocessingDocument2 = WordprocessingDocument.Open(temp1, true))
{
    Body body1 = wordprocessingDocument2.MainDocumentPart.Document.Body;

    OpenXmlElement newChild11 = body1.Elements<SectionProperties>().FirstOrDefault<SectionProperties>().CloneNode(true);
    newChild11.RemoveAllChildren<FooterReference>();
    newChild11.RemoveAllChildren<HeaderReference>();
    IEnumerable<PageMargin> source = newChild11.Descendants<PageMargin>();
    if (source.Count<PageMargin>() > 0)
        source.FirstOrDefault<PageMargin>().Bottom = (Int32Value)1710;
    else
        newChild11.Append((OpenXmlElement)new PageMargin()
        {
            Bottom = (Int32Value)1710
        });
    mathbody.AppendChild<OpenXmlElement>(newChild11);
    wordprocessingDocument2.MainDocumentPart.Document.RemoveAllChildren<Body>();
    wordprocessingDocument2.MainDocumentPart.Document.RemoveAllChildren<DocumentBackground>();
    wordprocessingDocument2.MainDocumentPart.Document.Append((OpenXmlElement)mathbody);
    wordprocessingDocument2.Save();
}

=========================================

-<w:drawing>


-<wp:inline wp14:editId="7BB75690" wp14:anchorId="3EDB3CC6" distR="0" distL="0" distB="0" distT="0">

<wp:extent cx="3960000" cy="2412000"/>

<wp:effectExtent r="2540" b="7620" t="0" l="0"/>


-<wp:docPr name="图表 1" id="6">


-<a:extLst xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">


-<a:ext uri="{FF2B5EF4-FFF2-40B4-BE49-F238E27FC236}">

<a16:creationId id="{05A109B0-8715-5C68-1F51-FAAAA0F641B6}" xmlns:a16="http://schemas.microsoft.com/office/drawing/2014/main"/>

</a:ext>

</a:extLst>

</wp:docPr>

<wp:cNvGraphicFramePr/>


-<a:graphic xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">


-<a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/chart">

<c:chart xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" r:id="rId4" xmlns:c="http://schemas.openxmlformats.org/drawingml/2006/chart"/>

</a:graphicData>

</a:graphic>

</wp:inline>

</w:drawing>

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions