合并两个Paragraph对象

时间:2011-12-13 08:19:12

标签: 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 ?
   }
}

3 个答案:

答案 0 :(得分: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 :(得分:4)

你可以更简单:

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

没有必要使用Chunk。

答案 2 :(得分:0)

import numpy as np
import matplotlib.pyplot as plt

# Initialise time
t_1 = np.arange(1,20,1)/100
t_2 = np.arange(21,40,1)/100

# Compute v(t)
v_1 = np.where(t_1<0.2, 20, 0)
v_2 = np.where(t_2<0.2, 20, 0)

# Compute f(t)
f_1 = np.where(t_1<=0.2, 20*t_1, 4)
f_2 = np.where(t_2<=0.2, 20*t_2, 4)

# Plotting as you are doing