Free Support Forum -

Cannot Figure Out Using Aspose Words (Java) to Write Out Hierarchical Structured Data

I am able to use the Aspose Words for Java to write out some text and save the document in a local directory (code is shown below). So far so good.

I am also able to structure my data in hierarchical levels and write out each level line by line in a hierarchical manner. The stand alone Java class code is also shown below. Aspose forum advisors can run this stand alone Java class to see exactly what I am trying to do.

But, I am having problem to figure out how to fit those two piece together. After I append a Section to a Document, append a Body to a Section, append a Paragraph to a Body, I have to create a Run. And the crucial part is to put the text at each level of the hierarchy as a parameter of the setText() method of the Run. For example:

hierarchyLevelParagraphRun.setText( indentString( indent ) + topic );

And I have to do it recursively for the PRE ORDER printing- and it is the exact place I cannot figure out "how to".

Let me post my code. The first is the Standalone Java class:

import java.util.List;

import java.util.ArrayList;

public class Topic {

String topic;

Topic t;

int indent = 0;

List subTopics = new ArrayList(); // holds all sub topics

public Topic( String foo){topic = foo;}

public void addSubTopic(Topic t){subTopics.add(t);}

public void showAll(){

System.out.println(topic); // show the parent first

for(int i = 0; i<subTopics.size(); i++){ // then show all children

((Topic) subTopics.get(i)).showAll();



// just to show you how to interleave other information along with the

// hierarchal info here is a version that does indenting.

public void showAllIndented(int level){

indent = level;

// same as above but with outlines indented to show level

System.out.println(indentString(indent) + topic);

for(int i = 0; i<subTopics.size(); i++){ // then show all children

((Topic) subTopics.get(i)).showAllIndented(level+1);



private static String indentString(int count){

String res = "";

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


res = res.concat(" ");


return res;


// finally some test code

public static void main(String[] args){

Topic root = new Topic("Animals");

Topic dog = new Topic("Dog");

Topic cat = new Topic("Cat");

Topic barks = new Topic("Barks");

Topic hacks = new Topic("Hacks up furball");








The second one is the Java class using the Aspose Words to create and save a document:

package docs;

import java.awt.Color;

import java.util.Date;

import com.aspose.words.*;

public class WriteSomething


private static final String objectiveTitle = "Let's Test - OPLAN 50XX Phase II";

public static void main( String []args )


Document wordMeasurementSummaryReport;

try {

wordMeasurementSummaryReport = new Document();


// check the package to import and the Java Date

Date now = new Date();

wordMeasurementSummaryReport.getCustomDocumentProperties().add( "Creation Date", now );

//Creating a new section node

Section wordMeasurementSummaryReportSection = new Section( wordMeasurementSummaryReport );

//Appending section to the document

wordMeasurementSummaryReport.appendChild( wordMeasurementSummaryReportSection );

//Setting properties for the section



//Creating the wordMeasurementSummaryReportBody section for the document

Body wordMeasurementSummaryReportBody = new Body( wordMeasurementSummaryReport );

wordMeasurementSummaryReportSection.appendChild( wordMeasurementSummaryReportBody );

//Creating wordMeasurementSummaryReportParagraph for the wordMeasurementSummaryReportBody -- Title

Paragraph measurementSummaryTitle = new Paragraph( wordMeasurementSummaryReport );

wordMeasurementSummaryReportBody.appendChild( measurementSummaryTitle );

//Formatting the wordMeasurementSummaryReportParagraph

measurementSummaryTitle.getParagraphFormat().setStyleName( "Heading 1" );

measurementSummaryTitle.getParagraphFormat().setAlignment( ParagraphAlignment.CENTER );

//Create a run of text in the wordMeasurementSummaryReportParagraph.

Run titleRun = new Run( wordMeasurementSummaryReport );

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

measurementSummaryTitle.appendChild( titleRun );

Paragraph hierarchyLevelParagraph = new Paragraph( wordMeasurementSummaryReport );

wordMeasurementSummaryReportBody.appendChild( hierarchyLevelParagraph );

hierarchyLevelParagraph.getParagraphFormat().setStyleName("Heading 2");


// write out some text

Run hierarchyLevelParagraphRun = new Run( wordMeasurementSummaryReport );

hierarchyLevelParagraphRun.setText( objectiveTitle );

hierarchyLevelParagraphRun.getFont().setColor( Color.BLACK );

hierarchyLevelParagraph.appendChild( hierarchyLevelParagraphRun );"C:\\TEMPORARY\\Write_a_Sentence.doc");

} catch (Exception e)







Thank you for that comprehensive posting but... it seems to be a little out of my responsibilities Smile [:)] Please understand me correctly, I can tell you how to work with Aspose.Words API, show sample code or help to implement some relatively simple tasks that deal directly with the API. But your task is too exhaustive to fall into this category. Do you want me to show how to indent text in paragraphs or build a document? I will surely do if you need.

Or, if you are totally lost, please elaborate your request. To be honest I didn't manage to fully understand what the problem is. You can manually create a document in Microsoft Word to demonstrate what you need to achieve and attach it to your post. Anyway, just try to make it easier to understand.

Thank you Smile [:)]

Thanks for your reply. I have figured out a way to get around the problem - I have to prepare my data well for the Aspose to use.

I think that the support for using the Aspose has been very good.