We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Using List Styles

Hi

I have a question pertaining to the styling of lists. I used the following code (Sample A)to create a list in a generated document. The output looked exactally the way I wanted to. The issue was when a end user then copy and pasted the output to another word document (a common occurance), it lost its formatting. From doing some research it seems that the soruce of the problem is that the destination document had conflicting styles.

So I thought the way to fix it was to create a List Style, but I have been having issues trying to use List Styles to look like the original. Sample B shows how I am now starting the list.

Question 1) Do you think I am going about solving the cut and paste issue the right way, with trying to assign a Style to the list

Question 2) If so how can I use List Styles to make it look like it was in my original Template based approach?

Thanks for your help:

Sample A

public static List CreateCompensationStructureList(Document document)

{

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

ListLevel level1 = list.ListLevels[0];

level1.NumberFormat = "\x0000.0";

level1.NumberPosition = 0;

level1.TabPosition = AsposeUtils.PointsInInch(0.45d);

level1.TextPosition = AsposeUtils.PointsInInch(0.45d);

ListLevel level2 = list.ListLevels[1];

level2.NumberStyle = NumberStyle.LeadingZero;

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

level2.NumberPosition = AsposeUtils.PointsInInch(0);

level2.TabPosition = AsposeUtils.PointsInInch(0.45d);

level2.TextPosition = AsposeUtils.PointsInInch(0.45d);

ListLevel level3 = list.ListLevels[2];

level3.NumberStyle = NumberStyle.LeadingZero;

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

level3.NumberPosition = AsposeUtils.PointsInInch(0.45d);

level3.TabPosition = AsposeUtils.PointsInInch(1.05);

level3.TextPosition = AsposeUtils.PointsInInch(1.05);

ListLevel level4 = list.ListLevels[3];

level4.NumberStyle = NumberStyle.LowercaseRoman;

level4.NumberFormat = "(\x0003)";

level4.NumberPosition = AsposeUtils.PointsInInch(1.05d);

level4.TabPosition = AsposeUtils.PointsInInch(1.65);

level4.TextPosition = AsposeUtils.PointsInInch(1.65);

ListLevel level5 = list.ListLevels[4];

level5.NumberStyle = NumberStyle.LowercaseLetter;

level5.NumberFormat = "(\x0004)";

level5.NumberPosition = AsposeUtils.PointsInInch(1.65d);

level5.TabPosition = AsposeUtils.PointsInInch(2.25);

level5.TextPosition = AsposeUtils.PointsInInch(2.25);

ListLevel level6 = list.ListLevels[5];

level6.NumberStyle = NumberStyle.LowercaseRoman;

level6.NumberFormat = "(\x0004.\x0005)";

level6.NumberPosition = AsposeUtils.PointsInInch(2.25d);

level6.TabPosition = AsposeUtils.PointsInInch(2.85);

level6.TextPosition = AsposeUtils.PointsInInch(2.85);

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

{

ListLevel level = list.ListLevels[i];

level.NumberPosition = AsposeUtils.PointsInInch(1.05d);

level.TabPosition = AsposeUtils.PointsInInch(1.65);

level.TextPosition = AsposeUtils.PointsInInch(1.65);

}

return list;

}

Sample B

#region CreateCompensationStructureList

private static Style GetCompensationStructureListStyle(Document doc)

