We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Add transparent text


I am trying to add a transparent text over(overlapping) a picture. However, I used the setHorizontalAlignment(100) function from Graph object but it did not set the text to where i wanted. Can you help me with it?

The code is below.

int alpha = 10;
int green = 0;
int red = 100;
int blue = 0;
// create Document instance
// Document doc = new Document();
// create page to pages collection of PDF file
// Page page = doc.getPages().add();
Page page = doc.getPages().get_Item(1);
// create Graph object
Graph canvas = new Graph(100, 400);
// create rectangle instance with certain dimensions
// Rectangle rect = new Rectangle(10, 100, 400, 400);
// create color object from Alpha color channel
// rect.getGraphInfo().setFillColor(Color.fromArgb(alpha, red, green, blue));
// add rectanlge to shapes collection of Graph object
// canvas.getShapes().add(rect);
// add graph object to paragraphs collection of page object
// set value to not change position for graph object
// canvas.setTop(value);
// create TextFragment instance with sample value
TextFragment text = new TextFragment(“pic of google logo”);
// create color object from Alpha channel
Color color = Color.getBlack();
// set color information for text instance
// add text to paragraphs collection of page instance
// save PDF file

Hello Jun,

Thanks for contacting support.

I have tried to add transparent text over an image with following code snippet and observed that the position of TextFragment was not as expected. Therefore, I have logged an issue as PDFJAVA-36815 in our issue tracking system. We will further investigate it and keep you updated with the status of its resolution. Please be patient and spare us little time.

We are sorry for the inconvenience.

Best Regards,


Please take into account that TextFragment.Rectangle property doesn’t support setter. So, changing of properties of the obtained rectangle also makes no effect.

We recommend using TextFragment.Position property of TextFragment or create more powerful TextParagraph object that supports the setting of the rectangle.

Please consider the following code:

        Document doc = new Document(dataDir + "Updated_document.pdf");
        ImagePlacementAbsorber abs = new ImagePlacementAbsorber();
        for (int i = 1; i <= abs.getImagePlacements().size(); i++)
            ImagePlacement image = abs.getImagePlacements().get_Item(i);
            Page page = image.getPage();
            Graph canvas = new Graph((float)page.getPageInfo().getWidth(), (float)page.getPageInfo().getHeight());
            TextFragment text = new TextFragment("transparent text");
            Color color = Color.fromArgb(30, 0, 255, 0);
            text.setPosition(new Position(image.getRectangle().getLLX(), image.getRectangle().getLLY()));
        doc.save(dataDir + "javaTransparent_Text2.pdf");