Friday, 30 July 2021

Insert an OLE object to an Excel worksheet using Java codes

Recently, I find a free third-party library called Free Spire.Office for Java and it supports inserting Word/Excel/PowerPoint/PDF documents as linked object or embedded object into Excel worksheets. This article will show how to insert a Word document as an embedded object into Excel using Java codes with the help of it.

DEPENDENCY

First of all, we need to download and install JDK and Intellij IDEA to create a development environment. Then we get the package of the free library from the official website, find Spire.Xls.jar in the “lib” folder. Finally, manually add it to IDEA.

Besides, there is another way to add the Jar file, which is using the following codes in the Maven Pom.xml file.

<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.office.free</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>

USING THE CODE

import com.spire.xls.*;
import com.spire.xls.core.IOleObject;
import com.spire.doc.*;
import com.spire.doc.documents.ImageType;
import java.awt.image.BufferedImage;

public class InsertOLE {
public static void main(String[] args) {

//Load the Excel document
Workbook workbook = new Workbook();
workbook.loadFromFile(
"C:\\Users\\Test1\\Desktop\\Sample.xlsx");
//Get the first worksheet
Worksheet worksheet = workbook.getWorksheets().get(0);

//Generate image
BufferedImage image = GenerateImage("C:\\Users\\Test1\\Desktop\\Test.docx");
//insert OLE object
IOleObject oleObject = worksheet.getOleObjects().add("C:\\Users\\Test1\\Desktop\\Test.docx", image, OleLinkType.Embed);
oleObject.setLocation(worksheet.getCellRange(
"B4"));
oleObject.setObjectType(OleObjectType.
ExcelWorksheet);
//Save the file
workbook.saveToFile("output/InsertOLE.xlsx", ExcelVersion.Version2010);
}

private static BufferedImage GenerateImage(String fileName) {

//Load the sample word document
Document document = new Document();
document.loadFromFile(fileName);

//Save the first page of word as an image
BufferedImage image = document.saveToImages(0, ImageType.Bitmap);
return image;
}
}

Output






Monday, 26 July 2021

Find and Highlight Text in PDF using Java codes

There is no doubt that highlighting a text using a color in a PDF file is a great strategy for reading and retaining key information, which can help in bringing important information immediately to the reader's attention. In this article, I’ll introduce how to find specified text and then highlight it using Java codes with the help of a free third-party library called FreeSpire.PDF for Java.

DEPENDENCY

In order to finish the operation mentioned above using Java codes, we need to create a development environment – downloading and installing JDK 1.8.0 and Intellij IDEA 2019. Furthermore, get the package of Free Spire.PDF for Java from the official website, and find Spire.Pdf.jar in the “lib” folder. Finally, add the Jar file to IDEA.

Of course, you can directly add the Jar file by using the following Maven configurations.

<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.pdf.free</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>

USING THE CODE

Actually, Free Spire.PDF for Java supports finding the specified text through two methods. One is

directly pointing out the content of text string, the other is defining a pattern and finding results that

match the pattern.

Find and Highlight Text by Text String

import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.general.find.PdfTextFind;
import java.awt.*;

public class FindAndHighlightTextString {
public static void main(String[] args) {
//Load the PDF file
PdfDocument pdf = new PdfDocument();
pdf.loadFromFile("C:\\Users\\Test1\\Desktop\\Test.pdf");
PdfTextFind[] result;

//Find and highlight the string “New Zealand” in every PDF page
for (PdfPageBase page : (Iterable<PdfPageBase>) pdf.getPages()) {
result = page.findText("cashless society",false,false).getFinds();
for (PdfTextFind find : result) {
find.applyHighLight(Color.yellow);
}
}

//Save the file
pdf.saveToFile("output/FindAndHighlightText.pdf");
}
}

Output


Find and Highlight Text by Regular Expression

import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.general.find.PdfTextFind;
import java.awt.*;

public class FindByRegularExpression {
public static void main(String[] args) {
//Load a PDF document
PdfDocument pdf = new PdfDocument();
pdf.loadFromFile("C:\\Users\\Test1\\Desktop\\Test.pdf");

//Create an object of PdfTextFind collection
PdfTextFind[] results;

//Loop through the pages
for (Object page : (Iterable) pdf.getPages()) {
PdfPageBase pageBase = (PdfPageBase) page;

//Define a regular expression
String pattern = "\\#\\w+\\b";

//Find all results that match the pattern
results = pageBase.findText(pattern).getFinds();

//Highlight the search results with yellow
for (PdfTextFind find : results) {
find.applyHighLight(Color.yellow);
}
}

//Save to file
pdf.saveToFile("output/FindTextByRegularExpression.pdf");
}
}

