Search code examples
c#office-interopdesktop-applicationword-automation

How to Add space between 2 pictures in office interop word c#?


im trying to add two pictures no matter what i do they will be placed on each other so how to add these two pictures exactly under each other with specified space in between?

  //first picture

      wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
            string imagename = "C:\\Users\\a_shi\\Desktop\\Man\\madarek\\man3.jpg";
            InlineShape pictureShape = wrdRng.InlineShapes.AddPicture(imagename);
            pictureShape.Width = 150;
            pictureShape.Height = 150;
            Word.Shape shape = pictureShape.ConvertToShape();
            shape.WrapFormat.Type = Word.WdWrapType.wdWrapInline;
            shape.Left = (float)Word.WdShapePosition.wdShapeCenter;
            shape.Top = (float)Word.WdShapePosition.wdShapeTop;
//what ive tried so far
            wrdRng.InsertParagraphAfter();
            wrdRng.InsertAfter("\v");

//second picture

    wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
                string pa = "C:\\Users\\a_shi\\Desktop\\some stuff\\wow.jpg";
                InlineShape pictureShape = wrdRng.InlineShapes.AddPicture(pa);
                pictureShape.Width = 100;
                pictureShape.Height = 150;
                Word.Shape shape1 = pictureShape.ConvertToShape();
                shape1.WrapFormat.Type = Word.WdWrapType.wdWrapSquare;
                shape1.Left = (float)Word.WdShapePosition.wdShapeCenter;
              shape1.Top = (float)Word.WdShapePosition.wdShapeTop

and how to add certain space (empty line) after any element in office interop word?


Solution

  • When theres nobody to help you... HELP YOURSELF! thats what im gonna do:

    So assume you wanna add space between 2 elements in office interop word, just follow the 3 steps below and you will do just fine:

    1)Define a new Range and set it to end of doc:

    object newrng1 = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
    

    2)Add a new paragraph to the range we just defined. (even if theres no content!) we do this to use ParagraphFormat property.

    Word.Paragraph oPara = oDoc.Content.Paragraphs.Add(ref newrng1);
    

    3)finally add the desired space, note that the LineSpacing property only accepts float.

     oPara.Range.ParagraphFormat.LineSpacing = 20.0f;