java left logo
java middle logo
java right logo
 

Home arrow Other API Tips arrow iText arrow Adding Javascript to HTML documents with Java
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4094
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
Adding Javascript to HTML documents with Java E-mail
User Rating: / 4
PoorBest 

iText library allows you to create PDF, RTF or HTML documents from Java classes at runtime. com.lowagie.text package gives facility to manipulate HTML documents.

The example below shows you how to add javascript code to an HTML file. On loading of the page, an alert will be shown. Clicking to the link will also call show() function.

import com.lowagie.text.*;
import java.io.*;
import com.lowagie.text.pdf.*;
import com.lowagie.text.html.*;
import com.lowagie.text.rtf.*;

class  createDocument
{
  public static void main(String args[])
  {  
    Document document = new Document(PageSize.A4, 50505050);
    try 
    {
      HtmlWriter pdf = HtmlWriter.getInstance(document, 
        new FileOutputStream("text.html"));

      document.addTitle("Hello World example");

      StringBuffer s = new StringBuffer();
      s.append("\t\tfunction load() {\n");
      s.append("\t\t  alert('Page has been loaded.');\n");
      s.append("\t\t}\n");
      s.append("\t\tfunction show(){\n");
      s.append("\t\t  alert('This is javascript');\n");
      s.append("\t\t}");

      document.add(new Header(HtmlTags.JAVASCRIPT, s.toString()));
      document.setJavaScript_onLoad("load()");
      document.open();
      document.add(new Paragraph("Hello World"));

      Anchor a  = new Anchor("Click");
      a.setReference("javascript:show()");
      document.add(a);

    
    
    catch(Exception de
    {
      System.err.println(de.getMessage())
    
    document.close()
  }
}


 Related Tips

 
Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.