Monday, 31 May 2021

Set and Get Slide Title in PowerPoint Using Java

In this tutorial, I’ll introduce how to set title for a slide of PowerPoint document, and then extract the text of the title using Java codes. It’s noted that I used a free third-party library called Free Spire.Presentation for Java to finish the operation above.

Before running codes, we need to add a Jar file to IDEA. You can download the package from the link, unzip it and find Spire.Presentation.jar in the “lib” folder, finally insert it to IDEA. Or you can reference it 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>

Set Slide Title

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

public class SetSlideTitle {
public static void main(String[] args) throws Exception {
//Create a Presentation instance
Presentation ppt = new Presentation();
//Get the first slide
ISlide slide = ppt.getSlides().get(0);

//Set title for the slide
slide.setTitle("Slide Title");

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

Output

Get Slide Title

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

public class GetSlideTitle {
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\\SetTitle.pptx");

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

//Print out the title of the slide
String tile = slide.getTitle();
System.out.println(tile);
}
}

Output




Thursday, 27 May 2021

Insert Page Break and Section Break into Word documents using Java

In a Word document, we can use page break to split a page into two pages, and can also use section break to start a new section. This article will demonstrate how to insert page break and section break into a Word document using Java codes.

Of course, it’s necessary to use a free third-party library called Free Spire.Doc for Java for finishing the operation above. You can download it from the E-iceblue official website or directly refer to the Jar file in the library 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.doc.free</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>

Using the code

Insert Page Break

Free Spire.Doc for Java provides the appendBreak method to insert page break into a Word document. Now I use the following codes to add page break to the paragraph 8 of a Word sample document.

import com.spire.doc.*;
import com.spire.doc.documents.*;

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

//get the first section
Section section = document.getSections().get(0);
//add page break to paragraph 8
Paragraph paragraph = section.getParagraphs().get(7);
paragraph.appendBreak(BreakType.Page_Break);

//save the resulting document
document.saveToFile("output/AddPageBreak.docx", FileFormat.Docx_2013);
}
}

Output


Insert Section Break

The following codes show how to insert section break to the paragraph 8 of a Word sample document by using the insertSectionBreak

method provided by Free Spire.Doc for Java.

import com.spire.doc.*;
import com.spire.doc.documents.*;

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

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

//add a section break to paragraph 8 and start the new section on the next page
Paragraph paragraph = section.getParagraphs().get(7);
paragraph.insertSectionBreak(SectionBreakType.New_Page);

//save the resultant document
document.saveToFile("output/AddSectionBreak.docx", FileFormat.Docx_2013);
}
}

Output




Monday, 24 May 2021

Merge and Split Cells in Excel using Java

When manipulating data in Excel worksheets, we often find ourselves in a situation where we need to merge or split some cells located in the same row or column, to make a sense of our table or to make it more neatly. This tutorial will demonstrate how to merge or split cells in Excel by using Java codes.

It’s necessary to use a free API called Free Spire.XLS for Java to realize the functions above. Before running codes, you need to add a Jar file in the API to IDEA. Please download the package from the link, unzip it and find the Jar file in the “lib” folder, or refer to it by adding 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.xls.free</artifactId>
<version>3.9.1</version>
</dependency>
</dependencies>

Merge Cells in Excel Using Java

import com.spire.xls.FileFormat;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;

public class MergeCell {
public static void main(String[] args) {
//Create a Workbook instance
Workbook workbook = new Workbook();
//Load the Excel file
workbook.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample.xlsx");

//Get the first worksheet
Worksheet sheet = workbook.getWorksheets().get(0);
//Merge cells by range
sheet.getRange().get("A1:E1").merge();

//Save the resulting file
workbook.saveToFile("output/MergeCells.xlsx", FileFormat.Version2013);
}
}

Output

Split Cells in Excel Using Java

import com.spire.xls.FileFormat;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;

