Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Generate Simple PDF using iText

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 779
    Comment on it

    Generate Simple PDF using iText: Some time we need to generate documents using our application. The document could be like pdf, word, excel etc. To generate these documents we need to take help of third party api.

    In this article we talk about generating PDF document using iText in java. Here iText is a very famous Java library created by Bruno Lowagie. The iText api allow us to create new pdf, modify pdf document etc kind of manipulation. We can use itext by adding following maven dependency in our POM.xml.

    <dependency>
    	<groupId>com.itextpdf</groupId>
    	<artifactId>itextpdf</artifactId>
    	<version>5.0.6</version>
    </dependency>

    Basic example to create PDF using iText : Here we will show you simple pdf document generation using the iText liberary. It will create the pdf with name 'FirstDocument.pdf' and having only one paragraph with simple text.

    package com.itext.samples;
    
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    
    import com.itextpdf.text.Document;
    import com.itextpdf.text.DocumentException;
    import com.itextpdf.text.Paragraph;
    import com.itextpdf.text.pdf.PdfWriter;
    
    public class BasicDocument {
    
    	public static void main(String[] args) {
    
            Document document = new Document();
            Paragraph paragraph = new Paragraph("This is a first para in PDF.");
    
            try {
                PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("/home/sumit/FirstDocument.pdf"));
    
                document.open();
                document.add(paragraph);
                document.close(); 
    		writer.close();
    
            } catch (DocumentException e) {
                e.printStackTrace();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
    }

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: