In this core java tutorial we will learn How to Create Phrase In Pdf (with lead) Using Itext library - core java tutorial in Java with program and examples.
What is Phrase?
A Phrase is a series of Chunks. Phrase has some main Font, but some of the chunks in the the phrase can have different font from the main Font.
Leading for all the Chunks in a Phrase remains same.
What is leading?
Leading is the distance between the baseline of two lines.
Also read :
Create Left, center and right aligned Paragraph In Pdf - iText java tutorial
Create Phrase In Pdf (with lead) Using Itext - iText java tutorial example
How to create Bar Chart In Pdf in java - iText java tutorial
Create Pie Chart In Pdf in java using itext - iText java tutorial
How to Modify - Add Text To Existing PDF in java - iText java tutorial example
Download all jars required to execute program >
Or, you may download jars from here
Program/Example to how to Create Phrase In Pdf (with lead) Using Itext library - core java tutorial
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfWriter;
/**
*
* Create Phrase In Pdf Using Itext Example -
* in iText library - core java tutorial
*
*/
public class PhraseInPdfUsingItextExample {
public static void main(String[] args) {
try {
String pdfFilePath = "e:/Phrase in Pdf Using Itext Example.pdf";
OutputStream fos = new FileOutputStream(new File(pdfFilePath));
Document document = new Document();
PdfWriter.getInstance(document, fos);
document.open();
// Here no lead is provided, so by default leading is 16
Phrase phrase1 = new Phrase("This is the Phrase with default leading = 16. ");
// Here leading is passed as 16 in Phrase constructor.
Phrase phrase2 = new Phrase(16, "This is the Phrase with leading = 16. ");
Phrase phrase3 = new Phrase(new Chunk("Adding new Phrase, default leading = 16. "));
//size of font is 16, so leading = 20 * 1.5 = 30
Font fontColour_Red = FontFactory.getFont(FontFactory.TIMES, 20f,
Font.BOLD, BaseColor.RED);
Phrase phrase4 = new Phrase(
"\nPhrase with leading = 20 * 1.5 = 30, because size of font is 20. "
+ "Adding few more words in this phrase.",
fontColour_Red);
document.add(phrase1);
document.add(phrase2);
document.add(phrase3);
document.add(phrase4);
document.close(); //close document
fos.close();
System.out.println("PDF created in >> "+ pdfFilePath);
} catch (Exception e) {
e.printStackTrace();
}
}
}
/* Output of above program
PDF created in >> e:/Phrase in Pdf Using Itext Example.pdf
*/
|
PDF formed after executing above java program (Create Phrase In Pdf (with lead) Using Itext ) will look like this >
Summary -
So in this core java tutorial we learned How to Create Phrase In Pdf (with lead) Using Itext library - core java tutorial in Java with program and examples.
Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.
References >
RELATED LINKS>
iText library tutorial in java - How to Create and work with PDF files in java
Creating first pdf and setting attributes using itext in java - iText tutorial >
Create the first Pdf Example Using iText library in java - iText java tutorial example
Setting Pdf Attributes like Title, Author, Creator, subject, Keywords, Header and CreationDate in java
Creating TABLES in pdf in java - iText tutorial >
How to Create the Table In Pdf in java - iText java tutorial
How to do the Cell Alignment In Pdf generated table in Java program
How to Set Cell Background Colour In Pdf Table in java example
How to Set Cell Background Colour In Pdf Table in java - iText java tutorial
How to Create Images In Pdf generated Table in java itext
How to Create Nested Tables in Pdf in java - iText java tutorial
How to Create the Table With Multiple Rows In Pdf in java program
Creating the Table and set the Column Width And Spacing In Pdf in java
Rotate cells, cell padding, horizontal and vertical alignment of Cell in table in Pdf in java itext - iText java tutorial example
Creating LIST in pdf in java - iText tutorial >
Create Greek List In pdf in java using iText - iText java tutorial
How to Create Roman List In pdf in java - iText java tutorial
Create ZapfDingbats List In pdf in java - iText java tutorial program
How to Create Ordered List In pdf in java using iText - iText java tutorial
Creating the UnOrdered List In pdf in java - iText java tutorial example
How to Create Lists And SubLists In Pdf in java - iText java tutorial
Creating PASSWORD PROTECTED PDF in java - iText tutorial >
How to Create Password Protected Pdf in java - iText Example
Labels:
Core Java
iText Pdf tutorial