Tutorial: iText by Example

Tab support

Tab support:
To support creating more complex RTF documents with tabular data, the RtfTab provides support for defining tab stops.

To do this, simply add one or more RtfTab objects to the Paragraph. Then in the Chunks use the "\t" tab character to specify where to place the tab stops.
// Define the Paragraph to add tab stops to
Paragraph par = new Paragraph();
            
// Add the tab stops to the paragraph
par.add(new RtfTab(70, RtfTab.TAB_LEFT_ALIGN));
par.add(new RtfTab(400, RtfTab.TAB_RIGHT_ALIGN));
            
// Add the text to the paragraph, placing the tab stops with \t
par.add("\tFirst the text on the left-hand side\tThis text is right aligned.");
    	  
The RtfTab takes two parameters:
  • position: The horizontal position at which to place the tab stop.
  • type: The type of tab stop to place.
    • TAB_LEFT_ALIGN: The text is left aligned on the tab stop position.
    • TAB_RIGHT_ALIGN: The text is right aligned on the tab stop position.
    • TAB_CENTER_ALIGN: The text is centre aligned on the tab stop position.
    • TAB_DECIMAL_ALIGN: The text is aligned on the decimal character. This might be different characters, depending on the locale setting.


An important aspect to keep in mind is that if you use more then one tab stop per line, it is up to you to make sure that the text from the first tab stop does not overflow into the second tab stop. iText cannot check for this condition.
Also add the RtfTabs before you add the text, as otherwise the results might be undefined.
Example: java com.lowagie.examples.rtf.features.tabs.BasicTabs
Generates a document demonstrating the use of tab stops: see BasicTabs.rtf
Extra jars needed in your CLASSPATH: itext-rtf.jar
Go to top of the page
Tab groups:
When repeatedly using the same grouping of RtfTabs in multiple Paragraphs, creating and adding them to each Paragraph is a lot of work. To simplify this, the RtfTabGroup class is provided. It groups a set of RtfTab objects, that can then easily be added to multiple Paragraphs.

Its use is very simple. Create a new RtfTabGroup object and then add the RtfTab to it using the RtfTabGroup.add(RtfTab) method.
// Construct the RtfTabGroup object
RtfTabGroup tabGroup = new RtfTabGroup();
// Add RtfTab tab stops at the desired positions
tabGroup.add(new RtfTab(400, RtfTab.TAB_RIGHT_ALIGN));
tabGroup.add(new RtfTab(500, RtfTab.TAB_DECIMAL_ALIGN));
            
// Create a Paragraph object
Paragraph par = new Paragraph();
// Add the tab group to the paragraph
par.add(tabGroup);
// Specify the tab positions using "\t"
par.add("Description\tDate\tAmount");
document.add(par);
    	  
Example: java com.lowagie.examples.rtf.features.tabs.TabGroups
Generates a document using the tab group feature: see TabGroups.rtf
Extra jars needed in your CLASSPATH: itext-rtf.jar
Go to top of the page

iText, the #1 Java-PDF library




Amazon books:
amazon.co.uk-link