Tuesday, 31 August 2021

Add Header and Footer to Excel Worksheets in JAVA

Headers and footers can define your Excel file while you print it, and they can show page numbers, file names, dates, or any other information you like to have on your printed file. Meanwhile, please note that headers and footers are only visible in Print Preview and Page Layout View. You won’t see them in Normal View worksheets.

This article will show you how to add header and footer to Excel using Java codes from the following aspects.

l  Add image header to Excel

l  Add text footer to Excel

l  Add different header and footer for Odd and Even pages

l  Add different header and footer for first page and other pages

DEPENDENCY

I used a free third-party library called Free Spire.XLS for Java to finish operations above. Before running codes, we need to add Spire.Xls.jar to IDEA. 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 CODES

Add image header to Excel

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

public class ImageHeader {
public static void main(String[] args) throws IOException {
//Create a workbook and load a file
Workbook workbook = new Workbook();

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

//Load an image from disk
BufferedImage image = ImageIO.read( new File("C:\\Users\\Test1\\Desktop\\Image.png"));

//Set the image header
worksheet.getPageSetup().setLeftHeaderImage(image);
worksheet.getPageSetup().setLeftHeader("&G");

//Set the view mode as layout
worksheet.setViewMode(ViewMode.Layout);

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

Output


Add text footer to Excel

import com.spire.xls.*;
public class TextFooter {
public static void main(String[] args) {
//Create a workbook and load a file
Workbook workbook = new Workbook();

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

//Set center footer
worksheet.getPageSetup().setCenterFooter("technical demo");

//Set the view mode as layout
worksheet.setViewMode(ViewMode.Layout);

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

Output


Add different header and footer for Odd and Even pages

import com.spire.xls.*;

public class HeaderFooter {
public static void main(String[] args) {
//Create a workbook and load a file
Workbook workbook = new Workbook();

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

worksheet.getCellRange("A1").setText("Page 1");
worksheet.getCellRange("J1").setText("Page 2");

//Set different header footer for Odd and Even pages
worksheet.getPageSetup().setDifferentOddEven((byte)1);

//Set the header footer with font, size, bold and color
worksheet.getPageSetup().setOddHeaderString( "&\"Arial\"&12&B&KFFC000 Odd_Header");
worksheet.getPageSetup().setOddFooterString ( "&\"Arial\"&12&B&KFFC000 Odd_Footer");
worksheet.getPageSetup().setEvenHeaderString ( "&\"Arial\"&12&B&KFF0000 Even_Header");
worksheet.getPageSetup().setEvenFooterString ( "&\"Arial\"&12&B&KFF0000 Even_Footer");

//Set the view mode as layout
worksheet.setViewMode(ViewMode.Layout);

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

Output




Add different header and footer for first page and other pages

import com.spire.xls.FileFormat;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
public class HeaderFooter2 {
public static void main(String[] args) {
//Create a Workbook instance
Workbook workbook = new Workbook();

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

//Insert text in A1 and J1
sheet.getCellRange("A1").setText("page 1");
sheet.getCellRange("J1").setText("page 2");

//Set different first page
sheet.getPageSetup().setDifferentFirst((byte)1);

//Set header string and footer string for the first page
sheet.getPageSetup().setFirstHeaderString("First header");
sheet.getPageSetup().setFirstFooterString("First footer");

//Set header string and footer string for other pages
sheet.getPageSetup().setCenterHeader("Header of other pages");
sheet.getPageSetup().setCenterFooter("Footer of other pages");

//Save the document
workbook.saveToFile("output/DifferentFirstPage.xlsx", FileFormat.Version2016);
}
}
Output


Thursday, 26 August 2021

Create bulleted lists, numbered lists and multilevel lists in PDF

A bulleted list is an unordered list which consists of items introduced by special symbols, a numbered list is an ordered list which contains numbers indicating the order of items and a multilevel list is a list of items which can contain other lists and every level of a multilevel list can be formatted by using various number formats including Arabic numerals, Roman numerals, letters and a combination of numbered and bulleted lists.

This article will show you 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.

DEPENDENCY

Before running codes, we need to add a Jar file called Spire.Pdf.jar to IDEA. There are two methods to do it. One is that downloading the package of the free library 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.free</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>

RUNNING CODES

Create a bulleted list in PDF using Java codes

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

public class BulletList {
public static void main(String[] args) {
//Create a PdfDocument object
PdfDocument doc = new PdfDocument();
//Set the margin
PdfMargins margin = new PdfMargins(60,60,40,40);
//Create one page
PdfPageBase page = doc.getPages().add(PdfPageSize.A4, margin);
float y = 10;
//Draw title
PdfBrush brush1 = PdfBrushes.getBlack();
PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", Font.BOLD, 16), true);
PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Center);
page.getCanvas().drawString("Categories List", font1, brush1, page.getCanvas().getClientSize().getWidth() / 2, y, format1);
y = y + (float) font1.measureString("Categories List", format1).getHeight();
y = y + 5;

//Draw text string and set the font
Rectangle2D rctg = new Rectangle2D.Float();
rctg.setFrame(new Point(0, 0), page.getCanvas().getClientSize());
PdfLinearGradientBrush brush = new PdfLinearGradientBrush(rctg, new PdfRGBColor(new PdfRGBColor(new Color(0,0,128))), new PdfRGBColor(new Color(255,69,0)), PdfLinearGradientMode.Vertical);
PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 12f, PdfFontStyle.Bold);
String formatted = "Beverages\nCondiments\nConfections\nDairy Products\nGrains/Cereals\nMeat/Poultry\nProduce\nSeafood";

//Create a bullet list
PdfListBase list = new PdfUnorderedList(formatted);
list.setFont(font);
list.setIndent(8);
list.setTextIndent(5);
list.setBrush(brush);
PdfLayoutResult result = list.draw(page, 0, y);

doc.saveToFile("output/BulletList.pdf");
doc.close();
}
}

Output


Create a numbered list in PDF using Java codes

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

public class NumberList {
public static void main(String[] args) {
//Create a PdfDocument object
PdfDocument doc = new PdfDocument();
//Set the margin
PdfMargins margin = new PdfMargins(60,60,40,40);
//Create one page
PdfPageBase page = doc.getPages().add(PdfPageSize.A4, margin);
float y = 10;
//Draw title
PdfBrush brush1 = PdfBrushes.getBlack();
PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", Font.BOLD, 16), true);
PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Center);
page.getCanvas().drawString("Categories List", font1, brush1, page.getCanvas().getClientSize().getWidth() / 2, y, format1);
y = y + (float) font1.measureString("Categories List", format1).getHeight();
y = y + 5;

//Draw text string and set the font
Rectangle2D rctg = new Rectangle2D.Float();
rctg.setFrame(new Point(0, 0), page.getCanvas().getClientSize());
PdfLinearGradientBrush brush = new PdfLinearGradientBrush(rctg, new PdfRGBColor(new PdfRGBColor(new Color(0,0,128))), new PdfRGBColor(new Color(255,69,0)), PdfLinearGradientMode.Vertical);
PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 12f, PdfFontStyle.Bold);
String formatted = "Beverages\nCondiments\nConfections\nDairy Products\nGrains/Cereals\nMeat/Poultry\nProduce\nSeafood";

//Create a number list
PdfSortedList list = new PdfSortedList(formatted);
list.setFont(font);
list.setIndent(8);
list.setTextIndent(5);
list.setBrush(brush);
PdfLayoutResult result = list.draw(page, 0, y);

doc.saveToFile("output/numberlist.pdf");
doc.close();
}
}
Output

Create a multilevel list in PDF using Java codes

import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfNumberStyle;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.PdfPageSize;
import com.spire.pdf.graphics.*;
import com.spire.pdf.lists.PdfListItem;
import com.spire.pdf.lists.PdfOrderedMarker;
import com.spire.pdf.lists.PdfSortedList;
import java.awt.*;
import java.awt.geom.Point2D;


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

//Set the margin
PdfMargins margin = new PdfMargins(60, 60, 40, 40);

//Create one page
PdfPageBase page = doc.getPages().add(PdfPageSize.A4, margin);

//Specify the initial coordinate
float x = 0;
float y = 15;

//Create two brushed
PdfBrush blackBrush = PdfBrushes.getBlack();
PdfBrush purpleBrush = PdfBrushes.getPurple();

//Create two fonts
PdfTrueTypeFont titleFont = new PdfTrueTypeFont(new java.awt.Font("Times New Roman", Font.BOLD, 12));
PdfTrueTypeFont listFont = new PdfTrueTypeFont(new java.awt.Font("Calibri Light", Font.PLAIN, 12));

//Draw title
String title = "XHTML Tutorials/FAQs:";
page.getCanvas().drawString(title, titleFont, blackBrush, x, y);
y = y + (float) titleFont.measureString(title).getHeight();
y = y + 5;

//Create two ordered makers, which are used to define the number style of sorted list
PdfOrderedMarker marker1 = new PdfOrderedMarker(PdfNumberStyle.Upper_Roman, listFont);
PdfOrderedMarker marker2 = new PdfOrderedMarker(PdfNumberStyle.Numeric, listFont);

//Create a parent list
String parentListContent = "Introduction To XHTML 1.0\n"
+ "Introduction To Tag and Attribute Syntax";
PdfSortedList parentList = new PdfSortedList(parentListContent);
parentList.setFont(listFont);
parentList.setIndent(8);
parentList.setBrush(purpleBrush);
parentList.setMarker(marker1);

//Create a sub list - "subList_1"
String subListContent_1 = "What Is XHTML?\n"
+ "What Does an XHMTL Document Look Like?\n"
+ "What Is the Relation between XHTML and HTML?\n"
+ "What Is the Relation between XHTML and XML?";
PdfSortedList subList_1 = new PdfSortedList(subListContent_1);
subList_1.setIndent(16);
subList_1.setFont(listFont);
subList_1.setBrush(purpleBrush);
subList_1.setMarker(marker2);

//Create another sub list -"subList_2"
String subListContent_2 = "What Is an XHTML Element?\n"
+ "How To Enter Comments into XHTML Documents?\n"
+ "How To Write the Opening Tag of an XHTML Element?";
PdfSortedList subList_2 = new PdfSortedList(subListContent_2);
subList_2.setIndent(16);
subList_2.setFont(listFont);
subList_2.setBrush(purpleBrush);
subList_2.setMarker(marker2);

//Set subList_1 as sub list of the first item of parent list
PdfListItem item_1 = parentList.getItems().get(0);
item_1.setSubList(subList_1);

//Set subList_2 as sub list of the second item of parent list
PdfListItem item_2 = parentList.getItems().get(1);
item_2.setSubList(subList_2);

//Draw parent list
PdfTextLayout textLayout = new PdfTextLayout();
textLayout.setBreak(PdfLayoutBreakType.Fit_Page);
textLayout.setLayout(PdfLayoutType.Paginate);
parentList.draw(page,new Point2D.Float(x,y),textLayout);

//Save to file
doc.saveToFile("output/MultiLevelList.pdf");
}
}
Output


