Java: Set the Font Color for the Text String on PDF

When you are drawing text into a PDF, you may need to define colorful brushes or pens in order to make the page more vivid. This article shows how to set the text string’s color space in a PDF document using Spire.PDF for Java.

Install Spire.PDF for Java

First, you need to add the Spire.Pdf.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file by adding the following code to your project's pom.xml file.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.pdf</artifactId>
        <version>10.10.7</version>
    </dependency>
</dependencies>
    

Set the Font Color for the Text String on PDF

Spire.PDF for Java offers PdfSolidBrush class to set the brush color for the text. It supports to define the brush color based on a particular RGB color space or a HTML color code.

  • Create a PdfDocument object.
  • Add a new page in the PDF using PdfDocument.getPages().add() method.
  • Create a PdfSolidBrush object based on a particular RGB color space or a HTML color code.
  • Create an object of PdfTrueTypeFont to set the font name, size and style.
  • Draw text on the page at the specified location using PdfPageBase.getCanvas().drawString() method.
  • Save the document to another PDF using PdfDocument.saveToFile() method.
  • Java
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.graphics.PdfRGBColor;
import com.spire.pdf.graphics.PdfSolidBrush;
import com.spire.pdf.graphics.PdfTrueTypeFont;

import java.awt.*;

       public class pdfBrush {
            public static void main(String[] args) throws Exception {


                //Create a PdfDocument object
                PdfDocument doc = new PdfDocument();
                //Add a page
                PdfPageBase page = doc.getPages().add();

                //Set the location
                float y = 30;

                //Create solid brush object and define the color
                PdfRGBColor rgb1 = new PdfRGBColor(Color.green);
                PdfSolidBrush brush1 = new PdfSolidBrush(rgb1);

                //RGB Color
                PdfRGBColor rgb2 = new PdfRGBColor(0,197,205);
                PdfSolidBrush brush2 = new PdfSolidBrush(rgb2);

                //HTML code color
                Color color = Color.decode("#A52A2A");
                PdfSolidBrush brush3 = new PdfSolidBrush(new PdfRGBColor(color));

                //Create true type font object
                Font font = new Font("Arial", java.awt.Font.BOLD, 14);
                PdfTrueTypeFont trueTypeFont = new PdfTrueTypeFont(font);

                //Draw text
                page.getCanvas().drawString("Set the text color with brush", trueTypeFont, brush1, 0, (y = y + 30f));
                page.getCanvas().drawString("Set the text color with RGB", trueTypeFont, brush2, 0, (y = y + 50f));
                page.getCanvas().drawString("Set the text color with HTML code color", trueTypeFont, brush3, 0, (y = y + 60f));

                //Save to file
                doc.saveToFile("output/CreatePdf.pdf");

            }
        }

Java: Set the Font Color for the Text String on PDF

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.