Formatting indentation under a bookmark

I want to add 3 lines of text at a bookmark and I have a function that inserts a line of text that I run 3 times. The problem is that it messes up the bookmark indentation and the indentation of the final line of text added. Please can anyone advise how to do this?

This is my code (NB I tried moving to the bookmark and to the end of the bookmark but neither worked):

        Document doc = new Document(filenameAndPath);
        DocumentBuilder builder = new DocumentBuilder(doc);

        //builder.MoveToBookmark(ssBookmarkTitle, false, true);
        Bookmark bookmark = doc.Range.Bookmarks[bookmarkTitle];
        builder.MoveTo(bookmark.BookmarkEnd);
        builder.ParagraphFormat.LeftIndent = 227;
        builder.Writeln(ssInsertionText);

        doc.Save(filenameAndPath);

@SCDGLC

To ensure a timely and accurate response, please attach the following resources here for testing:

  • Your input Word document.
  • Please attach the output Word file that shows the undesired behavior.
  • Please attach the expected output Word file that shows the desired behavior.

As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip and upload them.

I’ve tried all sorts of things but here is an example and I’ll post the sample file and test results (all 3 return the same results):

    private static void IndentTest1(string filenameAndPath)
    {
        Document doc = new Document(filenameAndPath);
        DocumentBuilder builder = new DocumentBuilder(doc);
        builder.MoveToBookmark("UniqueMarketReference", false, true);

        double indent = builder.CurrentParagraph.ParagraphFormat.LeftIndent;

        builder.Writeln("Test1");
        builder.ParagraphFormat.LeftIndent = indent;
        builder.Writeln("Test2");
        builder.ParagraphFormat.LeftIndent = indent;
        builder.Writeln("Test3");
        builder.ParagraphFormat.LeftIndent = indent;
        builder.Writeln("Test4");

        doc.Save(filenameAndPath);
    }

    private static void IndentTest2(string filenameAndPath)
    {
        Document doc = new Document(filenameAndPath);
        DocumentBuilder builder = new DocumentBuilder(doc);

        builder.MoveToBookmark("UniqueMarketReference", false, true);
        builder.Writeln("Test1");
        builder.MoveToBookmark("UniqueMarketReference", false, true);
        builder.Writeln("Test2");
        builder.MoveToBookmark("UniqueMarketReference", false, true);
        builder.Writeln("Test3");
        builder.MoveToBookmark("UniqueMarketReference", false, true);
        builder.Writeln("Test4");

        doc.Save(filenameAndPath);
    }


    private static void IndentTest3(string filenameAndPath)
    {
        Document doc = new Document(filenameAndPath);
        DocumentBuilder builder = new DocumentBuilder(doc);
        Bookmark bookmark = doc.Range.Bookmarks["UniqueMarketReference"];

        builder.MoveTo(bookmark.BookmarkEnd);
        builder.Writeln("Test1");
        builder.MoveTo(bookmark.BookmarkEnd);
        builder.Writeln("Test2");
        builder.MoveTo(bookmark.BookmarkEnd);
        builder.Writeln("Test3");
        builder.MoveTo(bookmark.BookmarkEnd);
        builder.Writeln("Test4");

        doc.Save(filenameAndPath);
    }

Aspose Word_Test Sample.zip (41.8 KB)

Also this code gives results as per this image…

    public void InsertTextAtBookmark(string filenameAndPath, string insertionText, string bookmarkTitle)
    {

        LoadLicense();

        Document doc = new Document(filenameAndPath);
        DocumentBuilder builder = new DocumentBuilder(doc);

        Bookmark bookmark = doc.Range.Bookmarks[bookmarkTitle];
        builder.MoveTo(bookmark.BookmarkEnd);
        builder.ParagraphFormat.LeftIndent = 227;
        builder.Writeln(insertionText);

        doc.Save(filenameAndPath);

    }

aspose word indent.PNG (1.1 KB)

@SCDGLC

You are facing the expected behavior of Aspose.Words. The output document is generated according to the code example. Please open the input and output documents and check the paragraph properties.

Could you please ZIP and attach your expected output Word document? We will then provide you more information about your query.

Attached is the kind of output I want to achieve. Note that there are 2 bookmarks and I want to programmatically insert text1, text2, text3, text4, etc. under the relevant bookmark using the bookmark name. Aspose Sample.zip (8.7 KB)
Many thanks.

@SCDGLC

Please set the ParagraphFormat.FirstLineIndent property as shown below to get the desired output. We have attached the output document with this post for your kind reference. 19.5.zip (19.1 KB)

Document doc = new Document(MyDir + "Aspose Word_Test Sample.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToBookmark("UniqueMarketReference", false, true);
                 
double indent = builder.CurrentParagraph.ParagraphFormat.LeftIndent;

builder.Writeln("Test1");
builder.ParagraphFormat.FirstLineIndent = 0;
builder.ParagraphFormat.LeftIndent = indent;
builder.Writeln("Test2");
builder.ParagraphFormat.LeftIndent = indent;
builder.Writeln("Test3");
builder.ParagraphFormat.LeftIndent = indent;
builder.Writeln("Test4");

doc.Save(MyDir + "19.5.docx");

That’s great thank you!

However please can I ask how I would achieve the same result when passing in the text one line at a time, e.g…

    private static void IndentBookmark(string filenameAndPath, string insertionText)
    {
        Document doc = new Document(filenameAndPath);
        DocumentBuilder builder = new DocumentBuilder(doc);
        builder.MoveToBookmark("UniqueMarketReference", false, true);
        builder.Writeln(insertionText);
        doc.Save(filenameAndPath);
    }

        // Call it 3 times...
        IndentBookmark(@"c:\temp\aspose test.docx","test1");
        IndentBookmark(@"c:\temp\aspose test.docx","test2");
        IndentBookmark(@"c:\temp\aspose test.docx","test3");

@SCDGLC

You can use following code example to get the desired output.

Document doc = new Document(MyDir + "Aspose Word_Test Sample.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToBookmark("UniqueMarketReference", false, true);

double indent = builder.CurrentParagraph.ParagraphFormat.LeftIndent;
builder.Writeln();
builder.ParagraphFormat.FirstLineIndent = 0;
builder.ParagraphFormat.LeftIndent = indent;
builder.Write("Test1" + ControlChar.ParagraphBreak + "Test2" + ControlChar.ParagraphBreak
    + "Test3" + ControlChar.ParagraphBreak
    + "Test4" + ControlChar.ParagraphBreak);

doc.Save(MyDir + "19.5.docx");

Thanks so much, however when repeatedly called it inserts with different formatting as per the image below. Is that overcome by inserting a paragraph keep formatting somewhere?

formatting.PNG (958 Bytes)

@SCDGLC

We have tested the scenario using the latest version of Aspose.Words for .NET 19.5 and have not found the shared issue.

We suggest you please set the paragraph formatting before inserting the text and insert the paragraph’s text one by one. Please read the following article.
Inserting Document Elements

Ok and many thanks for your help!