Developer Tools
|
Office Productivity Applications
|
Enterprise Solutions
|
|||||||||||||||||||||||







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 object PdfWriter w = PdfWriter.fileWriter("TransparencyDemo.pdf"); PdfDocument d = new PdfDocument(w); // Create a font object PdfFont f = PdfFont.create( "Helvetica", // font name PdfFont.STROKE_AND_FILL, // style 30, // size PdfEncodings.WINANSI // encoding ); // Create an image object PdfImage img = PdfImage.create("Logo.png"); // Create three pages PdfPage p1 = new PdfPage(); PdfPage p2 = new PdfPage(); PdfPage p3 = new PdfPage(); String str = "This is transparent text"; int y = 0; float transparency = 0.0f; double imgPos = 0.0; Color penColor = null; Color brushColor = null; // Set font size and color f.setSize(30); f.setColor(Color.magenta); // Write transparent text on the three pages p1.writeText( "Rendering Transparent Text", // text f, // font PdfTextFormatter.CENTER, // alignment 0, // x-coordinate 50 // y-coordinate ); p2.writeText("Rendering Transparent Shapes", f, PdfTextFormatter.CENTER, 0, 50); p3.writeText("Rendering Transparent Images", f, PdfTextFormatter.CENTER, 0, 50); // Change font size and color f.setColor(Color.green); f.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 - transparency // alpha ); penColor = new Color(0.0f, 1.0f, 0.0f, 1.0f - transparency); // Set page 1's brush and pen colors p1.setBrushColor(brushColor); // p1.setPenColor(penColor); // You can uncomment this line // Write transparent text on page 1 p1.writeText(str, f, PdfTextFormatter.CENTER, 0, 130 + y, true); // Set page 2's brush and pen colors p2.setBrushColor(brushColor); p2.setPenColor(penColor); // Draw transparent shapes on page 2 p2.drawEllipse(150, 130 + y, 230, 160 + y, true, true); p2.drawSquare(300, 130 + y, 50, true, true); p2.drawCircle(450, 155 + y, 25, true, true); // Set page 3's brush color p3.setBrushColor(brushColor); // Pen settings not required when drawing images // Draw transparent images on page 3 p3.drawImage( img, // image (p3.getWidth() - img.width())/2, // x-coordinate 130 + imgPos // y-coordinate ); // Change positions for next iteration imgPos = imgPos + img.height() + 2; y += 60; // Change transparency level for next iteration transparency += 0.2f; } // Add pages to document d.add(p1); d.add(p2); d.add(p3); // Write file to disk and open it afterwards d.setOpenAfterSave(true); d.write(); w.dispose();
Downloads:
* - This article is a PDFOne Java version of an earlier article written in March 2007 for our PDFOne .NET users.
---o0O0o---
| Our Developer Tools | |
|---|---|
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. |
XtremePDFConverter VCLA Delphi/C++Builder component to intelligently convert PDF to user-friendly Word RTF documents. |
|
PDFOne .NETA .NET PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, and bookmark PDF documents in .NET applications. |
XtremeDocumentStudio .NETMulti-format document-processing component suite for .NET developers |
PDFOne (for Java™)A Java™ PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, bookmark PDF documents in Java™ applications. |
XtremeFontEngine (for Java)Java font engine to render glyphs from Type 1, Type 2 (CFF), and TrueType fonts |
| Our Office Productivity Applications |
|---|
Free PDF ReaderA free, fast, and portable application for viewing, printing and converting PDF documents. |
| Privacy | Legal | Feedback | Newsletter | Resellers | © 2002-2013 Gnostice Information Technologies Private Limited. All rights reserved. |
This site is best viewed on a screen with minimum resolution of 1152 x 864 pixels. Windows XP users are advised to use Microsoft ClearType Tuning for optimal experience. Also, please use the latest version of a standards-compliant browser such as Firefox, Opera, or Dragon (Chromium).