Saturday, 25 September 2021

Add, Read and Remove Document Properties in Word using Java

In MS Word, document properties are information which is used to describe or identify Word documents. It has four different types of which standard properties and custom properties are the most common ones. You can set details, such as title, author and subject in standard properties, and you can assign a text, time, or numeric value to custom properties.

In this article, I’ll show you how to add, read and remove the common document properties mentioned above in Word using Java. In order to realize the operations, I’ll use a free third-party library called Free Spire.Doc for Java which is a free and professional Java API that allows you to create, convert, manipulate and print Word documents without using Microsoft Office. You can download the API’s JAR from the link or install 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.doc.free</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>

Add Document Properties to Word using Java

Add standard document properties to Word

I have an existing Word document, now I’ll set some information including author, subject, title, keywords for it.

The following are some steps to introduce how to do it.

l  Load an existing Word document using Document calss

l  Set relevant information for standard document properties using Document.getBuiltinDocumentProperties().setTitle() method.

l  Finally, save the resulting document using Document.saveToFile(String, FileFormat) method.

The full code snippets are shown below.

import com.spire.doc.Document;
import com.spire.doc.FileFormat;

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

//Add built-in document properties to the Word document
document.getBuiltinDocumentProperties().setTitle("Be Glad Your Nose is on Your Face");
document.getBuiltinDocumentProperties().setSubject("an English Poem");
document.getBuiltinDocumentProperties().setAuthor("Jack Prelutsky");
document.getBuiltinDocumentProperties().setCategory("English Poems");
document.getBuiltinDocumentProperties().setKeywords("Nose, Poem");
document.getBuiltinDocumentProperties().setComments("This English poem is interesting.");

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

The following screenshot shows the document properties we added to Word


Add custom document properties to Word

Free Spire.Doc for Java offers Document.getCustomDocumentProperties().add() method to add custom

document properties to Word, and you can find full code sample as below.

import com.spire.doc.Document;
import com.spire.doc.FileFormat;

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

//Add custom document properties to the Word document
document.getCustomDocumentProperties().add("TrackingID", "AB01");
document.getCustomDocumentProperties().add("Checked By", "Wilson");

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

Output


Read Document Properties in Word using Java

Read standard document properties in Word

import com.spire.doc.Document;

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

//Read built-in properties
System.out.println("Title: " + document.getBuiltinDocumentProperties().getTitle());
System.out.println("Subject: " + document.getBuiltinDocumentProperties().getSubject());
System.out.println("author: " + document.getBuiltinDocumentProperties().getAuthor());
System.out.println("category: " + document.getBuiltinDocumentProperties().getCategory());
System.out.println("keyWords: " + document.getBuiltinDocumentProperties().getKeywords());
System.out.println("comments: " + document.getBuiltinDocumentProperties().getComments());
}
}

Output


Read custom document properties in Word

Free Spire.Doc for Java supports reading custom document properties by getting the specified custom

document property through index or name using Document.getCustomDocumentProperties().get()

method. The following code sample will show you how to do it.

import com.spire.doc.Document;
import com.spire.doc.DocumentProperty;

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

//Read the first custom document property
//DocumentProperty property = document.getCustomDocumentProperties().get(0);
DocumentProperty property = document.getCustomDocumentProperties().get("TrackingID");
System.out.println("TrackingID: " + property.getValue());
}
}
Output


Delete Document Properties in Word using Java

Delete standard document properties in Word

You can delete relevant document properties by setting the value of   Document.getBuiltinDocumentProperties().setTitle()

method as null.

import com.spire.doc.Document;
import com.spire.doc.FileFormat;

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

//Remove built-in properties by setting the value to null
document.getBuiltinDocumentProperties().setTitle("");
document.getBuiltinDocumentProperties().setSubject("");
document.getBuiltinDocumentProperties().setAuthor("");
document.getBuiltinDocumentProperties().setCategory("");
document.getBuiltinDocumentProperties().setKeywords("");
document.getBuiltinDocumentProperties().setComments("");

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

Delete custom document properties in Word

You can also remove custom document properties by specifying its name using Document.getCustomDocumentProperties().remove() method.

import com.spire.doc.Document;
import com.spire.doc.FileFormat;

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

//Remove specific custom property by name
document.getCustomDocumentProperties().remove("TrackingID");
document.getCustomDocumentProperties().remove("Checked By");

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


