Java Code Examples for com.lowagie.text.FontFactory#register()

The following examples show how to use com.lowagie.text.FontFactory#register() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FontFactoryStylesTest.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Changing the style of a FontFactory Font.
 * 
 * @param args
 *            no arguments needed
 */
@Test
public void main() throws Exception {


	// step 1: creation of a document-object
	Document document = new Document();

	// step 2: creation of the writer
	PdfWriter.getInstance(document, PdfTestBase.getOutputStream("fontfactorystyles.pdf"));

	// step 3: we open the document
	document.open();

	String fontPathBase = new File(PdfTestBase.RESOURCES_DIR + "liberation-fonts-ttf").getAbsolutePath();
	// step 4: we add some content
	FontFactory.register(fontPathBase + "/LiberationSans-Regular.ttf");
	FontFactory.register(fontPathBase + "/LiberationSans-Italic.ttf");
	FontFactory.register(fontPathBase + "/LiberationSans-Bold.ttf");
	FontFactory.register(fontPathBase + "/LiberationSans-BoldItalic.ttf");
	
	
	Phrase myPhrase = new Phrase("This is font family Liberation Sans ", FontFactory.getFont("LiberationSans", 8));
	myPhrase.add(new Phrase("italic ", FontFactory.getFont("Arial", 8, Font.ITALIC)));
	myPhrase.add(new Phrase("bold ", FontFactory.getFont("Arial", 8, Font.BOLD)));
	myPhrase.add(new Phrase("bolditalic", FontFactory.getFont("Arial", 8, Font.BOLDITALIC)));
	document.add(myPhrase);

	// step 5: we close the document
	document.close();
}
 
Example 2
Source File: JRPdfExporter.java    From jasperreports with GNU Lesser General Public License v3.0 5 votes vote down vote up
protected static synchronized void registerFonts ()
{
	if (!fontsRegistered)
	{
		List<PropertySuffix> fontFiles = JRPropertiesUtil.getInstance(DefaultJasperReportsContext.getInstance()).getProperties(PDF_FONT_FILES_PREFIX);//FIXMECONTEXT no default here and below
		if (!fontFiles.isEmpty())
		{
			for (Iterator<PropertySuffix> i = fontFiles.iterator(); i.hasNext();)
			{
				JRPropertiesUtil.PropertySuffix font = i.next();
				String file = font.getValue();
				if (file.toLowerCase().endsWith(".ttc"))
				{
					FontFactory.register(file);
				}
				else
				{
					String alias = font.getSuffix();
					FontFactory.register(file, alias);
				}
			}
		}

		List<PropertySuffix> fontDirs = JRPropertiesUtil.getInstance(DefaultJasperReportsContext.getInstance()).getProperties(PDF_FONT_DIRS_PREFIX);
		if (!fontDirs.isEmpty())
		{
			for (Iterator<PropertySuffix> i = fontDirs.iterator(); i.hasNext();)
			{
				JRPropertiesUtil.PropertySuffix dir = i.next();
				FontFactory.registerDirectory(dir.getValue());
			}
		}

		fontsRegistered = true;
	}
}
 
Example 3
Source File: PdfWatermarkUtils.java    From bamboobsc with Apache License 2.0 5 votes vote down vote up
public static void main(String args[]) throws Exception {
	FontFactory.register("fonts/fireflysung.ttf"); // fonts/fireflysung.ttf in fireflysung.jar
	Font font = FontFactory.getFont("fonts/fireflysung.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
	PdfReader pdfReader = new PdfReader("/tmp/ex/test.pdf");
	PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileOutputStream("/tmp/ex/test-out.pdf"));
	addWatermark(pdfStamper, font.getBaseFont(), Color.RED, "測試");
	pdfStamper.close();
}
 
Example 4
Source File: RegisterFontFactorytListener.java    From xdocreport.samples with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void contextInitialized(ServletContextEvent event) {
	System.out.println(FontFactory.getRegisteredFonts());
	System.out.println("------------------------------");
	System.out.println(FontFactory.getFont("times-roman").hashCode());
	String fontFolder = event.getServletContext().getRealPath("font");
	
	FontFactory.register(fontFolder+"/Aller_Rg.ttf","times-roman");
	FontFactory.register(fontFolder+"/Aller_BdIt.ttf","times-bolditalic");
	FontFactory.register(fontFolder+"/Aller_Bd.ttf","times-bold");
	FontFactory.register(fontFolder+"/Aller_It.ttf","times-italic");
	System.out.println(FontFactory.getFont("times-roman").hashCode());
	System.out.println(FontFactory.getRegisteredFonts());

}
 
Example 5
Source File: RegisterFontTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Registering fonts with the fontfactory.
 */
@Test
public void main() throws Exception {


	String liberationPath = "src/test/resources/liberation-fonts-ttf/";
	FontFactory.register(liberationPath + "LiberationMono-Regular.ttf");
	FontFactory.register(liberationPath + "LiberationSans-Regular.ttf");
	FontFactory.register(liberationPath + "LiberationSerif-Regular.ttf");

	// step 1: creation of a document-object
	Document document = new Document();

	// step 2: creation of the writer
	PdfWriter.getInstance(document, PdfTestBase.getOutputStream("registerfont.pdf"));

	// step 3: we open the document
	document.open();

	// step 4: we add content to the document
	Font font0 = FontFactory.getFont(BaseFont.HELVETICA, BaseFont.WINANSI, 12);
	String text0 = "This is the quite popular built in font '" + BaseFont.HELVETICA + "'.";
	document.add(new Paragraph(text0, font0));
	Font font1 = FontFactory.getFont("LiberationMono", BaseFont.WINANSI, 12);
	String text1 = "This is the quite popular True Type font 'LiberationMono'.";
	document.add(new Paragraph(text1, font1));
	Font font2 = FontFactory.getFont("LiberationSans-Bold", BaseFont.WINANSI, 12);
	String text2 = "This is the quite popular True Type font 'LiberationSans-Bold'.";
	document.add(new Paragraph(text2, font2));
	Font font3 = FontFactory.getFont("LiberationSerif", BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 12);
	String text3 = "\u5951\u7d04\u8005\u4f4f\u6240\u30e9\u30a4\u30f3\uff11";
	document.add(new Paragraph(text3, font3));
	BufferedWriter out = new BufferedWriter(new OutputStreamWriter(PdfTestBase.getOutputStream("registered.txt")));
	out.write("These fonts were registered at the FontFactory:\r\n");
	for (Iterator i = FontFactory.getRegisteredFonts().iterator(); i.hasNext();) {
		out.write((String) i.next());
		out.write("\r\n");
	}
	out.write("\r\n\r\nThese are the families these fonts belong to:\r\n");
	for (Iterator i = FontFactory.getRegisteredFamilies().iterator(); i.hasNext();) {
		out.write((String) i.next());
		out.write("\r\n");
	}
	out.flush();
	out.close();

	// step 5: we close the document
	document.close();

}