This article will demonstrate how to insert and remove shapes in Excel file using Spire.XLS for Java.

Add shapes to Excel worksheet:

import com.spire.xls.*;
import com.spire.xls.core.*;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;

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

        String output = "output/AddShapesToExcelSheet.xlsx";

        //create a workbook.
        Workbook workbook = new Workbook();

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

        //add a triangle shape.
        IPrstGeomShape triangle = sheet.getPrstGeomShapes().addPrstGeomShape(2, 2, 100, 100, PrstGeomShapeType.Triangle);
        //fill the triangle with solid color.
        triangle.getFill().setForeColor( Color.YELLOW);
        triangle.getFill().setFillType( ShapeFillType.SolidColor);

        //add a heart shape.
        IPrstGeomShape heart = sheet.getPrstGeomShapes().addPrstGeomShape(2, 5, 100, 100, PrstGeomShapeType.Heart);
        //fill the heart with gradient color.
        heart.getFill().setForeColor(Color.RED);
        heart.getFill().setFillType(ShapeFillType.Gradient);

        //add an arrow shape with default color.
        IPrstGeomShape arrow = sheet.getPrstGeomShapes().addPrstGeomShape(10, 2, 100, 100, PrstGeomShapeType.CurvedRightArrow);

        //add a cloud shape.
        IPrstGeomShape cloud = sheet.getPrstGeomShapes().addPrstGeomShape(10, 5, 100, 100, PrstGeomShapeType.Cloud);
        //fill the cloud with custom picture
        BufferedImage image = ImageIO.read(new File("SpireXls.png"));
        cloud.getFill().customPicture(image, "SpireXls.png");
        cloud.getFill().setFillType( ShapeFillType.Picture);

        //save to file.
        workbook.saveToFile(output, ExcelVersion.Version2013);
        }
}

Output:

Insert and remove shapes in Excel in Java

Remove a particular shape or all shapes from Excel worksheet:

import com.spire.xls.*;

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

                 //Load the sample file
                Workbook workbook = new Workbook();
                workbook.loadFromFile("output/AddShapesToExcelSheet.xlsx");

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

                //delete the second shape in the worksheet
                sheet.getPrstGeomShapes().get(1).remove();

              /* //delete all shapes in the worksheet
                for (int i = sheet.getPrstGeomShapes().getCount()-1; i >= 0; i--)
                 {
                   sheet.getPrstGeomShapes().get(i).remove();
                 }*/

                //save to file.
                workbook.saveToFile("output/RemoveParticularShape.xlsx", ExcelVersion.Version2013);
            }
        }

Effective screenshot after remove the second shape from Excel worksheet:

Insert and remove shapes in Excel in Java

We have demonstrated how to add text and image header footer to Word document by using Spire.Doc for Java. This article will show you how to create different headers/footers for odd and even pages on Word document in Java applications.

import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.*;
import java.awt.*;

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

        String input = "multiPages.docx";
        String output = "output/oddAndEvenHeaderFooter.docx";

        //load the document
        Document doc = new Document();
        doc.loadFromFile(input);

        //get the first section
        Section section = doc.getSections().get(0);

        //set the DifferentOddAndEvenPagesHeaderFooter property as true
        section.getPageSetup().setDifferentOddAndEvenPagesHeaderFooter(true);

        //add odd header
        Paragraph P3 = section.getHeadersFooters().getOddHeader().addParagraph();
        TextRange OH = P3.appendText("Odd Header");
        P3.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
        OH.getCharacterFormat().setFontName("Arial");
        OH.getCharacterFormat().setFontSize(14);
        OH.getCharacterFormat().setTextColor(Color.BLUE);

        //add even header
        Paragraph P4 = section.getHeadersFooters().getEvenHeader().addParagraph();
        TextRange EH = P4.appendText("Even Header from E-iceblue Using Spire.Doc");
        P4.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
        EH.getCharacterFormat().setFontName("Arial");
        EH.getCharacterFormat().setFontSize(14);
        EH.getCharacterFormat().setTextColor(Color.GREEN);

        //add odd footer
        Paragraph P2 = section.getHeadersFooters().getOddFooter().addParagraph();
        TextRange OF = P2.appendText("Odd Footer");
        P2.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
        OF.getCharacterFormat().setFontName("Arial");
        OF.getCharacterFormat().setFontSize(14);
        OF.getCharacterFormat().setTextColor(Color.BLUE);

        //add even footer
        Paragraph P1 = section.getHeadersFooters().getEvenFooter().addParagraph();
        TextRange EF = P1.appendText("Even Footer from E-iceblue Using Spire.Doc");
        EF.getCharacterFormat().setFontName("Arial");
        EF.getCharacterFormat().setFontSize(14);
        P1.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
        EF.getCharacterFormat().setTextColor(Color.GREEN);

        //save the document
        doc.saveToFile(output, FileFormat.Docx);

    }
}

