TextFragment Class
Contains text content that can be added to a PDF page.
Declaration
public class TextFragment extends VisualFragment
Remarks
The following code snippet adds a text fragment with custom formatting:
import com.devexpress.docs.pdf.*;
import com.devexpress.drawing.*;
import com.devexpress.system.drawing.*;
import com.devexpress.drawing.printing.*;
import java.nio.channels.*;
import java.nio.file.*;
public class Main {
public static void main(String[] args) throws Exception {
// Create a new PDF document.
try (PdfDocument pdfDocument = new PdfDocument()) {
// Add a Letter-size page to the document.
Page page = pdfDocument.getPages().add(DXPaperKind.LETTER);
TextFragment text = new TextFragment();
text.setFont(new TextFont("Courier New"));
text.setLocation(new PointF(10, 740));
text.setText("The PDF Document API");
text.setFontSize(18);
page.addFragment(text);
// Save the document to a PDF file.
try (WritableByteChannel writableByteChannel =
FileChannel.open(Path.of("result.pdf"),
StandardOpenOption.CREATE,
StandardOpenOption.WRITE,
StandardOpenOption.TRUNCATE_EXISTING)) {
pdfDocument.save(writableByteChannel);
}
}
}
}
Refer to the following help topic for additional information: Add Text to PDF Documents.
Inherited Members
Inheritance
TextFragment()
Initializes a new instance of the TextFragment class.
Declaration
public TextFragment()
Methods
getCharacterSpacing() Method
Returns a character spacing value.
Declaration
public @Nullable Double getCharacterSpacing()
Returns
| Type | Description |
|---|---|
The character spacing value. |
getFont() Method
Returns the font used to draw the text.
Declaration
public TextFont getFont()
Returns
| Type | Description |
|---|---|
| TextFont | The font. |
getFontSize() Method
Returns the font size.
Declaration
public double getFontSize()
Returns
| Type | Description |
|---|---|
| double | The font size. |
getForegroundFill() Method
Returns the fill used to draw the text.
Declaration
public Fill getForegroundFill()
Returns
| Type | Description |
|---|---|
| Fill | The fill (foreground color). |
getHorizontalScaling() Method
Returns the horizontal scaling value.
Declaration
public @Nullable Double getHorizontalScaling()
Returns
| Type | Description |
|---|---|
The horizontal scaling value. |
getLineWidth() Method
Returns the line width used to draw text outlines.
Declaration
public @Nullable Double getLineWidth()
Returns
| Type | Description |
|---|---|
The line width. |
getStrikeout() Method
Returns whether strikeout is enabled.
Declaration
public boolean getStrikeout()
Returns
| Type | Description |
|---|---|
| boolean |
|
getStrikeoutFill() Method
Returns the fill used to draw strikeout lines.
Declaration
public Fill getStrikeoutFill()
Returns
| Type | Description |
|---|---|
| Fill | The fill used to draw strikeout lines. |
getText() Method
Returns the text content.
Declaration
public String getText()
Returns
| Type | Description |
|---|---|
| String | The text content. |
getTextLeading() Method
Returns the text leading value.
Declaration
public @Nullable Double getTextLeading()
Returns
| Type | Description |
|---|---|
The text leading value. |
getTextRenderingMode() Method
Returns the text rendering mode.
Declaration
public @Nullable TextRenderingMode getTextRenderingMode()
Returns
| Type | Description |
|---|---|
| TextRenderingMode | The text rendering mode. |
getTextRise() Method
Returns the text rise value.
Declaration
public @Nullable Double getTextRise()
Returns
| Type | Description |
|---|---|
The text rise value. |
getUnderline() Method
Returns whether underlining is enabled.
Declaration
public boolean getUnderline()
Returns
| Type | Description |
|---|---|
| boolean |
|
getUnderlineFill() Method
Returns the fill used to draw underline lines.
Declaration
public Fill getUnderlineFill()
Returns
| Type | Description |
|---|---|
| Fill | The fill used to draw underline lines. |
measure() Method
Calculates layout metrics for this text fragment.
Declaration
public LayoutMetrics measure()
Returns
| Type | Description |
|---|---|
| LayoutMetrics | Layout metrics for this text fragment. |
setCharacterSpacing(@Nullable Double value) Method
Sets the character spacing value.
Declaration
public void setCharacterSpacing(@Nullable Double value)
Parameters
| Name | Type | Description |
|---|---|---|
| value | The character spacing value. |
setFont(TextFont value) Method
Sets the font used to draw the text.
Declaration
public void setFont(TextFont value)
Parameters
| Name | Type | Description |
|---|---|---|
| value | TextFont | The font. |
setFontSize(double value) Method
Sets the font size.
Declaration
public void setFontSize(double value)
Parameters
| Name | Type | Description |
|---|---|---|
| value | double | The font size. |
setForegroundFill(Fill value) Method
Sets the fill used to draw the text.
Declaration
public void setForegroundFill(Fill value)
Parameters
| Name | Type | Description |
|---|---|---|
| value | Fill | The fill used to draw the text. |
setHorizontalScaling(@Nullable Double value) Method
Sets the horizontal scaling value.
Declaration
public void setHorizontalScaling(@Nullable Double value)
Parameters
| Name | Type | Description |
|---|---|---|
| value | The horizontal scaling value. |
setLineWidth(@Nullable Double value) Method
Sets the line width used to draw text outlines.
Declaration
public void setLineWidth(@Nullable Double value)
Parameters
| Name | Type | Description |
|---|---|---|
| value | The line width. |
setStrikeout(boolean value) Method
Sets whether strikeout is enabled.
Declaration
public void setStrikeout(boolean value)
Parameters
| Name | Type | Description |
|---|---|---|
| value | boolean |
|
setStrikeoutFill(Fill value) Method
Sets the fill used to draw strikeout lines.
Declaration
public void setStrikeoutFill(Fill value)
Parameters
| Name | Type | Description |
|---|---|---|
| value | Fill | The fill used to draw strikeout lines. |
setText(String value) Method
Sets the text content.
Declaration
public void setText(String value)
Parameters
| Name | Type | Description |
|---|---|---|
| value | String | The text content. |
setTextLeading(@Nullable Double value) Method
Sets the text leading value.
Declaration
public void setTextLeading(@Nullable Double value)
Parameters
| Name | Type | Description |
|---|---|---|
| value | The text leading value. |
setTextRenderingMode(@Nullable TextRenderingMode value) Method
Sets the text rendering mode.
Declaration
public void setTextRenderingMode(@Nullable TextRenderingMode value)
Parameters
| Name | Type | Description |
|---|---|---|
| value | TextRenderingMode | The text rendering mode. |
setTextRise(@Nullable Double value) Method
Sets the text rise value.
Declaration
public void setTextRise(@Nullable Double value)
Parameters
| Name | Type | Description |
|---|---|---|
| value | The text rise value. |
setUnderline(boolean value) Method
Sets whether underlining is enabled.
Declaration
public void setUnderline(boolean value)
Parameters
| Name | Type | Description |
|---|---|---|
| value | boolean |
|
setUnderlineFill(Fill value) Method
Sets the fill used to draw underline lines.
Declaration
public void setUnderlineFill(Fill value)
Parameters
| Name | Type | Description |
|---|---|---|
| value | Fill | The fill used to draw underline lines. |
split(PdfDocument document, int[] indices) Method
Splits this text fragment at the specified character indices.
Declaration
public TextFragment[] split(PdfDocument document, int[] indices)
Parameters
| Name | Type | Description |
|---|---|---|
| document | PdfDocument | The document that owns the resulting text fragments. |
| indices | int[] | Zero-based character indices at which to split the text. |
Returns
| Type | Description |
|---|---|
| TextFragment[] | An array of text fragments created by the split operation. |
split(TextMatchGroup group, TextFragment[][] matched, Runnable matched_sync, TextFragment[][] notMatched, Runnable notMatched_sync) Method
Splits this text fragment with the specified match group and outputs matched and unmatched fragments.
Declaration
public TextFragment[] split(TextMatchGroup group, TextFragment[][] matched, Runnable matched_sync, TextFragment[][] notMatched, Runnable notMatched_sync)
Parameters
| Name | Type | Description |
|---|---|---|
| group | TextMatchGroup | The match group that defines how to split the text. |
| matched | TextFragment[][] | An output container for fragments that match the group. |
| matched_sync | Runnable | A synchronization callback for the |
| notMatched | TextFragment[][] | An output container for fragments that do not match the group. |
| notMatched_sync | Runnable | A synchronization callback for the |
Returns
| Type | Description |
|---|---|
| TextFragment[] | An array of text fragments created by the split operation. |
split(TextMatchGroup group) Method
Splits this text fragment with the specified match group.
Declaration
public TextFragment[] split(TextMatchGroup group)
Parameters
| Name | Type | Description |
|---|---|---|
| group | TextMatchGroup | The match group that defines how to split the text. |
Returns
| Type | Description |
|---|---|
| TextFragment[] | An array of text fragments created by the split operation. |