Wednesday, 30 March 2022

Set the Slide Size of PowerPoint Documents in Java

While manipulating PowerPoint documents, you may need to change the slide size so as to make your results and infographics truly pop on the screen. Here’s how to programmatically change the slide size using Java codes.

DEPENDENCY

Before running codes, you’re required to add Spire.Presentation.jar to your Java program as a dependency. You can download the product package from this link, and find the JAR file in the lib folder. If you use Maven, you can type the following codes in the pom.xml to easily import the JAR 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.presentation.free</artifactId>
<version>5.1.0</version>
</dependency>
</dependencies>

Set the slide size to a predefined size

Free Spire.Presentation for Java supports changing the type of the slide size using Presentation.getSlideSize().setType() method. The following are detailed steps.

l  Create a Presentation instance.

l  Load a PowerPoint sample document using Presentation.loadFromFile() method.

l  Set the type of the slide size as A3 using Presentation.getSlideSize().setType(SlideSizeType value) method.

l  Save the output document to another file using Presentation.saveToFile() method.

import com.spire.presentation.*;

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

//Load a PowerPoint sample document
presentation.loadFromFile("C:\\Users\\Tina\\Desktop\\sample.pptx");

//Set the type of the slide size as Screen 4x3
presentation.getSlideSize().setType(SlideSizeType.SCREEN_4_X_3);

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

Set the slide size to a predefined size

Free Spire.Presentation for Java supports changing the type of the slide size using Presentation.getSlideSize().setType() method. The following are detailed steps.

l  Create a Presentation instance.

l  Load a PowerPoint sample document using Presentation.loadFromFile() method.

l  Set the type of the slide size as A3 using Presentation.getSlideSize().setType (SlideSizeType value) method.

l  Save the output document to another file using Presentation.saveToFile() method.

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

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

//Load a PowerPoint sample document
presentation.loadFromFile("C:\\Users\\Tina\\Desktop\\sample.pptx");

//Set the slide size type as Custom
presentation.getSlideSize().setType(SlideSizeType.CUSTOM);

//Set the width value and height value
presentation.getSlideSize().setSize(new Dimension(800,400));

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



Sunday, 27 March 2022

Change the Page Size and Page Orientation of a Word Document in Java

Most pages of Word documents are standard paper-size, but you can change the paper size or the page orientation (portrait or landscape) at any time to make the document look the way you want. This article will show you how to programmatically change the page size and page orientation of a Word document using Java codes.

DEPENDENCY

First of all, you need to download the package of a free third-party library called FreeSpire.Doc for Java from this link, and then add Spire.Doc.jar to your Java program. Or if you use Maven, just type the following code in the pom.xml file to easily import the JAR 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.doc.free</artifactId>
<version>5.2.0</version>
</dependency>
</dependencies>

USING THE CODE

 Free Spire.Doc for Java supports changing the default page size using the Section.getPageSetup().setPageSize() method and the default page orientation using Section.getPageSetup().setOrientation() method. Detailed steps are as follows.

l  Create a Document instance.

l  Load a Word sample document using Document.loadFromFile() method.

l  Get the first section of the document using Document.getSections().get() method.

l  Change the default page size using Section.getPageSetup().setPageSize() method.

l  Change the default page orientation using Section.getPageSetup().setOrientation() method.

l  Save the document to another file using Document.saveToFile() method.

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

public class WordPageSetup {
public static void main(String[] args) {
//Create a Document instance
Document doc= new Document();

//Load a Word sample document
doc.loadFromFile("sample.docx");

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

//Change the page size to A3
section.getPageSetup().setPageSize(PageSize.A3);

//Change the page orientation to Landscape
section.getPageSetup().setOrientation(PageOrientation.Landscape);

//Save the document to another file
doc.saveToFile("output/PageSetup.docx",FileFormat.Docx_2013);
}
}



Tuesday, 22 March 2022

Extract Images from a PDF Document in Java

Extracting images from a PDF file is not the same as converting a PDF file to an image. When you extract images from a PDF file, you can save the images to a folder while converting a PDF file to an image will make the entire document an image. The main focus of this article will show you how to programmatically extract images from a PDF document using Java codes.

DEPENDENCY

Before running codes, you’re required to add Spire.Pdf.jar to your Java program. You can get it from this link. Or if you use Maven, just type the following code in the pom.xml file to easily import the JAR 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.pdf.free</artifactId>
<version>5.1.0</version>
</dependency>
</dependencies>

USING THE CODE