Output:

Java Add different headers/footers for odd and even pages on Word

This article shows you how to create a chart in PowerPoint using the data from an existing Excel document. This solution relies on Spire.Office.jar. Please download the latest version from here and add it as a dependency in your project.

Below is a screenshot of the Excel document.

Create PowerPoint Chart from Excel Data in Java

import com.spire.presentation.FileFormat;
import com.spire.presentation.Presentation;
import com.spire.presentation.SlideSizeType;
import com.spire.presentation.charts.ChartStyle;
import com.spire.presentation.charts.ChartType;
import com.spire.presentation.charts.IChart;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;

import java.awt.geom.Rectangle2D;

public class CreateChartFromExcelData {

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

        //Create a Presentation object
        Presentation presentation = new Presentation();
        presentation.getSlideSize().setType(SlideSizeType.SCREEN_16_X_9);

        //Add a clustered column chart to slide
        Rectangle2D rect = new Rectangle2D.Float(200, 100, 550, 320);
        IChart chart = presentation.getSlides().get(0).getShapes().appendChart(ChartType.COLUMN_CLUSTERED,rect);

        //Clear the default dummy data
        chart.getChartData().clear(0,0,5,5 );

        //Load an existing Excel file to Workbook object
        Workbook wb = new Workbook();
        wb.loadFromFile("C:\\Users\\Administrator\\Desktop\\data.xlsx");

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

        //Import data from the sheet to chart table
        for (int r = 0; r < sheet.getAllocatedRange().getRowCount(); r++)
        {
            for (int c = 0; c < sheet.getAllocatedRange().getColumnCount(); c++)
            {
                chart.getChartData().get(r,c).setValue(sheet.getCellRange(r+1, c+1).getValue2());
            }
        }

        //Add chart title
        chart.getChartTitle().getTextProperties().setText("Male/Female Ratio Per Dept.");
        chart.getChartTitle().getTextProperties().isCentered(true);
        chart.getChartTitle().setHeight(25f);
        chart.hasTitle(true);

        //Set the series label
        chart.getSeries().setSeriesLabel(chart.getChartData().get("B1","C1"));

        //Set the category labels
        chart.getCategories().setCategoryLabels(chart.getChartData().get("A2","A5"));

        //Set the series values
        chart.getSeries().get(0).setValues(chart.getChartData().get("B2","B5"));
        chart.getSeries().get(1).setValues(chart.getChartData().get("C2", "C5"));

        //Apply built-in chart style
        chart.setChartStyle(ChartStyle.STYLE_11);

        //Set overlap
        chart.setOverLap(-50);

        //Set gap width
        chart.setGapWidth(200);

        //Save to file
        presentation.saveToFile("output/Chart.pptx", FileFormat.PPTX_2013);
    }
}

Create PowerPoint Chart from Excel Data in Java

Tuesday, 30 March 2021 02:37

Save PowerPoint Shapes as Images in Java

