合并两个Paragraph对象

oly*_*ren 4 java itext

我想在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 ?
   }
}
Run Code Online (Sandbox Code Playgroud)

oly*_*ren 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)); 
   }
}
Run Code Online (Sandbox Code Playgroud)

  • 接受!顺便说一句,你只写了`comb.add(new Chunk(text, font));` (2认同)

ric*_*ics 5

你可以更简单:

comb.add(parag1);
comb.add(parag2);
Run Code Online (Sandbox Code Playgroud)

块不是必需的。