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_list.getName());
paragraphRun.getFont().setColor(java.awt.Color.BLACK);
daysParagraph.appendChild(paragraphRun);
}
catch(Exception e)
{
logger.error("",e);
}
}
}
Thanks for helping me.