#This page shows how to use the elements of the API.
Introduction
This document will describe all elements of the Java2Word API and show how to use them. For each element, there are two sections "Description" and "Style - Superstylin".
"Description" is a short description of the element and has some sample code to illustrate the usage of the element.
The other section "Style - Superstylin" presents all possible styles applied to the elements with some examples as well.
We will start from the most popular/simple to the most complex.
The term "SuperStylin " means simply "Style". SuperStylin is the name I gave to the new Styling Framework. It is inspired on Groove Armada - Superstylin'
here is the link: http://www.youtube.com/watch?v=_z04eDMWOTw&feature=related
SuperStylin framework is still in development...
Remember that W2000 has been removed from the API. We will use only W2004 elements.
Document
Description
This is the main class of the API. Use this class to add other elements to the document. When you create an instance of Document.
IDocument myDoc = new Document2004 (); myDoc . getBody (). addEle ( new Paragraph ( "Hi! I am a paragraph" ));
The example above shows how to add one Paragraph to the body.
Body contains another two classes: Header, Footer.
Superstylin
Not implemented yet.
Heading
Description
Heading is the element utilized for creating hierarchy sections of the document. It is used to generate the document index. There are three levels of Headings: Heading1, Heading2 and Heading3.
See how to create a Heading1:
Heading1 h1 = new Heading1 ( "Heading 111" );
How to add it to the body of the Document:
myDoc . getBody (). addEle ( h1 );
Superstylin
- Align (options: CENTER, LEFT(default), RIGHT, JUSTIFIED.
Element that will receive the style:
Heading1 h1 = new Heading1 ( "Heading 111" );
Create the new heading style object and configure it.
HeadingStyle headingStyle = new HeadingStyle ();
headingStyle . setAlign ( HeadingStyle . Align . CENTER );
Apply the style to the element:
h1 . setStyle ( headingStyle );
- Bold (boolean)
headingStyle . setItalic ( true );
- Italic (boolean)
headingStyle . setBold ( true );
Paragraph
Description
The simplest way to create a paragraph is passing the content on the constructor:
Paragraph p01 = new Paragraph ( "I am a paragraph" );
Superstylin
If you want to superStylin your paragraph (or part of the paragraph), you have to use the new element called ParagraphPiece .
- bold (boolean)
piece01 . setStyle ( style );
- italic (boolean)
piece01 . setStyle ( style );
- underline (boolean)
piece01 . setUnderline ( style );
ParagraphPiece should be utilized only inside a Paragraph.
Example of ParagraphPiece use:
ParagraphPieceStyle style = new ParagraphPieceStyle (); //create style object
style . setBold ( true ); //set it up
style . setBold ( true );
//create your Piece of Paragraph
ParagraphPiece piece01 = new ParagraphPiece ( "I love Java and Ruby" );
//apply the style to the Piece piece01 . setStyle ( style ); Paragraph p01 = new Paragraph ( piece02 );
Lets imagine the situation where you want to have part of the text in bold like this: I love Java and Ruby You want to make the word "ruby" in bold.
Class Paragraph has another constructor that accepts an array or varargs of Piece objects.
ParagrapPiecehStyle style = new ParagraphPieceStyle (); style . setBold ( true ); ParagraphPiece piece01 = new ParagraphPiece ( "I love Java and " ); ParagraphPiece piece02 = new ParagraphPiece ( "Ruby" ); piece02 . setStyle ( style ); Paragraph p01 = new Paragraph ( piece01 , piece02 );
Notice that paragraph has become just a wrapper to put together all little pieces of text. When you use the default constructor, the code creates one ParagraphPiece behind the scenes.
IMPORTANT: If you want to apply superStylin to the WHOLE paragraph, at the moment, you can only apply ALIGN. You have to use the class ParagraphStyle .
myPar . getStyle (). setAlign ( ParagraphStyle . Align . CENTER );