import java.awt.Color;
import java.io.IOException;
import com.gnostice.pdfone.PDFOne;
import com.gnostice.pdfone.PdfDocument;
import com.gnostice.pdfone.PdfException;
import com.gnostice.pdfone.PdfMeasurement;
import com.gnostice.pdfone.PdfWriter;
import com.gnostice.pdfone.encodings.PdfEncodings;
import com.gnostice.pdfone.fonts.PdfFont;
public class PdfFont_Examples
{
// Activates the component PDFOne.jar
static
{
PDFOne.activate("T95VZE:W8HBPVA:74VQ8QV:LO4V8",
"9B1HRZAP:X5853ERNE:5EREMEGRQ:TX1R10");
}
// Takes pathname of a font file as an argument
public static void main(String[] args) throws IOException,
PdfException
{
PdfFont_Examples obj = new PdfFont_Examples();
// To try other examples, add the obj.<example_method>
// accordingly. For example, try:
// obj.GETTERS_AND_SETTERS_Example();
obj.create_Example(args[0]);
}
// This code segment creates several PdfFont objects using
// overloaded methods
public void create_Example(String fontPathname) throws IOException,
PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
"PdfFont_create_Example.pdf");
PdfDocument document = new PdfDocument(writer);
// Creates PdfFonts objects.
// If fontPathname is an Unicode font, use
// PdfEncodings.UTF_16BE for the encoding arguments.
PdfFont font1 = PdfFont.create("Courier",
21,
PdfEncodings.WINANSI);
PdfFont font2 = PdfFont.create(fontPathname,
21,
PdfEncodings.WINANSI,
PdfFont.EMBED_SUBSET);
PdfFont font3 = PdfFont.create("Courier",
PdfFont.BOLD | PdfFont.ITALIC,
21,
PdfEncodings.WINANSI);
PdfFont font4 = PdfFont.create(fontPathname,
PdfFont.UNDERLINE,
21,
PdfEncodings.WINANSI,
PdfFont.EMBED_FULL);
String pangram =
" - The five boxing wizards jump quickly";
// Write text using the above fonts
document.writeText(font1.getName() + pangram, font1, 100, 100);
document.writeText(font2.getName() + pangram, font2, 100, 200);
document.writeText(font3.getName() + pangram, font3, 100, 300);
document.writeText(font4.getName() + pangram, font4, 100, 400);
// Sets the file to be opened after it is written to
document.setOpenAfterSave(true);
// Writes the document object to file
document.write();
// Closes all I/O streams associated with this writer object
writer.dispose();
}
// This code segment prints the return values of getWidth methods
// for the font supplied as the main() argument
public void GetWidth_Example(String fontPathname)
throws IOException, PdfException
{
// Creates a font using console argument
PdfFont font = PdfFont.create(
fontPathname,
PdfFont.STROKE_AND_FILL,
11,
PdfEncodings.CP1252,
PdfFont.EMBED_SUBSET);
System.out.println(
"getWidth('a') = "
+ font.getWidth('a'));
System.out.println(
"getWidth('a', PdfMeasurement.MU_PIXELS) = "
+ font.getWidth('a', PdfMeasurement.MU_PIXELS));
System.out.println(
"getWidth(\"One two buckle my shoe\") = "
+ font.getWidth("One two buckle my shoe"));
System.out.println(
"getWidth(\"One two buckle my shoe\", "
+ "PdfMeasurement.MU_PIXELS)"
+ font.getWidth("One two buckle my shoe",
PdfMeasurement.MU_PIXELS));
System.out.println(
"font.getWidth(\"One two buckle my shoe \", "
+ "PdfMeasurement.MU_PIXELS, excludeEndSpaces=true)"
+ font.getWidth("One two buckle my shoe ",
PdfMeasurement.MU_PIXELS,
true));
}
// This code segment demonstrates get and set methods of PdfFont
// class
public void GETTERS_AND_SETTERS_Example(String fontPathname)
throws IOException, PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
"PdfFont_GETTERS_AND_SETTERS_Example.pdf");
PdfDocument document = new PdfDocument(writer);
// Creates a PdfFont object for the font supplied as the
// main() argument
PdfFont font = PdfFont.create(
fontPathname,
PdfFont.STROKE_AND_FILL,
21,
PdfEncodings.CP1252,
PdfFont.EMBED_SUBSET);
// Specifies font properties
font.setColor(Color.YELLOW);
font.setSize(24);
font.setStrokeColor(Color.RED);
font.setStrokeWidth(1);
font.setStyle(PdfFont.BOLD | PdfFont.ITALIC);
// Writes text using the font
document.writeText(
"THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG",
font,
150, 100);
// Creates a string with return values of the get methods of
// PdfFont class
String s =
"[ getAscent() = " + font.getAscent()
+ "] [ getAvgWidth() = " + font.getAvgWidth()
+ "] [ getCapHeight() = " + font.getCapHeight()
+ "] [ getColor() = " + font.getColor()
+ "] [ getDescent() = " + font.getDescent()
+ "] [ getEmbedType() = " + font.getEmbedType()
+ "] [ getEncoding = " + font.getEncoding()
+ "] [ getFirstChar() = " + font.getFirstChar()
+ "] [ getFlags() = " + font.getFlags()
+ "] [ getHeight() = " + font.getHeight()
+ "] [ getItalicangle() = " + font.getItalicangle()
+ "] [ getLastChar() = " + font.getLastChar()
+ "] [ getMaxWidth = " + font.getMaxWidth()
+ "] [ getName() = " + font.getName()
+ "] [ getSize() = " + font.getSize()
+ "] [ getStemH() = " + font.getStemH()
+ "] [ getStemV() = " + font.getStemV()
+ "] [ getStrokeWidth() = " + font.getStrokeWidth()
+ "] [ getStyle() = " + font.getStyle()
+ "] [ getStrokeColor() = " + font.getStrokeColor()
+ "] [ getType() = " + font.getType()
+ "] [ getUnderlinePosition() = "
+ font.getUnderlinePosition()
+ "] [ getUnderlineThickness() = "
+ font.getUnderlineThickness() + "]";
// Writes the string
document.writeText(s, 150, 175);
// Sets the file to be opened after it is written to
document.setOpenAfterSave(true);
// Writes the document object to file
document.write();
// Closes all I/O streams associated with this writer object
writer.dispose();
}
}