The following are detailed steps to extract images from a PDF document.

l  Create a PdfDocument instance and load a PDF sample document using PdfDocument.loadFromFile() method.

l  Loop through all pages of the document and extract images from the given page using PdfPageBase.extractImages() method.

l  Specify the path and name of the output document.

l  Save images as .png files.

import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class ExtractImage {
public static void main(String[] args) throws IOException {
//create a PdfDocument instance
PdfDocument doc = new PdfDocument();

//load a PDF sample file
doc.loadFromFile("sample.pdf");

//declare an int variable
int index = 0;

//loop through all pages
for (PdfPageBase page : (Iterable<PdfPageBase>) doc.getPages()) {

//extract images from the given page
for (BufferedImage image : page.extractImages()) {

//specify the file path and name
File output = new File("C:\\Users\\Tina\\Desktop\\ExtractedImages\\" + String.format("Image_%d.png", index++));

//save images as .png files
ImageIO.write(image, "PNG", output);
}
}
}
}



Friday, 18 March 2022

Add or Delete Comments in Excel worksheets using Java

Image that you have received an Excel document from another person and want to leave your feedback or make corrections. Adding a comment to a particular cell in the Excel worksheet is often the best method as it allows you to attach additional information without changing the original data. This article will demonstrate how to programmatically add or delete comments in Excel worksheets using Java codes.

DEPENDENCY

In this code sample, you need a free third-party library called Free Spire.XLS for Java. Download it from this link, and then add Spire.Xls.jar to your Java program. Or if you use Maven, type the following code in the pom.xml file to easily import the JAR file.

<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>5.1.0</version>
</dependency>
</dependencies>

Add a Comment to an Excel Worksheet

Free Spire.XLS for Java offers the CellRange.addComment() method to add a text comment to an Excel worksheet. The following are detailed steps.

l  Create a Workbook instance.

l  Load an Excel sample document using Workbook.loadFromFile() method.

l  Get a specified worksheet using Workbook.getWorksheets().get() method.

l  Add a comment to a specific cell range using CellRange.addComment() method and then set the comment content through the ExcelComment.setText() method.

l  Save the document to another file using Workbook.saveToFile() method.

import com.spire.xls.*;

public class AddComment {
public static void main(String[] args) {
//Load an Excel sample document
Workbook workbook = new Workbook();
workbook.loadFromFile("C:\\Users\\Tina\\Desktop\\sample.xlsx");

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

//Add regular comment to specific cell range D3
CellRange range = sheet.getCellRange("D3");
ExcelComment comment = range.addComment();
comment.setVisible(true);

//Set the author and comment content
String text = "It's located in the west of the Pacific Ocean";
String author = "Alex:";
comment.setText(author + "\r" + text);

//Save the document to another file
workbook.saveToFile("output/Addcomment.xlsx", ExcelVersion.Version2016);
}
}

Remove the Comment from an Excel Worksheet

The following are steps to remove a comment from an Excel worksheet:

l  Create a Workbook instance.

l  Load an Excel sample file using Workbook.loadFromFile() method.

l  Get a specific worksheet using WorksheetsCollection.get() method.

l  Get a comment in a specific cell range using CellRange.getComment() method and then delete the comment using ExcelCommentObject.remove() method.

l  Save the document to another file using Workbook.saveToFile() method.

import com.spire.xls.*;

public class RemoveComment {
public static void main(String[] args) {
//Create a Workbook instance
Workbook wb = new Workbook();

//Load an Excel sample file
wb.loadFromFile("C:\\Users\\Tina\\Desktop\\Addcomment.xlsx");

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

//Get the comment in a specific cell and remove it
sheet.getRange().get("D3").getComment().remove();

//Save to file
wb.saveToFile("output/DeleteComment.xlsx", ExcelVersion.Version2013);
wb.dispose();
}
}

Monday, 14 March 2022

Embed a Zip File in a PowerPoint Document using Java

ZIP is a file format for storing documents and compressing data, and it is one of several mainstream compression formats. This article will demonstrate how to embed a zip file as an OLE object in a PowerPoint document using Java codes.

DEPENDENCY

In order to finish the operation mentioned above, you need to download a package of Free Spire.Presentation for Java from this link, and then add Spire.Presentation.jar to your Java program. Or if you use Maven, you can type the following codes in the pom.xml file to easily import the Jar 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.presentation.free</artifactId>
<version>5.1.0</version>
</dependency>
</dependencies>

USING THE CODE

