TemplatesTextPDF.java Source code

Java tutorial

Introduction

Here is the source code for TemplatesTextPDF.java

Source

import java.io.FileOutputStream;

import com.lowagie.text.Document;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfTemplate;
import com.lowagie.text.pdf.PdfWriter;

public class TemplatesTextPDF {
    public static void main(String[] args) {
        Document document = new Document();

        try {
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("TemplatesTextPDF.pdf"));
            document.open();

            PdfContentByte cb = writer.getDirectContent();
            PdfTemplate template = cb.createTemplate(500, 200);

            template.beginText();
            BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
            template.setFontAndSize(bf, 12);
            template.setTextMatrix(10, 10);
            template.showText("10,10 (relative to the template!)");
            template.endText();

            cb.addTemplate(template, 0, 0);

            document.newPage();
            cb.addTemplate(template, 0, 0);

            cb.addTemplate(template, 2, 0, 0, 2, -200, 400);
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
        document.close();
    }

}