Developer Tools
|
Office Productivity Applications
|
Platform-Agnostic APIs
|
Home | Online Demos | Downloads | Buy Now | Support | About Us | News | Working Together | Contact Us
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 fontHelvetica.setSize(30); fontHelvetica.setColor(Color.magenta); // 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", fontHelvetica, PdfTextFormatter.CENTER, 0, 50); page3.writeText("Rendering Transparent Images", fontHelvetica, PdfTextFormatter.CENTER, 0, 50); // Change font size and color fontHelvetica.setColor(Color.green); fontHelvetica.setSize(30); 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 page1.setBrushColor(brushColor); page1.setPenColor(penColor); // Write transparent text on page 1 page1.writeText( transparentText, fontHelvetica, PdfTextFormatter.CENTER, 0, 130 + verticalOffset, true); // Set brush and pen colors of page 2 page2.setBrushColor(brushColor); page2.setPenColor(penColor); // 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 page3.setBrushColor(brushColor); // Pen settings not required when drawing filled images // Draw transparent images on page 3 page3.drawImage( logoImage, // image (page3.getWidth() - // x-coordinate logoImage.width())/2, 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 doc.add(page1); doc.add(page2); doc.add(page3); // Save the document to file doc.save("TransparencyDemo.pdf"); doc.close(); } catch (IOException | PdfException e) { // TODO Auto-generated catch block e.printStackTrace(); }
Downloads:
* - This article is a PDFOne Java version of an earlier article written in March 2007 for our PDFOne .NET users.
---o0O0o---
Our .NET Developer Tools | |
---|---|
![]() Gnostice Document Studio .NETMulti-format document-processing component suite for .NET developers. |
![]() PDFOne .NETA .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 DelphiMulti-format document-processing component suite for Delphi/C++Builder developers, covering both VCL and FireMonkey platforms. |
![]() eDocEngine VCLA Delphi/C++Builder component suite for creating documents in over 20 formats and also export reports from popular Delphi reporting tools. |
![]() PDFtoolkit VCLA 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 JavaMulti-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 | |
---|---|
![]() StarDocsCloud-hosted and On-Premises REST-based document-processing and document-viewing APIs |
Privacy | Legal | Feedback | Newsletter | Blog | Resellers | © 2002-2025 Gnostice Information Technologies Private Limited. All rights reserved. |