Output



Tuesday, 20 July 2021

[Java] Get the position of text in PowerPoint slides

Here is a PowerPoint slide and there is a English word called test in it. Now I want to get its location using Java codes. Through a lot of tests, I used a free third-party library called Free Spire.Presentation for Java to do it.


About Free Spire.Presentation for Java

Free Spire.Presentation for Java is a professional PowerPoint API that enables developers to create, read, write, convert and save PowerPoint documents in Java Applications. As an independent Java library, you only need to it without installing Microsoft PowerPoint on the system.

Dependency

First of all, we need to create a development environment by downloading and installing JDK and Intellij IDEA. Then get the package of library from the E-iceblue official website, find Spire.Presentation.jar in the “lib” folder. Finally, manually add it to IDEA through several steps as the following screenshot shows.

Of course, there is the other method to add it to IDEA. Create a Maven project in IDEA, and type the following codes in the pom.xml file. Finally, click the button “Import Changes”.

<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.presentation.free</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>
Using the code

Free Spire.Presentation for Java not only supports getting the x and y coordinates of the location relative

to slide, but also can print out the x and y coordinates of the location relative to shape.

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

import java.awt.geom.Point2D;

public class GetPositionOfText {
public static void main(String[] args) throws Exception {
//Create a Presentation instance
Presentation ppt = new Presentation();
//Load a PowerPoint document
ppt.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample.pptx");

//Get the first slide
ISlide slide = ppt.getSlides().get(0);
//Get the first shape
IAutoShape shape = (IAutoShape)slide.getShapes().get(0);
//Get location of text in the shape
Point2D location =shape.getTextFrame().getTextLocation();

//Print out the x and y coordinates of the location relative to slide
String point1="Text's position relative to Slide: x= "+location.getX()+" y = "+location.getY();
System.out.println(point1);

//Print out the x and y coordinates of the location relative to shape
String point2 = "Text's position relative to shape: x= " + (location.getX() - shape.getLeft()) + " y = " + (location.getY() - shape.getTop());
System.out.println(point2);
}
}

Output




Friday, 16 July 2021

Add, Delete and Reply to Comments in Word documents with Java codes

Adding comments in Word is a great way to mark up your documents without having to directly edit its content, and you can add comments to anything in Word, including text, images, charts, tables, etc. If there are comments on the document already, replying to them lets you have a discussion with others, even when you're not all in the document at the same time.

This article will introduce how to add a comment to the specified paragraph or text in Word, reply to the comment and then delete it using Java codes. It’s worth mentioning that the operation mentioned above need to be done with the help of a third-party library called Free Spire.Doc for Java.

BEFORE CODING

First of all, we need to create a development environment by installing JDK and Intellij IDEA, and then add a Jar file called Spire.Doc.jar which is in the library to IDEA. You can get the Jar file through the link or directly reference it by using the following Maven configuration.

<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc.free</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>

USING THE CODE

Add a comment to a specified paragraph in Word

import com.spire.doc.*;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.Comment;

public class AddCommentToParagraph {
public static void main(String[] args) {
//load a word document
Document document= new Document("C:\\Users\\Test1\\Desktop\\Sample.docx");

//Get the fifth paragraph of first section
Section section = document.getSections().get(0);
Paragraph paragraph = section.getParagraphs().get(4);

//Insert a new comment to the paragraph
Comment comment = paragraph.appendComment("The definition of Cashless Society");
comment.getFormat().setAuthor("Tina");
comment.getFormat().setInitial("CM");

//save the file
document.saveToFile("output/AddCommentToParagraph.docx", FileFormat.Docx);
}
}

Output


Add a comment to specified text in Word

import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.documents.CommentMark;
import com.spire.doc.documents.CommentMarkType;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.TextSelection;
import com.spire.doc.fields.Comment;

