2013-07-18 57 views
0

我正在閱讀.txt文件中的一些文本並將它們寫入到pdf文件中。但在我的PDF文件中,文本顯示爲一些較大的字體。儘管我已經將.txt文件的字體大小更改爲更大或更小,但pdf中的字體將相同。如何在寫入pdf文件時設置字體大小

部分的我的代碼,在文本文件中的工作與閱讀寫作

Document document = new Document(PageSize.A4.rotate(), 36, 36, 80, 160); 
........ 
......... 
      document.newPage(); 

      //Writing in a default page from the txt file 

      document.add(new com.lowagie.text.Paragraph("\n")); 
      iStream = new FileInputStream(path1); //path1 is location of .txt file 
      in = new DataInputStream(iStream); 
      is = new InputStreamReader(in); 
      br = new BufferedReader(is); 
      String strLine; 
      while ((strLine = br.readLine()) != null) 
      { 
       Paragraph para = new com.lowagie.text.Paragraph(strLine + "\n"); 
       para.setAlignment(Element.ALIGN_JUSTIFIED); 
       document.add(new com.lowagie.text.Paragraph(strLine + "\n")); 

      } 
      br.close(); 
      is.close(); 
      in.close(); 
      iStream.close(); 
      document.close(); 
      writer.close(); 

對不起,我利用iText的低版本,因爲現在我無法將任何第三方庫添加到我的偏東。這個版本的itext已經是我的Windchill了。

如何在寫入pdf文件時設置字體大小。任何幫助都會非常有幫助。

回答

2

很簡單:d

拳,我會改變一兩件事:

while ((strLine = br.readLine()) != null) { 
    Paragraph para = new com.lowagie.text.Paragraph(strLine + "\n"); 
    para.setAlignment(Element.ALIGN_JUSTIFIED); 
    document.add(para); 
} 

這樣,該段在文檔中合理的。你這樣做的方式,你改變了一個paragaph對象的文本對齊方式,但是然後向PDF添加了一個完全不同的新對象。

我們您的問題:
可以在每個Paragraph`構造器指定Font如下:

Paragraph para = new com.lowagie.text.Paragraph(strLine + "\n", FontFactory.getFont("Arial", 14f /*This is the size, but as a float!*/); 

的lowagie API的完整文檔可以發現here

最好的問候

+0

在哪裏可以傳遞字體大小到上面? –

+0

UPS持有,讓我改正這一點。根據lowagie中實現的FontFactory的文檔,這應該起作用。 – AndMim

相關問題