package tutorial.chapter02; import com.itextpdf.io.font.constants.StandardFonts; import com.itextpdf.kernel.font.PdfFontFactory; import com.itextpdf.kernel.geom.PageSize; import com.itextpdf.kernel.pdf.PdfDocument; import com.itextpdf.kernel.pdf.PdfPage; import com.itextpdf.kernel.pdf.PdfWriter; import com.itextpdf.kernel.pdf.canvas.PdfCanvas; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; /** * Simple drawing text example. */ public class C02E03_StarWars { public static final String DEST = "results/chapter02/star_wars.pdf"; public static void main(String args[]) throws IOException { File file = new File(DEST); file.getParentFile().mkdirs(); new C02E03_StarWars().createPdf(DEST); } public void createPdf(String dest) throws IOException { //Initialize PDF document PdfDocument pdf = new PdfDocument(new PdfWriter(dest)); //Add new page PageSize ps = PageSize.A4; PdfPage page = pdf.addNewPage(ps); PdfCanvas canvas = new PdfCanvas(page); List text = new ArrayList(); text.add(" Episode V "); text.add(" THE EMPIRE STRIKES BACK "); text.add("It is a dark time for the"); text.add("Rebellion. Although the Death"); text.add("Star has been destroyed,"); text.add("Imperial troops have driven the"); text.add("Rebel forces from their hidden"); text.add("base and pursued them across"); text.add("the galaxy."); text.add("Evading the dreaded Imperial"); text.add("Starfleet, a group of freedom"); text.add("fighters led by Luke Skywalker"); text.add("has established a new secret"); text.add("base on the remote ice world"); text.add("of Hoth..."); //Replace the origin of the coordinate system to the top left corner canvas.concatMatrix(1, 0, 0, 1, 0, ps.getHeight()); canvas.beginText() .setFontAndSize(PdfFontFactory.createFont(StandardFonts.COURIER_BOLD), 14) .setLeading(14 * 1.2f) .moveText(70, -40); for (String s : text) { //Add text and move to the next line canvas.newlineShowText(s); } canvas.endText(); //Close document pdf.close(); } }