none
Replace a text with an image in a word document using Open xml sdk RRS feed

  • Question

  • I am trying to replace a text with an image in a word document using Open Xml sdk. My code is erroring out on this line: DocumentFormat.OpenXml.Office.Drawing.Drawing element

     using (WordprocessingDocument myDoc =  WordprocessingDocument.Open("C:\\Users\\Desktop\\TestPdfs\\ModifiedDocs\\Test1.docx", true))
                {
                    MainDocumentPart mainPart = myDoc.MainDocumentPart;
                    DocumentFormat.OpenXml.Wordprocessing.PageNumber test = null ;
    
                    Text textPlaceHolder = myDoc.MainDocumentPart.Document.Body.Descendants<Text>().Where((x) => x.Text == "##Signature##").First();
    
                    if (textPlaceHolder == null)
                    {
                        Console.WriteLine("Text holder not found!");
                    }
                    else
                    {
                        var parent = textPlaceHolder.Parent;
    
                        try
                        {
                            DocumentFormat.OpenXml.Office.Drawing.Drawing element = new DocumentFormat.OpenXml.Office.Drawing.Drawing("C:\\Users\\Desktop\\TestPdfs\\Signature1.png");
    
                            // Insert image (the image created with your function) after text place holder.        
                            textPlaceHolder.Parent.InsertAfter<DocumentFormat.OpenXml.Office.Drawing.Drawing>(element, textPlaceHolder);
                            // Remove text place holder.
                            textPlaceHolder.Remove();
                        }
                        catch(Exception ex)
                        {
                            Console.Write(ex);
                        }                       
                        
                    }
    
                }


    • Edited by Lio1972 Wednesday, October 4, 2017 6:14 PM
    • Moved by Chenchen LiModerator Thursday, October 5, 2017 6:34 AM OpenXML,NotJSAPI
    Wednesday, October 4, 2017 6:13 PM

