Free Support Forum - aspose.com

InsertHTML while Inserting a list

Hello Forums,

I have a question about insertingHTML while inserting a list with the DocumentBuilder. When I insertHTML i would like to keep the List formating defined by aspose, but that appears not to be happening. Here is some code illustrating that affect.

Attached is the desired output.

You guys are great -- thanks in advance for your help.

Document doc = new Document();

// Create a list based on one of the Microsoft Word list templates.

List list = doc.Lists.Add(ListTemplate.OutlineHeadingsLegal);

DocumentBuilder builder = new DocumentBuilder(doc);

builder.ListFormat.List = list;

builder.Writeln("The quick brown fox...");

builder.Writeln("The quick brown fox...");

builder.ListFormat.ListIndent();

builder.Writeln("jumped over the lazy dog.");

builder.InsertHtml("

Testing instering HTML

  1. jumped over the html dog.
  2. jumped over the html dog 2.
"
);

builder.Writeln("jumped over the lazy dog.");

builder.ListFormat.ListOutdent();

builder.Writeln("The quick brown fox...");

builder.ListFormat.RemoveNumbers();

builder.Document.Save("Lists.CreateCustomList Out.doc");

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. Formatting of HTML snippet is used when you insert HTML into the document. In your HTML you have

tags that is rendered as regular paragraph. So text between

tags will not be list item.

You can try using the following code (pay attention to the highlighted snippets):

Document doc = new Document();

// Create a list based on one of the Microsoft Word list templates.

List list = doc.Lists.Add(ListTemplate.OutlineHeadingsLegal);

DocumentBuilder builder = new DocumentBuilder(doc);

builder.ListFormat.List = list;

builder.Writeln("The quick brown fox...");

builder.Writeln("The quick brown fox...");

builder.ListFormat.ListIndent();

builder.Writeln("jumped over the lazy dog.");

