Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Mon Feb 19, 2024 2:57 pm

Dear Team,

I am seeking guidance on how to create a transparent filled rectangle in Spire.PDF using Java. The objective is to ensure that text remains visible in the background through the transparency of the rectangle.

Attached, please find a screenshot illustrating the desired outcome. Any assistance or code examples would be greatly appreciated.

Thank you in advance.

Best regards,
Swanesh

swanesh.saxena
 
Posts: 2
Joined: Mon Feb 19, 2024 2:49 pm

Tue Feb 20, 2024 2:18 am

Hello,

Thank you for your inquiry.

To implement adding a rectangle with transparency in a PDF document, please refer to the code snippet provided below:
Code: Select all
// Create a new PdfDocument object
PdfDocument pdfDocument = new PdfDocument();

// Load the input PDF file
pdfDocument.loadFromFile("input.pdf");

// Get the first page of the PDF document
PdfPageBase page = pdfDocument.getPages().get(0);

// Define the color with RGBA values (255, 0, 0, 50)
Color color = new Color(255, 0, 0, 50);

// Create a PdfBrush with a solid color and transparency
PdfBrush brushes = new PdfSolidBrush(new PdfRGBColor(color));

// Create a PdfPen with a red color and line width of 1
PdfPen pen = new PdfPen(new PdfRGBColor(Color.RED), 1);

// Define a rectangle with coordinates (50, 70) and size 100x30
Rectangle2D rectangle2D = new Rectangle2D.Float(50, 70, 100, 30);

// Draw a rectangle on the page canvas with the defined pen and brush
page.getCanvas().drawRectangle(pen, brushes, rectangle2D);

// Save the modified PDF document to an output file
pdfDocument.saveToFile("result.pdf");

Please feel free to use this code snippet as a reference to add transparent rectangles to your PDF documents. If you have any further questions or need additional assistance, please do not hesitate to reach out.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1657
Joined: Wed Apr 07, 2021 2:50 am

Tue Feb 20, 2024 8:02 am

Hello Annika,

Thank you for the prompt response.

Below is the updated code that worked for me. setTransparency is the key here to make the rectangle text visible.

PdfRGBColor pdfRGBColor = new PdfRGBColor(new Color(255, 0, 0));
PdfSeparationColorSpace cs = new PdfSeparationColorSpace("MySpotColor", pdfRGBColor);
PdfSeparationColor color = new PdfSeparationColor(cs, 0.2f);
PdfSolidBrush brush = new PdfSolidBrush(color);

Rectangle2D.Double rect1 = new Rectangle2D.Double(x, y, width, height);
canvas.setTransparency(0.5f);
canvas.drawRectangle(pen, rect1);
canvas.drawRectangle(brush, rect1);

swanesh.saxena
 
Posts: 2
Joined: Mon Feb 19, 2024 2:49 pm

Tue Feb 20, 2024 9:04 am

Hello,

You're welcome.

Thank you for providing the solution, this will certainly help address similar issues that our customers may encounter.
Once again, I appreciate the information you have shared.
If you have any more questions or need further assistance, feel free to let us know.
Have a nice day.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1657
Joined: Wed Apr 07, 2021 2:50 am

Return to Spire.PDF