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.

Thu Jul 25, 2024 6:21 am

Hello.
I'm working with SpirePDF Java.
I'm drawing a grid and, if I do not set the row height, the height is dinamic according to the text inside the cell.
If I set the row height to a fix size, if the text exceed the available space, the text will be truncated.
I need to know when the text is truncated, to append "..." just to inform the user that the text isn't complete.

How to do it?

Thanks,
Mirko

mirko.golfieri
 
Posts: 7
Joined: Wed Jan 24, 2024 4:43 pm

Thu Jul 25, 2024 10:19 am

Hello,

Thanks for your inquiry.
You can refer to the following sample code to get the length and width of your string, then did some calculations based on the height and width of the cell. If you have any other question, please share us with your full testing code for further investigation.

Code: Select all
 PdfDocument doc = new PdfDocument();
        PdfPageBase page = doc.getPages().add();
        PdfGrid grid = new PdfGrid();
        grid.getStyle().setCellPadding(new PdfPaddings(0, 0, 0, 0));
        grid.getStyle().setFont(new PdfTrueTypeFont(new Font("Arial", Font.PLAIN, 10), true));
        grid.getStyle().setTextBrush(PdfBrushes.getBlack());
        grid.getStyle().setBackgroundBrush(PdfBrushes.getLightGray());
        PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", Font.PLAIN, 10), true);
        PdfBorders borders = new PdfBorders();
        borders.setAll(new PdfPen(PdfBrushes.getWhite(), 1f));
        String[] data = {"test;zzzzzzzzzzzzzzzzzzzzzzzzzzzzezzzzzeezzzzezzzzzezzzzzzsezzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz;1,391,190,000;18.2%"};
        String[][] dataSource = new String[data.length][];
        for (int i = 0; i < data.length; i++) {
            dataSource[i] = data[i].split("[;]", -1);
        }
        grid.setDataSource(dataSource);
        //Set the height of each row
        grid.getRows().get(0).setHeight(30f);
        grid.draw(page, 0, 30);
        float height = grid.getRows().get(0).getHeight();
        float width = grid.getColumns().get(1).getWidth();
        //One character width
        double aW = font.measureString("a").getWidth();
        //Height of one character
        double aH = font.measureString("a").getHeight();
        //Number of characters in a line
        int colNum = (int) Math.floor(width / aW);
        //Display line count
        int rowNum = (int) Math.floor(height / aH);
        //The position of the truncated character
        int objectCharPostion = rowNum * colNum;
        doc.saveToFile("Grid.pdf");
        doc.close();

Sincerely,
Amin
E-iceblue support team
User avatar

Amin.Gan
 
Posts: 229
Joined: Mon Jul 15, 2024 5:40 am

Return to Spire.PDF