This article shows you how to export shapes in a specific slide as images using Spire.Presentation for Java. Below is a screenshot of the sample PowerPoint document.

Save PowerPoint Shapes as Images in Java

import com.spire.presentation.ISlide;
import com.spire.presentation.Presentation;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;

public class SaveShapeAsImage {

    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\\chart and table.pptx");

        //Get the first slide
        ISlide slide = presentation.getSlides().get(0);

        //Declare a BufferedImage variable
        BufferedImage image;

        //Loop through the shapes in the slide
        for (int i = 0; i < slide.getShapes().getCount(); i++) {

            //Save the specific shape as image data
            image = slide.getShapes().saveAsImage(i);

            //Write data to png file
            File file = new File(String.format("ToImage-%d.png", i));
            ImageIO.write(image, "PNG", file);
        }
    }
}

Output

Save PowerPoint Shapes as Images in Java

This article demonstrates how to add and delete digital signature in an Excel file using Spire.XLS for Java.

Add digital signature

import com.spire.xls.*;
import com.spire.xls.digital.CertificateAndPrivateKey;

import java.util.Date;


public class AddDigitalSignature {
    public static void main(String[] args) throws Exception {
        //Load the sample document
        Workbook workbook=new Workbook();
        workbook.loadFromFile("Sample.xlsx");

        //Add digital signature
        CertificateAndPrivateKey cap = new CertificateAndPrivateKey("Test.pfx","e-iceblue");
        workbook.addDigitalSignature(cap, "e-iceblue",new Date());

        //Save the document
        String result="AddDigitalSignature.xlsx";
        workbook.saveToFile(result,ExcelVersion.Version2013);

    }
}

Effective screenshot:

Add and Delete Digital Signature in Excel in Java

Delete digital signature

import com.spire.xls.*;

public class RemoveDigitalSignature {
    public static void main(String[] args) throws Exception {
        //Load the sample document
        Workbook workbook=new Workbook();
        workbook.loadFromFile("AddDigitalSignature.xlsx");

        //Remove digital signature
        workbook.removeAllDigitalSignatures();

        //Save the document 
        String result="RemoveDigitalSignature.xlsx";
        workbook.saveToFile(result,ExcelVersion.Version2013);

    }
}

Output:

Add and Delete Digital Signature in Excel in Java

Tuesday, 09 March 2021 08:38

Set Table Alignment in Slides in Java

This article demonstrates how to horizontally and vertically align a table in a slide using Spire.Presentation for Java.

Below is a screenshot of the sample document.

Set Table Alignment in Slides in Java

import com.spire.presentation.FileFormat;
import com.spire.presentation.ITable;
import com.spire.presentation.Presentation;
import com.spire.presentation.ShapeAlignmentEnum;

public class AlignTable {

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

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

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

        //Declare a ITable variable
        ITable table = null;

        //Loop through the shapes in the first slide
        for (Object shape: presentation.getSlides().get(0).getShapes()
             ) {

            //Check if shape is an instance of ITable
            if (shape instanceof ITable)
            {
                //Convert shape to table
                table =(ITable)shape;
            }
        }

        //Horizontally align table to center
        table.setShapeAlignment(ShapeAlignmentEnum.ShapeAlignment.AlignCenter);

        //Vertically align table to middle
        table.setShapeAlignment(ShapeAlignmentEnum.ShapeAlignment.AlignMiddle);

        //Save to file
        presentation.saveToFile("AlignTable.pptx", FileFormat.PPTX_2013);
    }
}

Set Table Alignment in Slides in Java

This article demonstrate how to copy slides from one PowerPoint presentation to another using Spire.Presentation for Java.