Free Spire.Presentation for Java supports embedding a zip file in a new PowerPoint document. The detailed steps are listed below.

l  Create a Presentation instance and set the size of slides using Presentation.getSlideSize().setType() method.

l  Get a specific slide of the document using Presentation.getSlides().get() method.

l  Load a zip file and convert it to a byte[] object.

l  Load an image and add it to the slide as the display icon of the zip file using Presentaion.getImages().append() method.

l  Insert the zip file as an OLE object to the specific slide using ISlide.getShapes().appendOleObject() method.

l  Get the fill properties object of OleObject image using IOleObject.getSubstituteImagePictureFillFormat() method, and then set the embedded image using PictureFillFormat.getPicture().setEmbedImage() method.

l  Save the document to another file using Presentation.saveToFile() method.

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

import javax.imageio.ImageIO;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;

public class InsertZip {
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);

//Load a zip file and convert it to a byte[] object
String filePath = "sample.zip";
File zipFile = new File(filePath);
FileInputStream inputStream = new FileInputStream(zipFile);
byte[] data = new byte[(int) zipFile.length()];
inputStream.read(data, 0, data.length);

//Load an image file as the display icon
File file = new File("Image.jpeg");
BufferedImage image = ImageIO.read(file);
IImageData oleImage = presentation.getImages().append(image);

//Insert the zip file as an OLE object to the first slide
Rectangle2D rect = new Rectangle2D.Float(60, 60, image.getWidth(), image.getHeight());
IOleObject oleObject = slide.getShapes().appendOleObject("zip", data, rect);
oleObject.getSubstituteImagePictureFillFormat().getPicture().setEmbedImage(oleImage);
oleObject.setProgId("Package");

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


Monday, 7 March 2022

Convert XML to PDF in Java

An XML file is an Extensible Markup Language file. It is a plain text file that don’t do anything in and of themselves except describe the transportation, structure and storage of data. Converting XML to PDF can make the file easier to share because PDF is a more common and easy-to-access file format. This article will demonstrate how to programmatically convert XML to PDF using Java codes.

DEPENDENCY

First of all, you're required to get the package of a free third-party library called Free Spire.Doc for Java from this link, and add Spire.Doc.jar as a dependency in your Java program. If you use Maven, you can type the following code in the pom.xml file to import the JAR 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.doc.free</artifactId>
<version>5.2.0</version>
</dependency>
</dependencies>

USING THE CODE

Free Spire.Doc for Java supports converting XML to PDF using the Document.saveToFile() method. You can following the detailed steps below.

l  Create a Document instance.

l  Load an XML sample document using Document.loadFromFile() method.

l  Save the document as a PDF file using Document.saveToFile() method.

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

public class XMLToPDF {
public static void main(String[] args) {
//Create a Document instance
Document document = new Document();
//Load a XML sample document
document.loadFromFile("C:\\Users\\Tina\\Desktop\\sample.xml");
//Save the document to PDF
document.saveToFile("output/XMLToPDF.pdf", FileFormat.PDF );
}
}


Sunday, 6 March 2022

Convert HTML to Images in Java

HTML is commonly used for displaying data and information on websites, web applications, and different platforms. There are some cases when you may need to convert HTML to images like JPG, PNG, TIFF, BMP, etc since images are difficult to modify and can be accessed by anyone. This article will demonstrate how to perform the HTML to images conversion programmatically using Free Spire.Doc for Java.

DEPENDENCY

First of all, you need to get the package of Free Spire.Doc for Java from this link, and then manually add Spire.Doc.jar to your Java program. If you use Maven, you can type the following codes in the pom.xml file to easily import the JAR 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.doc.free</artifactId>
<version>5.2.0</version>
</dependency>
</dependencies>

Convert HTML to Image

 The following steps are to convert a HTML file to an image using Free Spire.Doc for Java.

l  Create a Document instance.

l  Load a HTML sample file using Document.loadFromFile(java.lang.String fileName,FileFormat fileFormat,XHTMLValidationType validationType) method.

l  Save the file to an image using Document.saveToImages() method.

import com.spire.doc.*;
import com.spire.doc.documents.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

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

//Load a HTML sample file
document.loadFromFile("sample.html", FileFormat.Html, XHTMLValidationType.None);

//Save to image. You can convert HTML to BMP, JPEG, PNG, GIF, Tiff etc.
BufferedImage image= document.saveToImages(0, ImageType.Bitmap);
String result = "output/HtmlToImage.png";
File file= new File(result);
ImageIO.write(image, "PNG", file);
}
}



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