Java/PDF RTF
Версия от 18:01, 31 мая 2010; (обсуждение)
- AWT Image
- Add Page
- Anchor
- Anchor Hyperlink
- Annotations
- Arc
- BMP
- Barcode
- Bookmarks
- Chapter
- Chapter Alignment
- Chapter Bookmarks
- Chunk
- Chunk Color
- Chunk Rendering Mode
- Chunk Size
- Chunk Skew
- Chunk Text
- Circle
- Close PDF
- Column
- Column Alignment
- Coordinate
- Debug
- Destinations
- Document Page Event
- Draw
- Draw State
- Draw Text
- Element
- Encrypted PDF
- Form
- Form Control
- Form Fill
- GState
- Gif
- Goto
- Graphics2D
- GreekLists
- HTML Hyperlink
- Hyphenation
- Image Annotation
- Image Manipulation
- Image Mask
- Image Position
- Image Rotate
- Image Scale
- JPG
- JTable to Pdf
- JavaScript Action
- Label
- Layer
- Leading
- Line
- List
- Margin
- Measurements
- Merge
- Multi Documents
- New Line
- New Page
- Outline
- PDF Action
- PDF Metadata
- PDF Read
- PDF Version
- PNG
- Page Background
- Page Count
- Page Footer Header
- Page Size
- Paragraph
- Paragraph Attributes
- Pattern
- Pause Resume
- PdfContentByte
- Phrase
- Portrait Landscape
- PostScript
- RTF Anchor
- RTF Writer
- Raw Code
- Rectangle
- RomanList
- Servlet
- Servlet Rtf
- Simple Table Cell
- Space Word Ratio
- Spot Colors
- Super Subscript
- Table
- Table Alignment
- Table Cell Alignment
- Table Cell Border
- Table Cell Color
- Table Cell Event
- Table Cell Image
- Table Cell Margin
- Table Cell Size
- Table Cell Span
- Table Column
- Table Default Cell
- Table Event
- Table Header
- Table Nested
- Table Padding
- Table Split
- Template
- Tif
- Transformation
- Transparency
- Unicode
- Viewer
- Viewer Preferences
- WMF
- Watermark
- ZapfDingbatsLists
- ZapfDingbatsNumberLists
- itext version
Содержание
- 1 Chunk Font Color
- 2 Chunks Font Background
- 3 Chunks Text Rise UnderLine
- 4 Chunk Underlined Text
- 5 Fixed Font Width
- 6 Font Color By PdfContentByte
- 7 Font Color in PDF
- 8 Font Encoding PDF
- 9 Font Extra Style: NORMAL
- 10 Font Extra Style: STRIKETHRU
- 11 Font Extra Style: UNDERLINE
- 12 Font Factory Styles: Bold
- 13 Font Factory Styles Example
- 14 Font Selection
- 15 Font Style 1
- 16 Font Style 2
- 17 Font Style 3
- 18 Full Font Names
- 19 Illustrate fonts
- 20 Listing Encodings for Pdf document
- 21 Pdf standard fonts
- 22 Register Fonts for PDF document
- 23 Strike Through Text
- 24 Table Cell with Font
- 25 True Type font Collections
- 26 True Type font From TTF File
- 27 Using BaseFont
- 28 Using Font Factory
- 29 Using FontFactory to construct fonts
- 30 Using Font Factory to Get all Registered Families
- 31 Using Registered Font
Chunk Font Color
import java.awt.Color;
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
public class ChunkFontColorPDF {
public static void main(String[] args) {
Document document = new Document();
try {
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("ChunkFontColorPDF.pdf"));
document.open();
Font red = FontFactory.getFont(FontFactory.HELVETICA, Font.DEFAULTSIZE, Font.BOLD, new Color(0xFF, 0x00, 0x00));
Paragraph p;
p = new Paragraph("Red is ");
p.add(new Chunk("red", red));
document.add(p);
} catch (Exception ioe) {
System.err.println(ioe.getMessage());
}
document.close();
}
}
Chunks Font Background
import java.awt.Color;
import java.io.FileOutputStream;
import java.net.URL;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.pdf.PdfWriter;
public class ChunksFontBackgroundPDF {
public static void main(String[] args) {
Document document = new Document();
try {
PdfWriter.getInstance(document, new FileOutputStream("ChunksFontBackgroundPDF.pdf"));
document.open();
Chunk test = new Chunk("some text");
float superscript = 8.0f;
test.setTextRise(superscript);
test.setBackground(new Color(0xFF, 0xDE, 0xAD));
test.setAnchor("http://www.jexp.ru");
test.setAnchor(new URL("http://www.jexp.ru"));
document.add(test);
} catch (Exception e) {
System.err.println(e.getMessage());
}
document.close();
}
}
Chunks Text Rise UnderLine
import java.awt.Color;
import java.io.FileOutputStream;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
public class ChunksTextRiseUnderLinePDF {
public static void main(String[] args) {
Document document = new Document();
try {
PdfWriter.getInstance(document, new FileOutputStream("ChunksTextRiseUnderLinePDF.pdf"));
document.open();
Chunk test = new Chunk("some text");
float subscript = -8.0f;
test.setTextRise(subscript);
test.setUnderline(new Color(0xFF, 0x00, 0x00), 3.0f, 0.0f, -5.0f + subscript, 0.0f, PdfContentByte.LINE_CAP_ROUND);
document.add(test);
} catch (Exception e) {
System.err.println(e.getMessage());
}
document.close();
}
}
Chunk Underlined Text
import java.io.FileOutputStream;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
public class UnderlinedTextPDF {
public static void main(String[] args) {
Document document = new Document();
try {
PdfWriter.getInstance(document, new FileOutputStream("UnderlinedTextPDF.pdf"));
document.open();
Chunk underlined = new Chunk("underlined");
underlined.setUnderline(0.2f, -2f);
Paragraph p = new Paragraph("The following chunk is ");
p.add(underlined);
document.add(p);
} catch (Exception ioe) {
System.err.println(ioe.getMessage());
}
document.close();
}
}
Fixed Font Width
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;
public class FixedFontWidthPDF {
public static void main(String[] args) {
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
try {
PdfWriter.getInstance(document, new FileOutputStream("FixedFontWidthPDF.pdf"));
document.open();
BaseFont bf = BaseFont.createFont("Helvetica", "winansi", false, false, null, null);
int widths[] = bf.getWidths();
for (int k = 0; k < widths.length; ++k) {
if (widths[k] != 0)
widths[k] = 1000;
}
bf.setForceWidthsOutput(true);
document.add(new Paragraph("Helvetica with fixed width.", new Font(bf)));
} catch (Exception de) {
de.printStackTrace();
}
document.close();
}
}
Font Color By PdfContentByte
import java.awt.Color;
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.FontFactory;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
public class FontColorByPdfContentBytePDF {
public static void main(String[] args) {
Document document = new Document();
try {
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("FontColorByPdfContentBytePDF.pdf"));
document.open();
BaseFont bf = FontFactory.getFont(FontFactory.COURIER).getCalculatedBaseFont(false);
PdfContentByte cb = writer.getDirectContent();
cb.beginText();
cb.setColorFill(new Color(0x00, 0xFF, 0x00));
cb.setFontAndSize(bf, 12);
cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "Grass is green", 250, 700, 0);
cb.endText();
} catch (Exception e) {
System.err.println(e.getMessage());
}
document.close();
}
}
Font Color in PDF
import java.awt.Color;
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
public class FontColorPDF {
public static void main(String[] args) {
Document document = new Document();
try {
PdfWriter.getInstance(document, new FileOutputStream("FontColorPDF.pdf"));
document.open();
Paragraph p;
p = new Paragraph("Roses are ");
p.add(new Chunk("red", FontFactory.getFont(FontFactory.HELVETICA, Font.DEFAULTSIZE, Font.BOLD, new Color(0xFF, 0x00, 0x00))));
document.add(p);
p = new Paragraph("Violets are ");
p.add(new Chunk("blue", FontFactory.getFont(FontFactory.HELVETICA, Font.DEFAULTSIZE, Font.ITALIC, new Color(0x00, 0x00, 0xFF))));
document.add(p);
} catch (Exception e) {
System.err.println(e.getMessage());
}
document.close();
}
}
Font Encoding PDF
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;
public class FontEncodingPDF {
public static void main(String[] args) {
Document document = new Document();
try {
PdfWriter.getInstance(document, new FileOutputStream("FontEncodingPDF.pdf"));
document.open();
BaseFont helvetica = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252,
BaseFont.EMBEDDED);
Font font = new Font(helvetica, 12, Font.NORMAL);
Chunk chunk = new Chunk("BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.EMBEDDED",font);
document.add(chunk);
} catch (Exception e) {
System.err.println(e.getMessage());
}
document.close();
}
}
Font Extra Style: NORMAL
import java.io.FileOutputStream;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.pdf.PdfWriter;
public class ExtraStyleNORMALPDF {
public static void main(String[] args) {
Document document = new Document();
try {
PdfWriter.getInstance(document, new FileOutputStream("ExtraStyleNORMALPDF.pdf"));
document.open();
document.add(new Chunk("underline", FontFactory.getFont(FontFactory.HELVETICA, Font.DEFAULTSIZE, Font.NORMAL)));
} catch (Exception e) {
System.err.println(e.getMessage());
}
document.close();
}
}
Font Extra Style: STRIKETHRU
import java.io.FileOutputStream;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.pdf.PdfWriter;
public class ExtraStyleSTRIKETHRUPDF {
public static void main(String[] args) {
Document document = new Document();
try {
PdfWriter.getInstance(document, new FileOutputStream("ExtraStyleSTRIKETHRUPDF.pdf"));
document.open();
document.add(new Chunk("underline", FontFactory.getFont(FontFactory.HELVETICA, Font.DEFAULTSIZE, Font.STRIKETHRU)));
} catch (Exception e) {
System.err.println(e.getMessage());
}
document.close();
}
}
Font Extra Style: UNDERLINE
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.html.HtmlWriter;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.rtf.RtfWriter2;
public class ExtraStyleUNDERLINEPDF {
public static void main(String[] args) {
Document document = new Document();
try {
PdfWriter.getInstance(document, new FileOutputStream("ExtraStyleUNDERLINEPDF.pdf"));
document.open();
document.add(new Chunk("underline", FontFactory.getFont(FontFactory.HELVETICA, Font.DEFAULTSIZE, Font.UNDERLINE)));
} catch (Exception e) {
System.err.println(e.getMessage());
}
document.close();
}
}
Font Factory Styles: Bold
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.PdfWriter;
public class FontFactoryStylesBoldPDF {
public static void main(String[] args) {
Document document = new Document();
try {
PdfWriter.getInstance(document, new FileOutputStream("FontFactoryStylesBoldPDF.pdf"));
document.open();
FontFactory.register("c:\\windows\\fonts\\arialbd.ttf");
document.add(new Phrase("bold ", FontFactory.getFont("Arial", 8, Font.BOLD)));
} catch (Exception e) {
System.err.println(e.getMessage());
}
document.close();
}
}
Font Factory Styles Example
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.PdfWriter;
public class FontFactoryStylesPDF {
public static void main(String[] args) {
Document document = new Document();
try {
PdfWriter.getInstance(document, new FileOutputStream(
"FontFactoryStylesPDF.pdf"));
document.open();
FontFactory.register("c:\\windows\\fonts\\arial.ttf");
Phrase myPhrase = new Phrase("This is font family Arial ", FontFactory.getFont("Arial", 8));
document.add(myPhrase);
} catch (Exception e) {
System.err.println(e.getMessage());
}
document.close();
}
}
Font Selection
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.FontSelector;
import com.lowagie.text.pdf.PdfWriter;
public class FontSelectionPDF {
public static void main(String[] args) throws Exception {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("FontSelectionPDF.pdf"));
document.open();
String text = "Text Text Text Text Text Text Text, Text Text Text Text Text Text Text Text, Text Text Text Text Text Text Text Text Text Text Text Text Text ";
FontSelector sel = new FontSelector();
sel.addFont(new Font(Font.TIMES_ROMAN, 12));
sel.addFont(new Font(Font.ZAPFDINGBATS, 12));
sel.addFont(new Font(Font.SYMBOL, 12));
Phrase ph = sel.process(text);
document.add(new Paragraph(ph));
document.close();
}
}
Font Style 1
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.PdfWriter;
public class FontStyle1PDF {
public static void main(String[] args) {
Document document = new Document();
try {
PdfWriter.getInstance(document, new FileOutputStream("FontStyle1PDF.pdf"));
document.open();
Phrase myPhrase = new Phrase("new Font(Font.TIMES_ROMAN, 8, Font.BOLD)", new Font(Font.TIMES_ROMAN, 8, Font.BOLD));
document.add(myPhrase);
} catch (Exception e) {
System.err.println(e.getMessage());
}
document.close();
}
}
Font Style 2
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.PdfWriter;
public class FontStyle2PDF {
public static void main(String[] args) {
Document document = new Document();
try {
PdfWriter.getInstance(document, new FileOutputStream("FontStyle2PDF.pdf"));
document.open();
Phrase myPhrase = new Phrase("new Font(Font.TIMES_ROMAN, 8, Font.BOLD)", new Font(Font.TIMES_ROMAN, 8, Font.BOLD));
document.add(myPhrase);
} catch (Exception e) {
System.err.println(e.getMessage());
}
document.close();
}
}
Font Style 3
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.PdfWriter;
public class FontStyle3PDF {
public static void main(String[] args) {
Document document = new Document();
try {
PdfWriter.getInstance(document, new FileOutputStream("FontStyle3PDF.pdf"));
document.open();
Phrase myPhrase = new Phrase("FontFactory.getFont(FontFactory.TIMES_ROMAN, 8, Font.BOLD)", FontFactory.getFont(FontFactory.TIMES_ROMAN, 8, Font.BOLD));
document.add(myPhrase);
} catch (Exception e) {
System.err.println(e.getMessage());
}
document.close();
}
}
Full Font Names
import com.lowagie.text.pdf.BaseFont;
public class FullFontNamesPDF {
public static void main(String[] args) {
try {
BaseFont bf = BaseFont.createFont("c:\\windows\\fonts\\arial.ttf", "winansi",
BaseFont.NOT_EMBEDDED);
System.out.println("postscriptname: " + bf.getPostscriptFontName());
String names[][] = bf.getFullFontName();
System.out.println("\n\nListing the full font name:\n\n");
for (int k = 0; k < names.length; ++k) {
System.out.println(names[k][3] + "\r\n");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Illustrate fonts
import java.io.FileOutputStream;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
public class FontListPDF {
public static void main(String[] args) {
try {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("FontListPDF.pdf"));
document.open();
Paragraph p = new Paragraph();
p.add(new Chunk("Font.TIMES_ROMAN", new Font(Font.TIMES_ROMAN, 12)));
p.add(Chunk.NEWLINE);
p.add(new Chunk("Font.ZAPFDINGBATS", new Font(Font.ZAPFDINGBATS, 12)));
p.add(Chunk.NEWLINE);
p.add(new Chunk("Font.SYMBOL", new Font(Font.SYMBOL, 12)));
p.add(Chunk.NEWLINE);
p.add(new Chunk("Font.BOLD|Font.SYMBOL", new Font(Font.BOLD|Font.SYMBOL, 12)));
p.add(Chunk.NEWLINE);
p.add(new Chunk("Font.STRIKETHRU", new Font(Font.STRIKETHRU, 12)));
p.add(Chunk.NEWLINE);
p.add(new Chunk("Font.STRIKETHRU|Font.BOLDITALIC", new Font(Font.STRIKETHRU|Font.BOLDITALIC, 12)));
p.add(Chunk.NEWLINE);
p.add(new Chunk("Font.HELVETICA", new Font(Font.HELVETICA, 12)));
p.add(Chunk.NEWLINE);
p.add(new Chunk("Font.HELVETICA", new Font(Font.HELVETICA, Font.DEFAULTSIZE)));
p.add(Chunk.NEWLINE);
p.add(new Chunk("Font.UNDERLINE", new Font(Font.UNDERLINE, Font.DEFAULTSIZE)));
p.add(Chunk.NEWLINE);
p.add(new Chunk("Font.COURIER ", new Font(Font.COURIER, Font.DEFAULTSIZE)));
document.add(new Paragraph(p));
document.close();
} catch (Exception de) {
de.printStackTrace();
}
}
}
Listing Encodings for Pdf document
import com.lowagie.text.pdf.BaseFont;
public class ListEncodingsPDF {
public static void main(String[] args) {
try {
BaseFont bfComic = BaseFont.createFont("c:\\windows\\fonts\\arial.ttf", BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
System.out.println("postscriptname: " + bfComic.getPostscriptFontName());
String[] codePages = bfComic.getCodePagesSupported();
System.out.println("All available encodings:\n\n");
for (int i = 0; i < codePages.length; i++) {
System.out.println(codePages[i]);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Pdf standard fonts
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
public class StandardFontsPDF {
public static void main(String[] args) {
Document document = new Document();
try {
PdfWriter.getInstance(document, new FileOutputStream("StandardFontsPDF.pdf"));
document.open();
document.add(new Paragraph("new Font(Font.COURIER, Font.DEFAULTSIZE, Font.NORMAL", new Font(Font.COURIER, Font.DEFAULTSIZE, Font.NORMAL)));
document.add(new Paragraph("new Font(Font.COURIER, Font.DEFAULTSIZE, Font.ITALIC)", new Font(Font.COURIER, Font.DEFAULTSIZE, Font.ITALIC)));
document.add(new Paragraph("new Font(Font.COURIER, Font.DEFAULTSIZE, Font.BOLD)", new Font(Font.COURIER, Font.DEFAULTSIZE, Font.BOLD)));
document.add(new Paragraph("new Font(Font.COURIER, Font.DEFAULTSIZE, Font.BOLD | Font.ITALIC)", new Font(Font.COURIER, Font.DEFAULTSIZE, Font.BOLD | Font.ITALIC)));
document.add(new Paragraph("new Font(Font.HELVETICA, Font.DEFAULTSIZE, Font.NORMAL)", new Font(Font.HELVETICA, Font.DEFAULTSIZE, Font.NORMAL)));
document.add(new Paragraph("new Font(Font.HELVETICA, Font.DEFAULTSIZE, Font.ITALIC)", new Font(Font.HELVETICA, Font.DEFAULTSIZE, Font.ITALIC)));
document.add(new Paragraph("new Font(Font.HELVETICA, Font.DEFAULTSIZE, Font.BOLD)", new Font(Font.HELVETICA, Font.DEFAULTSIZE, Font.BOLD)));
document.add(new Paragraph("new Font(Font.HELVETICA, Font.DEFAULTSIZE, Font.BOLDITALIC)", new Font(Font.HELVETICA, Font.DEFAULTSIZE, Font.BOLDITALIC)));
document.add(new Paragraph("new Font(Font.TIMES_ROMAN, Font.DEFAULTSIZE, Font.NORMAL)", new Font(Font.TIMES_ROMAN, Font.DEFAULTSIZE, Font.NORMAL)));
document.add(new Paragraph("new Font(Font.TIMES_ROMAN, Font.DEFAULTSIZE, Font.ITALIC)", new Font(Font.TIMES_ROMAN, Font.DEFAULTSIZE, Font.ITALIC)));
document.add(new Paragraph("new Font(Font.TIMES_ROMAN, Font.DEFAULTSIZE, Font.BOLD)", new Font(Font.TIMES_ROMAN, Font.DEFAULTSIZE, Font.BOLD)));
document.add(new Paragraph("new Font(Font.TIMES_ROMAN, Font.DEFAULTSIZE, Font.BOLDITALIC)", new Font(Font.TIMES_ROMAN, Font.DEFAULTSIZE, Font.BOLDITALIC)));
document.add(new Paragraph("new Font(Font.SYMBOL)", new Font(Font.SYMBOL)));
document.add(new Paragraph("new Font(Font.ZAPFDINGBATS)", new Font(Font.ZAPFDINGBATS)));
} catch (Exception e) {
System.err.println(e.getMessage());
}
document.close();
}
}
Register Fonts for PDF document
import java.util.Iterator;
import com.lowagie.text.FontFactory;
public class RegisterFontPDF {
public static void main(String[] args) {
FontFactory.register("c:\\windows\\fonts\\comicbd.ttf");
FontFactory.register("c:\\windows\\fonts\\comic.ttf");
try {
System.out.println("These fonts were registered at the FontFactory:");
for (Iterator i = FontFactory.getRegisteredFonts().iterator(); i.hasNext();) {
System.out.println(i.next());
}
System.out.println("These are the families these fonts belong to:");
for (Iterator i = FontFactory.getRegisteredFamilies().iterator(); i.hasNext();) {
System.out.println((String) i.next());
}
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
}
Strike Through Text
import java.io.FileOutputStream;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.pdf.PdfWriter;
public class StrikeThroughTextPDF {
public static void main(String[] args) {
Document document = new Document();
try {
PdfWriter.getInstance(document, new FileOutputStream("StrikeThroughTextPDF.pdf"));
document.open();
Chunk strikethru = new Chunk("strike through example");
strikethru.setUnderline(0.5f, 3f);
document.add(strikethru);
} catch (Exception ioe) {
System.err.println(ioe.getMessage());
}
document.close();
}
}
Table Cell with Font
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.PageSize;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
public class TableCellWithFontPDF {
public static void main(String[] args) {
Document document = new Document(PageSize.A4);
try {
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("TableCellWithFontPDF.pdf"));
document.open();
PdfPTable table = new PdfPTable(1);
PdfPCell cell = new PdfPCell(new Phrase("phrase without font"));
table.addCell(cell);
table.addCell(new Phrase("A", FontFactory.getFont(FontFactory.HELVETICA, 8)));
table.addCell("no font");
document.add(table);
} catch (Exception e) {
System.err.println(e.getMessage());
}
document.close();
}
}
True Type font Collections
import com.lowagie.text.pdf.BaseFont;
public class TrueTypeCollectionsPDF {
public static void main(String[] args) {
try {
String[] names = BaseFont.enumerateTTCNames("c:\\windows\\fonts\\msgothic.ttc");
for (int i = 0; i < names.length; i++) {
System.out.println("font " + i + ": " + names[i]);
}
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
}
True Type font From TTF File
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;
public class TrueTypeFromTTFFilePDF {
public static void main(String[] args) {
Document document = new Document();
try {
PdfWriter.getInstance(document, new FileOutputStream("TrueTypeFromTTFFilePDF.pdf"));
document.open();
BaseFont bfComic = BaseFont.createFont("c:\\windows\\fonts\\arial.ttf", BaseFont.CP1252,
BaseFont.NOT_EMBEDDED);
Font font = new Font(bfComic, 12);
String text1 = "arial.ttf";
document.add(new Paragraph(text1, font));
} catch (Exception e) {
System.err.println(e.getMessage());
}
document.close();
}
}
Using BaseFont
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfImportedPage;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfWriter;
public class UsingBaseFontPDF {
public static void main(String[] args) {
try {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("UsingBaseFontPDF.pdf"));
document.open();
PdfContentByte cb = writer.getDirectContent();
document.newPage();
BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
cb.beginText();
cb.setFontAndSize(bf, 14);
cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "www.jexp.ru", 100 / 2, 40, 0);
cb.endText();
document.close();
} catch (Exception de) {
de.printStackTrace();
}
}
}
Using Font Factory
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.TreeSet;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;
public class UsingFontFactoryPDF {
public static void main(String[] args) {
Document document = new Document();
try {
PdfWriter.getInstance(document, new FileOutputStream("UsingFontFactoryPDF.pdf"));
document.open();
Paragraph p = new Paragraph("Font Families", FontFactory.getFont(FontFactory.HELVETICA, 16f));
document.add(p);
FontFactory.registerDirectories();
TreeSet families = new TreeSet(FontFactory.getRegisteredFamilies());
for (Iterator i = families.iterator(); i.hasNext(); ) {
p = new Paragraph((String) i.next());
document.add(p);
}
} catch (Exception e) {
System.err.println(e.getMessage());
}
document.close();
}
}
Using FontFactory to construct fonts
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
public class FontFactoryFontsPDF {
public static void main(String[] args) {
Document document = new Document();
try {
PdfWriter.getInstance(document, new FileOutputStream("FontFactoryFontsPDF.pdf"));
document.open();
document.add(new Paragraph("FontFactory.getFont(FontFactory.COURIER, Font.DEFAULTSIZE, Font.NORMAL)", FontFactory.getFont(FontFactory.COURIER, Font.DEFAULTSIZE, Font.NORMAL)));
document.add(new Paragraph("FontFactory.getFont(FontFactory.COURIER, Font.DEFAULTSIZE, Font.ITALIC)", FontFactory.getFont(FontFactory.COURIER, Font.DEFAULTSIZE, Font.ITALIC)));
document.add(new Paragraph("FontFactory.getFont(FontFactory.COURIER, Font.DEFAULTSIZE, Font.BOLD)", FontFactory.getFont(FontFactory.COURIER, Font.DEFAULTSIZE, Font.BOLD)));
document.add(new Paragraph("FontFactory.getFont(FontFactory.COURIER, Font.DEFAULTSIZE, Font.BOLD | Font.ITALIC)", FontFactory.getFont(FontFactory.COURIER, Font.DEFAULTSIZE, Font.BOLD | Font.ITALIC)));
document.add(new Paragraph("FontFactory.getFont(FontFactory.HELVETICA, Font.DEFAULTSIZE, Font.NORMAL)", FontFactory.getFont(FontFactory.HELVETICA, Font.DEFAULTSIZE, Font.NORMAL)));
document.add(new Paragraph("FontFactory.getFont(FontFactory.HELVETICA, Font.DEFAULTSIZE, Font.ITALIC)", FontFactory.getFont(FontFactory.HELVETICA, Font.DEFAULTSIZE, Font.ITALIC)));
document.add(new Paragraph("FontFactory.getFont(FontFactory.HELVETICA, Font.DEFAULTSIZE, Font.BOLD)", FontFactory.getFont(FontFactory.HELVETICA, Font.DEFAULTSIZE, Font.BOLD)));
document.add(new Paragraph("FontFactory.getFont(FontFactory.HELVETICA, Font.DEFAULTSIZE, Font.BOLDITALIC)", FontFactory.getFont(FontFactory.HELVETICA, Font.DEFAULTSIZE, Font.BOLDITALIC)));
document.add(new Paragraph("FontFactory.getFont(FontFactory.TIMES_ROMAN, Font.DEFAULTSIZE, Font.NORMAL)", FontFactory.getFont(FontFactory.TIMES_ROMAN, Font.DEFAULTSIZE, Font.NORMAL)));
document.add(new Paragraph("FontFactory.getFont(FontFactory.TIMES_ROMAN, Font.DEFAULTSIZE, Font.ITALIC)", FontFactory.getFont(FontFactory.TIMES_ROMAN, Font.DEFAULTSIZE, Font.ITALIC)));
document.add(new Paragraph("FontFactory.getFont(FontFactory.TIMES_ROMAN, Font.DEFAULTSIZE, Font.BOLD)", FontFactory.getFont(FontFactory.TIMES_ROMAN, Font.DEFAULTSIZE, Font.BOLD)));
document.add(new Paragraph("FontFactory.getFont(FontFactory.TIMES_ROMAN, Font.DEFAULTSIZE, Font.BOLDITALIC)", FontFactory.getFont(FontFactory.TIMES_ROMAN, Font.DEFAULTSIZE, Font.BOLDITALIC)));
document.add(new Paragraph("FontFactory.getFont(FontFactory.SYMBOL, Font.DEFAULTSIZE, Font.NORMAL)", FontFactory.getFont(FontFactory.SYMBOL, Font.DEFAULTSIZE, Font.NORMAL)));
document.add(new Paragraph("FontFactory.getFont(FontFactory.ZAPFDINGBATS, Font.DEFAULTSIZE, Font.NORMAL)", FontFactory.getFont(FontFactory.ZAPFDINGBATS, Font.DEFAULTSIZE, Font.NORMAL)));
} catch (Exception e) {
System.err.println(e.getMessage());
}
document.close();
}
}
Using Font Factory to Get all Registered Families
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.TreeSet;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;
public class UsingFontFactoryToGetAllRegisterFamilies {
public static void main(String[] args) {
Document document = new Document();
try {
PdfWriter.getInstance(document, new FileOutputStream("UsingFontFactoryToGetAllRegisterFamilies.pdf"));
document.open();
Paragraph p = new Paragraph("Font Families", FontFactory.getFont(FontFactory.HELVETICA, 16f));
document.add(p);
FontFactory.registerDirectories();
TreeSet families = new TreeSet(FontFactory.getRegisteredFamilies());
for (Iterator i = families.iterator(); i.hasNext();) {
String name = (String) i.next();
p = new Paragraph(name, FontFactory.getFont(name, BaseFont.WINANSI, BaseFont.EMBEDDED));
document.add(p);
}
} catch (Exception e) {
System.err.println(e.getMessage());
}
document.close();
}
}
Using Registered Font
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;
public class UsingRegisterFontPDF {
public static void main(String[] args) {
FontFactory.register("c:\\windows\\fonts\\comicbd.ttf");
Document document = new Document();
try {
PdfWriter.getInstance(document, new FileOutputStream("UsingRegisterFontPDF.pdf"));
document.open();
Font font1 = FontFactory.getFont("ComicSansMS", BaseFont.WINANSI, 12);
String text1 = "True Type font "ComicSansMS".";
document.add(new Paragraph(text1, font1));
} catch (Exception e) {
System.err.println(e.getMessage());
}
document.close();
}
}