import com.spire.presentation.FileFormat;
import com.spire.presentation.Presentation;

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

        //Create a Presentation object to load document one
        Presentation pptOne= new Presentation();
        pptOne.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample_1.pptx");

        //Create a Presentation object to load document two
        Presentation pptTwo = new Presentation();
        pptTwo.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample_2.pptx");

        //Insert the specific slide of document one to the specified position on document two
        pptTwo.getSlides().insert(0,pptOne.getSlides().get(0));

        //Append the specific slide of document one to the end of the document two
        pptTwo.getSlides().append(pptOne.getSlides().get(4));

        //Save document two to file
        pptTwo.saveToFile("CopySlides.pptx", FileFormat.PPTX_2013);
    }
}

Copy Slides from One Presentation to Another in Java

Tuesday, 23 February 2021 07:30

Add multiple watermarks in presentation slides

Spire.Presentation supports to insert text watermark and image watermark to PowerPoint document. This article will show you how to use Spire.Presentation to add multiple watermarks to the presentation slides in C#/VB.NET.

C#
using Spire.Presentation;
using Spire.Presentation.Drawing;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace WatermarkDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PPT document and load file
            Presentation presentation = new Presentation();
            presentation.LoadFromFile("Sample.pptx");

            //Get the size of the watermark string
            Font font = new Font("Arial", 20);
            String watermarkText = "E-iceblue";
            SizeF size = TextRenderer.MeasureText("E-iceblue", font);
            float x = 30;
            float y = 80;
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    //Define a rectangle range
                    RectangleF rect = new RectangleF(x, y, size.Width, size.Height);

                    //Add a rectangle shape with a defined range
                    IAutoShape shape = presentation.Slides[0].Shapes.AppendShape(Spire.Presentation.ShapeType.Rectangle, rect);

                    //Set the style of the shape
                    shape.Fill.FillType = FillFormatType.None;
                    shape.ShapeStyle.LineColor.Color = Color.White;
                    shape.Rotation = -45;
                    shape.Locking.SelectionProtection = true;
                    shape.Line.FillType = FillFormatType.None;

                    //Add text to the shape
                    shape.TextFrame.Text = watermarkText;
                    TextRange textRange = shape.TextFrame.TextRange;
                    //Set the style of the text range
                    textRange.Fill.FillType = FillFormatType.Solid;
                    textRange.Fill.SolidColor.Color = Color.FromArgb(120, Color.HotPink);
                    textRange.EastAsianFont = new TextFont(font.Name);
                    textRange.FontHeight = font.Size;

                    x += (100 + size.Width);
                }
                x = 30;
                y += (100 + size.Height);
            }

            //Save the document
            presentation.SaveToFile("Watermark_result.pptx", FileFormat.Pptx2010);

        }
    }
}
VB.NET
Imports Spire.Presentation
Imports Spire.Presentation.Drawing
Imports System
Imports System.Drawing
Imports System.Windows.Forms

Namespace WatermarkDemo
    
    Class Program
        
        Private Shared Sub Main(ByVal args() As String)
            'Create a PPT document and load file
            Dim presentation As Presentation = New Presentation
            presentation.LoadFromFile("Sample.pptx")
            'Get the size of the watermark string
            Dim font As Font = New Font("Arial", 20)
            Dim watermarkText As String = "E-iceblue"
            Dim size As SizeF = TextRenderer.MeasureText("E-iceblue", font)
            Dim x As Single = 30
            Dim y As Single = 80
            Dim i As Integer = 0
            Do While (i < 3)
                Dim j As Integer = 0
                Do While (j < 3)
                    'Define a rectangle range
                    Dim rect As RectangleF = New RectangleF(x, y, size.Width, size.Height)
                    'Add a rectangle shape with a defined range
                    Dim shape As IAutoShape = presentation.Slides(0).Shapes.AppendShape(Spire.Presentation.ShapeType.Rectangle, rect)
                    'Set the style of the shape
                    shape.Fill.FillType = FillFormatType.None
                    shape.ShapeStyle.LineColor.Color = Color.White
                    shape.Rotation = -45
                    shape.Locking.SelectionProtection = true
                    shape.Line.FillType = FillFormatType.None
                    'Add text to the shape
                    shape.TextFrame.Text = watermarkText
                    Dim textRange As TextRange = shape.TextFrame.TextRange
                    'Set the style of the text range
                    textRange.Fill.FillType = FillFormatType.Solid
                    textRange.Fill.SolidColor.Color = Color.FromArgb(120, Color.HotPink)
                    textRange.EastAsianFont = New TextFont(font.Name)
                    textRange.FontHeight = font.Size
                    x = (x + (100 + size.Width))
                    j = (j + 1)
                Loop
                
                x = 30
                y = (y + (100 + size.Height))
                i = (i + 1)
            Loop
            
            'Save the document
            presentation.SaveToFile("Watermark_result.pptx", FileFormat.Pptx2010)
        End Sub
    End Class
