Calculate the Width of the Given String in PowerPoint Presentation in Java

How do I calculate the width of the given string provided any specific font. I have text box in slide with given x width. I need to allow only certain number of characters in a string with width less than x.

For ex consider a string

This is a sample String to calculate width of text using Aspose method.

@VaradS,
Thank you for posting the question.

To calculate the width of a text string in a PowerPoint presentation, please try using the getRect() methods from the IParagraph and IPortion interfaces. I hope this will help you. Please let us know if you have any additional questions.

@andrey.potapov ,
Thanks for the reply.
I have to handle a use case in ppt where if the text width increases more than 665 I need to remove characters from end and add ellipsis (…). I need help to create substring based on the given text width and add ellipsis at the end so the final width is not more than 665.

I’m sharing a sample program with you where I have added a string with more than 665 width, here I have to remove few characters from end and add ellipsis so that the final text width is not greater than 665.
Could you help me here to create substring based on width of the string?

public static void main(String[] args)
{
        // Initialize a Presentation object
        Presentation presentation = new Presentation();

        
        ISlide slide = presentation.getSlides().get_Item(0);
        double [] d1 = {690};
        double [] d2 = {20};
        
        ITable table = slide.getShapes().addTable(15,50, d1, d2);
        ICell cell1 = table.getRows().get_Item(0).get_Item(0);
        cell1.getTextFrame().getParagraphs().clear();
        ITextFrame textFrame = cell1.getTextFrame();
        Paragraph paragraph = new Paragraph();
        textFrame.getParagraphs().add(paragraph);
        paragraph.setText("WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW");
        FontData fontData = new FontData("Arial");
        paragraph.getParagraphFormat().setFontAlignment(FontAlignment.Top);
        paragraph.getParagraphFormat().getDefaultPortionFormat().setLatinFont(fontData);
        paragraph.getParagraphFormat().getDefaultPortionFormat().setFontBold(NullableBool.False);
        paragraph.getParagraphFormat().getDefaultPortionFormat().setFontHeight(11);
        paragraph.getParagraphFormat().getBullet().setType(BulletType.Symbol);
        paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid);
        paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor()
                .setColor(Color.BLACK);
        java.awt.geom.Rectangle2D rectangle2D = paragraph.getRect();
        System.out.println(rectangle2D.getWidth());
        System.out.println(rectangle2D.getHeight());

        try {
            presentation.save("output.pptx", SaveFormat.Pptx);
        } catch (Exception e) {
            e.printStackTrace();
        }
}

@VaradS,
It will take me some time to solve the problem, I will get back to you soon.

@VaradS,
Thank you for your patience. This cannot be done automatically. This is an algorithmic problem that you can solve, for example, using the bisection method:
You can calculate the width of the text string. If the width is too big, reduce the string by half. If half is too small, increase the string by half of the remaining length, and so on. When calculating the width of a substring, you also need to take into account the ellipsis.