Bullets list not correctly formatted

Hello,

I am trying to format a list that has been created from a HTML source like this one :

Test 2

  • bullet 1
  • bullet2
  • bullet3

Test 3

  • bullet 1
  • bullet 2
  • bullet3

I am using the InsertHTML method but this gives me a list with some weird space between each list (you’ll see in attachment the list with the extra space, and what I’d like to generate)

I tryed to use :

doc.NodeChangingCallback = new HandleNodeFont(font.Name, font.Size, cColor,0.0,0.0); 

(0.0 are the values used for SpaceBefore and SpaceAfter)

builder.InsertHtml§;

In the NodeInserted HandleNodeFont that implements INodeChangingCallback i am setting the differents values like the font, size, color but the space after and space before does not seem to be applied. Can you please tell me what is wrong :

public void NodeInserted(NodeChangingArgs args)
{
    if (args.Node.NodeType == NodeType.Paragraph)
    {
        ((Paragraph)args.Node).ParagraphFormat.SpaceAfterAuto = false;
        ((Paragraph)args.Node).ParagraphFormat.SpaceBeforeAuto = false;
        if (mSpaceBefore > -1)
            ((Paragraph)args.Node).ParagraphFormat.SpaceBefore = mSpaceBefore;

        if (mSpaceAfter > -1)
            ((Paragraph)args.Node).ParagraphFormat.SpaceAfter = mSpaceAfter;

        ((Paragraph)args.Node).ParagraphFormat.LineSpacingRule = LineSpacingRule.Exactly;
        ((Paragraph)args.Node).ParagraphFormat.LineSpacing = 1.0;
        if (((Paragraph)args.Node).IsListItem)
        {
            if (color != null)
                ((Paragraph)args.Node).ListFormat.ListLevel.Font.Color = color;
            ((Paragraph)args.Node).ListFormat.ListLevel.Font.Size = mSize;
        }
    }

    if (args.Node.NodeType == NodeType.Run)
    {
        if (!string.IsNullOrEmpty(mName))
            ((Run)args.Node).Font.Name = mName;
        if (mSpaceBefore > -1)
            ((Run)args.Node).ParentParagraph.ParagraphFormat.SpaceBefore = mSpaceBefore;
        if (mSpaceAfter > -1)
            ((Run)args.Node).ParentParagraph.ParagraphFormat.SpaceAfter = mSpaceAfter;
        if (mSize > -1)
            ((Run)args.Node).Font.Size = mSize;

    }
}

Thanks,

Guillaume

[Edit]
I’m adding the source from where I am inserting the HTML :

private static void WriteBulletsInDescriptionRawHTML(ref Document doc, ref DocumentBuilder builder, string p, Color cColor)
{
    Font font = builder.Font;
    SCVUtils.GetFontForText(ref font);
    doc.NodeChangingCallback = new HandleNodeFont(font.Name, font.Size, cColor, 0.0, 0.0);
    builder.InsertHtml§;
    builder.ListFormat.ApplyBulletDefault();
    builder.ListFormat.ListLevel.Font.Color = cColor;
    doc.NodeChangingCallback = null;
    builder.ListFormat.RemoveNumbers();
}

Hi,

Thanks for your inquiry. Please use the following code snippet to remove the extra spacing from your HTML just before saving the HTML to DOCX format:

foreach (Paragraph para in doc.GetChildNodes(NodeType.Paragraph, true))
{
    para.ParagraphFormat.SpaceAfterAuto = false;
    para.ParagraphFormat.SpaceAfter = 0;
    para.ParagraphFormat.SpaceBeforeAuto = false;
    para.ParagraphFormat.SpaceBefore = 0;
}

Best Regards,

Thanks for your answer.

This is interesting and it actually solves my problem in part. I would like to apply this just to a part of my document, not to the whole document. Is there any way to identify the nodes and apply to selected paragraph ?

Thanks,

Guillaume

Hi Guillaume,

Thanks for your inquiry.