public class AddCommentToText {
public static void main(String[] args) {
//Load a Word document
Document doc = new Document();
doc.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample.docx");

//Find the text string in the document
TextSelection[] finds = doc.findAllString("a cashless society is one in which financial transactions " +
"are handled through the transfer of digital information instead of physical money in the form of banknotes " +
"or coins", false, true);

for (TextSelection find : finds) {
//Create comment start mark and comment end mark
CommentMark commentmarkStart = new CommentMark(doc);
commentmarkStart.setType(CommentMarkType.Comment_Start);
CommentMark commentmarkEnd = new CommentMark(doc);
commentmarkEnd.setType(CommentMarkType.Comment_End);

//Create a comment
Comment comment = new Comment(doc);
comment.getBody().addParagraph().setText("The definition of Cashless Society");
comment.getFormat().setAuthor("Tina");

//Find the paragraph where the string is located in
Paragraph para = find.getAsOneRange().getOwnerParagraph();
//Get the index of the string in the paragraph
int index = para.getChildObjects().indexOf(find.getAsOneRange());

//Add the comment to the paragraph
para.getChildObjects().add(comment);
//Insert the comment start mark and comment end mark to the paragraph based on the index
para.getChildObjects().insert(index, commentmarkStart);
para.getChildObjects().insert(index + 2, commentmarkEnd);
}

//Save the resulting document
doc.saveToFile("output/AddCommentToText.docx",FileFormat.Docx_2013);
}
}
Output

Reply to a comment in Word

import com.spire.doc.*;
import com.spire.doc.FileFormat;
import com.spire.doc.fields.*;

public class ReplyToComment {
public static void main(String[] args) {
//load a word document
Document document= new Document("C:\\Users\\Test1\\Desktop\\AddCommentToParagraph.docx");

//Get the first comment
Comment comment1 = document.getComments().get(0);

//add the new comment as a reply to the selected comment.
Comment replyComment1 = new Comment(document);
replyComment1.getFormat().setAuthor("Emma");
replyComment1.getBody().addParagraph().appendText("You never need to use paper bills or coins in a Cashless Society");
comment1.replyToComment(replyComment1);

//save the file
document.saveToFile("output/ReplyToComment.docx", FileFormat.Docx);
}
}

Output


Delete a specified comment in Word

import com.spire.doc.*;
import com.spire.doc.FileFormat;

public class DeleteComment {
public static void main(String[] args) {
//load a word document
Document document= new Document("C:\\Users\\Test1\\Desktop\\ReplyToComment.docx");

//remove the first comment
document.getComments().removeAt(0);

//save the file.
document.saveToFile("output/DeleteComment.docx", FileFormat.Docx);
}
}

Output



Monday, 12 July 2021

[Java] Replace the specified text in Excel worksheets with images

Requirement

Here I have an Excel worksheet, and now I want to replace the following text in the second column with images using Java codes with the help of a free third-party library.


Solution

I used Free Spire.XLS for Java to finish the operation above. First of all, we need to create a development environment – downloading and installing JDK and Intellij IDEA. Then we can get the package of the third-party library from the link, find Spire.Xls.jar in the “lib” folder and finally add it to IDEA. Or we can reference the Jar file by using the following Maven configurations.

<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.xls.free</artifactId>
<version>3.9.1</version>
</dependency>
</dependencies>
Using the code

You can replace the specified text with images through several steps as follows.

Step 1: Create a Workbook instance and load an Excel document.

Step 2: Get the worksheet we’ll manipulate by index.

Step 3: Find the text string and set its content as null.

Step 4: Get the location of the cell and add an image to it.

Step 5: Save the resulting document to the specified path.

import com.spire.xls.*;

