2017-08-16 153 views
0

我想使用自定義字體。我放置在資源文件夾中並嘗試訪問。但我無法訪問。我試圖無法在Java應用程序的pdfbox中加載自定義字體

  PDPage page = new PDPage(); 
      doc.addPage(page); 
      String dir = "../resources/font/raleway/"; 
      PDType0Font font1BoldRaleway = PDType0Font.load(doc, new File(dir + "Raleway-Bold.ttf")); 

請建議,在那裏我應該將這些字體以及如何加載它。我正在獲取字體路徑不正確。

回答

0

如果字體是在類路徑中,你可以使用它像這樣

InputStream fontInputStream = getClass().getClassLoader().getResourceAsStream(trueTypeFont.getFile()); return PDType0Font.load(doc, fontInputStream);

其中File trueTypeFont = new File("font/raleway/Raleway-Bold.ttf");

+0

trueTypeFont.getFile() - 這條線給不兼容的類型。文件不能轉換爲java.lang.String。 – user1213199

+0

我怎樣才能把字體放在classpath中。我的意思是我應該放哪? – user1213199

相關問題