End Namespace

Output:

Add multiple watermarks in presentation slides

This article demonstrates how to apply a shadow effect to the text in a PowerPoint slide using Spire.Presentation for Java.

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

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

public class SetShadowEffect {

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

        //Create a Presentation object
        Presentation presentation = new Presentation();
        presentation.getSlideSize().setType(SlideSizeType.SCREEN_16_X_9);

        //Get the first slide
        ISlide slide = presentation.getSlides().get(0);

        //Add a rectangle to slide
        IAutoShape shape = slide.getShapes().appendShape(ShapeType.RECTANGLE,new Rectangle2D.Float(50,80,500,100));
        shape.getFill().setFillType(FillFormatType.NONE);
        shape.getLine().setFillType(FillFormatType.NONE);

        //Set text of the shape
        shape.appendTextFrame("Text shading on slide");

        //Set font style
        shape.getTextFrame().getTextRange().setFontHeight(38f);
        shape.getTextFrame().getTextRange().setLatinFont(new TextFont("Arial Black"));
        shape.getTextFrame().getTextRange().getFill().setFillType(FillFormatType.SOLID);
        shape.getTextFrame().getTextRange().getFill().getSolidColor().setColor(Color.BLACK);

        //Create a OuterShadowEffect object
        OuterShadowEffect outerShadow= new OuterShadowEffect();

        //Set the shadow effect
        outerShadow.setBlurRadius(0);
        outerShadow.setDirection(50);
        outerShadow.setDistance(10);
        outerShadow.getColorFormat().setColor(Color.orange);

        //Apply shadow effect to text
        shape.getTextFrame().getTextRange().getEffectDag().setOuterShadowEffect(outerShadow);

        //Save to file
        presentation.saveToFile("output/AddShadow.pptx", FileFormat.PPTX_2013);
    }
}

Apply a Shadow Effect to Text in PowerPoint in Java

This article will demonstrate how to use Spire.XLS for Java to remove the formulas but keep the values on the Excel worksheet.

Firstly, view the original Excel:

Java remove the formulas but keep the values on Excel worksheet

import com.spire.xls.*;

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

        String inputFile = "Sample.xlsx";
        String outputFile="output/removeFormulasButKeepValues_result.xlsx";

        //Create a workbook.
        Workbook workbook = new Workbook();
        //Load the file from disk.
        workbook.loadFromFile(inputFile);
        //Loop through worksheets.
        for (Worksheet sheet : (Iterable) workbook.getWorksheets())
        {
            //Loop through cells.
            for (CellRange cell : (Iterable) sheet.getRange())
            {
                //If the cell contains formula, get the formula value, clear cell content, and then fill the formula value into the cell.
                if (cell.hasFormula())
                {
                    Object value = cell.getFormulaValue();
                    cell.clear(ExcelClearOptions.ClearContent);
                    cell.setValue(value.toString());
                }
            }
        }
        //Save to file
        workbook.saveToFile(outputFile, ExcelVersion.Version2013);
    }
}

Output:

Java remove the formulas but keep the values on Excel worksheet

Page 1 of 131