Spire.XLS for Java provides you with the ability to shrink text to fit in a cell by using the setShrinkToFit method of the CellStyleObject class. The setShrinkToFit method accepts the following parameter:

boolean: specify whether to shrink text to fit in a cell.

The following example shows how to shrink text to fit in a cell in Excel using Spire.XLS for Java.

import com.spire.xls.*;

public class ShrinkTextToFitInACell {
    public static void main(String []args) throws Exception {
        //Create a workbook instance
        Workbook workbook = new Workbook();

        //Load the Excel file
        workbook.loadFromFile("Sample.xlsx");

        //Get the first worksheet
        Worksheet sheet = workbook.getWorksheets().get(0);

        //Get the cell range to shrink text
        CellRange cell = sheet.getRange().get("B2:B3");

        //Enable “shrink to fit”
        cell.getCellStyle().setShrinkToFit(true);

        //Save the file
        workbook.saveToFile("ShrinkTextToFitInACell.xlsx", ExcelVersion.Version2013);
    }
}

The input Excel:

Shrink Text to Fit in a Cell in Excel in Java

The output Excel:

Shrink Text to Fit in a Cell in Excel in Java

Change Font Styles in PowerPoint in Java

2021-08-24 06:51:22 Written by Koohji

This article demonstrates how to change font styles (font name, font size, font color, bold, italic and underlined) of an existing PowerPoint document by using Spire.Presentation for Java.

import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;

import java.awt.*;

public class ChangeFontStyles {

    public static void main(String[] args) throws Exception {

        //Create a Presentation object
        Presentation presentation = new Presentation();

        //Load the sample PowerPoint file
        presentation.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pptx");

        //Get the text shape
        IAutoShape shape = (IAutoShape) presentation.getSlides().get(0).getShapes().get(0);

        //Get the first paragraph and change the font color of it
        ParagraphEx paragraph = shape.getTextFrame().getParagraphs().get(0);
        PortionEx textRange =  paragraph.getFirstTextRange();
        textRange.getFormat().getFill().setFillType(FillFormatType.SOLID);
        textRange.getFormat().getFill().getSolidColor().setColor(Color.blue);

        //Get the second paragraph and make the text bold, italic and unlined
        paragraph = shape.getTextFrame().getParagraphs().get(1);
        textRange = paragraph.getFirstTextRange();
        textRange.getFormat().isBold(TriState.TRUE);
        textRange.getFormat().isItalic(TriState.TRUE);
        textRange.getFormat().setTextUnderlineType(TextUnderlineType.DASHED);

        //Get the third paragraph and change the font name and size
        paragraph = shape.getTextFrame().getParagraphs().get(2);
        textRange = paragraph.getFirstTextRange();
        textRange.getFormat().setLatinFont(new TextFont("Segoe Print"));
        textRange.getFormat().setFontHeight(22f);

        //Save the document
        presentation.saveToFile("output/ChangeFontStyles.pptx", FileFormat.PPTX_2013);
    }
}

Change Font Styles in PowerPoint in Java

This article demonstrates how to add multiline watermarks to a PowerPoint slide using Spire.Presentation for Java. To add watermarks to all slides, use one more for loop outside the two for loops in the following code snippet.

import com.spire.presentation.*;
import com.spire.presentation.Presentation;
import com.spire.presentation.drawing.*;

import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;


//Create a Presentation object
Presentation presentation = new Presentation();

//Load the sample PowerPoint file
presentation.loadFromFile("C:\\Users\\Administrator\\Desktop\\Spire.Presentation.pptx");

//Specify watermark text
String watermarkText = "E-iceblue";

//Get the size of the watermark text
Image image = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = (Graphics2D) image.getGraphics();
Font font = new Font("Arial", Font.BOLD, 20);
g2d.setFont(font);
FontMetrics fm = g2d.getFontMetrics();
Rectangle2D strSize = fm.getStringBounds(watermarkText, g2d);

//Initialize x and y coordinate
float x = 30;
float y = 80;

for (int rowNum = 0; rowNum < 4; rowNum++) {
    for (int colNum = 0; colNum < 5; colNum++) {

        //Add a rectangle shape
        Rectangle2D rect = new Rectangle2D.Float(x, y, (float) strSize.getWidth() + 10, (float) strSize.getHeight());
        IAutoShape shape = presentation.getSlides().get(0).getShapes().appendShape(ShapeType.RECTANGLE, rect);

        //Set the style of the shape
        shape.getFill().setFillType(FillFormatType.NONE);
        shape.getShapeStyle().getLineColor().setColor(new Color(1, 1, 1, 0));
        shape.setRotation(-45);
        shape.getLocking().setSelectionProtection(true);
        shape.getLine().setFillType(FillFormatType.NONE);

        //Add watermark text to the shape
        shape.getTextFrame().setText(watermarkText);
        PortionEx textRange = shape.getTextFrame().getTextRange();

        //Set the style of the text range
        textRange.getFill().setFillType(FillFormatType.SOLID);
        textRange.getFill().getSolidColor().setColor(Color.pink);
        textRange.setLatinFont(new TextFont(font.getName()));
        textRange.setFontMinSize(font.getSize());

        x += (100 + strSize.getWidth());

    }
    x = 30;
    y += (100 + strSize.getHeight());
}

//Save the document
presentation.saveToFile("output/Watermark.pptx", FileFormat.PPTX_2013);

Add Multiline Watermarks to PowerPoint in Java

page 26