How to Create PDF Files in Java

 Create a small Java program that shows the ability of some iText library. You will of learn how to create a document, add a pdf writer (listener), Some add content to the created document, and last, but not least, close the document. You will learn how to create documents, add the writer pdf (listener), add some content to the document created, and last but not least, close the document.
This is just a quick intro to the creation of PDF files with Java, if you're serious about automatic PDF creation using Java, then read Bruno Lowagie's tutorial (link at the end). These are just quick intro to the creation of a PDF file with Java, if you are serious about automatic PDF creation using Java, then read the tutorial Bruno Lowagie's (link at end).
Difficulty: Difficulty:
Challenging Quite Challenging moderately
Instructions Instructions
Things you'll need: the things you need:
• Java Runtime Environment Java Runtime Environment
• Eclipse Eclipse
• library iText iText library 1. Make sure you have Java Runtime Environment, Eclipse and iText installed. Make sure you have Java Runtime Environment, Eclipse and iText installed.

2. In Eclipse create a new project, and name it what ever you like at Might That particular moment. In Eclipse create a new project, and name it what ever you might like at the time. Then right click on the project name> Properties> Java Build Path> Add External Jars> Find your iText-2.1.5.jar> Open Press> Press Ok. Then right click on the name of the project> Properties> Java Build Path> Add External jars> Find your 2.1.5.jar iText-> Click open> Press Ok.

3. Create a document object: Create a document object:
Document document = new Document (); Document document = new Document ();

4. Create a writer That listens to the document and directs a PDF stream to a file: Create a writer who listens and directs the flow of documents into PDF files:
FileOutputStream FOS = new FileOutputStream ("c: / / ElloUniverse.pdf");
FileOutputStream FOS = new FileOutputStream ("c: / / ElloUniverse.pdf");
PdfWriter.getInstance (document, FOS); PdfWriter.getInstance (documents, FOS);
5. Open the pdf document using this java code: Open the pdf document using java code: document.open (); document.open ();
6. Add Some text (ie two paragraphs) to your PDF document: Add some text (ie, two paragraphs) into your PDF document: document.add (new Paragraph ("ello !!!")); document.add Universe (new Paragraph (" Ello Universe !!!")); Color = new CMYKColor CMYKColor (0, 1, (float) 0.2, (float) 0667); CMYKColor new CMYKColor color = (0, 1, (float) 0.2, (float) 0.667);
String text = "kievan wrote more: different color and font type."; String text = "Kiev wrote more: different colors and type fonts.";
Fonts o = FontFactory.getFont (FontFactory.TIMES_BOLD, 14, Font.BOLD, color); Fonts o = FontFactory.getFont (FontFactory.TIMES_BOLD, 14, Font.BOLD, color);
Paragraph p = new Paragraph (text, o); Paragraph p = new Paragraph (text, o); document.add (p); document.add (p);
7. Close the PDF document: Close the PDF document: document.close (); document.close ();
8. / / This is the complete / / This is the complete / / Java source code That / / Java source code
/ / Generates a PDF document. / / Generate PDF document. / / ElloUniverse.java / / / / ElloUniverse.java / / /////////////////////// /////////////////////// import java.io.FileOutputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.IOException; import com.lowagie.text.pdf.CMYKColor; com.lowagie.text.pdf.CMYKColor imports;
import com.lowagie.text.pdf.PdfWriter; com.lowagie.text.pdf.PdfWriter imports; com.lowagie.text import .*; import com.lowagie.text .*; public class public class ElloUniverse ElloUniverse
{{ public static void main (String [] args) public static void main (String [] args)
{{ System.out.println ("ello Universe !!!"); System.out.println (" Ello Universe !!!"); / / Create a document object / / Create the document object
Document document = new Document (); Document document = new Document (); try try {{ / / Create a writer That listens to the document / / Create a writer who listens to the document / / And directs a PDF-stream to a file / / and directs a PDF-stream to a file FileOutputStream FOS = new FileOutputStream ("c: \ \ ElloUniverse.pdf"); FileOutputStream FOS = new FileOutputStream ("c: \ \ ElloUniverse.pdf"); PdfWriter.getInstance (document, FOS); PdfWriter.getInstance (documents, FOS); / / Open the document / / Open the document document.open (); document.open (); / / Add a couple of paragraphs to the document / / Add a few paragraphs to the document document.add (new Paragraph ("ello !!!")); document.add Universe (new Paragraph (" Ello Universe !!!")); Color = new CMYKColor CMYKColor (0, 1, (float) 0.2, (float) 0667); CMYKColor new CMYKColor color = (0, 1, (float) 0.2, (float) 0.667);
String text = "kievan wrote more: different color and font type."; String text = "Kiev wrote more: different colors and type fonts.";
Fonts o = FontFactory.getFont (FontFactory.TIMES_BOLD, 14, Font.BOLD, color); Fonts o = FontFactory.getFont (FontFactory.TIMES_BOLD, 14, Font.BOLD, color); Paragraph p = new Paragraph (text, o); Paragraph p = new Paragraph (text, o);
document.add (p); document.add (p); }} catch (DocumentException de) catch (DocumentException de) {{ System.err.println (de.getMessage ()); System.err.println (de.getMessage ());
}} catch (IOException IOE) catch (IOException IOE)
{{ System.err.println (ioe.getMessage ()); System.err.println (ioe.getMessage ()); }} / / Close the document / / Close the document document.close (); document.close (); }} }}

source : http://www.ehow.com

0 komentar:

Posting Komentar