public class ReplaceTextWithImage {
public static void main(String[] args) {
//Load an Excel document
Workbook workbook = new Workbook();
workbook.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample.xlsx");
//Get the first worksheet
Worksheet worksheet = workbook.getWorksheets().get(0);

//Find the text string "Chinese flag"
CellRange[] ranges1 = worksheet.findAllString("Chinese flag", false, false);
for (CellRange range1 : ranges1) {
//set the text as null
range1.setText("");

//get the row and column of the searched range
int row1 = range1.getRow();
int column1 = range1.getColumn();
//Add the image to the searched range
worksheet.getPictures().add(row1, column1, "C:\\Users\\Test1\\Desktop\\logo1.png", ImageFormatType.Png);

//Find the text string "American flag"
CellRange[] ranges2 = worksheet.findAllString("American flag", false, false);
for (CellRange range2 : ranges2) {
//set the text as null
range2.setText("");

//get the row and column of the searched range
int row2 = range2.getRow();
int column2 = range2.getColumn();
//Add the image to the searched range
worksheet.getPictures().add(row2, column2, "C:\\Users\\Test1\\Desktop\\logo2.jpg", ImageFormatType.Jpeg);

//Find the text string "English flag"
CellRange[] ranges3 = worksheet.findAllString("English flag", false, false);
for (CellRange range3 : ranges3) {
//set the text as null
range3.setText("");

//get the row and column of the searched range
int row3 = range3.getRow();
int column3 = range3.getColumn();
//Add the image to the searched range
worksheet.getPictures().add(row3, column3, "C:\\Users\\Test1\\Desktop\\logo3.jpg", ImageFormatType.Jpeg);

//Save the document to file
workbook.saveToFile("output/ReplaceTextWithImage.xlsx", ExcelVersion.Version2013);
}
}
}
}
}

Output



Tuesday, 6 July 2021

Create Fillable Form Fields in PDF programmatically with Java

PDF fillable forms are frequently used to collect data and information of users and common form fields include text box, radio button, check box, list box and combo box. This article will introduce how to create a fillable PDF form using a free third-party library called Free Spire.PDF for Java.

Before running Java codes, we need to add a Jar file called Spire.Pdf.jar which is located in this library to IDEA. You can get it from the official website, or reference it using the following Maven configurations.

<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.pdf.free</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>

Using the code

Here I’ll create a new PDF document and add fillable form fields to it.

package FormField;
import java.awt.*;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.EnumSet;
import com.spire.pdf.FileFormat;
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.fields.*;
import com.spire.pdf.graphics.*;