public class SplitCell {
public static void main(String[] args) {
//Create a Workbook instance
Workbook workbook = new Workbook();
//Load the Excel file
workbook.loadFromFile("C:\\Users\\Test1\\Desktop\\MergeCells.xlsx");

//Get the first worksheet
Worksheet sheet = workbook.getWorksheets().get(0);
//Split cells by range
sheet.getRange().get("A1:E1").unMerge();

//Save the resulting file
workbook.saveToFile("output/SplitCells.xlsx", FileFormat.Version2013);
}
}

Output



Friday, 21 May 2021

Convert PDF to Excel using a free API

This article will introduce how to convert PDF to Excel using a free API called Free Spire.PDF for Java. Actually, I used the API to convert PDF to Image/Word previously, and to be honestly, it is the best one I have used so far because demos it provided are simple to know and the resulting documents can meet my requirements after running relevant codes.

You can download the product package from E-icblue official website, unzip it and get Spire.Pdf.jar in the “lib” folder, then add the jar file to IDEA. Of course, you can directly refer to it by using the following Maven configuration.

<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.pdf.free</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>

Using the code

The following is my PDF document including a table, and now I want to convert it to Excel.


import com.spire.pdf.*;

public class ConvertPDFToXLS {
public static void main(String[] args) {
//Create PDF document
PdfDocument pdf = new PdfDocument();
//Load the PDF document from disk.
pdf.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample.pdf");
//Save the document
pdf.saveToFile("output/pdfToExcel.xlsx", FileFormat.XLSX);
}
}

Output



Monday, 17 May 2021

Convert all elements in PowerPoint slides to Images using Java

I used a free third-party library named Free Spire.Presentation for Java to convert PowerPoint documents to other formats, such as PDF, XPS, Image, and I wrote a relevant tutorial accordingly. About converting PPT to Image in that article, I used a method called ISlide.SaveAsImage() to convert a specific presentation slide to Image. Recently, I find that the library also supports converting each element in slides to image, such as such as chart, table, shape, textbox.

In this article, I’ll take the following PowerPoint document including some elements as an example, and then use Free Spire.Presentation for Java to convert them to several images.

We still need to add Spire.Presentation.jar in the library to IDEA. You can get a package from the link, unzip it and find the Jar file in the “lib” folder. Or directly refer to it 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>

Using the code

import com.spire.presentation.ISlide;
import com.spire.presentation.Presentation;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;

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

//Traverse every slides and every shapes on the slides
int k = 0;
for (int i = 0; i < presentation.getSlides().getCount(); i++) {
ISlide slide = presentation.getSlides().get(i);
for (int j = 0; j < slide.getShapes().getCount(); j++) {
String outputFile = "output/";
String fileName = outputFile + String.format("shapeToImage-%1$s.png", k);
//Save every single shape as image
BufferedImage image = slide.getShapes().saveAsImage(j);
ImageIO.write(image, "PNG", new File(fileName));
k++;
}
}
}
}
Output




Wednesday, 12 May 2021

[Java] create mail merge and merge text value in Word

This article will introduce how to create a mail merge template and then merge the text value using Java codes with the help pf a free API called Free Spire.Doc for Java.

Before running codes, we should add a Jar file in the API to IDEA. You can get it from the link, or refer to it 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.doc.free</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>

Using the code