Tuesday, 24 August 2021

Insert Audio and Video into PowerPoint slides using Java

We often need to create PowerPoint presentations for work, study or other reasons. Actually, in the media-rich world where we live, presentations are no longer a simple series of slides filled with text and pictures, and we can also insert video and audio into them. This tutorial will show you how it is done using Java codes with the help of a free third-party library called Free Spire.Presentation for Java.

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 CODES

Insert Audio into PowerPoint Slide using Java Codes

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

public class InsertAudio {
public static void main(String[] args) throws Exception {
//create a Presentation object and load an example PowerPoint file
Presentation presentation = new Presentation();
presentation.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample.pptx");

//add a shape to the first slide
Rectangle2D.Double labelRect= new Rectangle2D.Double(50, 120, 100, 50);
IAutoShape labelShape = presentation.getSlides().get(0).getShapes().appendShape(ShapeType.RECTANGLE, labelRect);
labelShape.getLine().setFillType(FillFormatType.NONE);
labelShape.getFill().setFillType(FillFormatType.NONE);
labelShape.getTextFrame().setText("Audio:");
labelShape.getTextFrame().getTextRange().setFontHeight(28);
labelShape.getTextFrame().getTextRange().setLatinFont(new TextFont("Myriad Pro Light"));
labelShape.getTextFrame().getTextRange().getFill().setFillType(FillFormatType.SOLID);
labelShape.getTextFrame().getTextRange().getFill().getSolidColor().setColor(Color.BLACK);

//append an audio file to the slide
Rectangle2D.Double audioRect = new Rectangle2D.Double(170, 120, 50, 50);
presentation.getSlides().get(0).getShapes().appendAudioMedia((new java.io.File("C:\\Users\\Test1\\Desktop\\Music.wav")).getAbsolutePath(), audioRect);

//save to file
presentation.saveToFile("output/InsertAudio.pptx", FileFormat.PPTX_2010);
presentation.dispose();
}
}