builder.InsertHtml("Testing instering HTML

  1. jumped over the html dog.
  2. jumped over the html dog 2.
");

builder.ListFormat.List = list;

builder.ListFormat.ListIndent();

builder.Writeln("jumped over the lazy dog.");

builder.ListFormat.ListOutdent();

builder.Writeln("The quick brown fox...");

builder.ListFormat.RemoveNumbers();

builder.Document.Save(@"Test215\out.doc");

Hope this helps.

Best regards.

First of all -- i think you guys have done a very nice job with your Lists API -- you make something very complicated, simple.

Thank you this gets be close, but I am hung up on one thing. Follows is a more real to life example:

I have removed the

tags. In its place are
and that seems to work fine. The issue is getting the the ordered list in the html to lineup with the listLevel.TextPosition in the main list.

I want the "1." of the inner list to align with the text "Testing inserting HTML".

Thank you,

Jonathan

private void btnTest_Click(object sender, EventArgs e)

{

Document doc = new Document();

List list = doc.Lists.Add(ListTemplate.OutlineHeadingsLegal);

ListLevel level1 = list.ListLevels[0];

level1.NumberFormat = "\x0000.0";

level1.NumberPosition = 0;

level1.TabPosition = PointsInInch(0.4d);

level1.TextPosition = PointsInInch(0.4d);

ListLevel level2 = list.ListLevels[1];

level2.NumberStyle = NumberStyle.LeadingZero;

level2.NumberFormat = "\x0000.\x0001";

level2.NumberPosition = PointsInInch(0);

level2.TabPosition = PointsInInch(0.4d);

level2.TextPosition = PointsInInch(0.4d);

ListLevel level3 = list.ListLevels[2];

level3.NumberStyle = NumberStyle.LeadingZero;

level3.NumberFormat = "\x0000.\x0001.\x0002";

level3.NumberPosition = PointsInInch(0.4d);

level3.TabPosition = PointsInInch(1);

level3.TextPosition = PointsInInch(1);

DocumentBuilder builder = new DocumentBuilder(doc);

//builder.ParagraphFormat.SpaceAfterAuto = true;

builder.ListFormat.List = list;

builder.Writeln("The quick brown fox...");

builder.Writeln("The quick brown fox...");

builder.ListFormat.ListIndent();

builder.Writeln("jumped over the lazy dog.");

builder.ListFormat.ListIndent();

builder.InsertHtml("Testing instering HTML adsf adsf adsf adsf adsfklajsdf klajsdf kljasdfkl jakl;sdfj kl;adsjf klajsdf klajsdfkl jasdf klja;sdf aklsd;fj klasdjf afewiajdsvm ioasdfklmnewf oipasdf kamnsdklfaiweojaweiorj aosdf aisdjfoaweriaewjio adsjf aidsjfoaewjioaejw ioadsfj oo

asfasd asdf adsf adsf asdf asdf asdf adsf asdf adsf adsf adsf asdf adsf adsfaadsf adsf ads

  1. Outer HTML List 1.
  2. Outer HTML List 2.
    1. Inner HTML List 1
    2. Inner HTML List 2
");

builder.ListFormat.List = list;

builder.ListFormat.ListIndent();

builder.Writeln("jumped over the lazy dog.");

builder.ListFormat.ListOutdent();

builder.Writeln("The quick brown fox...");

builder.ListFormat.RemoveNumbers();

builder.Document.Save("Lists.CreateCustomList Out.doc");

}

public double PointsInInch(int inches) { return Convert.ToDouble(72 * inches);}

public double PointsInInch(double inches){return 72 * inches;}

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for you inquiry. I think that items of inserted HTML list should be members of main list. You can try to achieve this using the following code.

//Create temporary list

List tempList = null;

foreach (Paragraph par in doc.FirstSection.Body.Paragraphs)

{

if (par.IsListItem)

{

//Set temporary list

if (tempList == null)

tempList = par.ListFormat.List;

else

{

if ((par.PreviousSibling as Paragraph) != null)

{

//If prewiouse Paragraph is member od temporary list

//Then current paragraph should also be member of this list

if ((par.PreviousSibling as Paragraph).ListFormat.List.Equals(tempList))

par.ListFormat.List = tempList;

else

//Else reset temporary list

tempList = null;

}

}

}

else

{

//Else reset temporary list

tempList = null;

}

}

Insert this code directly before saving document.

Hope this helps.

Also you can use standard Aspose.Words feature to convert between measurement units. See the following link for more information.

http://www.aspose.com/documentation/file-format-components/aspose.words-for-.net-and-java/converting-between-measurement-units.html

Best regards.

Thanks Alexey,

I am almost there. I used a slightly modified approach to yours. I wanted the inner lists from the html to be inserted at the next ListLevelNumber. Follows shows my solution.

My current (and hopefully last List question) is in my inserted html after I insert a list, the text afterwards see bolded text("More Text at the end") appears as a new item in the list rather than continuing the text prior to the insertion of the list.

I also notice the same affect when there is a table in the html.

Any ideas on how to acheive? (attached is the desired result)

Thanks for your help!

private void btnTest_Click(object sender, EventArgs e)

{

Document doc = new Document();

List list = doc.Lists.Add(ListTemplate.OutlineHeadingsLegal);

ListLevel level1 = list.ListLevels[0];

level1.NumberFormat = "\x0000.0";

level1.NumberPosition = 0;

level1.TabPosition = ConvertUtil.InchToPoint(0.45d);

level1.TextPosition = ConvertUtil.InchToPoint(0.45d);

ListLevel level2 = list.ListLevels[1];

level2.NumberStyle = NumberStyle.LeadingZero;

level2.NumberFormat = "\x0000.\x0001";

level2.NumberPosition = ConvertUtil.InchToPoint(0d);

level2.TabPosition = ConvertUtil.InchToPoint(0.45d);

level2.TextPosition = ConvertUtil.InchToPoint(0.45d);

ListLevel level3 = list.ListLevels[2];

level3.NumberStyle = NumberStyle.LeadingZero;

level3.NumberFormat = "\x0000.\x0001.\x0002";

level3.NumberPosition = ConvertUtil.InchToPoint(0.45d);

level3.TabPosition = ConvertUtil.InchToPoint(1.05);

level3.TextPosition = ConvertUtil.InchToPoint(1.05);

ListLevel level4 = list.ListLevels[3];

level4.NumberStyle = NumberStyle.LowercaseRoman;

level4.NumberFormat = "(\x0003)";

level4.NumberPosition = ConvertUtil.InchToPoint(1.05d);

level4.TabPosition = ConvertUtil.InchToPoint(1.65);

level4.TextPosition = ConvertUtil.InchToPoint(1.65);

ListLevel level5 = list.ListLevels[4];

level5.NumberStyle = NumberStyle.LowercaseLetter;

level5.NumberFormat = "(\x0004)";

level5.NumberPosition = ConvertUtil.InchToPoint(1.65d);

level5.TabPosition = ConvertUtil.InchToPoint(2.25);

level5.TextPosition = ConvertUtil.InchToPoint(2.25);

for (int i = 5; i < list.ListLevels.Count; i++)

{

ListLevel level = list.ListLevels[i];

level.NumberPosition = ConvertUtil.InchToPoint(1.05d);

level.TabPosition = ConvertUtil.InchToPoint(1.65);

level.TextPosition = ConvertUtil.InchToPoint(1.65);

}

DocumentBuilder builder = new DocumentBuilder(doc);

builder.ListFormat.List = list;

builder.Writeln("The quick brown fox...");

builder.Writeln("The quick brown fox...");

builder.ListFormat.ListIndent();

builder.Writeln("jumped over the lazy dog.");

builder.ListFormat.ListIndent();

Node startingNode = builder.CurrentParagraph as Node;

builder.InsertHtml("Testing instering HTML adsf adsf adsf adsf adsfklajsdf klajsdf kljasdfkl jakl;sdfj kl;adsjf klajsdf klajsdfkl jasdf klja;sdf aklsd;fj klasdjf afewiajdsvm ioasdfklmnewf oipasdf kamnsdklfaiweojaweiorj aosdf aisdjfoaweriaewjio adsjf aidsjfoaewjioaejw ioadsfj oo

asfasd asdf adsf adsf asdf asdf asdf adsf asdf adsf adsf adsf asdf adsf adsfaadsf adsf ads

  1. Outer HTML List 1.
  2. Outer HTML List 2.
    1. Inner HTML List 1
    2. Inner HTML List 2

More Text at the end");

Node endingNode = builder.CurrentParagraph as Node;

Node node = endingNode;

while (node != startingNode)

{

ProcessNode(startingNode, node);

node = node.PreviousSibling;

}

ProcessNode(startingNode, node);

builder.ListFormat.List = list;

builder.ListFormat.ListIndent();

builder.Writeln("jumped over the lazy dog.");

builder.ListFormat.ListOutdent();

builder.Writeln("The quick brown fox...");

builder.ListFormat.RemoveNumbers();

builder.Document.Save("Lists.CreateCustomList Out.doc");

}

private static void ProcessNode(Node startingNode, Node node)

{

if (node is Paragraph)

{

Paragraph paragraph = node as Paragraph;

if (paragraph.IsListItem)

{

if (paragraph.ListFormat.List.ListId

!= (startingNode as Paragraph).ListFormat.List.ListId)

{

paragraph.ListFormat.List = (startingNode as Paragraph).ListFormat.List;

paragraph.ListFormat.ListLevelNumber = paragraph.ListFormat.ListLevelNumber + (startingNode as Paragraph).ListFormat.ListLevelNumber + 1;

}

}

}

}

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. I think that in this case you should use HTML paragraph instead .

More Text at the end

Hope this helps.

Best regards.

Morning Alexey,

That did not work for me. I would like that "More Text at the end" to be a continuation of the list item 2.01.01. The text should be aligned with "Testing instering HTML". Does that make sense?

Thanks for your help.

Jonathan

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. Actually, list item is a paragraph, and text “More Text at the end” also is a paragraph and is not list item. You can try specifying left margin to emulate this. Something like this

More Text at the end

Hope this helps.

Best regards.

Interesting,



So let me see if I understand this. As a rule you can not embed a table or new list into an existing list item. Doing so would breaks the list – so the best that you can do is fake it by changing the margins of the elements that follow.



So to fix this i will need to loop through the nodes of the document, check to see if paragraphs exist after a table or list, and increase there indent by the previous list item indentation. (Sounds hard – i better get to work!)



– jonathan

Hello!<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

A list can be put inside another list since multilevel lists are supported. If you’d like to place here a table or an extra text paragraph or an image (some inline content) you have to set indent. That’s exactly the same what is done by MS Word. Logically this content won’t be a part of your list but it will simulate this. Looks a bit difficult but what else can we invent for this case?

On the other hand you can consider using soft line breaks instead of paragraph breaks inside list items. Line break doesn’t start a new paragraph so the whole text will be one list item.

Regards,