Free Support Forum - aspose.com

Difficuties in Iterating and Setting Indentation

I am asked to generate MS Words document using Java.

The contents of the Words document have to be presented similar to the style of a tree with nodes. Except that I have to write out the ‘full title’ (i.e. a longer String of text) of all nodes and use indentation instead of dotted lines. For example:

First Level Node 1: Today is Sunday

Second Level Node 1: I am going to take care of household chores.

Third Level Node 1: sweep floors

Third Level Node 2: mow lawn

Third Level Node 3: do laundry

Second Level Node 2: I am going to go grocery shopping

Third Level Node 1: buy meat

Third Level Node 2: buy fruits

Third Level Node 3: buy soft drinks

Third Level Node 4: buy cereal

First Level Node 2: Today is Monday

Second Level Node 1: I am going to drive to work.

Second Level Node 2: I am going to ran errand.

What is the best way to generate a Word document in the style described above? Should the First Level Node be considered as a new Section or a new Paragraph?

I have some very preliminary draft of the Aspose code. It does not iterate as I expected and I am having problem with indenting the second level and the third level titles:

try

{

//Creating document for the report

Document wordDailyTasksReport = new Document();

//Making the current document empty

wordDailyTasksReport.removeAllChildren();

//Creating a new section node

Section wordDailyTasksReportSection = new Section(wordDailyTasksReport);

//Appending section to the document

wordDailyTasksReport.appendChild(wordDailyTasksReportSection);

//Setting properties for the section

wordDailyTasksReportSection.getPageSetup().setSectionStart(SectionStart.NEW_PAGE);

wordDailyTasksReportSection.getPageSetup().setPaperSize(PaperSize.LETTER);

//Creating the wordDailyTasksReportBody section for the document

Body wordDailyTasksReportBody = new Body(wordDailyTasksReport);

wordDailyTasksReportSection.appendChild(wordDailyTasksReportBody);

//For the Day of a Week

if (choices.getTheDay())

{

//Creating wordDailyTasksReportParagraph for the wordDailyTasksReportBody -- Title

Paragraph dayTitle = new Paragraph(wordDailyTasksReport);

wordDailyTasksReportBody.appendChild(dayTitle);

//Formatting the wordDailyTasksReportParagraph

dayTitle.getParagraphFormat().setStyleName("???????");

dayTitle.getParagraphFormat().setAlignment(ParagraphAlignment.??????);

//Create a run of text in the wordDailyTasksReportParagraph.

Run titleRun = new Run(wordDailyTasksReport);

titleRun.setText(ClientLocale.getResourceProperty(ClientLocale.DAILY_TASKS_REPORT_DAYS));

titleRun.getFont().setColor(java.awt.Color.BLACK);

dayTitle.appendChild(titleRun);

Day[] d_list = plan.getDays();

for (int i=0; i<d_list.length; i++)

{

try

{

Paragraph daysParagraph = new Paragraph(wordDailyTasksReport);

wordDailyTasksReportBody.appendChild(daysParagraph);

daysParagraph.getParagraphFormat().setStyleName("??????");

daysParagraph.getParagraphFormat().setAlignment(ParagraphAlignment.??????);

Run paragraphRun = new Run(wordDailyTasksReport);

paragraphRun.setText(d_listIdea [I].getName());

paragraphRun.getFont().setColor(java.awt.Color.BLACK);

daysParagraph.appendChild(paragraphRun);

}

catch(Exception e)

{

logger.error("",e);

}

}

}

Thanks for helping me.

To build hierarchical documents you can basically use two techniques: setting heading level of the paragraph and using lists. These techniques can also be used in combination. For example, you can set heading level 1 for top level nodes, and set list levels to distinguish hierarchy for other nodes.

Here is the codeшт c# illustrating the point:

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

// Set heading level of this paragraph to 1.

builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;

builder.Writeln("First level node");

// Set heading level of this paragraph to 2.

builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading2;

// Set left indent of this paragraph to 10 points.

builder.ParagraphFormat.LeftIndent = 10;

builder.Writeln("Second level node");

// Clear paragraph formatting.

builder.ParagraphFormat.ClearFormatting();

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

// apply it to the current paragraph in the document builder.

builder.ListFormat.List = doc.Lists.Add(ListTemplate.NumberArabicDot);

// There are 9 levels in this list, lets try them all.

for (int i = 0; i < 9; i++)

{

builder.ListFormat.ListLevelNumber = i;

builder.Writeln("Level " + i.ToString());

}

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

// and apply it to the current paragraph in the document builder.

builder.ListFormat.List = doc.Lists.Add(ListTemplate.BulletDiamonds);

// There are 9 levels in this list, lets try them all.

for (int i = 0; i < 9; i++)

{

builder.ListFormat.ListLevelNumber = i;

builder.Writeln("Level " + i.ToString());

}

// This is a way to stop list formatting.

builder.ListFormat.List = null;

If you have trouble with converting it to Java please inform me. I will ask our Java developer to assist you in porting it to Java.

Hope this helps,