Friday, 24 September 2021

Convert PDF to HTML and vice versa in Java

This tutorial will introduce how to convert PDF to HTML and convert HTML to PDF using Java codes with the help of a third-party library called Spire.PDF for Java.

Add Spire.Pdf.jar to IDEA

Before coding, we need to add a Jar file called Spire.Pdf.jar to Intellij IDEA, and there are two methods to do it.

Method One: download the package of Spire.PDF for Java from the link, unzip it and find Spire.Pdf.jar in the “lib” folder. Finally manually add it to IDEA.

Method Two: create a Maven project in IDEA, type the following codes in the pom.xml file and then 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.pdf</artifactId>
<version>.4.8.7</version>
</dependency>
</dependencies>

Using the codes

Convert PDF to HTML

import com.spire.pdf.*;

public class PDFToHTML {
public static void main(String[] args) {
//Load the PDF file
PdfDocument pdf = new PdfDocument();
pdf.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample.pdf");
//Save to HTML format
pdf.saveToFile("output/ToHTML.html", FileFormat.HTML);
}
}

Output



Convert PDF to HTML with embedded SVG/Image and save HTML to stream

import com.spire.pdf.*;
import java.io.*;

public class PDFToHTMLWithEmbeddedSVG {
public static void main(String[] args) throws FileNotFoundException {

//Load the sample document file
PdfDocument pdf = new PdfDocument();
pdf .loadFromFile("C:\\Users\\Test1\\Desktop\\Sample.pdf");

//Set the bool useEmbeddedSvg and useEmbeddedImg as true
pdf .getConvertOptions().setPdfToHtmlOptions(true,true);

//Save to stream
File outFile = new File("output/toHTML_out.html");
OutputStream outputStream = new FileOutputStream(outFile);
pdf.saveToStream(outputStream, FileFormat.HTML);
pdf.close();
}
}
Convert HTML to PDF
import com.spire.pdf.graphics.PdfMargins;
import com.spire.pdf.htmlconverter.qt.HtmlConverter;
import com.spire.pdf.htmlconverter.qt.Size;

public class HTMLToPDF {
public static void main(String[] args) {
//define the HTML link and result PDF
String url = "https://www.e-iceblue.com/";
String fileName = "output/HTMLToPDF.pdf";
//Set the plugin path
String pluginPath = "D:/Qt/plugins_32";
HtmlConverter.setPluginPath(pluginPath);
//convert HTML to PDF and set the size of the result PDF page
HtmlConverter.convert(url, fileName, true, 1000000, new Size(600f, 900f), new PdfMargins(0));
}
}

Tuesday, 21 September 2021

Copy Worksheets in Excel using Java codes

When working in Excel, you’ll sometimes need to create one or more copies of your spreadsheet. Here I’ll introduce how to use Java codes to copy a specified worksheet within an Excel workbook or between two different workbooks.

BEFORE CODING

I used a free third-party library called Free Spire.XLS for Java in this tutorial. Before coding, we need to add a Jar file named Spire.Xls.jar to IDEA, and there are two methods to do it. One is that downloading the package of the free library from the link, finding Spire.Xls.jar in the “lib” folder and then manually adding it to IDEA. The other is that creating a Maven project in IDEA, typing the following codes in the pom.xml file and finally clicking 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.xls.free</artifactId>
<version>3.9.1</version>
</dependency>
</dependencies>

USING THE CODE

How to copy a worksheet within an Excel workbook

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

public class CopySheetWithinWorkbook {
public static void main(String[] args) {
//Create a Workbook object
Workbook workbook = new Workbook();

//Load the sample Excel file
workbook.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample.xlsx");

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

//Add a new worksheet
Worksheet newSheet = workbook.getWorksheets().add(originalSheet.getName()+" - Copy");

//Copy the worksheet to new sheet
newSheet.copyFrom(originalSheet);

//Save to file
workbook.saveToFile("output/CopySheetWithinWorkbook.xlsx");
}
}

Output

How to copy a worksheet from an Excel workbook to another workbook

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

