Styling Or Formatting PDF In Java Using iText

Chapter: iText Last Updated: 11-08-2016 20:15:33 UTC

Program:

            /* ............... START ............... */
                
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import com.itextpdf.text.Chapter;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.CMYKColor;
import com.itextpdf.text.pdf.PdfWriter;
import com.lowagie.text.Section;

public class JavaiTextFormattingPDF {

	public static void main(String[] args) {

		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();
		try {
			PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("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);
		    com.itextpdf.text.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();
		}

	}
}
                /* ............... END ............... */
        

Output


Styling Or Formatting PDF In Java Using iText

Tags

Styling Or Formatting PDF, iText, Java

Similar Programs Chapter Last Updated
Java Set Background To Table Example iText 22-07-2017
Java PDF Hello World Example iText 14-08-2016
Java PDF Formatting Using iText iText 14-08-2016
Read Existing PDF In Java iText 14-08-2016
Read OR Modify PDF File In Java Using iText iText 12-08-2016
PDF With Limited Permissions In Java Using iText iText 12-08-2016
Password Protected PDF Files Using Java iText iText 12-08-2016
Create Lists In PDF Using Java iText iText 10-08-2016
Create Tables In PDF Using Java iText iText 10-08-2016
Add Images To PDF In Java Using iText iText 10-08-2016
File Attributes To PDF In Java Using iText iText 10-08-2016
PDF Creation In Java Using iText iText 10-08-2016

1