none
How to insert text in Word that indent 10 characters (or 5 cm) from the page margin using C# RRS feed

  • Question

  • Hi,

    I am having a word file that include some text lines.

    Now I want to insert text "Hello" that indent 10 characters (or 5 cm) from the page left margin (using namespace: Microsoft.Office.Interop.Word)

    Do you know how to do that ?

    Thank you very much.

    Monday, January 7, 2013 9:55 AM

Answers

  • Hi Taibc

    In that case, you could use either a table and place the text in cells

    OR

    You can define TabStop points at 5 and 10 centimeters and place a TAB character (ANSI 9) in front of eah word.

    The main difference between a table and a tab stop is that, in a table the text will wrap when it reaches the "end point" (just short of 10 cm). With a tab stop, if the text is too long, it will "push" what follows (in this case "World") futher to the right (to the next (default) tab stop).

    The method definition for adding a tab stop:
      objectParagraph.TabStops.Add(position as Single, alignment as WdAlignmentTabAlignment, leader as String), the last two arguments being option. Position will set the position from the left in points, so you'd want to use CentimetersToPoints as explained previously.


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by Taibc Friday, January 11, 2013 7:33 AM
    Friday, January 11, 2013 7:00 AM
    Moderator

All replies

  • Hi Taibc

    The LeftIndent property of the Paragraph object is what you're looking for. It expects a value in Points.

    Word provides a set of converters you can use to specify a certain number of centimeters (or points). They're members of the Application object. For example: wdApp.CentimetersToPoints(value). So, you could do something liek this, for example (off the top of my head, so there may be small syntax typos!):

      doc.Paragraphs[3].LeftIndent = wdApp.CentimetersToPoints(5);

    If you're going to be needing this formatting more than once or twice in the document you should consider creating a STYLE and apply the style, rather than the "direct formatting".


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, January 10, 2013 4:18 PM
    Moderator
  • Hi Taibc

    The LeftIndent property of the Paragraph object is what you're looking for. It expects a value in Points.

    Word provides a set of converters you can use to specify a certain number of centimeters (or points). They're members of the Application object. For example: wdApp.CentimetersToPoints(value). So, you could do something liek this, for example (off the top of my head, so there may be small syntax typos!):

      doc.Paragraphs[3].LeftIndent = wdApp.CentimetersToPoints(5);

    If you're going to be needing this formatting more than once or twice in the document you should consider creating a STYLE and apply the style, rather than the "direct formatting".


    Cindy Meister, VSTO/Word MVP, my blog

    Thanks Cindy,

    We only have LeftIndent property for Paragraph (not text) ? Is it possible to insert text "Hello" and "World" on the same line, and "Hello" is indented 5 point, "World" is indent 10 point ?

    Thanks and regards,

    Friday, January 11, 2013 1:07 AM
  • Hi Taibc

    In that case, you could use either a table and place the text in cells

    OR

    You can define TabStop points at 5 and 10 centimeters and place a TAB character (ANSI 9) in front of eah word.

    The main difference between a table and a tab stop is that, in a table the text will wrap when it reaches the "end point" (just short of 10 cm). With a tab stop, if the text is too long, it will "push" what follows (in this case "World") futher to the right (to the next (default) tab stop).

    The method definition for adding a tab stop:
      objectParagraph.TabStops.Add(position as Single, alignment as WdAlignmentTabAlignment, leader as String), the last two arguments being option. Position will set the position from the left in points, so you'd want to use CentimetersToPoints as explained previously.


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by Taibc Friday, January 11, 2013 7:33 AM
    Friday, January 11, 2013 7:00 AM
    Moderator
  • Yes. Thanks Cindy very much.
    Friday, January 11, 2013 7:34 AM
  • Hi Cindy,

    Could you give an example of placing a TAB character (ANSI 9) in front of each word. Would really appreciate it. Thanks.

    Friday, May 24, 2013 1:44 PM