Chunk
represents text that is added to the document. Technically chunk is a string
with specific font properties.
Chuck
class provides following constructors to define a Chunk instance.
public
Chunk()
public
Chunk(final Chunk ck)
public
Chunk(final String content)
public
Chunk(final String content, final Font font)
public
Chunk(final char c)
public
Chunk(final char c, final Font font)
public
Chunk(final Image image, final float offsetX, final float offsetY)
public
Chunk(final Image image, final float offsetX, final float offsetY,final boolean
changeLeading)
public
Chunk(final DrawInterface separator)
public
Chunk(final DrawInterface separator, final boolean vertical)
Following
step-by-step procedure explains, how to write chunk of data to pdf file.
Step 1: Define Document instance.
Document
document = new Document();
Step 2: Get PdfWriter instance and set
leading.
PdfWriter
writer = PdfWriter.getInstance(document, new
FileOutputStream("countries.pdf"));
writer.setInitialLeading(16);
‘writer.setInitialLeading(16)’ specify the space between two lines. If you
comment this line, all the text is written on same line.
Step 3: Define font object.
Font font
= FontFactory.getFont(FontFactory.COURIER, 20, Font.NORMAL, BaseColor.BLACK);
Step 4: Define Chunk instance.
Chunk
chunk = new Chunk("data", font);
Step 5: Write the chunk to the document.
document.add(chunk);
document.add(Chunk.NEWLINE);
Step 6: After adding the data, close the
document.
document.close();
Following
example writes list of countries to the pdf document ‘countries.pdf’ using
Chuck.
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.pdf.PdfWriter; public class HelloWorld { private static List<String> countries = Arrays.asList("Afghanistan", "Armenia", "Azerbaijan", "Bahrain", "Bangladesh", "Bhutan", "Brunei", "Cambodia", "China", "Cyprus", "Georgia", "India", "Indonesia", "Iran", "Iraq", "Israel", "Japan", "Jordan", "Kazakhstan", "Kuwait", "Kyrgyzstan", "Laos", "Lebanon", "Malaysia", "Maldives", "Mongolia", "Myanmar (Burma)", "Nepal", "North Korea", "Oman", "Pakistan", "Palestine", "Philippines", "Qatar", "Russia", "Saudi Arabia", "Singapore", "South Korea", "Sri Lanka", "Syria", "Taiwan", "Tajikistan", "Thailand", "Timor-Leste", "Turkey", "Turkmenistan", "United Arab Emirates (UAE)", "Uzbekistan", "Vietnam", "Yemen"); private static Font font = FontFactory.getFont(FontFactory.COURIER, 20, Font.NORMAL, BaseColor.BLACK); private static Font fontHeading = FontFactory.getFont(FontFactory.HELVETICA, 25, Font.BOLD, new BaseColor(255, 0, 0)); public static void main(String args[]) throws FileNotFoundException, DocumentException { Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("countries.pdf")); writer.setInitialLeading(16); document.open(); document.add(new Chunk("List Of Countries in Asia", fontHeading)); document.add(Chunk.NEWLINE); document.add(Chunk.NEWLINE); for (String country : countries) { Chunk chunk = new Chunk(country, font); document.add(chunk); document.add(Chunk.NEWLINE); } document.close(); } }
No comments:
Post a Comment