IITDU Forum
Would you like to react to this message? Create an account in a few clicks or log in to continue.

PDF Generation in Java

4 posters

Go down

PDF Generation in Java Empty PDF Generation in Java

Post by BIT0112-Rokon Wed May 26, 2010 10:08 am

PDF version of this tutorial is available HERE

Do you want to print something as a pdf in your java application? if, this text is for you. Its prety simple.
First you need download a jar. download it from here.
its 4.09MB in size.
now you need to configure it in your eclipse IDE. I use eclise. so no idea about others how to
configure. I hope you the other IDE users, know the best.

follow the steps-
1. Create a new java project in Eclipse named "Pdf Converter"
2. Right click on the Pdf Converter project in the package explorer view and select build path then
select Add libraries. there will pop up a box name add libraries. Select User library, press on the button at right top
side named "user library"
3. Click on New. Give a name of iText library and then Click ok.
4. now click on Add Jars button and browse the iText.jar in the directory.
5. Click OK.

Now you can use the classes of iText. The com.lowagie.text.Document is the main class for PDF document generation. First we need to instantiate it. Then we will write on it using the com.lowagie.text.pdf.PdfWriter class.

now lets see a example that print hello world as a pdf.

Code:

package iTextImpl;

import java.awt.Color;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;

public class HelloWorld {
   private static String text = "This is a simple tutorial on how to print your text"
         + "as a pdf.\n To print as pdf format you need to configure iText library.\n"
         + "This is a open source library.\n\n\n\n\n";

   public static void main(String[] args) {
      // document object created that describe the document attributes
      // like page size, margin etc
      Document document = new Document(PageSize.A4, 50, 50, 50, 50);
      try {
         // PdfWriter.getInstance() creates a PDF document writer that writes
         // PDF syntax to concerned file by a FileOutputStream.
         PdfWriter.getInstance(document, new FileOutputStream("hello.pdf"));
         document.open();

         document.addTitle("simple test pdf");

         document.add(new Paragraph(text, FontFactory.getFont(
               FontFactory.HELVETICA_OBLIQUE, 14, Font.NORMAL, new Color(
                     0, 150, 200))));

         // create a chunk object using chunk class of itext library.
         Chunk chunk = new Chunk("This is a simple pdf file created by: ");

         // set the distance between text and line.
         chunk.setTextRise(10.0f);
         chunk.setUnderline(new Color(10, 160, 10), 0, (float) 0.2, 3, 0,
               PdfContentByte.LINE_CAP_PROJECTING_SQUARE);

         document.add(chunk);

         document.add(new Paragraph("Bazlur Rahman", FontFactory.getFont(
               FontFactory.COURIER, 16, Font.BOLD,
               new Color(100, 150, 100))));

         document.close();

      } catch (FileNotFoundException e) {
         e.printStackTrace();
      } catch (DocumentException e) {
         e.printStackTrace();
      }

   }
}


Last edited by bit0112-rokon on Wed Aug 04, 2010 6:19 pm; edited 1 time in total
BIT0112-Rokon
BIT0112-Rokon
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : O+
Posts : 673
Points : 1269

http://blog.codexplo.org

Back to top Go down

PDF Generation in Java Empty Re: PDF Generation in Java

Post by BIT0122-Amit Thu May 27, 2010 1:54 am

good work Smile
rep++
BIT0122-Amit
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

https://iitdu.forumotion.com

Back to top Go down

PDF Generation in Java Empty Re: PDF Generation in Java

Post by BIT0107-Toma Thu May 27, 2010 4:40 am

PDF Generation in Java 567595 PDF Generation in Java 567595 PDF Generation in Java 567595
I was in need of this...
tui ei post ta na dile toke call kortam eita janar jonno. PDF Generation in Java Icon_cheers call korte hoilo na..
BIT0107-Toma
BIT0107-Toma
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : B+
Posts : 280
Points : 453

Back to top Go down

PDF Generation in Java Empty Re: PDF Generation in Java

Post by BIT0112-Rokon Thu May 27, 2010 5:13 am

Hey I've written an api with this itext. All about this will be available here
BIT0112-Rokon
BIT0112-Rokon
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : O+
Posts : 673
Points : 1269

http://blog.codexplo.org

Back to top Go down

PDF Generation in Java Empty Re: PDF Generation in Java

Post by andrewb Tue Oct 19, 2010 5:31 pm

Well from what I've found out, if you are looking for a document generation system on Java, take a look at this Java document generation site. It has basic info on all the vendors. It makes for a great starting point.

andrewb
Pre-Alpha Release
Pre-Alpha Release

Posts : 1
Points : 1

Back to top Go down

PDF Generation in Java Empty Re: PDF Generation in Java

Post by BIT0112-Rokon Sun Jun 05, 2011 5:28 am

Thanks andrewb Smile
BIT0112-Rokon
BIT0112-Rokon
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : O+
Posts : 673
Points : 1269

http://blog.codexplo.org

Back to top Go down

PDF Generation in Java Empty Re: PDF Generation in Java

Post by Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum