|
Creating PDF, RTF or HTML document from a Java class at runtime |
|
|
iText library allows you to create PDF, RTF or HTML documents from Java
classes at runtime. Manipulation of these types of documents are also possible with this library.
The example below shows you how to create PDF/RTF and HTML documents with iText (Just uncomment the appropriate line for the format you want.):
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, 50, 50, 50, 50);
try
{
PdfWriter pdf = PdfWriter.getInstance(document ,
new FileOutputStream("text.pdf"));
//HtmlWriter pdf = HtmlWriter.getInstance(document,
// new FileOutputStream("text.html"));
//RtfWriter2 pdf = RtfWriter2.getInstance(document,
// new FileOutputStream("text.rtf"));
document.addSubject("This is the result of a Test.");
HeaderFooter header = new HeaderFooter(
new Phrase("This is a header."), false);
HeaderFooter footer = new HeaderFooter(
new Phrase("This is page "), new Phrase("."));
footer.setAlignment(Element.ALIGN_CENTER);
document.setHeader(header);
document.setFooter(footer);
document.open();
document.add(new Paragraph("This is test message"));
}
catch(DocumentException de)
{
System.err.println(de.getMessage());
}
catch(Exception de)
{
System.err.println(de.getMessage());
}
document.close();
}
}
|
Related Tips
|
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.