0

I have a picture of internet . I want save it in special part of word .

I find special part with this code .

  XNamespace w =
          "http://schemas.openxmlformats.org/wordprocessingml/2006/main";
        XNamespace r =
            "http://schemas.openxmlformats.org/officeDocument/2006/relationships";


        MainDocumentPart mainPart = wordDoc.MainDocumentPart;
        XDocument mainDocumentXDoc = GetXDocument(wordDoc);
        var q1 = mainDocumentXDoc
        .Root
        .Element(w + "body")
        .Elements();

        // project collection of tuples containing element and type
        var q2 = q1
            .Select(s =>
            {
                string keyForGroupAdjacent = ".NonContentControl";
                if (s.Name == w + "sdt")
                    keyForGroupAdjacent = (s.Element(w + "sdtPr").Element(w + "tag") != null) ? s.Element(w + "sdtPr").Element(w + "tag").Attribute(w + "val").Value : "";
                if (s.Name == w + "sectPr")
                    keyForGroupAdjacent = null;
                return new
                {
                    Element = s,
                    KeyForGroupAdjacent = keyForGroupAdjacent
                };
            }
            ).Where(s => s.KeyForGroupAdjacent != null);



            foreach (var item in q2)
        {
            if (item.Element.Value == "[متن]")
            {
                item.Element.AddFirst(**Picture**);
                break;
            }
        }

Who do add this picture in item ?

1 Answer 1

0

I find :)

I search imagepart with name (Picture 1 ). Then replace my new picture with old picture .

  IEnumerable<Blip> Blips = from run in mainPart.Document.Descendants<Drawing>()
                                      where
                                         run.Descendants<Inline>().First() != null
                                         &&
                                         run.Descendants<Inline>().First().DocProperties.Name != null
                                      &&
                                      run.Descendants<Inline>().First().DocProperties.Name.ToString() == "Picture 1"
                                      select run.Descendants<Blip>().First();
            string imageId = "default value";

            if (Blips != null || Blips.Count() > 0)
            {
                imageId = Blips.First().Embed.Value;
                ImagePart imagePart1 = (ImagePart)mainPart.GetPartById(imageId);
                imagePart1.FeedData(stream);
            }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.