How to render Colored Text in Apache PDFBox

stuffel

Yes, it seems a weird question, but I was not able to render a colored text in PDFBox.

Usually the code for generating text looks like that:

//create some document and page...
PDDocument document = new PDDocument();
PDPage page = new PDPage(PDRectangle.A4);

//defined some font
PDFont helveticaRegular = PDType1Font.HELVETICA;

//content stream for writing the text
PDPageContentStream contentStream = new PDPageContentStream(document, page);

contentStream.beginText();
contentStream.setFont(helveticaRegular, 16);
contentStream.setStrokingColor(1f,0.5f,0.2f);
contentStream.newLineAtOffset(64, page.getMediaBox().getUpperRightY() - 64);
contentStream.showText("The hopefully colored text");
contentStream.endText();

//closing the stream
contentStream.close();

[...] //code for saving and closing the document. Nothing special

Interestingly the setStrokingColor is the only method accepting colors on the stream. So I thought this is the way to color something in PDFBox.

BUT: I'm not getting any color to the text. So I guess this is a method for other types of content.

Does anybody know how to achieve a colored text in PDFBox?

mkl

You use

contentStream.setStrokingColor(1f,0.5f,0.2f);

But in PDFs text by default is not drawn by stroking a path but by filling it. Thus, you should try

contentStream.setNonStrokingColor(1f,0.5f,0.2f);

instead.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to move to the next line when adding text using Apache PDFBox

From Dev

How to extract text from a PDF file with Apache PDFBox

From Dev

How to make PDF text selectable using Apache PDFBox?

From Dev

How to print html text in pdf using apache pdfbox

From Dev

Apache PDFBox PDFTextStripper access text parts of the page, how can I?

From Dev

How to extract label text from Push button using Apache PDFBox?

From Dev

How to send a colored text message?

From Dev

Java: Apache PDFbox Extract highlighted text

From Dev

Apache PdfBox Rotate Crop Box Only Not Text

From Dev

How to render part of a page as an image using PDFBox?

From Dev

How to create Table using Apache PDFBox

From Dev

How to split a PDF using Apache PDFBox?

From Dev

How to split a PDF using Apache PDFBox?

From Dev

How to clip a background on a colored text (only in css)

From Dev

How to select specific colored text in a RichTextBox control?

From Dev

How do you render a cube with different colored sides in OpenGL?

From Dev

Using Apache PDFBox for extracting text getting wrong charecters?

From Dev

Apache PDFBox 2.0 - Text is not shown in created PDF file

From Dev

Remove OCR text from PDF document using Apache PDFBox

From Dev

PDFBox 2.0: how to detect bold text here

From Dev

pdfbox for pdf creation: how to do the text layout?

From Dev

How to render html as text

From Dev

How to move image to the top of the PDF page using Apache PDFBox?

From Dev

How to generate multiple lines in PDF using Apache pdfbox

From Dev

How to get colored pdf text output with docbook via dblatex?

From Dev

How to get Colored Build Output from Make in Sublime Text 3?

From Dev

How to change cell text to bold or colored if the date is today?

From Dev

How to make 2 colored text in a line in a marker infowindow?

From Dev

Apache PDFBox rotate PDImageXObject

Related Related

  1. 1

    How to move to the next line when adding text using Apache PDFBox

  2. 2

    How to extract text from a PDF file with Apache PDFBox

  3. 3

    How to make PDF text selectable using Apache PDFBox?

  4. 4

    How to print html text in pdf using apache pdfbox

  5. 5

    Apache PDFBox PDFTextStripper access text parts of the page, how can I?

  6. 6

    How to extract label text from Push button using Apache PDFBox?

  7. 7

    How to send a colored text message?

  8. 8

    Java: Apache PDFbox Extract highlighted text

  9. 9

    Apache PdfBox Rotate Crop Box Only Not Text

  10. 10

    How to render part of a page as an image using PDFBox?

  11. 11

    How to create Table using Apache PDFBox

  12. 12

    How to split a PDF using Apache PDFBox?

  13. 13

    How to split a PDF using Apache PDFBox?

  14. 14

    How to clip a background on a colored text (only in css)

  15. 15

    How to select specific colored text in a RichTextBox control?

  16. 16

    How do you render a cube with different colored sides in OpenGL?

  17. 17

    Using Apache PDFBox for extracting text getting wrong charecters?

  18. 18

    Apache PDFBox 2.0 - Text is not shown in created PDF file

  19. 19

    Remove OCR text from PDF document using Apache PDFBox

  20. 20

    PDFBox 2.0: how to detect bold text here

  21. 21

    pdfbox for pdf creation: how to do the text layout?

  22. 22

    How to render html as text

  23. 23

    How to move image to the top of the PDF page using Apache PDFBox?

  24. 24

    How to generate multiple lines in PDF using Apache pdfbox

  25. 25

    How to get colored pdf text output with docbook via dblatex?

  26. 26

    How to get Colored Build Output from Make in Sublime Text 3?

  27. 27

    How to change cell text to bold or colored if the date is today?

  28. 28

    How to make 2 colored text in a line in a marker infowindow?

  29. 29

    Apache PDFBox rotate PDImageXObject

HotTag

Archive