5

I need your help in adding a custom font "arial.ttf" which is stored under the resources folder in my project in the FontFactory.register method in iText.

The font path is as follows in the project from Windows Explorer:

public_html\resources\fonts\arial.ttf

The code for referring to the font is:

FontFactory.register("/resources/fonts/arial.ttf", "my_bold_font");
Font myBoldFont = FontFactory.getFont("my_bold_font");

However when I ran the Java method, it always gives me the error:

java.io.IOException: /resources/fonts/arial.ttf not found as file or resource.

I tried with different paths for example:

/public_html/resources/fonts/arial.ttf

../resources/fonts/arial.ttf

/fonts/arial.ttf

/arial.ttf

But the result is the same that the file can't be found. So how to refer to the file?

99maas
  • 1,239
  • 12
  • 34
  • 59

4 Answers4

7

You can get the path of the 'font' which are present in the resources folder, using contextClassLoader and can be used in the FontFactory file path.

URL font_path = Thread.currentThread().getContextClassLoader().getResource("fontname");
FontFactory.register(font_path.toString(), "test_font");

I have tested this code and it works fine.

Root
  • 955
  • 1
  • 16
  • 39
1

The code was done by:

 FontFactory.register(System.getProperty("file.separator")+"resources"+System.getProperty("file.separator")+"fonts"+System.getProperty("file.separator")+"arial.‌​ttf", "my_bold_font");
 Font myBoldFont = FontFactory.getFont("my_bold_font");
99maas
  • 1,239
  • 12
  • 34
  • 59
0

I have tried this for adding all fonts that are stored in a folder "src/main/webapp/resources/fonts" to the XMLWorkerFontProvider.

It worked successfully.

XMLWorkerFontProvider fontImp = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);

URL font_path = Thread.currentThread().getContextClassLoader().getResource("resources/fonts");

File directory = new File(font_path.getPath());

//get all the files from a directory

File[] fList = directory.listFiles();

for (File file : fList){

    System.out.println("Font File Path******************************   " +file.getPath());
    fontImp.register(file.getPath());
} 
FontFactory.setFontImp(fontImp);

Document document = new Document();
Rectangle one = new Rectangle(width,height);
document.setPageSize(one);
document.setMargins(1, 1, 1, 1);
ByteArrayOutputStream bos1 = new ByteArrayOutputStream();
PdfWriter pdfWriter=PdfWriter.getInstance(document, new FileOutputStream(folderPath+"/"+fileName));
document.open();

InputStream is = new ByteArrayInputStream(html.getBytes());
//XMLWorkerHelper.getInstance().parseXHtml(pdfWriter,document, is);
XMLWorkerHelper.getInstance().parseXHtml(pdfWriter,document, is, null, null,fontImp);
document.close();
Tunaki
  • 132,869
  • 46
  • 340
  • 423
Harish
  • 425
  • 3
  • 7
  • 19
-1

I am using Spring Boot 2.2 with iText 5 (openpdf) and this code is working well when I execute the Spring Boot app.

import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.pdf.BaseFont;
import java.awt.*;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;

public class CustomFont {

    public static Font myFont;

    /**
     * This method should be loaded when you start the app for the first time.
     * @param resourceLoader The resource you can get using Spring @Autowired annotation and passing it to here.
     */
    public static void registerFont(ResourceLoader resourceLoader) {

        try {

            // Note the "classpath: " syntax.
            // I am storing my font in: src/main/resources/fonts/my-custom-font.ttf
            Resource resource = resourceLoader.getResource("classpath:/fonts/my-custom-font.ttf");
            FontFactory.register(resource.getURL().getPath(), "aliasCustomFontName");

        } catch (Exception e) {
            // When executing the unit tests, the font is not found so I am catching and ignoring it.
            // The fonts are not important for the unit tests in my case.
            e.printStackTrace();
        }

        myFont = FontFactory.getFont("aliasCustomFontName", BaseFont.WINANSI, true, 1, Font.NORMAL, Color.WHITE);
    }

    /**
     * You can use the custom font in your code with: CustomFont.myFont
     */

}