Replacing indented text with html table looses indentation

I implemented an IReplacingCallback to replace a text placeholder in a document with a html string. The simplest way to do that would be like:

@Override
public int replacing(ReplacingArgs e) throws Exception {
	List<Run> runs = DocumentProcessingUtil.findAndSplitMatchRuns(e);
	String htmlText = new HtmlProvider().getHtmlToBeInserted();
	Node matchNode = e.getMatchNode();
	DocumentBuilder builder = new DocumentBuilder((Document) matchNode.getDocument());
	builder.moveTo(matchNode);
	builder.insertHtml(htmlText, true);
	runs.forEach(Run::remove);
	return ReplaceAction.SKIP;
}

This works in most cases. If my placeholder in the template Word document has been indented, I would expect that the inserted html is indented, too.
This is the case for all html elements except tables. Tables that get inserted via insertHtml always have a left indent of zero regardless of the paragraph format.

@sebastian.rollwage.c

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.
  • Please create a simple Java application ( source code without compilation errors ) that helps us to reproduce your problem on our end and attach it here for testing.

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.

Sorry for not answering that long - totally missed that. However the issue recently resurfaced again so I created the requested input, output, expected output and sample application. Please find it attached.

aspose-indentation-bug.zip (46.8 KB)

@sebastian.rollwage.c Thank you for additional information. The described behavior is expected since the table from HTML is not inserted inside paragraph, but it is inserted next to it, since both Table and Paragraph are block level nodes. In your case, if you need paragraph indent to be applied to the inserted table, it will be required to do document postprocessing. For example see the following modified replace evaluator that post process the inserted HTML content:

static class ReplaceWithHtmlEvaluator implements IReplacingCallback {
    public int replacing(ReplacingArgs e) throws Exception {
        DocumentBuilder builder = new DocumentBuilder((Document) e.getMatchNode().getDocument());
        builder.moveTo(e.getMatchNode().getAncestor(NodeType.PARAGRAPH));

        // Get the current paragraph
        Paragraph para = builder.getCurrentParagraph();
        double leftIndent = para.getParagraphFormat().getLeftIndent();

        builder.insertHtml(HTML_INPUT);

        // Now set indent applied to the paragraph to the inserted content.
        Node currentNode = builder.getCurrentParagraph();
        while (currentNode != null && currentNode != para)
        {
            if(currentNode.getNodeType() == NodeType.PARAGRAPH)
                ((Paragraph)currentNode).getParagraphFormat().setLeftIndent(leftIndent);
            else if(currentNode.getNodeType() == NodeType.TABLE)
                ((Table)currentNode).setLeftIndent(leftIndent);

            currentNode = currentNode.getPreviousSibling();
        }
        para.remove();
        return ReplaceAction.SKIP;
    }
}

Thank you! Didn’t assume it had to be set manually due to both being block level nodes. I’ll check if that can somehow be applied to the original code.

1 Like