Free Support Forum - aspose.com

How to insert html block between numbered list as appropriate indentations

Hello,

I try to insert html data between numberedList item. But occured a problem about indentation.
html data which is inserted, starts the begining of line. But it should be start on previous list item indentation.
Plz help me about this issue.
We use Implementation-Title: Aspose.Words for Java
Specification-Version: 4.0.2.0


here is the my code snippet:
private void docCreate(DocumentItem item, DocumentBuilder builder)
{
try
{
com.aspose.words.List tempList = builder.getListFormat().getList();
int level = builder.getListFormat().getListLevelNumber();
builder.setBold(true);
builder.writeln(item.getItemName());
builder.setBold(false);
if (!StringUtility.isNullOrEmpty(item.getContent()))
{
builder.getListFormat().removeNumbers();
builder.insertHtml(item.getContent());
builder.getListFormat().setList(tempList);
builder.getListFormat().setListLevelNumber(level);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}



current out:
5. Item
Item desc.
5.1. ChildItem 1
ChildItem1 desc
5.1.1. ChildItem 1-1
5.1.1.1. ChildItem 1-1-1
ChildItem 1-1-1 desc
5.1.1.2. ChildItem 1-1-2
5.1.2. ChildItem 1-2
ChildItem 1-2 desc
5.2. ChildItem 2


expected out:
5. Item
Item desc.
5.1. ChildItem 1
ChildItem1 desc
5.1.1. ChildItem 1-1
5.1.1.1. ChildItem 1-1-1
ChildItem 1-1-1 desc
5.1.1.2. ChildItem 1-1-2
5.1.2. ChildItem 1-2
ChildItem 1-2 desc
5.2. ChildItem 2

brgds…

Hi,


Thanks for your inquiry.

Please note that you can not merge the formatting of an HTML fragment with formatting of a destination document directly by using the InsertHtml method. The InsertHtml method does not work this way; it uses only formatting of the HTML fragment and does not take destination formatting into account. The HTML fragment that is being inserted into a document should be formatted either before the insertion (by applying CSS styles to HTML elements) or after it (by setting DOM node properties).

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

Best regards,

Hi,

Thx for your reply. So how can i set left margin of html text according to listLevel?
Or do i have to handle all structure as html ?

Hi,


Thanks for your inquiry. You need to supply a suitable value for the ‘margin-left’ style attribute in HTML fragment as follows:

ChildItem1 desc


Best regards,

Dear Mr Hafeez,

As a matter of fact, I've asked how can I know that suitable value for the margin left attribute. Is it possible to reach by using listLevelNumber stuff? Is there a ration between them?

brgds

Hi,


Thanks for your inquiry. I think, you can calculate the margin-left attribute value from Paragraph’s left indentation. Please see the following code:
NodeCollection paragraphs = doc.getChildNodes(NodeType.PARAGRAPH, true);
for (Paragraph para : (Iterable<Paragraph>)paragraphs)
{
if (para.isListItem())
{
System.out.println(para.getParagraphFormat().getLeftIndent());
}
}
I hope, this helps.

Best regards,

Thank you for your politely assistance we will be try acc to this info.

Hi again Mr Hafeez,

I applied your solution. could you plz see my code snippet below:

private void docCreate(DocumentItem item, DocumentBuilder builder, boolean isLast)
{
try
{
com.aspose.words.List tempList = builder.getListFormat().getList();
int level = builder.getListFormat().getListLevelNumber();
builder.setBold(true);
builder.writeln(item.getItemName());
if (!StringUtility.isNullOrEmpty(item.getContent()))
{
builder.setBold(false);
double f = builder.getCurrentParagraph().getParagraphFormat().getLeftIndent();
System.out.println(f);
builder.getListFormat().removeNumbers();

builder.insertHtml(setIndentation(item.getContent(), f));
builder.getListFormat().setList(tempList);
builder.getListFormat().setListLevelNumber(level);
}

if (isLast)
builder.getListFormat().removeNumbers();
}
catch (Exception e)
{
e.printStackTrace();
}
}

private String setIndentation(String content, double level)
{
//level *= 25;
content = content.replaceAll("

", "");
content = content.replaceAll("

", "");
return "

" + content + "

";
}

But, it does not make supply the exactly right alignment. i.e. when returns 18.0, essentially it must be return 25.0. I attached the out file for your consideration, you may want to take a look at page 2.

Best regards.

Hi Mustafa,


Thanks for the additional information. I think, you need to implement INodeChangingCallback interface if you want to set custom indentation for the Paragraphs being inserted via InsertHtml method. Please try running the following code snippet:
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

builder.getListFormat().setList(doc.getLists().add(ListTemplate.NUMBER_ARABIC_DOT));

for (int i = 0; i < 9; i++) {
builder.getListFormat().setListLevelNumber(i);
builder.writeln("Level " + i);
}

HandleNodeChanging_ListIndent hanlder = new HandleNodeChanging_ListIndent();
doc.setNodeChangingCallback(hanlder);

NodeCollection paragraphs = doc.getChildNodes(NodeType.PARAGRAPH, true);
for (Paragraph paragraph : (Iterable<Paragraph>) paragraphs) {
if (paragraph.isListItem()) {
builder.moveTo(paragraph);
hanlder.leftIndent = paragraph.getParagraphFormat().getLeftIndent();

    builder<font color="BLUE"><b>.</b></font>insertHtml<font color="BLUE"><b>(</b></font><font color="PURPLE">"<p>ChildItem1 desc</p>"</font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
<font color="BLUE"><b>}</b></font>

}

builder.getDocument().save(“C:\Temp\Out.doc”);


static class HandleNodeChanging_ListIndent implements INodeChangingCallback {
public double leftIndent = 0.0;
<font color="RED"><b>public</b></font> <font color="RED"><b>void</b></font> nodeInserted<font color="BLUE"><b>(</b></font>NodeChangingArgs args<font color="BLUE"><b>)</b></font> <font color="RED"><b>throws</b></font> Exception <font color="BLUE"><b>{</b></font>
    <font color="RED"><b>if</b></font> <font color="BLUE"><b>(</b></font>args<font color="BLUE"><b>.</b></font>getNode<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>.</b></font>getNodeType<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font> <font color="BLUE">=</font><font color="BLUE">=</font> NodeType<font color="BLUE"><b>.</b></font>RUN<font color="BLUE"><b>)</b></font> <font color="BLUE"><b>{</b></font>
        Run run <font color="BLUE">=</font> <font color="BLUE"><b>(</b></font><font color="BLUE"><b>(</b></font>Run<font color="BLUE"><b>)</b></font> args<font color="BLUE"><b>.</b></font>getNode<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
        Paragraph para <font color="BLUE">=</font> run<font color="BLUE"><b>.</b></font>getParentParagraph<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>

para.getParagraphFormat().setLeftIndent(leftIndent);

    <font color="BLUE"><b>}</b></font>
<font color="BLUE"><b>}</b></font>

<font color="RED"><b>public</b></font> <font color="RED"><b>void</b></font> nodeInserting<font color="BLUE"><b>(</b></font>NodeChangingArgs args<font color="BLUE"><b>)</b></font> <font color="RED"><b>throws</b></font> Exception <font color="BLUE"><b>{</b></font>
    <font color="GREEN"><i>// Do Nothing

}

<font color="RED"><b>public</b></font> <font color="RED"><b>void</b></font> nodeRemoved<font color="BLUE"><b>(</b></font>NodeChangingArgs args<font color="BLUE"><b>)</b></font> <font color="RED"><b>throws</b></font> Exception <font color="BLUE"><b>{</b></font>
    <font color="GREEN"><i>// Do Nothing

}

<font color="RED"><b>public</b></font> <font color="RED"><b>void</b></font> nodeRemoving<font color="BLUE"><b>(</b></font>NodeChangingArgs args<font color="BLUE"><b>)</b></font> <font color="RED"><b>throws</b></font> Exception <font color="BLUE"><b>{</b></font>
    <font color="GREEN"><i>// Do Nothing

}
}

I hope, this helps.

Best regards,