public class CopySheetBetweenTwoWorkbooks {
public static void main(String[] args) {
//Create a Workbook object to load the source document
Workbook wb1 = new Workbook();
wb1.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample1.xlsx");
//Get the first worksheet to copy
Worksheet sheet1 = wb1.getWorksheets().get(0);

//Create another Workbook object to load the destination document
Workbook wb2 = new Workbook();
wb2.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample2.xlsx");
//Add the copy of selected sheet to destination document
Worksheet sheet2 = wb2.getWorksheets().get(0);
sheet2.setName("Copied");
sheet2.copyFrom(sheet1);

//Save to another file
wb2.saveToFile("output/CopySheetBetweenTwoWorkbooks.xlsx", FileFormat.Version2013);
}
}

Output




Friday, 17 September 2021

Auto Fit Text or Shape in PowerPoint using Java codes

Among free third-party libraries I used, Free Spire.Presentation for Java supports automatically shrinking text to fit a shape or resizing a shape to fit text by setting AutofitType as NORMAL or SHAPE. Today I’ll introduce how to do so using Java codes.

DEPENDENCY

First of all, we need to add a Jar file called Spire.Presentation.jar to IDEA. There are two methods to do it.

Method One: Download the package of the free library from the link, find Spire.Presentation.jar in the “lib” folder and then manually add it to IDEA.

Method Two: Create a Maven project in IDEA, type the following codes in the pom.xml file and 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

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

