maulik kansara
maulik kansara

Reputation: 1107

How to get correct Horizontal Text Boundary

Basically I am implementing a function which check all numbering styles and create same replica but without numbers. This will be used and following paragraph style.

I am able to get all the properties but not able to set the FirstLineIndent correctly.

Example,

current result:

  1. This is a main paragraph

This is a following paragraph

Expected Result:

  1. This is a main paragraph

    This is a following paragraph

Following paragraph text should start just below the main paragraph.

so far implementation is like below:

float GetFirstCharactorPosition(Document doc, string styleName)
{
    try
    {
    // get the main style from the document styles
        Style partStyle = doc.Styles((styleName));

    // ho to start of the document
        doc.Application.Selection.GoTo(WdGoToItem.wdGoToSection, WdGoToDirection.wdGoToFirst);
        doc.ActiveWindow.Selection.HomeKey(WdUnits.wdStory);

    // select first section
        Section firstSec = doc.Sections(1);
    
    // add paragraph at the last in the section
        if ((firstSec.Range.Paragraphs.Last.Range.Characters.Count > 1))
            firstSec.Range.InsertParagraphAfter();

    // get last paragraph and apply main numbering style
        Paragraph lastpara = doc.Paragraphs.Last;
        Range rng = lastpara.Range.Duplicate;
        rng.Style = partStyle;
        rng.Text = "test";
        //rng.Collapse(WdCollapseDirection.wdCollapseEnd);
        //rng.MoveEnd(-1);
    
    // get Text boundary 
        float firstCharPosition = System.Convert.ToSingle(rng.Information(WdInformation.wdHorizontalPositionRelativeToTextBoundary));
        rng.Delete();
        return firstCharPosition;
    }
    catch (Exception ex)
    {
        return ParagraphProps.LeftIndent;
    }
    finally
    {
    }
}

In above example, lets say, position start from 108 points and text start from 144 points. then Text Boundary returns 108 instead of 144.

If i run the same on VBA window then it is giving correct 144.

Upvotes: 0

Views: 40

Answers (0)

Related Questions