Create & Data write into PDF

//Required Jar : itext-pdfa-5.4.5.jar

import java.io.FileOutputStream;
import com.itextpdf.text.Document;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

public class iTextPDFTutorial {
public void iTextPDF() throws Exception{
String FILE = “C:/Users/Youtility/Desktop/sampleiText.pdf”;
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(FILE));
document.open();
document.add(new Paragraph(“Hello iText”));
document.add(new Paragraph(“I will be printed in PDF with the help of iText”));
document.close();

}

public static void main(String args[]){
iTextPDFTutorial get = new iTextPDFTutorial();
try {
get.iTextPDF();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

Ex: 2
import java.io.FileOutputStream;
import org.junit.Test;
import com.itextpdf.text.Chapter;
import com.itextpdf.text.Document;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Section;
import com.itextpdf.text.pdf.CMYKColor;
import com.itextpdf.text.pdf.PdfWriter;

public class PDFfileWork {

Font blueFont = FontFactory.getFont(FontFactory.HELVETICA, 8, Font.NORMAL, new CMYKColor(255, 0, 0, 0));
Font redFont = FontFactory.getFont(FontFactory.COURIER, 12, Font.BOLD, new CMYKColor(0, 255, 0, 0));
Font yellowFont = FontFactory.getFont(FontFactory.COURIER, 14, Font.BOLD, new CMYKColor(0, 0, 255, 0));
Document document = new Document();

@Test
public void pdfWorkTest(){
try {
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(“C:\\Users\\Youtility\\Desktop\\StylingExample.pdf”));
document.open();
//document.add(new Paragraph(“Styling Example”));

//Paragraph with color and font styles
Paragraph paragraphOne = new Paragraph(“Some colored paragraph text”, redFont);
document.add(paragraphOne);

//Create chapter and sections
Paragraph chapterTitle = new Paragraph(“Chapter Title”, yellowFont);
Chapter chapter1 = new Chapter(chapterTitle, 1);
chapter1.setNumberDepth(0);

Paragraph sectionTitle = new Paragraph(“Section Title”, redFont);
Section section1 = chapter1.addSection(sectionTitle);

Paragraph sectionContent = new Paragraph(“Section Text content”, blueFont);
section1.add(sectionContent);

document.add(chapter1);

document.close();
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s