Add drawing rectangle in a page

hello,

i try to use un exemple from documentation

How to Add Drawing with Transparent Color in PDF using Java

but this exempe have two errors in java
at the line
rectangle.getGraphInfo().setColor(new Color(255, 0, 0, 128));
can you help me to correct that.

Fabien

@fabien.levalois

To correct the errors you’re encountering in your Java code when trying to add a drawing rectangle with a transparent color, you should ensure that you are using the correct methods and classes from the Aspose.PDF library.

Here’s a revised example based on the typical usage of the Aspose.PDF library for Java:

import com.aspose.pdf.BorderInfo;
import com.aspose.pdf.BorderSide;
import com.aspose.pdf.Color;
import com.aspose.pdf.Document;
import com.aspose.pdf.GraphInfo;
import com.aspose.pdf.Page;
import com.aspose.pdf.drawing.Graph;
import com.aspose.pdf.drawing.Rectangle;

public class AddDrawingWithTransparentColor {
    public static void main(String[] args) {
        int alpha = 128; // Set the alpha value for transparency
        int red = 255;
        int green = 0;
        int blue = 0;

        // Create a Color object using Alpha RGB
        Color alphaColor = Color.fromArgb(alpha, red, green, blue);

        // Instantiate Document object
        Document document = new Document();
        // Add a page to the document
        Page page = document.getPages().add();

        // Create a Graph object with certain dimensions
        Graph graph = new Graph(300, 400);
        // Set border for the Graph object
        graph.setBorder(new BorderInfo(BorderSide.All, Color.getBlack()));
        // Add the graph object to the page
        page.getParagraphs().add(graph);

        // Create a Rectangle object with certain dimensions
        Rectangle rectangle = new Rectangle(0, 0, 100, 50);
        // Create GraphInfo object for the Rectangle instance
        GraphInfo graphInfo = rectangle.getGraphInfo();
        // Set color information for the GraphInfo instance
        graphInfo.setColor(Color.getRed());
        // Set fill color for the GraphInfo
        graphInfo.setFillColor(alphaColor);
        // Add the rectangle shape to the shapes collection of the graph object
        graph.getShapes().add(rectangle);

        // Save the PDF file
        document.save("AddDrawingWithTransparentColor.pdf");
        System.out.println("Drawing added successfully with transparent color.");
    }
}

Key Points:

  • Ensure that you are using the fromArgb method to create a color with an alpha channel for transparency.
  • The setFillColor method is used to apply the transparent color to the rectangle.
  • Make sure to save the document after adding the shapes.

This example should help you resolve the errors you were facing. If you have any further questions or issues, feel free to ask!