All replies

  • Hello,

    Drawing element should be in DocumentFormat.OpenXml.Wordprocessing instead.

    I suggest you download Open XML SDK Tool and create two documents. One is the original document with text. The other is your target document. Then you could use the tool to compare the two files and generate code.

    E.g. 

    using System.Linq;
    using DocumentFormat.OpenXml.Packaging;
    using DocumentFormat.OpenXml.Wordprocessing;
    using DocumentFormat.OpenXml;
    using Wp = DocumentFormat.OpenXml.Drawing.Wordprocessing;
    using A = DocumentFormat.OpenXml.Drawing;
    using Pic = DocumentFormat.OpenXml.Drawing.Pictures;
    using A14 = DocumentFormat.OpenXml.Office2010.Drawing;
    
    namespace GeneratedCode
    {
        public class GeneratedClass
        {
            public  void ChangeMainDocumentPart(MainDocumentPart mainDocumentPart1)
            {
                Document document1 = mainDocumentPart1.Document;
    
                Body body1=document1.GetFirstChild<Body>();
    
                Paragraph paragraph1=body1.GetFirstChild<Paragraph>();
                SectionProperties sectionProperties1=body1.GetFirstChild<SectionProperties>();
    
                Paragraph paragraph2 = new Paragraph(){ RsidParagraphMarkRevision = "00A76B44", RsidParagraphAddition = "00E42D53", RsidParagraphProperties = "00A76B44", RsidRunAdditionDefault = "00A76B44" };
                BookmarkStart bookmarkStart1 = new BookmarkStart(){ Name = "_GoBack", Id = "0" };
    
                Run run1 = new Run();
    
                RunProperties runProperties1 = new RunProperties();
                NoProof noProof1 = new NoProof();
    
                runProperties1.Append(noProof1);
    
                Drawing drawing1 = new Drawing();
    
                Wp.Inline inline1 = new Wp.Inline(){ DistanceFromTop = (UInt32Value)0U, DistanceFromBottom = (UInt32Value)0U, DistanceFromLeft = (UInt32Value)0U, DistanceFromRight = (UInt32Value)0U };
                Wp.Extent extent1 = new Wp.Extent(){ Cx = 5943600L, Cy = 3714750L };
                Wp.EffectExtent effectExtent1 = new Wp.EffectExtent(){ LeftEdge = 0L, TopEdge = 0L, RightEdge = 0L, BottomEdge = 0L };
                Wp.DocProperties docProperties1 = new Wp.DocProperties(){ Id = (UInt32Value)1U, Name = "Picture 1" };
    
                Wp.NonVisualGraphicFrameDrawingProperties nonVisualGraphicFrameDrawingProperties1 = new Wp.NonVisualGraphicFrameDrawingProperties();
    
                A.GraphicFrameLocks graphicFrameLocks1 = new A.GraphicFrameLocks(){ NoChangeAspect = true };
                graphicFrameLocks1.AddNamespaceDeclaration("a", "http://schemas.openxmlformats.org/drawingml/2006/main");
    
                nonVisualGraphicFrameDrawingProperties1.Append(graphicFrameLocks1);
    
                A.Graphic graphic1 = new A.Graphic();
                graphic1.AddNamespaceDeclaration("a", "http://schemas.openxmlformats.org/drawingml/2006/main");
    
                A.GraphicData graphicData1 = new A.GraphicData(){ Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture" };
    
                Pic.Picture picture1 = new Pic.Picture();
                picture1.AddNamespaceDeclaration("pic", "http://schemas.openxmlformats.org/drawingml/2006/picture");
    
                Pic.NonVisualPictureProperties nonVisualPictureProperties1 = new Pic.NonVisualPictureProperties();
                Pic.NonVisualDrawingProperties nonVisualDrawingProperties1 = new Pic.NonVisualDrawingProperties(){ Id = (UInt32Value)1U, Name = "test.jpg" };
                Pic.NonVisualPictureDrawingProperties nonVisualPictureDrawingProperties1 = new Pic.NonVisualPictureDrawingProperties();
    
                nonVisualPictureProperties1.Append(nonVisualDrawingProperties1);
                nonVisualPictureProperties1.Append(nonVisualPictureDrawingProperties1);
    
                Pic.BlipFill blipFill1 = new Pic.BlipFill();
    
                A.Blip blip1 = new A.Blip(){ Embed = "rId4", CompressionState = A.BlipCompressionValues.Print };
    
                A.BlipExtensionList blipExtensionList1 = new A.BlipExtensionList();
    
                A.BlipExtension blipExtension1 = new A.BlipExtension(){ Uri = "{28A0092B-C50C-407E-A947-70E740481C1C}" };
    
                A14.UseLocalDpi useLocalDpi1 = new A14.UseLocalDpi(){ Val = false };
                useLocalDpi1.AddNamespaceDeclaration("a14", "http://schemas.microsoft.com/office/drawing/2010/main");
    
                blipExtension1.Append(useLocalDpi1);
    
                blipExtensionList1.Append(blipExtension1);
    
                blip1.Append(blipExtensionList1);
    
                A.Stretch stretch1 = new A.Stretch();
                A.FillRectangle fillRectangle1 = new A.FillRectangle();
    
                stretch1.Append(fillRectangle1);
    
                blipFill1.Append(blip1);
                blipFill1.Append(stretch1);
    
                Pic.ShapeProperties shapeProperties1 = new Pic.ShapeProperties();
    
                A.Transform2D transform2D1 = new A.Transform2D();
                A.Offset offset1 = new A.Offset(){ X = 0L, Y = 0L };
                A.Extents extents1 = new A.Extents(){ Cx = 5943600L, Cy = 3714750L };
    
                transform2D1.Append(offset1);
                transform2D1.Append(extents1);
    
                A.PresetGeometry presetGeometry1 = new A.PresetGeometry(){ Preset = A.ShapeTypeValues.Rectangle };
                A.AdjustValueList adjustValueList1 = new A.AdjustValueList();
    
                presetGeometry1.Append(adjustValueList1);
    
                shapeProperties1.Append(transform2D1);
                shapeProperties1.Append(presetGeometry1);
    
                picture1.Append(nonVisualPictureProperties1);
                picture1.Append(blipFill1);
                picture1.Append(shapeProperties1);
    
                graphicData1.Append(picture1);
    
                graphic1.Append(graphicData1);
    
                inline1.Append(extent1);
                inline1.Append(effectExtent1);
                inline1.Append(docProperties1);
                inline1.Append(nonVisualGraphicFrameDrawingProperties1);
                inline1.Append(graphic1);
    
                drawing1.Append(inline1);
    
                run1.Append(runProperties1);
                run1.Append(drawing1);
                BookmarkEnd bookmarkEnd1 = new BookmarkEnd(){ Id = "0" };
    
                paragraph2.Append(bookmarkStart1);
                paragraph2.Append(run1);
                paragraph2.Append(bookmarkEnd1);
                body1.InsertBefore(paragraph2,paragraph1);
    
                paragraph1.Remove();
                sectionProperties1.RsidR = "00E42D53";
                sectionProperties1.RsidRPr = "00A76B44";
            }
    
    
        }
    }
    

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, October 5, 2017 8:29 AM
    Moderator