public class CreateFormField {
public static void main(String[] args) throws Exception {
//create a PdfDocument object
PdfDocument doc = new PdfDocument();

//add a page
PdfPageBase page = doc.getPages().add();

//initialize x and y coordinates
float baseX = 100;
float baseY = 0;

//create brush objects
PdfSolidBrush brush1 = new PdfSolidBrush(new PdfRGBColor(Color.BLUE));
PdfSolidBrush brush2 = new PdfSolidBrush(new PdfRGBColor(Color.black));

//create font
PdfFont font = new PdfFont(PdfFontFamily.Times_Roman, 12f, EnumSet.of(PdfFontStyle.Regular));

//add a textbox to pdf
String text = "TextBox:";
page.getCanvas().drawString(text, font, brush1, new Point2D.Float(0, baseY));
Rectangle2D.Float tbxBounds = new Rectangle2D.Float(baseX, baseY , 150, 15);
PdfTextBoxField textBox = new PdfTextBoxField(page, "textbox");
textBox.setBounds(tbxBounds);
textBox.setText("Hello World");
textBox.setFont(font);
doc.getForm().getFields().add(textBox);
baseY +=25;

//add checkboxes to pdf
page.getCanvas().drawString("CheckBox:", font, brush1, new Point2D.Float(0, baseY));
java.awt.geom.Rectangle2D.Float rec1 = new java.awt.geom.Rectangle2D.Float(baseX, baseY, 15, 15);
PdfCheckBoxField checkBoxField = new PdfCheckBoxField(page, "checkbox1");
checkBoxField.setBounds(rec1);
checkBoxField.setChecked(false);
page.getCanvas().drawString("Option 1", font, brush2, new Point2D.Float(baseX + 20, baseY));
java.awt.geom.Rectangle2D.Float rec2 = new java.awt.geom.Rectangle2D.Float(baseX + 70, baseY, 15, 15);
PdfCheckBoxField checkBoxField1 = new PdfCheckBoxField(page, "checkbox2");
checkBoxField1.setBounds(rec2);
checkBoxField1.setChecked(false);
page.getCanvas().drawString("Option 2", font, brush2, new Point2D.Float(baseX+90, baseY));
doc.getForm().getFields().add(checkBoxField);
doc.getForm().getFields().add(checkBoxField1);
baseY += 25;

//add a listbox to pdf
page.getCanvas().drawString("ListBox:", font, brush1, new Point2D.Float(0, baseY));
java.awt.geom.Rectangle2D.Float rec = new java.awt.geom.Rectangle2D.Float(baseX, baseY, 150, 50);
PdfListBoxField listBoxField = new PdfListBoxField(page, "listbox");
listBoxField.getItems().add(new PdfListFieldItem("Item 1", "item1"));
listBoxField.getItems().add(new PdfListFieldItem("Item 2", "item2"));
listBoxField.getItems().add(new PdfListFieldItem("Item 3", "item3"));;
listBoxField.setBounds(rec);
listBoxField.setFont(font);
listBoxField.setSelectedIndex(0);
doc.getForm().getFields().add(listBoxField);
baseY += 60;

//add radiobuttons to pdf
page.getCanvas().drawString("RadioButton:", font, brush1, new Point2D.Float(0, baseY));
PdfRadioButtonListField radioButtonListField = new PdfRadioButtonListField(page, "radio");
PdfRadioButtonListItem radioItem1 = new PdfRadioButtonListItem("option1");
radioItem1.setBounds(new Rectangle2D.Float(baseX, baseY, 15, 15));
page.getCanvas().drawString("Option 1", font, brush2, new Point2D.Float(baseX + 20, baseY));
PdfRadioButtonListItem radioItem2 = new PdfRadioButtonListItem("option2");
radioItem2.setBounds(new Rectangle2D.Float(baseX + 70, baseY, 15, 15));
page.getCanvas().drawString("Option 2", font, brush2, new Point2D.Float(baseX + 90, baseY));
radioButtonListField.getItems().add(radioItem1);
radioButtonListField.getItems().add(radioItem2);
radioButtonListField.setSelectedIndex(0);
doc.getForm().getFields().add(radioButtonListField);
baseY += 25;

//add a combobox to pdf
page.getCanvas().drawString("ComboBox:", font, brush1, new Point2D.Float(0, baseY));
Rectangle2D.Float cmbBounds = new Rectangle2D.Float(baseX, baseY, 150, 15);
PdfComboBoxField comboBoxField = new PdfComboBoxField(page, "combobox");
comboBoxField.setBounds(cmbBounds);
comboBoxField.getItems().add(new PdfListFieldItem("Item 1", "item1"));
comboBoxField.getItems().add(new PdfListFieldItem("Item 2", "itme2"));
comboBoxField.getItems().add(new PdfListFieldItem("Item 3", "item3"));
comboBoxField.getItems().add(new PdfListFieldItem("Item 4", "item4"));
comboBoxField.setSelectedIndex(0);
comboBoxField.setFont(font);
doc.getForm().getFields().add(comboBoxField);
baseY += 25;

//add a signature field to pdf
page.getCanvas().drawString("Signature:", font, brush1, new Point2D.Float(0, baseY));
PdfSignatureField sgnField= new PdfSignatureField(page,"sgnField");
Rectangle2D.Float sgnBounds = new Rectangle2D.Float(baseX, baseY, 150, 80);
sgnField.setBounds(sgnBounds);
doc.getForm().getFields().add(sgnField);
baseY += 90;

//add a button to pdf
page.getCanvas().drawString("Button:", font, brush1, new Point2D.Float(0, baseY));
Rectangle2D.Float btnBounds = new Rectangle2D.Float(baseX, baseY, 50, 15);
PdfButtonField buttonField = new PdfButtonField(page, "button");
buttonField.setBounds(btnBounds);
buttonField.setText("Submit");
buttonField.setFont(font);
doc.getForm().getFields().add(buttonField);

//save to file
doc.saveToFile("output/AddFormField.pdf", FileFormat.PDF);
}
}

The screenshot below shows output PDF document containing form fields.



 

Friday, 2 July 2021

[Java] Set shadow effects for text/shapes/images in PowerPoint slides

In PowerPoint documents, shadows will make your objects pop out of your slide through making flat 2 dimensional objects appear like 3 dimensional objects. In this article, we’ll learn three types of shadow effects in PowerPoint — Outer, Inner and Perspective, and also know how to set these shadow effects for text/shapes/images in PowerPoint slide using Java codes without installing Microsoft PowerPoint.

DEVELOPMENT ENVIRONMENT

We need to use a free third-party library called Free Spire.Presentation for Java to manipulate the operation above in Java applications. Get the product package from the link, find Spire.Presentation.jar in the “lib” folder and then manually add it to the project. Or you can directly reference the Jar file by using the following Maven configurations.

<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.presentation.free</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>

ABOUT THREE TYPES OF SHADOW EFFECTS

Outer shadow