public class AutoFitTextOrShape {
public static void main(String[] args) throws Exception {
//create a Presentation instance
Presentation presentation = new Presentation();

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

//add a shape to slide
IAutoShape textShape1 = slide.getShapes().appendShape(ShapeType.RECTANGLE, new Rectangle2D.Float(50,50,200,80));

//add text to shape
textShape1.getTextFrame().setText("Your nose,through thick and thin,remains between your eyes and chin,not pasted on some other place,be glad your nose is on your face!");
//set the auto-fit type to normal, which means the text automatically shrinks to fit the shape when text overflows the shape
textShape1.getTextFrame().setAutofitType(TextAutofitType.NORMAL);

//add another shape to slide
IAutoShape textShape2 = slide.getShapes().appendShape(ShapeType.RECTANGLE, new Rectangle2D.Float(350, 50, 200, 80));
textShape2.getTextFrame().setText("Be glad your nose in on your face");

//set the auto-fit type to shape, which means the shape size automatically decreases or increases to fit text
textShape2.getTextFrame().setAutofitType(TextAutofitType.SHAPE);

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

Output




Monday, 13 September 2021

Create Bulleted Lists, Numbered Lists and Multilevel Lists in Word

In the previous article, I introduced how to create a bulleted list, a numbered list and a multilevel list in PDF using Java codes with the help of a free third-party library called Free Spire.PDF for Java. Today this tutorial will demonstrate how to do the operations above in Word using another free third-party library called Free Spire.Doc for Java.

DEPENDENCY

It is the same as the steps before that we need to add a Jar file to IDEA. We can download the package of the free library from the link, find Spire.Doc.jar in the “lib” folder and then manually add it to IDEA. Or create a Maven project in IDEA, type the following codes in the pom.xml file and finally clicking 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.doc.free</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>

USING THE CODE

Create a bulleted list and a numbered list in Word

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

public class BulletedAndNumberedLists {
public static void main(String[] args) {
//load the Word document
Document document = new Document();
//add a section
Section section = document.addSection();

//add 8 paragraphs
Paragraph paragraph1 = section.addParagraph();
paragraph1.appendText(
"Bulleted List");
paragraph1.applyStyle(BuiltinStyle.
Heading_1);
Paragraph paragraph2 = section.addParagraph();
paragraph2.appendText(
"Chapter 1");
Paragraph paragraph3 = section.addParagraph();
paragraph3.appendText(
"Chapter 2");
Paragraph paragraph4 = section.addParagraph();
paragraph4.appendText(
"Chapter 3");
Paragraph paragraph5 = section.addParagraph();
paragraph5.appendText(
"Numbered List");
paragraph5.applyStyle(BuiltinStyle.
Heading_1);
Paragraph paragraph6 = section.addParagraph();
paragraph6.appendText(
"Chapter 1");
Paragraph paragraph7 = section.addParagraph();
paragraph7.appendText(
"Chapter 2");
Paragraph paragraph8 = section.addParagraph();
paragraph8.appendText(
"Chapter 3");

//create bulleted list for the 2-4 paragraphs
for(int i = 1; i < 4; i++){
Paragraph para = section.getParagraphs().get(i);
para.getListFormat().applyBulletStyle();
para.getListFormat().getCurrentListLevel().setNumberPosition(-
10);
}

//create numbered list for the 6-8 paragraphs
for(int i = 5; i < 8; i++){
Paragraph para = section.getParagraphs().get(i);
para.getListFormat().applyNumberedStyle();
para.getListFormat().getCurrentListLevel().setNumberPosition(-
10);
}

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

Output


Create a multilevel list in Word

import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.*;

public class MultilevelList {
public static void main(String[] args) {
//Create a Document object
Document document = new Document();

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

//Create a ListStyle object
ListStyle listStyle = new ListStyle(document, ListType.Numbered);
listStyle.setName("CustomStyle");

//Set the list pattern type and number prefix of each level
listStyle.getLevels().get(0).setPatternType(ListPatternType.Arabic);
listStyle.getLevels().get(1).setNumberPrefix("\u0000.");
listStyle.getLevels().get(1).setPatternType(ListPatternType.Arabic);
listStyle.getLevels().get(2).setNumberPrefix("\u0000.\u0001.");
listStyle.getLevels().get(2).setPatternType(ListPatternType.Arabic);

//Add the custom list style to the list styles collection
document.getListStyles().add(listStyle);

//Add first paragraph and apply list style to it
//The default list level is the first level if you don't set a list level number
Paragraph paragraph = section.addParagraph();
paragraph.appendText("The first item");
paragraph.applyStyle(BuiltinStyle.Heading_1);
paragraph.getListFormat().applyStyle(listStyle.getName());

//Add second paragraph and apply list style to it
paragraph = section.addParagraph();
paragraph.appendText("The second item");
paragraph.applyStyle(BuiltinStyle.Heading_1);
paragraph.getListFormat().applyStyle(listStyle.getName());

//Add third paragraph, apply list style and set the list level number
paragraph = section.addParagraph();
paragraph.appendText("The first sub-item");
paragraph.applyStyle(BuiltinStyle.Heading_2);
paragraph.getListFormat().setListLevelNumber(1);
paragraph.getListFormat().applyStyle(listStyle.getName());

//Add third paragraph, apply list style and set the list level number
paragraph = section.addParagraph();
paragraph.appendText("The second sub-item");
paragraph.applyStyle(BuiltinStyle.Heading_2);
paragraph.getListFormat().continueListNumbering();
paragraph.getListFormat().applyStyle(listStyle.getName());

//Add forth paragraph, apply list style and set the list level number
paragraph = section.addParagraph();
paragraph.appendText("A sub-sub-item");
paragraph.applyStyle(BuiltinStyle.Heading_5);
paragraph.getListFormat().setListLevelNumber(2);
paragraph.getListFormat().applyStyle(listStyle.getName());

//Add fifth paragraph and apply list style to it
paragraph = section.addParagraph();
paragraph.appendText("The third item");
paragraph.applyStyle(BuiltinStyle.Heading_1);
paragraph.getListFormat().applyStyle(listStyle.getName());

//Save the document
document.saveToFile("output/MultiLevelList.docx", FileFormat.Docx);
}
}

Output



Monday, 6 September 2021

Convert PDF to OFD in Java

Spire.PDF for Java 4.8.7 provides the PdfDocument.saveToFile() method to support converting PDF to OFD. This article will demonstrate how to do it with Java codes.

BEFORE CODING

We need to create a development environment by downloading and installing JDK 1.8.0 and Intellij IDEA. Then add a Jar called Spire.Pdf.jar to IDEA and there are two methods to finish it. One is that downloading the package of Spire.PDF for Java from the link, finding Spire.Pdf.jar in the “lib” folder and then manually adding it to IDEA. The other is that creating a Maven project in IDEA, typing the following codes in the pom.xml file and finally clicking 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.Pdf </artifactId>
<version>4.8.7</version>
</dependency>
</dependencies>

USING THE CODE

import com.spire.pdf.FileFormat;
import com.spire.pdf.PdfDocument;

public class ToOFD {
public static void main(String[] args) {
//Create a PdfDocument instance
PdfDocument pdf = new PdfDocument();
//Load a PDF file
pdf.loadFromFile("C:\\Users\\Test1\\Desktop\\sample.pdf");
//Convert to OFD and save the resulting document to a specified path
pdf.saveToFile("output/ConvertPDFToOFD.ofd", FileFormat.OFD);
}
}

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