2011-12-13 92 views
4

我想在PDF中插入一段包含粗體和非粗體區域的文本,但我不知道該怎麼做?合併兩個段落對象

我正在使用iText5(java)。

這是我的代碼:

public class CreatePdf{ 
    private Font bigFont = FontFactory.getFont(FontFactory.HELVETICA, "Windows-1254", 12, Font.BOLD, new Color(0, 0, 0)); 
    private Font smallFont = FontFactory.getFont(FontFactory.HELVETICA, "Windows-1254", 8, Font.NORMAL, new Color(0, 0, 0)); 

    public void create(){ 
     Paragraph parag1=new Paragraph("Number: ",bigFont);//This gonna be bold font 
     Paragraph parag2=new Paragraph("12", smallFont); //This gonna be normal font 

     //Create one paragraph from these two paragraphs. But How ? 
    } 
} 
+0

我找到了解決方案:'段落梳=新的段落(); comb.add(new Chunk(parag1))com.add(new Chunk(parag2))'; – olyanren

回答

6

我找到了解決辦法:

public class CreatePdf{ 
    private Font bigFont = FontFactory.getFont(FontFactory.HELVETICA, "Windows-1254", 12, Font.BOLD, new Color(0, 0, 0)); 
    private Font smallFont = FontFactory.getFont(FontFactory.HELVETICA, "Windows-1254", 8, Font.NORMAL, new Color(0, 0, 0)); 

    public void create(){ 
     Paragraph parag1=new Paragraph("Number: ",bigFont);//This gonna be bold font 
     Paragraph parag2=new Paragraph("12", smallFont); //This gonna be normal font 
     Paragraph comb=new Paragraph(); 
     comb.add(new Chunk(parag1)) 
     comb.add(new Chunk(parag2)); 
    } 
} 
+1

接受它!順便說一句,你只是寫'comb.add(new Chunk(text,font));' – sdespolit

4

您可以簡單:

comb.add(parag1); 
comb.add(parag2); 

塊是沒有必要的。

+0

在5的更高版本中,這是唯一有效的方法,因爲你不能再從段落或短語中創建塊 – simpleuser