Outer shadow preset is used to make the object pop out of the screen. Here is a screenshot of text which

is from a PowerPoint slide, and now I’ll use Java codes to set an outer shadow effect for it.

Java codes:

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 SetShadowEffectForText {
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 the slide
IAutoShape shape = slide.getShapes().appendShape(ShapeType.RECTANGLE,new Rectangle2D.Float(80,60,800,200));
shape.getFill().setFillType(FillFormatType.NONE);
shape.getLine().setFillType(FillFormatType.NONE);

//Fill the shape with text
shape.appendTextFrame("SHADOW EFFECTS");

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

//Set an outer shadow effect for the text through the OuterShadowEffect method
OuterShadowEffect outerShadow= new OuterShadowEffect();
outerShadow.setBlurRadius(0);
outerShadow.setDirection(50);
outerShadow.setDistance(10);
outerShadow.getColorFormat().setColor(Color.yellow);

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

//Save the resulting file
presentation.saveToFile("output/SetShadowEffectForText.pptx", FileFormat.PPTX_2013);
}
}

The generated text is showed as below after setting an outer shadow effect for it.


Inner shadow

Inner shadow preset makes your shape look like it is pressed inside from the surface. Use the following

Java codes to set an inner shadow effect for a shape in a PowerPoint slide.

import com.spire.presentation.*;
import com.spire.presentation.drawing.*;
import java.awt.*;
import java.awt.geom.Rectangle2D;

public class SetShadowEffectForShape {
public static void main(String[] args) throws Exception {
//Create a Presentation object
Presentation ppt = new Presentation();

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

//Add a rectangle to the slide
Rectangle2D rect1 = new Rectangle2D.Float(200, 150, 300, 200);
IAutoShape shape = slide.getShapes().appendShape(ShapeType.RECTANGLE, rect1);
shape.getFill().setFillType(FillFormatType.SOLID);
shape.getFill().getSolidColor().setColor(Color.white);
shape.getLine().setFillType(FillFormatType.NONE);

//Set an inner shadow effect for the shape through the InnerShadowEffect method
InnerShadowEffect innerShadow = new InnerShadowEffect();
innerShadow.setBlurRadius(20);
innerShadow.setDirection(0);
innerShadow.setDistance(0);
innerShadow.getColorFormat().setColor(Color.BLACK);

//Apply the inner shadow effect to the shape
shape.getEffectDag().setInnerShadowEffect(innerShadow);

//save the resulting file
ppt.saveToFile("output/setShadowEffectForShape.pptx", FileFormat.PPTX_2013);
}
}
A comparison between two shapes is showed as below.

Perspective Shadow

Perspective shadow makes your 2D shapes look like 3D shapes. I’ll set the shadow effect for an image

in a PowerPoint slide using the Java codes below.

import com.spire.presentation.*;
import com.spire.presentation.drawing.*;
import java.awt.*;
import java.awt.geom.Rectangle2D;

public class SetShadowEffectForImage {
public static void main(String[] args) throws Exception {
//Create a Presentation object
Presentation ppt = new Presentation();

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

//Add a rectangle to the slide.
Rectangle2D rect = new Rectangle2D.Float(120, 100, 250, 270);
IAutoShape shape = slide.getShapes().appendShape(ShapeType.RECTANGLE, rect);

//Fill the shape with a picture
shape.getFill().setFillType(FillFormatType.PICTURE);
shape.getFill().getPictureFill().getPicture().setUrl("C:\\Users\\Test1\\Desktop\\logo.png");
shape.getLine().setFillType(FillFormatType.NONE);
shape.getFill().getPictureFill().setFillType(PictureFillType.STRETCH);

//Set a perspective shadow effect for the image through the PresetShadowEffect method
PresetShadow presetShadow = new PresetShadow();
presetShadow.setPreset(PresetShadowValue.BACK_RIGHT_PERSPECTIVE);
presetShadow.getColorFormat().setColor(Color.lightGray);

//Apply the perspective shadow effect to the image
shape.getEffectDag().setPresetShadowEffect(presetShadow);

//Save the resulting file
ppt.saveToFile("output/SetShadowEffectForImage.pptx", FileFormat.PPTX_2013);
}
}

From the comparison image, it feels as if the dolphin is standing vertically on the floor.



Change PDF Versions in Java

In daily work, you might need to change the version of a PDF document you have in order to ensure compatibility with another version which a...