Using the TextFrame and Paragraph components: paragraphs that mix fonts and colors, numbered paragraphs and highlighted keywords.
Example_14.pdf — the PDF this example creates.
package examples;
import java.io.*;
import java.util.*;
import com.pdfjet.*;
import com.pdfjet.fonts.*;
/**
* Example_14.java
*/
public class Example_14 {
public Example_14() throws Exception {
PDF pdf = new PDF(
new BufferedOutputStream(
new FileOutputStream("Example_14.pdf")));
pdf.setCompliance(Compliance.PDF_UA_1);
pdf.setTitle("Paragraphs in a Text Frame");
Font f1 = new Font(pdf, IBMPlexSans.Regular);
f1.setSize(10f);
Font f2 = new Font(pdf, IBMPlexSans.Bold);
f2.setSize(10f);
Font f3 = new Font(pdf, IBMPlexSans.Italic);
f3.setSize(10f);
Page page = new Page(pdf, Letter.PORTRAIT);
List<Paragraph> paragraphs = new ArrayList<Paragraph>();
Paragraph paragraph = new Paragraph()
.add(new TextLine(f1,
"The small business centres offer practical resources, from step-by-step info on setting up your business to sample business plans to a range of business-related articles and books in our resource libraries.")
.setUnderline(true))
.add(new TextLine(f2, "This text is bold!").setTextColor(Color.blue));
paragraphs.add(paragraph);
paragraph = new Paragraph()
.add(new TextLine(f1,
"The centres also offer free one-on-one consultations with business advisors who can review your business plan and make recommendations to improve it.")
.setUnderline(true))
.add(new TextLine(f3, "This text is using italic font.").setTextColor(Color.green));
paragraphs.add(paragraph);
// The two paragraphs are a numbered list. The number of a paragraph
// is its label, which the text frame draws to the left of it and
// reads before it.
int paragraphNumber = 1;
for (Paragraph p : paragraphs) {
p.setListLabel(new TextLine(f2, paragraphNumber + "."), 15f);
paragraphNumber++;
}
TextFrame text = new TextFrame(paragraphs);
text.setLocation(70f, 50f);
text.setWidth(500f);
text.setBorders(true);
text.setBorderColor(Color.blue);
text.drawOn(page);
Map<String, Integer> colorMap = new HashMap<String, Integer>();
colorMap.put("Physics", Color.firebrick);
colorMap.put("physics", Color.firebrick);
colorMap.put("Experimentation", Color.sienna);
colorMap.put("science", Color.blue);
paragraphs = Paragraph.paragraphsFromFile(f1, "data/physics.txt");
// The paragraphs under each heading are a numbered list.
paragraphNumber = 1;
for (Paragraph p : paragraphs) {
if (p.startsWith("**")) {
p.setStructureType(StructElem.H1);
p.getTextLines().get(0).setFont(f2).setFontSize(24f);
p.getTextLines().get(0).setTextColor(Color.navy);
paragraphNumber = 1;
} else {
p.setTextColor(Color.dimgray);
p.setHighlightColors(colorMap);
p.setListLabel(new TextLine(f2, paragraphNumber + "."), 15f);
paragraphNumber++;
}
}
text = new TextFrame(paragraphs);
text.setLocation(70f, 150f);
text.setWidth(500f);
text.setBorders(true);
text.setBorderColor(Color.blue);
text.drawOn(page);
// The rule beside each paragraph reaches from its top to its bottom,
// which the frame knows only after it has drawn it.
for (Paragraph p : paragraphs) {
if (!p.startsWith("**")) {
new Line(p.getX1() - 3f, p.getY1(), p.getX1() - 3f, p.getY2())
.setStrokeColor(Color.navy)
.setStrokeWidth(1f).drawOn(page);
}
}
pdf.complete();
}
public static void main(String[] args) throws Exception {
long time0 = System.currentTimeMillis();
new Example_14();
long time1 = System.currentTimeMillis();
System.out.printf("Example_14 => %4d ms%n", time1 - time0);
}
} // End of Example_14.java
©2026 PDFjet Software.