I think, you should enclose the Paragraphs (either normal or lists) within a Bookmark, then you will easily be able to adjust spacing of Paragraphs after using the code suggested in the following article:
https://docs.aspose.com/words/net/how-to-extract-selected-content-between-nodes-in-a-document/

I hope, this will help

Best Regards,

Hello sorry for the delay and thank you for your answer.

I tryed to use bookmarks with builder.StartBookmark(“MainContent”); and builder.EndBookmark(“MainContent”);

but when I try ti retrieve those bookmarks I am getting a nullPointerException on doc.Range.Bookmarks[“MainContent”].BookmarkStart

Actually there is no bookmark at all is the bookMark dictionnary of doc.Range.Bookmarks.

Are those settings reset by any kind of action ?

Thanks

Edit : In order to have a document that looks like what I need, I am moving the different sections to the first one, to have only one section and during the building of the document I am calling sevaral times UpdatePageLaoyout. Is that a pb ? It seems that after that my bookmarks diseapear

Hi,

Thanks for your inquiry. I tried inserting Bookmarks using the code mentioned in the following article and everything went as expected on my side:
https://docs.aspose.com/words/net/working-with-bookmarks/

Please make sure if the Bookmarks actually didn’t get deleted during moving Section contents to the first Section.

Best Regards,

Yes, Indeed I think it is due to something like that. I’m trying to look for which part causes the bookmark to be deleted.

Do you know if the method UpdatePageLayout could delete thoses bookmarks ?

Thanks

Hi Guillaume,

Thanks for your inquiry. No, it doesn’t remove any Bookmarks. This method just builds the page layout of the document, formats it into pages and updates the page number related fields in the document such as PAGE, PAGES, PAGEREF and REF. The up-to-date page layout information is required for a correct rendering of the document to fixed-page formats e.g. PDF.

Best Regards,

Hi, thanks for your answer. Actually I succeeded in keeping my bookmark in my document. But something strange is happening. In order to reformat some of the paragraph that contains the bullets I surrounded the InsertHTML that writes the bullets in the document by bookmark. But when my document is finished and I try to recover my bookmarks (with the metod ExtractContent) to reformat the bullet, the Node contained in the bookmark is not corresponding to the paragraph that I wanted to get. It giving me the table in wich that part of my document is uncluded (see the joined document, the bookmark seems to represent the bullets lists but it does not seems to work in the code).

Any Idea of what is going wrong ?

Thanks a lot,

Guillaume

Hi,

I really need some help on why the extraction of the bookmarks does not give me the nodes I was waiting for.

Thanks,

Guillaume

Hi Guillaume,

Thanks for your inquiry. I am working over your query and will get back to you soon.

Best Regards,

Hi Guillaume,

Thanks for the additional information. I think, in your case you should use DocumentVisitor to extract lists between bookmarks from one document. Please follow the link to learn more about DocumentVisitor:
https://reference.aspose.com/words/net/aspose.words/documentvisitor/

The following code extract Run and Paragraph nodes between BookmarkStart and BookmarkEnd nodes:

public class MyBookmarkExtractor : DocumentVisitor
{
    public MyBookmarkExtractor(string name)
    {
        isSkipNode = true;
        bookmarkName = name;
        nodes = new ArrayList();
    }
    public ArrayList GetNodes()
    {
        return nodes;
    }
    public override VisitorAction VisitRun(Run run)
    {
        if (!isSkipNode)
        {
            AppendNode(run);
        }
        return VisitorAction.Continue;
    }
    public override VisitorAction VisitParagraphEnd(Paragraph para)
    {
        if (!isSkipNode)
            AppendNode(para);
        return VisitorAction.Continue;
    }
    public override VisitorAction VisitBookmarkStart(BookmarkStart start)
    {
        if (start.Name.Equals(bookmarkName))
            isSkipNode = false;
        return VisitorAction.Continue;
    }
    public override VisitorAction VisitBookmarkEnd(BookmarkEnd end)
    {
        if (end.Name.Equals(bookmarkName))
            return VisitorAction.Stop;
        return VisitorAction.Continue;
    }
    private void AppendNode(Node n)
    {
        nodes.Add(n);
    }
    private bool isSkipNode;
    private string bookmarkName;
    private ArrayList nodes;
}
Document doc = new Document(@"C:\Temp\ExAsposeBoomarkBullets.docx");
MyBookmarkExtractor extractor = new MyBookmarkExtractor("Bullet_1");
doc.Accept(extractor);
ArrayList nodes = extractor.GetNodes();

