PDFOne (for Java)
Create, edit, view, print & enhance PDF documents and forms in Java SE/EE
J2SE J2EE Windows Linux Mac (OS X)

How To Set Transparency Levels For PDF Elements

Render text, shapes, and images in PDF with varying levels of transparency.
By Lokesh Vardhan Yadav .G and Santhanam L.

In this article, you will learn how to specify transparency levels for PDF elements such as text, shapes, and images.*

To render the above-mentioned PDF elements, PDFOne Java uses a pen and a brush. The pen is used to stroke text and shapes. The brush is used to fill text and shapes.

You can set the color for the pen and brush of a PDF page (PdfPage) or a PDF document (PdfDocument) object.

After you set a color for the pen and brush, PDF elements that you render on a page or document will be stroked and filled using that color.

While specifying a color for the pen and brush, you can also include an alpha value that would affect the transparency level with which the coloring operations of the pen and brush are performed.

This is how semi-transparent text and shapes are rendered. When rendering semi-transparent images, however, the alpha value of the color of the brush alone is used. The pen setting plays no role here.

Finally, here is a code snippet that shows how to render text, shapes and images with varying levels of transparency.

// Create a PDF document
PdfDocument doc = new PdfDocument();

try {
  // Create a Helvetica font instance
  PdfFont fontHelvetica;
  fontHelvetica = 
    PdfFont.create("Helvetica",             // font name
                   PdfFont.STROKE_AND_FILL, // pen and brush style
                   30,                      // size
                   PdfEncodings.WINANSI);   // encoding
  // Set font size and color

  // Create an image instance
  PdfImage logoImage = PdfImage.create("logo.png");

  // Create three pages
  PdfPage page1 = new PdfPage();
  PdfPage page2 = new PdfPage();
  PdfPage page3 = new PdfPage();

  String transparentText = "This text is transparent.";

  int verticalOffset = 0;
  float transparencyChange = 0.0f;
  double imageVerticalOffset = 0.0;

  Color penColor = null;
  Color brushColor = null;

  // Write transparent text on the three pages
  page1.writeText("Rendering Transparent Text", // text
                  fontHelvetica,                // font
                  PdfTextFormatter.CENTER,      // alignment
                  0,                            // x-coordinate
                  50);                          // y-coordinate
  page2.writeText("Rendering Transparent Shapes", 
                  0, 50);
  page3.writeText("Rendering Transparent Images", 
                  0, 50);

  // Change font size and color

  for (int i = 0; i < 5; i++) {
    // Create new brush and pen color
    brushColor = new Color(1.0f,                  // red
                           1.0f,                  // green
                           0.0f,                  // blue
                           1.0f - transparencyChange);  // alpha
    penColor = new Color(0.0f, 1.0f, 0.0f, 1.0f - transparencyChange);
    // Set brush and pen colors of page 1
    // Write transparent text on page 1
      130 + verticalOffset, 
    // Set brush and pen colors of page 2
    // Draw transparent shapes on page 2
    page2.drawEllipse(150, 130 + verticalOffset, 230, 160 + verticalOffset, true, true);
    page2.drawSquare(300, 130 + verticalOffset, 50, true, true);
    page2.drawCircle(450, 155 + verticalOffset, 25, true, true);
    // Set brush color of page 3
    // Pen settings not required when drawing filled images
    // Draw transparent images on page 3
      logoImage,                  // image
      (page3.getWidth() -         // x-coordinate
      130 + imageVerticalOffset); // y-coordinate

    // Change offsets for next iteration
    imageVerticalOffset = imageVerticalOffset + logoImage.height() + 2;
    verticalOffset += 60;

    // Change transparency level for next iteration
    transparencyChange += 0.2f;

  // Add the pages to the document

  // Save the document to file
} catch (IOException | PdfException e) {
  // TODO Auto-generated catch block


* - This article is a PDFOne Java version of an earlier article written in March 2007 for our PDFOne .NET users.


Our .NET Developer Tools
Gnostice Document Studio .NET

Multi-format document-processing component suite for .NET developers.


A .NET PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, and bookmark PDF documents in .NET applications.

Our Delphi/C++Builder developer tools
Gnostice Document Studio Delphi

Multi-format document-processing component suite for Delphi/C++Builder developers, covering both VCL and FireMonkey platforms.

eDocEngine VCL

A Delphi/C++Builder component suite for creating documents in over 20 formats and also export reports from popular Delphi reporting tools.

PDFtoolkit VCL

A Delphi/C++Builder component suite to edit, enhance, view, print, merge, split, encrypt, annotate, and bookmark PDF documents.

Our Java developer tools
Gnostice Document Studio Java

Multi-format document-processing component suite for Java developers.

PDFOne (for Java)

A Java PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, bookmark PDF documents in Java applications.

Our Platform-Agnostic Cloud and On-Premises APIs

Cloud-hosted and On-Premises REST-based document-processing and document-viewing APIs

Privacy | Legal | Feedback | Newsletter | Blog | Resellers © 2002-2023 Gnostice Information Technologies Private Limited. All rights reserved.