Free Support Forum - aspose.com

How To Generate a Line of Text by Segments of that Line One at a Time?

I can prepare a single line of text for the Aspose Words (for Java) to write to a Document; for example:

private static final String titleText = "[FSCO-MOE3.1] Objective 50XX under the Phase II";

where FSCO-MOE3.1 is an ID.

What I really would like to do is as follows (writing each segment of that line one at a time):

First, write several spaces;

Second, write the ID; for example, 'FSCO-MOE3.1', enclosed in a pair of square bracket;

Third, write a space

Fourth, write in bold the text; for example, 'Objective 50XX under the Phase II'.

How do I do something like what described above? Is it possible to be done? I can use a Java Array to store the information. For example, the first element of the String Array is the ID and the second element of the String Array is the text. How do I modify the code shown below to accomplish writing those segments one at a time?

Thank you for your help.

Paragraph hierarchyLevelParagraph = new Paragraph( wordMeasurementSummaryReport );

wordMeasurementSummaryReportBody.appendChild( hierarchyLevelParagraph );

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

hierarchyLevelParagraph.getParagraphFormat().setAlignment(ParagraphAlignment.LEFT);

Run hierarchyLevelParagraphRun = new Run( wordMeasurementSummaryReport );

hierarchyLevelParagraphRun.setText( titleText );

hierarchyLevelParagraphRun.getFont().setName( "Arial" );

hierarchyLevelParagraphRun.getFont().setSize( 10 );

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

hierarchyLevelParagraph.appendChild( hierarchyLevelParagraphRun );

Hi,

Sorry for delay. To dynamically add a text to the document (as well as any other types of nodes) it is handy to use the DocumentBuilder class. So in your case the code could look as follows (sorry it's in C# but the Java implementation is almost similar):

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

builder.Write(" "); // Several spaces.

builder.Write("[FSCO-MOE3.1]"); // The ID.

builder.Write(" "); // A space.

builder.Font.Bold = true;

builder.Write("Objective 50XX under the Phase II");

I am completely lost.

See, I created a Run first; then, I have the text and its color, etc., ready. Thereafter, I append the Run to the Paragraph.

Now, I do not know how to change my code to DocumentBuilder as you suggested. How do I append the DocumentBuilder (with all its settings) to the Paragraph that I created?

By the way, it looks that the Font is default to Italic. And I would like to have normal (i.e., non-italic). I have:

hierarchyLevelParagraphRun.getFont().setItalic( false );

in my code; but, it does not seem to work.

Ooops, I am able to set the Font to non-italic.

The problem is to use the DocumentBuilder and fit it in my code.

Let's take it slow then.

Basically, DocumentBuilder does the same job that you do when you are adding nodes manually. That is, it provides several formatting objects for every type of node (returned by the Font, ParagraphFormat etc properties) and the formatting you set up on these objects applies to the nodes you insert. Runs that you insert automatically get added to the current paragraph. In the code sample shown above there is only one paragraph in the document because a new document always has an empty paragraph. If you need to insert another paragraph to the document, call the Writeln method. After that, all new runs added by means of the Write method will be added to this new paragraph.

Let's expand the mentioned code snippet and insert three subsequent paragraphs into the document:

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

builder.Write(" "); // Several spaces.

builder.Write("[FSCO-MOE3.1]"); // The ID.

builder.Write(" "); // A space.

builder.Font.Bold = true;

builder.Writeln("Objective 50XX under the Phase II"); // Insert the text and paragraph break.

builder.Write(" "); // Several spaces.

builder.Write("[FSCO-MOE3.1]"); // The ID.

builder.Write(" "); // A space.

builder.Font.Italic = true;

builder.Writeln("Objective 50XX under the Phase II"); // Insert the text and paragraph break.

builder.Write(" "); // Several spaces.

builder.Write("[FSCO-MOE3.1]"); // The ID.

builder.Write(" "); // A space.

builder.Font.Underline = Underline.Double;

builder.Writeln("Objective 50XX under the Phase II"); // Insert the text and paragraph break.

Here's the result:

DocumentBuilder

As you can see, now we have a document containing 3 paragraphs with different run formatting. Note the formatting is not reset to default once a new paragraph is added. You should call Font.ClearFormatting if you need this.

So did you get the idea? DocumentBuilder is a handy alternative to manipulating nodes (basically, to the adding of nodes) because it allows to avoid explicit creation of nodes and setting formatting to each node.

Also read the Inserting Documents Dynamically section of the Aspose.Words Wiki:

http://www.aspose.com/wiki/default.aspx/Aspose.Words/Home_Page.html

Thanks a lot for your continuing support.

I begin to get the idea; but, am still running into problems - how do I move cursor within a "line"?

The example that you gave is really good - I have to write out my data line by line. And I have learned to use 'writeln' to start the next line.

For "each line" that I write, it has:

1. a color-filled circle; (It is OK because you have given an example to draw it.)

2. a space;

3. a color-filled arrow shape; (It is OK because you have given an example to draw it.)

4. a space;

5. an ID enclosed in a pair of square bracket; ( I am able to do it. )

6. a space; (It is done using the DocumentBuilder.write() method.)

7. a text string in bold. ( I am able to do it. )

The problem is that the circle, arrow, and the beginning of the text all start at the very beginning of a line - they overlap onto each other. The builder.write( " " ); does not work in the context.

Besides, I cannot use the getTop() and getLeft() methods of those images to control the position of those images in each line. The reason is that each line is indented and the indentation varies for each line.

It may be "possible" to use the getLeft() method; but I do not see the getTop() method gets me anywhere. I have to write out many lines.

How do I resolve the overlapping problem?

Here is my code:

Document wordMeasurementSummaryReport;

try {

wordMeasurementSummaryReport = new Document();

DocumentBuilder builder = new DocumentBuilder( wordMeasurementSummaryReport );

Shape circleShape = new Shape( wordMeasurementSummaryReport, ShapeType.ELLIPSE ); // a circle

circleShape.setFillColor( Color.RED );

circleShape.setWidth( 15 );

circleShape.setHeight( 15 );

builder.insertNode( circleShape );

builder.write(" "); // A space.

Shape arrowShape = new Shape( wordMeasurementSummaryReport, ShapeType.LEFT_RIGHT_ARROW ); // an arrow

arrowShape.setWidth( 15 );

arrowShape.setHeight( 15 );

arrowShape.setFillColor( Color.BLACK );

builder.insertNode( arrowShape );

builder.write(" "); // A space.

builder.write("[FSCO-MOE3.1]"); // The ID.

builder.write(" "); // A space.

builder.getFont().setBold( true );

builder.write("Objective 50XX under the Phase II"); // The text string.

wordMeasurementSummaryReport.save("C:\\TEMPORARY\\Write_a_Sentence.doc");

} catch (Exception e)

{

// TODO Auto-generated catch block

e.printStackTrace();

}

No problems. To align shapes with text and to avoid overlapping (and also avoid bothering with calculating their coordinates), just make them inline:

shape.WrapType = WrapType.Inline;

That's it Smile [:)]

Thank you very much for your help. Now, I am able to put images and text in the same line.