Output


Insert Video into PowerPoint Slide Using Java Codes

import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;

public class InsertVideo {
public static void main(String[] args) throws Exception {
//create a Presentation object and load an example PowerPoint file
Presentation presentation = new Presentation();
presentation.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample.pptx");

//add a shape to the first slide
Rectangle2D.Double labelRect = new Rectangle2D.Double(50, 120, 100, 50);
IAutoShape labelShape = presentation.getSlides().get(0).getShapes().appendShape(ShapeType.RECTANGLE, labelRect);
labelShape.getLine().setFillType(FillFormatType.NONE);
labelShape.getFill().setFillType(FillFormatType.NONE);
labelShape.getTextFrame().setText("Video:");
labelShape.getTextFrame().getTextRange().setFontHeight(28);
labelShape.getTextFrame().getTextRange().setLatinFont(new TextFont("Myriad Pro Light"));
labelShape.getTextFrame().getTextRange().getFill().setFillType(FillFormatType.SOLID);
labelShape.getTextFrame().getTextRange().getFill().getSolidColor().setColor(Color.BLACK);

//append a video file to the slide and set the cover image
Rectangle2D.Double videoRect = new Rectangle2D.Double(150, 120, 400, 225);
IVideo video = presentation.getSlides().get(0).getShapes().appendVideoMedia((new java.io.File("C:\\Users\\Test1\\Desktop\\Video.mp4")).getAbsolutePath(), videoRect);
BufferedImage coverImage = ImageIO.read( new File("C:\\Users\\Test1\\Desktop\\image.png"));
video.getPictureFill().getPicture().setEmbedImage(presentation.getImages().append(coverImage));

//save to file
presentation.saveToFile("output/InsertVideo.pptx", FileFormat.PPTX_2010);
presentation.dispose();
}
}