Please let me know if I can be of any further assistance.

Best Regards,

Hello,

Thanks for your help. The visitor finally soved my issue. I can now format the bullet list I want.

Guillaume

Hi Guillaume,

Thanks for your feedback. It is perfect that you managed to achieve what you’re looking for. If we can help you with anything else, please feel free to ask.

Best Regards,

Hi !

Actually, yes, I have this issue I’ve posted in this post: <a href="Node incorrectly cut to to the next page

Thanks,

Guillaume

Hi Guillaume,

Thanks for your inquiry. Did you manage to fix the issue on your side or are you still in need of assistance?

Best Regards,

Yes I managed to correct the issue with the section break at the wrong place.
But I have anther issue with the WriteHTML and the bookmark.
Each time I use WriteHTML to display a list i have a empty line following my text. I saw on this post <a href="InsertHTML and Carriage Return that I could use builder.CurrentParagraph.Remove();
It is actually working but it is deleting my bookmark I am using to format my text… How can I do in order to not have this empty line ?
You’ll see in attachement one file with the correct list formatting and the bookmarks but with the empty line and one file where I use CurrentParagraph.Remove which bookmarks and formatting diseapeared.

I could really use a hand on this, it is the last part before I finished the document.

Thanks,

Guillaume

Hi Guillaume,
Thanks for your inquiry. Please try using the following code snippet:

Document doc = new Document(@"C:\Temp\bm.docx");

DocumentBuilder builder = new DocumentBuilder(doc);

builder.MoveToBookmark("bm");
builder.InsertHtml("<ul><li>bullet1</li><li>bullet2</li><li>bullet3</li></ul>Test3<ul><li>bullet 1</li><li>bullet2</li><li>bullet3</li></ul>");

Paragraph currPara = builder.CurrentParagraph;
Paragraph prevPara = builder.CurrentParagraph.PreviousSibling as Paragraph;

prevPara.AppendChild(builder.CurrentParagraph.FirstChild);

builder.MoveTo(currPara);

builder.CurrentParagraph.Remove();

builder.Document.Save(@"C:\Temp\out.docx");

I hope, this helps.
Best Regards,

Hello,

It’s me again.

Unfortunately the snipet you gave me is not working properly. I am having a nullPointer Exception when I try to add something after the bullets list.

Here is the code I am using for inserting my bullets :

builder.StartBookmark(String.Format("Bullet_{0}", boucle));
builder.InsertHtml§;
builder.EndBookmark(String.Format("Bullet_{0}", boucle));
Paragraph currPara = builder.CurrentParagraph;
Paragraph prevPara = builder.CurrentParagraph.PreviousSibling as Paragraph;
prevPara.AppendChild(builder.CurrentParagraph.FirstChild);
builder.MoveTo(currPara);
builder.CurrentParagraph.Remove();

If you try to do something like builder.insertHTML(“toto”) just after you snippet, you should have the error too.

I tryed to add afther this builder.moveto(prevPara) but the result is not what I am expecting. My list does not appears correctly.

Thanks in advance for your help, we are getting close I can fell it !

Guillaume

Hi Guillaume,

Thanks for the additional information. Please move the cursor to a valid location after removing the current paragraph before inserting new content using DocumentBuilder; for example, you can call builder.MoveToDocumentEnd(); method before using builder.insertHTML(“toto”).

I hope, this helps.

Best Regards,