Phrase is
list of chunk objects. Assume it is like list of Chunk objects, you can add any
number of Chunk objects to it. Once you added all the chunk objects to the
phrase, you can add this phrase object to the document.
For
example, following function adds list of chunk objects to a phrase and return.
public static Phrase getPhrase() { Phrase phrase = new Phrase(); int counter = 0; for (String country : countries) { Chunk chunk = new Chunk(country, font); phrase.add(chunk); String code = "(" + countryCodes.get(counter) + ")"; counter++; Chunk superScript = new Chunk(code, superScriptFont); superScript.setTextRise(6); phrase.add(superScript); phrase.add(Chunk.NEWLINE); phrase.add(Chunk.NEWLINE); } return phrase; }
Following
statement adds the Phrase instance to the document.
document.add(getPhrase());
Following
is the complete working application.
import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.util.Arrays; import java.util.List; import com.itextpdf.text.BaseColor; import com.itextpdf.text.Chunk; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Font; import com.itextpdf.text.FontFactory; import com.itextpdf.text.Phrase; import com.itextpdf.text.pdf.PdfWriter; public class HelloWorld { private static List<String> countries = Arrays.asList("India - Assamese", "India - Bengali", "India - English", "India - Gujarati", "India - Hindi", "India - Kannada", "India - Konkani", "India - Malayalam", "India - Marathi", "India - Oriya", "India - Punjabi", "India - Sanskrit", "India - Tamil", "India - Telugu"); private static List<String> countryCodes = Arrays.asList("as-IN", "bn-IN", "en-IN", "gu-IN", "hi-IN", "kn-IN", "kok-IN", "ml-IN", "mr-IN", "or-IN", "pa-IN", "sa-IN", "ta-IN", "te-IN"); private static Font font = FontFactory.getFont(FontFactory.COURIER, 20, Font.NORMAL, BaseColor.BLACK); private static Font superScriptFont = FontFactory.getFont(FontFactory.COURIER, 10, Font.ITALIC, BaseColor.BLACK); private static Font fontHeading = FontFactory.getFont(FontFactory.HELVETICA, 25, Font.BOLD, new BaseColor(255, 0, 0)); public static Phrase getPhrase() { Phrase phrase = new Phrase(); int counter = 0; for (String country : countries) { Chunk chunk = new Chunk(country, font); phrase.add(chunk); String code = "(" + countryCodes.get(counter) + ")"; counter++; Chunk superScript = new Chunk(code, superScriptFont); superScript.setTextRise(6); phrase.add(superScript); phrase.add(Chunk.NEWLINE); phrase.add(Chunk.NEWLINE); } return phrase; } public static void main(String args[]) throws FileNotFoundException, DocumentException { Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream("countries.pdf")); document.open(); document.add(new Chunk("Langauage codes in India", fontHeading)); document.add(Chunk.NEWLINE); document.add(getPhrase()); document.close(); } }
As you see
the code, I don’t mention the statement for leading.
PdfWriter writer =
PdfWriter.getInstance(document, new
FileOutputStream("countries.pdf"));
writer.setInitialLeading(16);
It is
because, when you create Phrase object, it creates default leading of 16.
public
Phrase(final float leading) {
this.leading = leading;
font = new Font();
}
public
Phrase() {
this(16);
}
No comments:
Post a Comment