Output



Thursday, 19 August 2021

Add, Count, Retrieve and Remove Variables in Word using Java

In a Word document, a variable is seen as a container storing information which may change frequently and using variables in source documents allows you to quickly and easily control the content in generated documents. This article will demonstrate how to use Java codes to add a variable to Word, count the number of variables in Word, retrieve the name and value of variables and remove specified variables with the help of Free Spire.Doc for Java.

BEFORE CODES

Before running codes, we need to create a running environment by installing JDK 1.8.0 and Intellij IDEA. The next thing is adding a Jar file to IDEA and there are two methods to do it. One is getting the package of the library from the link, finding Spire.Xls.jar in the “lib” folder and then manually adding it to IDEA. The other is that 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>
<url>http://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc.free</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>

RUNNING THE CODE

Add a Variable

The following code snippets add a document variable named "A1" with a value of 12 to a Word document.

import com.spire.doc.Document;
import com.spire.doc.FieldType;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;

public class AddVariables {
public static void main(String[] args) {
//Create a Document instance
Document document = new Document();
//Add a section
Section section = document.addSection();

//Add a paragraph to the section
Paragraph paragraph = section.addParagraph();

//Add a DocVariable field to the paragraph
paragraph.appendField("A1", FieldType.Field_Doc_Variable);

//Add a document variable to the DocVariable field
document.getVariables().add("A1", "12");

//Update fields in the document
document.isUpdateFields(true);

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

Output


Count the number of Variables

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

//Get the number of variables in the document
int number = document.getVariables().getCount();

StringBuilder content = new StringBuilder();
content.append("The number of variables is: " + number);

System.out.println(content.toString());
}
}

Output


Retrieve Name and Value of a Variable

import com.spire.doc.Document;

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

//Retrieve the name of a variable by index
String s1 = document.getVariables().getNameByIndex(0);

