Remove Watermark from Word Document in Java
This article demonstrates how to remove watermark including text watermark and image watermark from a Word document using Spire.Doc for Java.
The following screenshot shows the example Word document which contains an image watermark:
import com.spire.doc.Document; import com.spire.doc.FileFormat; public class RemoveWatermark { public static void main(String[] args){ //Create a Document instance Document doc = new Document(); //Load the Word document doc.loadFromFile("ImageWatermark.docx"); //Set the watermark as null to remove the image (or text) watermark doc.setWatermark(null); //Save the document doc.saveToFile("RemoveWatermark.docx", FileFormat.Docx_2013); } }
The following screenshot shows the output Word document after removing watermark:
Create Pivot Table in Excel in Java
This article will demonstrate how to create Pivot Table in Excel by Spire.XLS for Java in Java applications.
import com.spire.xls.*; public class CreatePivotTable { public static void main(String[] args) { //Load a sample Excel workbook Workbook workbook = new Workbook(); workbook.loadFromFile("Sample.xlsx"); //Get the first worksheet Worksheet sheet = workbook.getWorksheets().get(0); //Select the data source range CellRange dataRange = sheet.getCellRange("A1:C19"); PivotCache cache = workbook.getPivotCaches().add(dataRange); //Add a PivotTable to the worksheet and set the location and cache of it PivotTable pt = sheet.getPivotTables().add("Pivot Table", sheet.getCellRange("E10"), cache); //Drag the fields to the row area. PivotField pf=null; if (pt.getPivotFields().get("Name") instanceof PivotField){ pf= (PivotField) pt.getPivotFields().get("Name"); } pf.setAxis(AxisTypes.Row); PivotField pf2 =null; if (pt.getPivotFields().get("Area") instanceof PivotField){ pf2= (PivotField) pt.getPivotFields().get("Area"); } pf2.setAxis(AxisTypes.Row); //Drag the field to the data area. pt.getDataFields().add(pt.getPivotFields().get("Population"), "SUM of Count", SubtotalTypes.Sum); //Set PivotTable style pt.setBuiltInStyle(PivotBuiltInStyles.PivotStyleMedium12); //Save the document workbook.saveToFile("output/CreatePivotTable.xlsx", ExcelVersion.Version2013); } }
Effective screenshot after creating Pivot Table in Excel:
Delete Blank Rows and Columns in Excel in Java
This article demonstrates how to delete blank rows and columns in an Excel document using Spire.XLS for Java.
Sample Document
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 the sample document Workbook wb = new Workbook(); wb.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.xlsx "); //Get the first worksheet Worksheet sheet = wb.getWorksheets().get(0); //Loop through the rows for (int i = sheet.getLastRow(); i >= 1; i--) { //Detect if a row is blank if (sheet.getRows()[i-1].isBlank()) { //Remove blank row sheet.deleteRow(i); } } //Loop through the columns for (int j = sheet.getLastColumn(); j >= 1; j--) { //Detect if a column is blank if (sheet.getColumns()[j-1].isBlank()) { //Remove blank column sheet.deleteColumn(j); } } //Save the document wb.saveToFile("DeleteBlankRowsAndColumns.xlsx", ExcelVersion.Version2016); } }
Output
Set Style and Border of Table in Word in Java
In this article, we will introduce how to set style and border of a Word table using Spire.Doc for Java.
The following screenshot shows the table before setting style and border:
import com.spire.doc.Document; import com.spire.doc.FileFormat; import com.spire.doc.Section; import com.spire.doc.Table; import com.spire.doc.documents.BorderStyle; import com.spire.doc.documents.DefaultTableStyle; import java.awt.*; public class SetTableStyleAndBorder { public static void main(String[] args){ //create a Document instance Document document = new Document(); //Load the Word document document.loadFromFile("tableSample.docx"); Section section = document.getSections().get(0); //get the first table Table table = section.getTables().get(0); //apply the table style table.applyStyle(DefaultTableStyle.Colorful_List); //set right border of table table.getTableFormat().getBorders().getRight().setBorderType(BorderStyle.Hairline); table.getTableFormat().getBorders().getRight().setLineWidth(1.0F); table.getTableFormat().getBorders().getRight().setColor(Color.RED); //set top border of table table.getTableFormat().getBorders().getTop().setBorderType(BorderStyle.Hairline); table.getTableFormat().getBorders().getTop().setLineWidth(1.0F); table.getTableFormat().getBorders().getTop().setColor(Color.GREEN); //set left border of table table.getTableFormat().getBorders().getLeft().setBorderType(BorderStyle.Hairline); table.getTableFormat().getBorders().getLeft().setLineWidth(1.0F); table.getTableFormat().getBorders().getLeft().setColor(Color.YELLOW); //set bottom border of table table.getTableFormat().getBorders().getBottom().setBorderType(BorderStyle.Dot_Dash); //set vertical and horizontal border table.getTableFormat().getBorders().getVertical().setBorderType(BorderStyle.Dot); table.getTableFormat().getBorders().getHorizontal().setBorderType(BorderStyle.None); table.getTableFormat().getBorders().getVertical().setColor(Color.ORANGE); //save the result file document.saveToFile("setTableStyleAndBorder.docx", FileFormat.Docx_2013); } }
The following screenshot shows the table after setting style and border:
How to Repeat an Animation in PowerPoint in Java
By default, an animation plays only one time and does not repeat. However, we can make the animation to play more than once by setting the repeat type of it. This article demonstrates how to accomplish this function using Spire.Presentation for Java.
import com.spire.presentation.*; import com.spire.presentation.drawing.animation.AnimationEffect; public class RepeatAnimation { public static void main(String[] args) throws Exception { //Create a Presentation instance Presentation ppt = new Presentation(); //Load a PowerPoint document ppt.loadFromFile("Animation.pptx"); //Get the first slide ISlide slide = ppt.getSlides().get(0); //Get the first animation effect on the slide AnimationEffect animation = slide.getTimeline().getMainSequence().get(0); //Set the animation effect to repeat forever until the end of slide. animation.getTiming().setAnimationRepeatType(AnimationRepeatType.UtilEndOfSlide); //Save the result document ppt.saveToFile("RepeatAnimation.pptx", FileFormat.PPTX_2013); } }
Output:
Insert Subscript and Superscript in Excel in Java
This article demonstrates how to insert subscript and superscript in an Excel document using Spire.XLS for Java.
import com.spire.xls.*; import java.awt.*; public class InsertSubscriptSuperscript { 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 to B2 and D2 sheet.getCellRange("B2").setText("This is an example of Subscript:"); sheet.getCellRange("D2").setText("This is an example of Superscript:"); //Insert text to B3 and apply subscript effect CellRange range = sheet.getCellRange("B3"); range.getRichText().setText("R100-0.06"); ExcelFont font = workbook.createFont(); font.isSubscript(true); font.setColor(Color.red); range.getRichText().setFont(4, 8, font); //Insert text to D3 and apply superscript effect range = sheet.getCellRange("D3"); range.getRichText().setText("a2 + b2 = c2"); font = workbook.createFont(); font.isSuperscript(true); range.getRichText().setFont(1, 1, font); range.getRichText().setFont(6, 6, font); range.getRichText().setFont(11, 11, font); //Auto fit column width sheet.getAllocatedRange().autoFitColumns(); //Save the docuemnt workbook.saveToFile("output/SubSuperScript.xlsx", ExcelVersion.Version2016); } }
How to set the layout of the slide in Java
This article will demonstrate how to set the layout of the slide via Spire.Presentation in Java applications. There are 11 kinds of layout on Microsoft PowerPoint and Spire.Presentation supports all of them.
Set one layout of the slide
import com.spire.presentation.*; public class setSlideLayout { public static void main(String[] args) throws Exception{ //Create an instance of presentation document Presentation ppt = new Presentation(); //Remove the default slide ppt.getSlides().removeAt(0); //Append a slide and set the layout for slide ISlide slide = ppt.getSlides().append(SlideLayoutType.TITLE); //Add content for Title and Text IAutoShape shape = (IAutoShape)slide.getShapes().get(0); shape.getTextFrame().setText("Spire.Presentation"); shape = (IAutoShape)slide.getShapes().get(1); shape.getTextFrame().setText("Set the Layout of Slide as Title"); //Save the document ppt.saveToFile("SlideLayout.pptx", FileFormat.PPTX_2013); ppt.dispose(); } }
Set the different layout of the slides
import com.spire.presentation.*; public class setDifferentSlideLayout { public static void main(String[] args) throws Exception{ //Create a PPT document Presentation presentation = new Presentation(); //Remove the default slide presentation.getSlides().removeAt(0); //Loop through slide layouts for (SlideLayoutType type : SlideLayoutType.values()) { //Append slide by specified slide layout presentation.getSlides().append(type); } //Save the document presentation.saveToFile("Result.pptx", FileFormat.PPTX_2013); presentation.dispose(); } }
Effective screenshot:
Add and Remove Form Controls in Excel in Java
Spire.XLS for Java enables developers to add and manipulate multiple types of form controls, e.g. text box, option button, check box and combo box in Excel files in Java applications.
The following examples will show you how to add and remove text box, option button, check box and combo box form controls in an Excel file using Spire.XLS for Java.
Add Form Controls
import com.spire.xls.*; import com.spire.xls.core.*; import java.awt.*; public class AddFormControls { 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("AddControls.xlsx", ExcelVersion.Version2013); } }
Remove Form Controls
import com.spire.xls.*; public class RemoveFormControls { public static void main(String[] args){ //Load an Excel file Workbook workbook = new Workbook(); workbook.loadFromFile("AddControls.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
Java find and highlight data in Excel
With Spire.XLS for java, we could find and replace data in Excel easily. This article will demonstrate how to find and highlight data in Excel.
Firstly, view the sample document:
import com.spire.xls.*; import java.awt.*; public class FindandHighlight { public static void main(String[] args) { //Load the sample document Workbook workbook = new Workbook(); workbook.loadFromFile("Sample.xlsx"); //Get the second worksheet Worksheet worksheet = workbook.getWorksheets().get(1); //Find the text string "Mexico" CellRange[] ranges = worksheet.findAllString("Mexico", true, true); for (CellRange range : ranges) { //Set the color to highlight the text range.getCellStyle().setColor(Color.yellow); } //Save the document to file workbook.saveToFile("output/FindandHighlight.xlsx", ExcelVersion.Version2010); } }
Effective screenshot after find and highlight the data in Excel:
Copy Cell Range in Excel in Java
This article demonstrates how to copy a cell range within a worksheet or between two worksheets in a single Excel document by using Spire.XLS for Java.
Copy a cell range within a worksheet
import com.spire.xls.ExcelVersion; import com.spire.xls.Workbook; import com.spire.xls.Worksheet; public class CopyRow { public static void main(String[] args) { //Create a Workbook instance Workbook wb = new Workbook(); //Load a sample Excel file wb.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.xlsx", ExcelVersion.Version2013); //Get the first worksheet Worksheet sheet = wb.getWorksheets().get(0); //Copy a cell range within a worksheet sheet.copy(sheet.getCellRange("A1:H1"),sheet.getCellRange("A10:H10"),true); //Save the document wb.saveToFile("CopyRangeWithinSheet.xlsx", ExcelVersion.Version2013); } }
Copy a cell range from one worksheet to another
import com.spire.xls.ExcelVersion; import com.spire.xls.Workbook; import com.spire.xls.Worksheet; public class CopyRow { public static void main(String[] args) { //Create a Workbook instance Workbook wb = new Workbook(); //Load a sample Excel file wb.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.xlsx", ExcelVersion.Version2013); //Get the first worksheet Worksheet sheet1 = wb.getWorksheets().get(0); //Get the second worksheet Worksheet sheet2 = wb.getWorksheets().get(1); //Copy a cell range from sheet 1 to sheet 2 sheet1.copy(sheet1.getCellRange("A1:H1"),sheet2.getCellRange("A1:H1"),true); //Auto fit column width in sheet 2 for (int i = 0; i < 8; i++) { sheet2.autoFitColumn(i+1); } //Save the document wb.saveToFile("CopyRangeBetweenSheets.xlsx", ExcelVersion.Version2013); } }