import com.spire.doc.*;
import com.spire.doc.documents.Paragraph;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MailMerge {
public static void main(String[] args) throws Exception {
//Create a Document instance
Document document = new Document();

//Add a section
Section section = document.addSection();

//Add 3 paragraphs to the section
Paragraph para = section.addParagraph();
Paragraph para2 = section.addParagraph();
Paragraph para3 = section.addParagraph();

//Add mail merge templates to each paragraph
para.setText("Contact Name: ");
para.appendField("Contact Name", FieldType.Field_Merge_Field);
para2.setText("Phone: ");
para2.appendField("Phone", FieldType.Field_Merge_Field);
para3.setText("Date: ");
para3.appendField("Date", FieldType.Field_Merge_Field);

//Set the value for the mail merge template by the field name
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = formatter.format(currentTime);
String[] filedNames = new String[]{"Contact Name", "Phone", "Date"};
String[] filedValues = new String[]{"John Smith", "+1 (69) 123456", dateString};

//Merge the specified value into template
document.getMailMerge().execute(filedNames, filedValues);

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

Output


Monday, 10 May 2021

Delete Blank Rows and Columns in Excel using Java

Requirement

I have an Excel document as below and there are some blank rows and columns in the second worksheet. Now I want to delete all blank rows and columns once through running Java codes in the background, and the operation can be finished without installing Microsoft Office. 


Solution

I need to use a third-party library to accomplish the operation above. After finding and testing some libraries, I find a free and professional API called Free Spire.XLS for Java can do it best.

Before running codes, I need to install JDK and Intellij IDEA, and then add a Jar file in the library to IDEA. I can get it through the official download link, or refer to it by using the following Maven configuration.

<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.xls.free</artifactId>
<version>3.9.1</version>
</dependency>
</dependencies>

Using the code

import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;

public class DeleteBlankRowsAndColumns {
public static void main(String[] args) {
//Load an Excel document
Workbook wb = new Workbook();
wb.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample.xlsx");

//Get the second worksheet
Worksheet sheet = wb.getWorksheets().get(1);

//Loop through the rows
for (int i = sheet.getLastRow(); i >= 1; i--)
{
//Detect if rows are blank
if (sheet.getRows()[i-1].isBlank())
{
//Remove blank rows
sheet.deleteRow(i);
}
}

//Loop through the columns
for (int j = sheet.getLastColumn(); j >= 1; j--)
{
//Detect if columns are blank
if (sheet.getColumns()[j-1].isBlank())
{
//Remove blank columns
sheet.deleteColumn(j);
}
}

//Save the resulting document
wb.saveToFile("output/DeleteBlankRowsAndColumns.xlsx", ExcelVersion.Version2016);
}
}

Output


Thursday, 6 May 2021

Add or Delete Pages in PDF using Java

When we are manipulating PDF documents, sometimes we need to delete some pages in documents to reserve the others or insert new pages to add some new contents. This tutorial will demonstrate how to delete a page in PDF and then add a new page to it using Java codes.

It’s worth mentioning that I used a free API called Free Spire.PDF for Java. Before running codes, we need to add a Jar file to IDEA. We can get it from the link, or directly refer to it by using the following Maven configuration.

<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.pdf.free</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>

You can a PDF document including two pages as below.


Now we’ll use the following Java codes to delete the second page and add a new page including some new text.

import com.spire.pdf.*;
import java.awt.*;
import com.spire.pdf.graphics.*;
import java.awt.geom.Rectangle2D;

public class AddDeletePage {
public static void main(String[] args) {
//Create a PdfDocument object and load a PDF file
PdfDocument pdf = new PdfDocument();
pdf.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample.pdf");

//Delete the second page
pdf.getPages().removeAt(1);

//Add a new page to PDF document
PdfPageBase page = pdf.getPages().add();

//Draw text string to the page and set the font for it.
PdfTrueTypeFont font= new PdfTrueTypeFont(new Font("Arial Unicode MS",Font.PLAIN,12),true);

PdfRGBColor blue = new PdfRGBColor();
blue.setB((byte) 255);
PdfSolidBrush brush = new PdfSolidBrush(blue);
Rectangle2D.Float rctg1 = new Rectangle2D.Float();
rctg1.setRect(0,70,page.getCanvas().getClientSize().getWidth() / 2,100);
page.getCanvas().drawString("New page added by Free Spire.PDF for Java ", font, brush, rctg1);

//Save the document
pdf.saveToFile("output/AddOrDeletePage.pdf");
}
}

Output


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...