Add transparent text

Hi,


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
page.getParagraphs().add(canvas);
// set value to not change position for graph object
canvas.setChangePosition(true);
canvas.setHorizontalAlignment(100);
canvas.setVerticalAlignment(670);
// canvas.setTop(value);
canvas.setZIndex(10);
// 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
text.getTextState().setForegroundColor(color);
// add text to paragraphs collection of page instance
page.getParagraphs().add(text);
// save PDF file
doc.save(“Transparent_Text.pdf”);

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,

@jun.luo

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();
        doc.getPages().get_Item(1).accept(abs);
        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());
            page.getParagraphs().add(canvas);
            canvas.setChangePosition(false);
            TextFragment text = new TextFragment("transparent text");
            Color color = Color.fromArgb(30, 0, 255, 0);
            text.getTextState().setForegroundColor(color);
            text.setPosition(new Position(image.getRectangle().getLLX(), image.getRectangle().getLLY()));
            page.getParagraphs().add(text);
        }
        doc.save(dataDir + "javaTransparent_Text2.pdf");