//Retrieve the value of a variable by index
String s2 = document.getVariables().getValueByIndex(0);

//Retrieve the value of a variable by name
String s3 = document.getVariables().get("A1");

System.out.println("The name of the variable retrieved by index 0 is: " + s1);
System.out.println("The value of the variable retrieved by index 0 is: " + s2);
System.out.println("The value of the variable retrieved by name \"A1\" is: " + s3);
}
}

Output


Remove a specific Variable

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

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

//Remove a variable by name
document.getVariables().remove("A1");

//Update fields in the document
document.isUpdateFields (true);

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

Tuesday, 17 August 2021

Add and Remove Form Controls in Excel using Java

Recently I used a free third-party library called Free Spire.XLS for Java and it supports adding and manipulating multiple types of form controls, such as text box, option button, check box and combo box in Excel files using Java codes. Today this article will introduce how to do it.

First of all, we need to create a running environment by installing JDK 1.8.0 and Intellij IDEA 2019. Next, add a Jar file called Spire.Xls.jar to IDEA. There are two methods to do it. One is that get the package of the library from the link, find Spire.Xls.jar in the “lib” folder and then manually add it to IDEA. The other is that 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>
<url>http://repo.e-iceblue.cn/repository/maven-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

Add Form Controls to an Excel Worksheet using Java

import com.spire.xls.*;
import com.spire.xls.core.*;
import java.awt.*;

public class AddFormControl {
public static void main(String[] args) {
//Create a Workbook instance
Workbook workbook = new Workbook();
//Get the first worksheet
Worksheet sheet = workbook.getWorksheets().get(0);

sheet.getCellRange("A2").setText("Name: ");
//Add a text box
ITextBoxShape textbox = sheet.getTextBoxes().addTextBox(2, 2, 18, 65);
textbox.setText("Shaun");
textbox.getFill().setForeColor(Color.PINK);
textbox.setHAlignment(CommentHAlignType.Center);
textbox.setVAlignment(CommentVAlignType.Center);

sheet.getCellRange("A4").setText("Gender: ");
//Add an option button
IRadioButton radiobutton1 = sheet.getRadioButtons().add(4, 2, 18, 65);
radiobutton1.setText("Male");
//Add an option button
IRadioButton radiobutton2 = sheet.getRadioButtons().add(4, 4, 18, 65);
radiobutton2.setText("Female");

sheet.getCellRange("A6").setText("Hobby: ");
//Add a check box
ICheckBox checkbox1 = sheet.getCheckBoxes().addCheckBox(6, 2, 18, 100);
checkbox1.setCheckState(CheckState.Checked);
checkbox1.setText("Photography");
//Add a check box
ICheckBox checkbox2 = sheet.getCheckBoxes().addCheckBox(6, 4, 18, 65);
checkbox2.setCheckState(CheckState.Checked);
checkbox2.setText("Travel");

sheet.getCellRange("A8").setText("Profession: ");
sheet.getCellRange("A20").setText("Student");
sheet.getCellRange("A21").setText("Teacher");
sheet.getCellRange("A22").setText("Doctor");
//Add a combo box
IComboBoxShape combobox = sheet.getComboBoxes().addComboBox(8, 2, 18, 65);
combobox.setListFillRange(sheet.getCellRange("A20:A22"));
combobox.setSelectedIndex(1);

for (int column = 1; column < 5; column ++)
{
sheet.setColumnWidth(column, 15f);
}

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

Output


Remove
Form Controls from an Excel Worksheet

import com.spire.xls.*;
public class RemoveFormControl {
public static void main(String[] args) {
//Load an Excel file
Workbook workbook = new Workbook();
workbook.loadFromFile("C:\\Users\\Test1\\Desktop\\AddFormControls.xlsx");
//Get the first worksheet
Worksheet sheet = workbook.getWorksheets().get(0);

//Remove option buttons from the worksheet
for(int j = 0; j < sheet.getRadioButtons().getCount(); j ++){
sheet.getRadioButtons().get(j).remove();
}

//Remove check boxes from the worksheet
for(int i = 0; i < sheet.getCheckBoxes().getCount(); i ++){
sheet.getCheckBoxes().get(i).remove();
}

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

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