com.itextpdf.text.Phrase类的使用及代码示例

x33g5p2x  于2022-01-26 转载在 其他  
字(10.9k)|赞(0)|评价(0)|浏览(135)

本文整理了Java中com.itextpdf.text.Phrase类的一些代码示例,展示了Phrase类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Phrase类的具体详情如下:
包路径:com.itextpdf.text.Phrase
类名称:Phrase

Phrase介绍

[英]A Phrase is a series of Chunks.

A Phrase has a main Font, but some chunks within the phrase can have a Font that differs from the main Font. All the Chunks in a Phrase have the same leading.

Example:

// When no parameters are passed, the default leading = 16 
Phrase phrase0 = new Phrase(); 
Phrase phrase1 = new Phrase("this is a phrase"); 
// In this example the leading is passed as a parameter 
Phrase phrase2 = new Phrase(16, "this is a phrase with leading 16"); 
// When a Font is passed (explicitly or embedded in a chunk), the default leading = 1.5 * size of the font 
Phrase phrase3 = new Phrase("this is a phrase with a red, normal font Courier, size 12", FontFactory.getFont(FontFactory.COURIER, 12, Font.NORMAL, new Color(255, 0, 0))); 
Phrase phrase4 = new Phrase(new Chunk("this is a phrase")); 
Phrase phrase5 = new Phrase(18, new Chunk("this is a phrase", FontFactory.getFont(FontFactory.HELVETICA, 16, Font.BOLD, new Color(255, 0, 0)));

[中]APhrase是一系列Chunks。
一个Phrase有一个主Font,但是短语中的一些块可以有一个与主Font不同的FontPhrase中的所有Chunk都有相同的leading
例子:

// When no parameters are passed, the default leading = 16 
Phrase phrase0 = new Phrase(); 
Phrase phrase1 = new Phrase("this is a phrase"); 
// In this example the leading is passed as a parameter 
Phrase phrase2 = new Phrase(16, "this is a phrase with leading 16"); 
// When a Font is passed (explicitly or embedded in a chunk), the default leading = 1.5 * size of the font 
Phrase phrase3 = new Phrase("this is a phrase with a red, normal font Courier, size 12", FontFactory.getFont(FontFactory.COURIER, 12, Font.NORMAL, new Color(255, 0, 0))); 
Phrase phrase4 = new Phrase(new Chunk("this is a phrase")); 
Phrase phrase5 = new Phrase(18, new Chunk("this is a phrase", FontFactory.getFont(FontFactory.HELVETICA, 16, Font.BOLD, new Color(255, 0, 0)));

代码示例

代码示例来源:origin: org.technbolts/gutenberg

public static Function<PageInfos, Phrase> create(final Styles styles,
                         final Object fontKey,
                         final String firstPageTemplate,
                         final String otherPageTemplate,
                         final Phrase otherPage) {
  return new Function<PageInfos, Phrase>() {
    @Override
    public Phrase apply(PageInfos pageInfos) {
      Font font = styles.getFontOrDefault(fontKey);
      if (pageInfos.getRawPageNumber() == 1) {
        if (firstPageTemplate != null) {
          return new Phrase(firstPageTemplate, font);
        }
      } else {
        if (otherPage != null)
          return otherPage;
        else if (otherPageTemplate != null) {
          String text = otherPageTemplate;
          text = text.replace("${chapterTitle}", defaultString(pageInfos.chapterTitle()));
          text = text.replace("${sectionTitle}", defaultString(pageInfos.sectionTitle()));
          return new Phrase(text, font);
        }
      }
      return null;
    }
  };
}

代码示例来源:origin: com.itextpdf/itextg

/**
 * Process the text so that it will render with a combination of fonts
 * if needed.
 * @param text the text
 * @return a <CODE>Phrase</CODE> with one or more chunks
 */
public Phrase process(String text) {
  if (fonts.size() == 0)
    throw new IndexOutOfBoundsException(MessageLocalization.getComposedMessage("no.font.is.defined"));
  char cc[] = text.toCharArray();
  int len = cc.length;
  StringBuffer sb = new StringBuffer();
  Phrase ret = new Phrase();
  currentFont = null;
  for (int k = 0; k < len; ++k) {
    Chunk newChunk = processChar(cc, k, sb);
    if (newChunk != null) {
      ret.add(newChunk);
    }
  }
  if (sb.length() > 0) {
    Chunk ck = new Chunk(sb.toString(), currentFont != null ? currentFont : fonts.get(0));
    ret.add(ck);
  }
  return ret;
}

代码示例来源:origin: com.itextpdf/itextpdf

/**
 * Copy constructor for <CODE>Phrase</CODE>.
 * @param phrase the Phrase to copy
 */
public Phrase(final Phrase phrase) {
  super();
  this.addAll(phrase);
  setLeading(phrase.getLeading(), phrase.getMultipliedLeading());
  font = phrase.getFont();
  tabSettings = phrase.getTabSettings();
  setHyphenation(phrase.getHyphenation());
}

代码示例来源:origin: com.itextpdf/itextpdf

/**
 * Adds an <CODE>Element</CODE> to the <CODE>Paragraph</CODE>.
 *
 * @param    o the element to add.
 * @return true is adding the object succeeded
 */
@Override
public boolean add(Element o) {
  if (o instanceof List) {
    List list = (List) o;
    list.setIndentationLeft(list.getIndentationLeft() + indentationLeft);
    list.setIndentationRight(indentationRight);
    return super.add(list);
  }
  else if (o instanceof Image) {
    super.addSpecial(o);
    return true;
  }
  else if (o instanceof Paragraph) {
    super.addSpecial(o);
    return true;
  }
  return super.add(o);
}

代码示例来源:origin: org.technbolts/gutenberg

Phrase p = new Phrase();
p.addAll(head);

代码示例来源:origin: com.itextpdf/itextpdf

Phrase p = new Phrase(true);
p.setLeading(leading);
p.font = font;
if (font.getFamily() != FontFamily.SYMBOL && font.getFamily() != FontFamily.ZAPFDINGBATS && font.getBaseFont() == null) {
    if (index > 0) {
      String firstPart = string.substring(0, index);
      p.add(new Chunk(firstPart, font));
      string = string.substring(index);
      string = string.substring(1);
    p.add(new Chunk(buf.toString(), symbol));
  p.add(new Chunk(string, font));

代码示例来源:origin: com.itextpdf/itextpdf

return addChunk((Chunk) element);
case Element.PHRASE:
case Element.PARAGRAPH:
    e = (Element) element2;
    if (e instanceof Chunk) {
      success &= addChunk((Chunk)e);
      success &= this.add(e);

代码示例来源:origin: com.itextpdf/itextpdf

/**
 * Adds a collection of <CODE>Chunk</CODE>s
 * to this <CODE>Phrase</CODE>.
 *
 * @param    collection    a collection of <CODE>Chunk</CODE>s, <CODE>Anchor</CODE>s and <CODE>Phrase</CODE>s.
 * @return    <CODE>true</CODE> if the action succeeded, <CODE>false</CODE> if not.
 * @throws    ClassCastException    when you try to add something that isn't a <CODE>Chunk</CODE>, <CODE>Anchor</CODE> or <CODE>Phrase</CODE>
 */
@Override
public boolean addAll(final Collection<? extends Element> collection) {
  for (Element e: collection) {
    this.add(e);
  }
  return true;
}

代码示例来源:origin: stackoverflow.com

Phrase content = new Phrase("Blah blah blah", Font);

Float fontSize = content.getFont().getSize();
Float capHeight = content.getFont().getBaseFont().getFontDescriptor(BaseFont.CAPHEIGHT, fontSize);

Float padding = 5f;    

PdfPCell cell = new PdfPCell(content);
cell.setPadding(padding);
cell.setPaddingTop(capHeight - fontSize + padding);

代码示例来源:origin: org.technbolts/gutenberg

@SuppressWarnings("unchecked")
@Override
public void process(int level, Node node, InvocationContext context) {
  TreeNavigation nav = context.treeNavigation();
  boolean isHeaderCell = nav.ancestorTreeMatches(TableCellNode.class, TableRowNode.class, TableHeaderNode.class);
  CellStyler cellStyler = context.peekCellStyler();
  context.pushFont(cellStyler.cellFont());
  List<Element> elements = context.collectChildren(level, node);
  context.popFont();
  Phrase phrase = new Phrase();
  phrase.addAll(elements);
  int colspan = ((TableCellNode) node).getColSpan();
  PdfPCell cell = isHeaderCell ? headerCell(phrase) : new PdfPCell(phrase);
  cell.setColspan(colspan);
  cellStyler.applyStyle(cell);
  context.append(cell);
}

代码示例来源:origin: com.itextpdf/itextg

Phrase p = new Phrase(true);
p.setLeading(leading);
p.font = font;
if (font.getFamily() != FontFamily.SYMBOL && font.getFamily() != FontFamily.ZAPFDINGBATS && font.getBaseFont() == null) {
    if (index > 0) {
      String firstPart = string.substring(0, index);
      p.add(new Chunk(firstPart, font));
      string = string.substring(index);
      string = string.substring(1);
    p.add(new Chunk(buf.toString(), symbol));
  p.add(new Chunk(string, font));

代码示例来源:origin: com.itextpdf/itextg

/**
 * Adds an <CODE>Element</CODE> to the <CODE>Paragraph</CODE>.
 *
 * @param    o the element to add.
 * @return true is adding the object succeeded
 */
@Override
public boolean add(Element o) {
  if (o instanceof List) {
    List list = (List) o;
    list.setIndentationLeft(list.getIndentationLeft() + indentationLeft);
    list.setIndentationRight(indentationRight);
    return super.add(list);
  }
  else if (o instanceof Image) {
    super.addSpecial(o);
    return true;
  }
  else if (o instanceof Paragraph) {
    super.addSpecial(o);
    return true;
  }
  return super.add(o);
}

代码示例来源:origin: com.itextpdf/itextg

return addChunk((Chunk) element);
case Element.PHRASE:
case Element.PARAGRAPH:
    e = (Element) element2;
    if (e instanceof Chunk) {
      success &= addChunk((Chunk)e);
      success &= this.add(e);

代码示例来源:origin: com.itextpdf/itextg

/**
 * Adds a collection of <CODE>Chunk</CODE>s
 * to this <CODE>Phrase</CODE>.
 *
 * @param    collection    a collection of <CODE>Chunk</CODE>s, <CODE>Anchor</CODE>s and <CODE>Phrase</CODE>s.
 * @return    <CODE>true</CODE> if the action succeeded, <CODE>false</CODE> if not.
 * @throws    ClassCastException    when you try to add something that isn't a <CODE>Chunk</CODE>, <CODE>Anchor</CODE> or <CODE>Phrase</CODE>
 */
@Override
public boolean addAll(final Collection<? extends Element> collection) {
  for (Element e: collection) {
    this.add(e);
  }
  return true;
}

代码示例来源:origin: com.itextpdf/itextpdf

/**
 * Adds a <CODE>Chunk</CODE> to the current text array. Will not have any
 * effect if addElement() was called before.
 *
 * @param chunk the text
 */
public void addText(final Chunk chunk) {
  if (chunk == null || composite) {
    return;
  }
  addText(new Phrase(chunk));
}

代码示例来源:origin: com.itextpdf/itextpdf

/**
 * Process the text so that it will render with a combination of fonts
 * if needed.
 * @param text the text
 * @return a <CODE>Phrase</CODE> with one or more chunks
 */
public Phrase process(String text) {
  if (getSize() == 0)
    throw new IndexOutOfBoundsException(MessageLocalization.getComposedMessage("no.font.is.defined"));
  char cc[] = text.toCharArray();
  int len = cc.length;
  StringBuffer sb = new StringBuffer();
  Phrase ret = new Phrase();
  currentFont = null;
  for (int k = 0; k < len; ++k) {
    Chunk newChunk = processChar(cc, k, sb);
    if (newChunk != null) {
      ret.add(newChunk);
    }
  }
  if (sb.length() > 0) {
    Chunk ck = new Chunk(sb.toString(), currentFont != null ? currentFont : getFont(0));
    ret.add(ck);
  }
  return ret;
}

代码示例来源:origin: org.technbolts/gutenberg

@Override
  public void process(int level, Node node, InvocationContext context) {
    ExpLinkNode linkNode = (ExpLinkNode) node;
    String url = context.variableResolver().resolve(linkNode.url);

    Font anchorFont = new FontCopier(context.peekFont())
        .style(Font.UNDERLINE)
        .color(Colors.DARK_RED)
        .get();

    context.pushFont(anchorFont);
    List<Element> subs = context.collectChildren(level, node);
    context.popFont();

    Phrase p = new Phrase();
    p.addAll(subs);

    Anchor anchor = new Anchor(p);
    anchor.setReference(url);
    context.append(anchor);
  }
}

代码示例来源:origin: com.itextpdf/itextg

/**
 * Copy constructor for <CODE>Phrase</CODE>.
 * @param phrase the Phrase to copy
 */
public Phrase(final Phrase phrase) {
  super();
  this.addAll(phrase);
  setLeading(phrase.getLeading(), phrase.getMultipliedLeading());
  font = phrase.getFont();
  tabSettings = phrase.getTabSettings();
  setHyphenation(phrase.getHyphenation());
}

代码示例来源:origin: com.itextpdf/itextg

/**
 * Adds a cell element.
 *
 * @param text the text for the cell
 */
public void addCell(final String text) {
  addCell(new Phrase(text));
}

代码示例来源:origin: com.centurylink.mdw/mdw-common

private void printVariables(Chapter chapter, List<Variable> variables, int parentLevel) {
  Paragraph sTitle = new Paragraph("Process Variables", sectionFont);
  Section section = chapter.addSection(sTitle, parentLevel == 0 ? 0 : (parentLevel + 1));
  com.itextpdf.text.List list = new com.itextpdf.text.List(false, false, 10.0f);
  for (Variable var : variables) {
    Phrase phrase = new Phrase();
    phrase.add(new Chunk(var.getName(), fixedWidthFont));
    String v = var.getType();
    if (var.getDescription() != null)
      v += " (" + var.getDescription() + ")";
    phrase.add(new Chunk(": " + v + "\n", normalFont));
    list.add(new ListItem(phrase));
  }
  section.add(list);
}

相关文章