Java/PDF RTF/Draw Text
Drawing Text with RGB Fill
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
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 DrawTextWithRGBFillPDF {
public static void main(String[] args) {
Document document = new Document();
try {
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(
"DrawTextWithRGBFillPDF.pdf"));
document.open();
PdfContentByte cb = writer.getDirectContent();
PdfTemplate template = cb.createTemplate(500, 200);
BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
template.beginText();
template.setFontAndSize(bf, 180);
template.setRGBColorFill(0xFF, 0x00, 0x00);
template.showTextAligned(PdfContentByte.ALIGN_LEFT, "PDF", 125f, 35f, 0f);
template.resetRGBColorFill();
template.endText();
cb.addTemplate(template, 0, 1, -1, 0, 500, 200);
} catch (Exception e) {
System.err.println(e.getMessage());
}
document.close();
}
}
Show Text Using PdfContentByte
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Image;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
public class ShowTextUsingPdfContentBytePDF {
public static void main(String[] args) throws Exception{
Rectangle pageSize = new Rectangle(780, 525);
Document document = new Document(pageSize);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("ShowTextUsingPdfContentBytePDF.pdf"));
document.open();
PdfContentByte cb = writer.getDirectContent();
cb.setFontAndSize(BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, false), 24);
cb.beginText();
cb.showTextAligned(Element.ALIGN_CENTER, "Show text using PdfContentByte", 595f, 262f, 0f);
cb.endText();
cb.stroke();
document.close();
}
}