Java/PDF RTF — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
 
м (1 версия)
 
(нет различий)

Текущая версия на 06:00, 1 июня 2010

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();
  }
}