Document Builder's insertHtml(“some html”) method creates a new line

builder.insertHtml(“some html”) create new line pre and last. How can i remove it?

@namlb1,

Thanks for your inquiry. To ensure a timely and accurate response, please ZIP and attach the following resources here for testing:

  • Your simplified input file
  • Aspose.Words 19.10 generated output DOCX file showing the undesired behavior
  • Your expected DOCX Word document showing the correct output. You can create expected document by using MS Word.

As soon as you get these pieces of information ready, we will start investigation into your issue and provide you code to achieve the same output by using Aspose.Words. Thanks for your cooperation.

this is my code:

DocumentBuilder builder =new DocumentBuilder(doc);
//test
builder.moveToBookmark("test");
builder.insertHtml("<p>This my content 1</p><p>This my content 2</p>");

my template (you should focus on “test” bookmark)
https://drive.google.com/open?id=1urg7ezvh_tzfqydpp-vd4ymhjcteaver

and my resultresult.PNG (10.4 KB)
Thank for support!

@namlb1,

Please pass the following string in InsertHtml method to get the desired results:

builder.insertHtml("<p>This my content 1</p>This my content 2");

Sorry for delay. Thank for your answer
But this is not my case. i can’t control data from front-end with many p tag. My data have many tag p in string. Can you offer me other solution?
Thank.

@namlb1,
You can build logic on the following code to get the desired output:

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

HandleNodeChanging handler = new HandleNodeChanging();
doc.setNodeChangingCallback(handler);

builder.insertHtml("<p>This my content 1</p><p>This my content 2</p><p>This my content 3</p>");

Paragraph lastPara = (Paragraph) handler.getInsertedParagraphs().get(handler.getInsertedParagraphs().size() - 1);
lastPara.remove();

doc.save("E:\\Temp\\19.10.docx");

public static class HandleNodeChanging implements INodeChangingCallback
{
    private ArrayList mInsertedParagraphs = new ArrayList();

    public ArrayList getInsertedParagraphs()
    {
        return mInsertedParagraphs;
    }

    public void nodeInserted(NodeChangingArgs args) throws Exception
    {
        if (args.getNode().getNodeType() == NodeType.PARAGRAPH)
            mInsertedParagraphs.add(args.getNode());
    }

    public void nodeInserting(NodeChangingArgs args) throws Exception
    {
        // Do Nothing
    }

    public void nodeRemoved(NodeChangingArgs args) throws Exception
    {
        // Do Nothing
    }

    public void nodeRemoving(NodeChangingArgs args) throws Exception
    {
        // Do Nothing
    }
}

Thank you very much!