Set and Get Slide Title in PowerPoint in Java
This article demonstrates how to set and get slide title in a PowerPoint document using Spire.Presentation for Java.
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("Tile Text"); //Save the result document ppt.saveToFile("SetTitle.pptx", FileFormat.PPTX_2013); } }
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("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); } }
Java: Hide or Show Gridlines in Excel
Gridlines are horizontal and vertical faint lines that differentiate between cells in a worksheet. All Excel worksheets have gridlines by default, but sometimes you may need to remove the gridlines as they might interfere with your work. In this article, you will learn how to programmatically show or hide/remove gridlines in an Excel worksheet using Spire.XLS for Java.
Install Spire.XLS for Java
First, you're required to add the Spire.Xls.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml 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.xls</artifactId> <version>14.3.2</version> </dependency> </dependencies>
Hide or Show Gridlines in Excel
The detailed steps are as follows.
- Create a Workbook object.
- Load a sample Excel document using Workbook.loadFromFile() method.
- Get a specified worksheet using Workbook.getWorksheets().get() method.
- Hide or show gridlines in the specified worksheet using Worksheet.setGridLinesVisible() method.
- Save the result file using Workbook.saveToFile() method.
- Java
import com.spire.xls.ExcelVersion; import com.spire.xls.Workbook; import com.spire.xls.Worksheet; public class HideOrShowGridlines { public static void main(String[] args) { //Create a Workbook object Workbook workbook = new Workbook(); //Load a sample Excel document workbook.loadFromFile("E:\\Files\\Test.xlsx"); //Get the first worksheet Worksheet worksheet = workbook.getWorksheets().get(0); //Hide gridlines worksheet.setGridLinesVisible(false); ////Show gridlines //worksheet.setGridLinesVisible(true); //Save the document workbook.saveToFile("HideGridlines.xlsx", ExcelVersion.Version2016); } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
Java extract text from SmartArt in PowerPoint
This article will introduce how to extract text from SmartArt in PowerPoint in Java applications.
Firstly view the sample document:
import com.spire.presentation.Presentation; import com.spire.presentation.diagrams.ISmartArt; import java.io.*; public class extractTextFromSmartArt { public static void main(String[] args) throws Exception { Presentation presentation = new Presentation(); presentation.loadFromFile("Sample.pptx"); //Create a new TXT File String result = "output/extractTextFromSmartArt.txt"; File file=new File(result); if(file.exists()){ file.delete(); } file.createNewFile(); FileWriter fw =new FileWriter(file,true); BufferedWriter bw =new BufferedWriter(fw); bw.write("Below is extracted text from SmartArt:" + "\r\n"); //Traverse through all the slides of the PPT file and find the SmartArt shapes. for (int i = 0; i < presentation.getSlides().getCount(); i++) { for (int j = 0; j < presentation.getSlides().get(i).getShapes().getCount(); j++) { if (presentation.getSlides().get(i).getShapes().get(j) instanceof ISmartArt) { ISmartArt smartArt = (ISmartArt)presentation.getSlides().get(i).getShapes().get(j); //Extract text from SmartArt and append to the StringBuilder object. for (int k = 0; k < smartArt.getNodes().getCount(); k++) { bw.write(smartArt.getNodes().get(k).getTextFrame().getText() + "\r\n"); } } } } bw.flush(); bw.close(); fw.close(); } }
Effective screenshot of the extracting Text from SmartArt:
Java: Change Font Color in Word
Changing the font color of a certain paragraph or text can help you make the paragraph or text stand out in your Word document. In this article, we will demonstrate how to change the font color in Word in Java using Spire.Doc for Java library.
Install Spire.Doc for Java
First of all, you're required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml 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</artifactId> <version>12.3.1</version> </dependency> </dependencies>
Change Font Color of a Paragraph in Java
The following are the steps to change the font color of a paragraph in a Word document:
- Create a Document instance.
- Load the Word document using Document.LoadFromFile() method.
- Get the desired section using Document.getSections().get(sectionIndex) method.
- Get the desired paragraph that you want to change the font color of using Section.getParagraphs().get(paragraphIndex) method.
- Create a ParagraphStyle instance.
- Set the style name and font color using ParagraphStyle.setName() and ParagraphStyle.getCharacterFormat().setTextColor() methods.
- Add the style to the document using Document.getStyles().add() method.
- Apply the style to the paragraph using Paragraph.applyStyle() method.
- Save the result document using Document.saveToFile() method.
- Java
import com.spire.doc.Document; import com.spire.doc.FileFormat; import com.spire.doc.Section; import com.spire.doc.documents.Paragraph; import com.spire.doc.documents.ParagraphStyle; import java.awt.*; public class ChangeFontColorForParagraph { public static void main(String []args){ //Create a Document instance Document document = new Document(); //Load a Word document document.loadFromFile("Sample.docx"); //Get the first section Section section = document.getSections().get(0); //Change text color of the first Paragraph Paragraph p1 = section.getParagraphs().get(0); ParagraphStyle s1 = new ParagraphStyle(document); s1.setName("Color1"); s1.getCharacterFormat().setTextColor(new Color(188, 143, 143)); document.getStyles().add(s1); p1.applyStyle(s1.getName()); //Change text color of the second Paragraph Paragraph p2 = section.getParagraphs().get(1); ParagraphStyle s2 = new ParagraphStyle(document); s2.setName("Color2"); s2.getCharacterFormat().setTextColor(new Color(0, 0, 139));; document.getStyles().add(s2); p2.applyStyle(s2.getName()); //Save the result document document.saveToFile("ChangeParagraphTextColor.docx", FileFormat.Docx); } }
Change Font Color of a Specific Text in Java
The following are the steps to change the font color of a specific text in a Word document:
- Create a Document instance.
- Load a Word document using Document.loadFromFile() method.
- Find the text that you want to change font color of using Document.findAllString() method.
- Loop through all occurrences of the searched text and change the font color for each occurrence using TextSelection.getAsOneRange().getCharacterFormat().setTextColor() method.
- Save the result document using Document.saveToFile() method.
- Java
import com.spire.doc.Document; import com.spire.doc.FileFormat; import com.spire.doc.documents.TextSelection; import java.awt.*; public class ChangeFontColorForText { public static void main(String []args){ //Create a Document instance Document document = new Document(); //Load a Word document document.loadFromFile("Sample.docx"); //Find the text that you want to change font color for TextSelection[] text = document.findAllString("Spire.Doc for .NET", false, true); //Change the font color for the searched text for (TextSelection seletion : text) { seletion.getAsOneRange().getCharacterFormat().setTextColor(Color.red); } //Save the result document document.saveToFile("ChangeCertainTextColor.docx", FileFormat.Docx); } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
Set Different Header and Footer for the First Page in Java
This article demonstrates how to set different header and footer for the fisrt page using Spire.XLS for Java.
import com.spire.xls.FileFormat; import com.spire.xls.Workbook; import com.spire.xls.Worksheet; public class SetDifferentHeaderFooter { 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("DifferentFirstPage.xlsx", FileFormat.Version2016); } }
Java add the traffic lights icons to Excel
This article will demonstrate how to add the traffic lights icons in Java applications by using Spire.XLS for Java.
import com.spire.xls.*; import com.spire.xls.core.IConditionalFormat; import com.spire.xls.core.spreadsheet.collections.XlsConditionalFormats; import java.awt.*; public class setTrafficLightsIcons { public static void main(String[] args) { //Create a workbook Workbook workbook = new Workbook(); //Add a worksheet Worksheet sheet = workbook.getWorksheets().get(0); //Add some data to the cell range and set the format for them sheet.getCellRange("A1").setText("Traffic Lights"); sheet.getCellRange("A2").setNumberValue(0.95); sheet.getCellRange("A2").setNumberFormat("0%"); sheet.getCellRange("A3").setNumberValue(0.5); sheet.getCellRange("A3").setNumberFormat("0%"); sheet.getCellRange("A4").setNumberValue(0.1); sheet.getCellRange("A4").setNumberFormat("0%"); sheet.getCellRange("A5").setNumberValue(0.9); sheet.getCellRange("A5").setNumberFormat("0%"); sheet.getCellRange("A6").setNumberValue(0.7); sheet.getCellRange("A6").setNumberFormat("0%"); sheet.getCellRange("A7").setNumberValue(0.6); sheet.getCellRange("A7").setNumberFormat("0%"); //Set the height of row and width of column for Excel cell range sheet.getAllocatedRange().setRowHeight(20); sheet.getAllocatedRange().setColumnWidth(25); //Add a conditional formatting XlsConditionalFormats conditional = sheet.getConditionalFormats().add(); conditional.addRange(sheet.getAllocatedRange()); IConditionalFormat format1 = conditional.addCondition(); //Add a conditional formatting of cell range and set its type to CellValue format1.setFormatType(ConditionalFormatType.CellValue); format1.setFirstFormula("300"); format1.setOperator(ComparisonOperatorType.Less); format1.setFontColor(Color.black); format1.setBackColor(Color.lightGray); //Add a conditional formatting of cell range and set its type to IconSet conditional.addRange(sheet.getAllocatedRange()); IConditionalFormat format = conditional.addCondition(); format.setFormatType(ConditionalFormatType.IconSet); format.getIconSet().setIconSetType(IconSetType.ThreeTrafficLights1); //Save to file String result = "output/setTrafficLightsIcons_result.xlsx"; workbook.saveToFile(result, ExcelVersion.Version2013); } }
Effective screenshot of traffic lights icons on Excel worksheet:
Add picture to Excel chart in Java
This article will introduce how to add a picture to Excel chart in java applications by using Spire.XLS for java.
import com.spire.xls.*; import com.spire.xls.core.IPictureShape; import com.spire.xls.core.IShape; import java.awt.*; public class addPictureToChart { public static void main(String[] args) { //Load the document from disk Workbook workbook = new Workbook(); workbook.loadFromFile("Sample.xlsx"); //get the first worksheet Worksheet sheet = workbook.getWorksheets().get(0); //get the first chart Chart chart = sheet.getCharts().get(0); //add the picture to chart and set its format IShape picture = chart.getShapes().addPicture("48.png"); ((IPictureShape) picture).getLine().setDashStyle(ShapeDashLineStyleType.DashDotDot); ((IPictureShape) picture).getLine().setForeColor(Color.blue); //save the document String result = "output/AddPictureToChart.xlsx"; workbook.saveToFile(result, ExcelVersion.Version2010); } }
Effective screenshot after adding picture to Excel Chart:
How to Mail Merge Image in Word in Java
This article demonstrates how to mail merge image in Word document in Java using Spire.Doc for Java.
The template document:
import com.spire.doc.Document; import com.spire.doc.FileFormat; import com.spire.doc.reporting.MergeImageFieldEventArgs; import com.spire.doc.reporting.MergeImageFieldEventHandler; import java.text.SimpleDateFormat; import java.util.Date; public class SimpleMailMerge { public static void main(String[] args) throws Exception { //create a Document instance Document document = new Document(); //load the template document document.loadFromFile("template - Copy.docx"); //specify the merge field name String[] filedNames = new String[]{"image"}; //specify the path of image String[] filedValues = new String[]{"logo.png"}; //invoke the mail merge event to load image document.getMailMerge().MergeImageField = new MergeImageFieldEventHandler() { public void invoke(Object sender, MergeImageFieldEventArgs args) { mailMerge_MergeImageField(sender, args); } }; //execute mail merge document.getMailMerge().execute(filedNames, filedValues); //save file document.saveToFile("MailMergeImage.docx", FileFormat.Docx_2013); } //create a mail merge event to load image private static void mailMerge_MergeImageField(Object sender, MergeImageFieldEventArgs field) { String filePath = field.getImageFileName(); if (filePath != null && !"".equals(filePath)) { try { field.setImage(filePath); } catch (Exception e) { e.printStackTrace(); } } } }
The output document:
Java: Highlight Values Above or Below Average in Excel
If you have a lot of data in Excel, finding the special values can be challenging. In this situation, you can use the conditional formatting to automatically highlight the cells that contain the value that meets certain criteria. This article introduces how to highlight values that are above or below the average using conditional formatting in Java, using Spire.XLS for Java.
Install Spire.XLS for Java
First, you're required to add the Spire.Xls.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml 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.xls</artifactId> <version>14.3.2</version> </dependency> </dependencies>
Highlight Values Above or Below Average Excel in Java
Below are the steps to highlight values above or below average in Excel using Spire.XLS for Java.
- Create a Workbook object.
- Load an Excel file using Workbook.loadFromFile() method.
- Get a specific worksheet from the workbook using Workbook.getWorksheets.get(index) method.
- Add a conditional formatting to the worksheet using Worksheet.getConditionalFormats().add() method and return an object of XlsConditionalFormats class.
- Set the cell range where the conditional formatting will be applied using XlsConditionalFormats.AddRange() method.
- Add an average condition using XlsConditionalFormats.addAverageCondition() method, specify the average type to above and change the background color of the cells that meet the condition to yellow.
- Add another average condition to change the background color of the cells that contain the value below average to light gray.
- Save the workbook to an Excel file using Workbook.saveToFile() method.
- Java
import com.spire.xls.AverageType; import com.spire.xls.ExcelVersion; import com.spire.xls.Workbook; import com.spire.xls.Worksheet; import com.spire.xls.core.IConditionalFormat; import com.spire.xls.core.spreadsheet.collections.XlsConditionalFormats; import java.awt.*; public class HighlightValuesAboveAndBelowAverage { public static void main(String[] args) { //Create a Workbook object Workbook workbook = new Workbook(); //Load an Excel file workbook.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.xlsx"); //Get the first worksheet Worksheet sheet = workbook.getWorksheets().get(0); //Add a conditional format to the worksheet XlsConditionalFormats format = sheet.getConditionalFormats().add(); //Set the range where the conditional format will be applied format.addRange(sheet.getRange().get("F2:F14")); //Add a condition to highlight values above average with yellow IConditionalFormat condition1 = format.addAverageCondition(AverageType.Above); condition1.setBackColor(Color.yellow); //Add a condition to highlight values below average with light gray IConditionalFormat condition2 = format.addAverageCondition(AverageType.Below); condition2.setBackColor(Color.lightGray); //Get the count of values below average sheet.getRange().get("F17").setFormula("=COUNTIF(F2:F14,\"<\"&AVERAGE(F2:F14))"); //Get the count of values above average sheet.getRange().get("F18").setFormula("=COUNTIF(F2:F14,\">\"&AVERAGE(F2:F14))"); //Save the workbook to an Excel file workbook.saveToFile("output/HighlightValues.xlsx", ExcelVersion.Version2016); } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
Java: Set, Retrieve, or Delete Document Properties in PowerPoint
Document properties of PowerPoint presentations hold immense value in managing and organizing presentations. The information in properties, including title, author, keywords, etc., provides a concise summary, aids in categorization and searchability, and contributes to maintaining a comprehensive presentation history. However, some useless document properties need to be deleted to prevent them from affecting document management. This article is going to show how to add, retrieve, or delete document properties in PowerPoint presentations using Spire.Presentation for Java.
- Add Document Properties to a PowerPoint Presentation
- Retrieve Document Properties from a PowerPoint Presentation
- Delete Document Properties of a PowerPoint Presentation
Install Spire.Presentation for Java
First of all, you're required to add the Spire.Presentation.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml 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</artifactId> <version>9.3.1</version> </dependency> </dependencies>
Add Document Properties to a PowerPoint Presentation
Spire.Presentation for Java provides a set of methods under IDocumentProperty class to enable users to set and retrieve document properties of a presentation after getting the properties using the Presentation.getDocumentProperty() method. The detailed steps for adding document properties to a PowerPoint presentation are as follows:
- Create an object of Presentation class.
- Load a presentation file using Presentation.loadFromFile() method.
- Get the document properties of the presentation using Presentation.getDocumentProperty() method.
- Set the document properties using methods under IDocumentProperty class.
- Save the presentation using Presentation.saveToFile() method.
- Java
import com.spire.presentation.FileFormat; import com.spire.presentation.IDocumentProperty; import com.spire.presentation.Presentation; public class addPresentationProperties { public static void main(String[] args) throws Exception { //Create an object of Presentation class Presentation presentation = new Presentation(); //Load a PowerPoint presentation presentation.loadFromFile("Sample.pptx"); //Get the properties of the presentation and add items IDocumentProperty property = presentation.getDocumentProperty(); property.setTitle("Annual Business Analysis Report"); property.setSubject("Business Analysis"); property.setAuthor("Taylor"); property.setManager("John"); property.setCompany("E-iceblue"); property.setCategory("Report"); property.setKeywords("Operating Analysis; Quarterly Operating Data; Growth"); property.setComments("The report has been revised and finalized and does not require further consultation."); //Save the presentation file presentation.saveToFile("AddProperties.pptx", FileFormat.AUTO); presentation.dispose(); } }
Retrieve Document Properties from a PowerPoint Presentation
The detailed steps for retrieving document properties from a PowerPoint presentation are as follows:
- Create an object of Presentation class.
- Load a presentation file using Presentation.loadFromFile() method.
- Get the properties of the presentation using Presentation.getDocumentProperty() method.
- Retrieve property information using methods under IDocumentProperty class and write it to a text file.
- Java
import com.spire.presentation.IDocumentProperty; import com.spire.presentation.Presentation; import java.io.FileWriter; public class retrievePresentationProperties { public static void main(String[] args) throws Exception { //Create an object of Presentation class Presentation presentation = new Presentation(); //Load a presentation file presentation.loadFromFile("AddProperties.pptx"); //Get the properties of the presentation IDocumentProperty property = presentation.getDocumentProperty(); //Get the properties and write them to a text file String properties = "Title: " + property.getTitle() + "\r\n" + "Subject: " + property.getSubject() + "\r\n" + "Author: " + property.getAuthor() + "\r\n" + "Manager: " + property.getManager() + "\r\n" + "Company: " + property.getCompany() + "\r\n" + "Category: " + property.getCategory() + "\r\n" + "Keywords: " + property.getKeywords() + "\r\n" + "Comments: " + property.getComments(); FileWriter presentationProperties = new FileWriter("PresentationProperties.txt"); presentationProperties.write(properties); presentationProperties.close(); } }
Delete Document Properties of a PowerPoint Presentation
Removing document properties from a presentation is similar to adding properties. Just set the property data to null and the document properties can be removed. The details are as follows:
- Create an object of Presentation class.
- Load a presentation file using Presentation.loadFromFile() method.
- Get the document properties of the presentation using Presentation.getDocumentProperty() method.
- Set the document properties to null using methods under IDocumentProperty class.
- Save the presentation using Presentation.saveToFile() method.
- Java
import com.spire.presentation.FileFormat; import com.spire.presentation.IDocumentProperty; import com.spire.presentation.Presentation; public class deletePresentationProperties { public static void main(String[] args) throws Exception { //Create an object of Presentation class Presentation presentation = new Presentation(); //Load a PowerPoint presentation presentation.loadFromFile("AddProperties.pptx"); //Get the properties of the presentation and set the properties to null IDocumentProperty property = presentation.getDocumentProperty(); property.setTitle(""); property.setSubject(""); property.setAuthor(""); property.setManager(""); property.setCompany(""); property.setCategory(""); property.setKeywords(""); property.setComments(""); //Save the presentation file presentation.saveToFile("DeleteProperties.pptx", FileFormat.AUTO); presentation.dispose(); } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.