dcsimg
Login | Register   
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Tip of the Day
Language: Java
Expertise: Advanced
Apr 17, 2006

WEBINAR:

On-Demand

Application Security Testing: An Integral Part of DevOps


Generate RTF Word Documents Using a Java Application

Very few alternatives are available to someone wanting to generate RTF documents using Java. The best and easiest way is to use the iText third-party freeware library. Just plug in the .jar file and implement it into your program. The following example illustrates how to generate a simple RTF document:
  1. Download itext-xversion.jar here.
  2. Set the .jar file into the classpath:
    
    set classpath = %CLASSPATH%;c:\lib\itext-xversion.jar
    
  3. Save the following file into your system as HelloWorld.java:
    
    import java.io.*;
    
    import com.lowagie.text.*;
    import com.lowagie.text.rtf.*;
    
    public class HelloWorld {
        public static void main(String[] args) {
            
            System.out.println("This example generate a RTF file name Sample.rtf");
            
            // Create Document object
            Document myDoc = new Document();
            
            try {
                
                // Create writer to listen document object
                // and directs RTF Stream to the file Sample.rtf
                RtfWriter2.getInstance(document, new FileOutputStream("Sample.rtf"));
    
                // open the document object
                document.open();
                
                // Create a paragraph 
    	    Paragraph p = new Paragraph();
    	    p.add("Helloworld in Rtf file..amazing isn't");
    		
    	    // Add the paragraph to document object
                document.add(p);
            }
            catch(Exception e) {
                System.out.println(e);
            }
            //close the document
            document.close();
        }
    } 
    
  4. Compile the classfile using javac:
    
    c:\generate>javac Helloworld.java
    
    This generates a class file named Helloworld.class in the same directory.
  5. Execute the class using Java:
    
    c:\generate> java Helloworld
    
Once it's executed, you can see that a new file named Sample.rtf was generated in the same directory. Open it and see this text:

 'Helloworld in Rtf file..amazing isn't'

Elayaraja David
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date