{

string styleName = "CompensationStructureList";

bool styleExists = false;

foreach (Style style in doc.Styles)

{

if (style.Type == StyleType.List && style.Name == styleName)

{

styleExists = true;

break;

}

}

if (styleExists)

{

return doc.Styles[styleName];

}

else

{

Style listStyle = doc.Styles.Add(StyleType.List, styleName);

List list = listStyle.List;

// Check some basic rules about the list that defines a list style.

Debug.Assert(list.IsListStyleDefinition);

Debug.Assert(!list.IsListStyleReference);

Debug.Assert(list.IsMultiLevel);

Debug.Assert(listStyle == list.Style);

ListLevel level1 = list.ListLevels[0];

level1.NumberFormat = "\x0000.0";

level1.NumberPosition = 0;

level1.TabPosition = AsposeUtils.PointsInInch(0.45d);

level1.TextPosition = AsposeUtils.PointsInInch(0.45d);

ListLevel level2 = list.ListLevels[1];

level2.NumberStyle = NumberStyle.LeadingZero;

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

level2.NumberPosition = AsposeUtils.PointsInInch(0);

level2.TabPosition = AsposeUtils.PointsInInch(0.45d);

level2.TextPosition = AsposeUtils.PointsInInch(0.45d);

ListLevel level3 = list.ListLevels[2];

level3.NumberStyle = NumberStyle.LeadingZero;

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

level3.NumberPosition = AsposeUtils.PointsInInch(0.45d);

level3.TabPosition = AsposeUtils.PointsInInch(1.05);

level3.TextPosition = AsposeUtils.PointsInInch(1.05);

ListLevel level4 = list.ListLevels[3];

level4.NumberStyle = NumberStyle.LowercaseRoman;

level4.NumberFormat = "(\x0003)";

level4.NumberPosition = AsposeUtils.PointsInInch(1.05d);

level4.TabPosition = AsposeUtils.PointsInInch(1.65);

level4.TextPosition = AsposeUtils.PointsInInch(1.65);

ListLevel level5 = list.ListLevels[4];

level5.NumberStyle = NumberStyle.LowercaseLetter;

level5.NumberFormat = "(\x0004)";

level5.NumberPosition = AsposeUtils.PointsInInch(1.65d);

level5.TabPosition = AsposeUtils.PointsInInch(2.25);

level5.TextPosition = AsposeUtils.PointsInInch(2.25);

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

{

ListLevel level = list.ListLevels[i];

level.NumberPosition = AsposeUtils.PointsInInch(1.05d);

level.TabPosition = AsposeUtils.PointsInInch(1.65);

level.TextPosition = AsposeUtils.PointsInInch(1.65);

}

return listStyle;

}

}

public static List CreateCompensationStructureList(Document document)

{

Style listStyle = GetCompensationStructureListStyle(document);

List list = document.Lists.Add(listStyle);

return list;

}

#endregion // CreateCompensationStructureList

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

Thanks for your inquiry. You are on the correct way. List create using styles should be copied correctly. But it seems that there is some kind of issue with list formatting. I created new issue #5613 in our defect database. We will investigate this issue and provide you more information.

Best regards.

Thanks for logging the issue. Can you give me a time frame on diagnosis? Days, Weeks, or Months? Just will help communicate a timeframe to my clients.



Thanks!

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

Thanks for your inquiry. Currently I can’t provide you estimate. Please expect a reply before next hotfix (within 3-4 weeks).

Best regards.

It has been almost a month. Any update?

Thanks!

Jonathan

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

Thanks for your inquiry. Unfortunately this issue is still unresolved. I will consult with our developers and provide you more information.

Best regards.

Hello,

May I suggest that instead of creating a new List Style, you import one from another Word template. This way, you wouldn’t have to programmaticaly create it. In order to do so, please check this thread :
Copy Style from One Document to Another

Hope this helps.

Dav

Thanks for your reply.



I just tried your approach following the linked Thread. But I could not figure out how to modify the example to get it to work for copying a List Style. (In fact the example explictly states that it does not work for lists, but that was as of 2005 and I am pretty sure that you support it now.) Could you please give me some psydo-code to accomplish copying a List Style into a new document.



I created a word doc called “List Template” that has a list with the assigned list style of “My List Style” and I want to insert that style into another document.



Thanks for your help,



Jonathan

Hi

Thanks for your inquiry. Unfortunately there is no built-in functionality to copy styles between documents (this is issue #3286 in our defect database). So you can try using workaround suggested by miklovan.

Best regards.

I am happy to use a work around, but the example laid out by miklovan I was unable to follow for List Styles. Can you show me how to modify it for it to work for list styles? Sorry for the confusion.



Thanks for your help.



Best,

Jonathan

Hi

Thanks for your inquiry. You can use the following simple code:

Document src = new Document(@"Test092\src.doc");

Document dst = new Document(@"Test092\dst.doc");

Node dstNode = dst.ImportNode(src.FirstSection, true, ImportFormatMode.KeepSourceFormatting);

dst.AppendChild(dstNode);

dst.ChildNodes.Remove(dstNode);

dst.Save(@"Test092\out.doc");

But your source document should contain list which style should be imported.. Please see the attached document.

Best regards.