Java replace the text with image in Excel worksheet
This article will show you how to replace the searched text with image in Excel worksheet by using Spire.XLS in Java applications.
Sample Excel:
import com.spire.xls.*; import java.io.IOException; public class replaceTextwithImage { public static void main(String[] args) throws IOException { //Load the sample Excel document Workbook workbook = new Workbook(); workbook.loadFromFile("Sample.xlsx"); //Get the first worksheet Worksheet worksheet = workbook.getWorksheets().get(0); //Find the text string {{Image}} CellRange[] ranges = worksheet.findAllString("{{Image}}", false, false); for (CellRange range : ranges) { //set the text as null range.setText(""); //get the row and column of the searched range int row = range.getRow(); int column = range.getColumn(); //Add the image to the searched range worksheet.getPictures().add(row, column, "logo.jpg", ImageFormatType.Jpeg); //Save the document to file workbook.saveToFile("replaceTextwithImage.xlsx", ExcelVersion.Version2013); } } }
Output:
Link to a Bookmark in a Word Document in C#/VB.NET
This article shows you how to create a hyperlink to a bookmark within the same Word document by using Spire.Doc with C# and VB.NET.
using Spire.Doc; using Spire.Doc.Documents; namespace LinkToBookmark { class Program { static void Main(string[] args) { //Create a Document object Document doc = new Document(); //Add two sections Section section1 = doc.AddSection(); Section section2 = doc.AddSection(); //Insert a paragraph in section 2 and add a bookmark named "myBookmark" to it Paragraph bookmarkParagrapg = section2.AddParagraph(); bookmarkParagrapg.AppendText("Here is a bookmark"); BookmarkStart start = bookmarkParagrapg.AppendBookmarkStart("myBookmark"); bookmarkParagrapg.Items.Insert(0, start); bookmarkParagrapg.AppendBookmarkEnd("myBookmark"); //Link to the bookmark Paragraph paragraph = section1.AddParagraph(); paragraph.AppendText("Link to a bookmark: "); paragraph.AppendHyperlink("myBookmark", "Jump to a location in this document", HyperlinkType.Bookmark); //Save to file doc.SaveToFile("LinkToBookmark.docx", FileFormat.Docx2013); } } }
Imports Spire.Doc Imports Spire.Doc.Documents Namespace LinkToBookmark Class Program Shared Sub Main(ByVal args() As String) 'Create a Document object Document doc = New Document() 'Add two sections Dim section1 As Section = doc.AddSection() Dim section2 As Section = doc.AddSection() 'Insert a paragraph in section 2 and add a bookmark named "myBookmark" to it Dim bookmarkParagrapg As Paragraph = section2.AddParagraph() bookmarkParagrapg.AppendText("Here is a bookmark") Dim start As BookmarkStart = bookmarkParagrapg.AppendBookmarkStart("myBookmark") bookmarkParagrapg.Items.Insert(0, start) bookmarkParagrapg.AppendBookmarkEnd("myBookmark") 'Link to the bookmark Dim paragraph As Paragraph = section1.AddParagraph() paragraph.AppendText("Link to a bookmark: ") paragraph.AppendHyperlink("myBookmark", "Jump to a location in this document", HyperlinkType.Bookmark) 'Save to file doc.SaveToFile("LinkToBookmark.docx", FileFormat.Docx2013) End Sub End Class End Namespace
Insert OLE Object in Excel in Java applications
Spire.XLS for Java supports to insert Word, Excel, PowerPoint slide and PDF as linked object or embedded object into Excel Worksheet. This article will show you how to insert a Word document as an embedded object into Excel by using Spire.XLS for Java in Java applications.
import com.spire.xls.*; import com.spire.xls.core.IOleObject; import com.spire.doc.*; import com.spire.doc.documents.ImageType; import java.awt.image.BufferedImage; public class insertOLEObjects { public static void main(String[] args) { String docFile = "Sample.docx"; String outputFile = "output/insertOLEObjects_result.xlsx"; //Load the Excel document Workbook workbook = new Workbook(); workbook.loadFromFile("Sample.xlsx"); //Get the first worksheet Worksheet worksheet = workbook.getWorksheets().get(0); //Generate image BufferedImage image = GenerateImage(docFile); //insert OLE object IOleObject oleObject = worksheet.getOleObjects().add(docFile, image, OleLinkType.Embed); oleObject.setLocation(worksheet.getCellRange("B4")); oleObject.setObjectType(OleObjectType.ExcelWorksheet); //Save the file workbook.saveToFile(outputFile, ExcelVersion.Version2010); } private static BufferedImage GenerateImage(String fileName) { //Load the sample word document Document document = new Document(); document.loadFromFile(fileName); //Save the first page of word as an image BufferedImage image = document.saveToImages(0, ImageType.Bitmap); return image; } }
Output:
Set ASCII Characters as Bullet Points in Word in Java
This article shows you how to set ASCII characters (special symbols) as bullet points in Word documents using Spire.Doc for Java.
import com.spire.doc.Document; import com.spire.doc.FileFormat; import com.spire.doc.Section; import com.spire.doc.documents.ListStyle; import com.spire.doc.documents.ListType; import com.spire.doc.documents.Paragraph; public class SetBulletPoints { public static void main(String[] args) { //Create a Document object and add a section Document doc = new Document(); Section section = doc.addSection(); //Create four list styles based on different ASCII characters ListStyle listStyle1 = new ListStyle(doc, ListType.Bulleted); listStyle1.getLevels().get(0).setBulletCharacter("\u006e"); listStyle1.getLevels().get(0).getCharacterFormat().setFontName("Wingdings"); listStyle1.setName("liststyle1"); doc.getListStyles().add(listStyle1); ListStyle listStyle2 = new ListStyle(doc, ListType.Bulleted); listStyle2.getLevels().get(0).setBulletCharacter("\u0075"); listStyle2.getLevels().get(0).getCharacterFormat().setFontName("Wingdings"); listStyle2.setName("liststyle2"); doc.getListStyles().add(listStyle2); ListStyle listStyle3 = new ListStyle(doc, ListType.Bulleted); listStyle3.getLevels().get(0).setBulletCharacter("\u00b2"); listStyle3.getLevels().get(0).getCharacterFormat().setFontName("Wingdings"); listStyle3.setName("liststyle3"); doc.getListStyles().add(listStyle3); ListStyle listStyle4 = new ListStyle(doc, ListType.Bulleted); listStyle4 .getLevels().get(0).setBulletCharacter("\u00d8"); listStyle4 .getLevels().get(0).getCharacterFormat().setFontName("Wingdings"); listStyle4.setName("liststyle4"); doc.getListStyles().add(listStyle4); //Add four paragraphs and apply list style separately Paragraph p1 = section.getBody().addParagraph(); p1.appendText("Spire.Doc for .NET"); p1.getListFormat().applyStyle(listStyle1.getName()); Paragraph p2 = section.getBody().addParagraph(); p2.appendText("Spire.PDF for .NET"); p2.getListFormat().applyStyle(listStyle2.getName()); Paragraph p3 = section.getBody().addParagraph(); p3.appendText("Spire.XLS for .NET"); p3.getListFormat().applyStyle(listStyle3.getName()); Paragraph p4= section.getBody().addParagraph(); p4.appendText("Spire.Presentation for .NET"); p4.getListFormat().applyStyle(listStyle4.getName()); //Save to file doc.saveToFile("SetBulletCharacter.docx", FileFormat.Docx); } }
Add an Endnote to Word in Java
This article shows you how to add an endnote to Word documents using Spire.Doc for Java.
import com.spire.doc.*; import com.spire.doc.documents.Paragraph; import com.spire.doc.fields.Footnote; import com.spire.doc.fields.TextRange; import java.awt.*; public class AddEndnote { public static void main(String[] args) { //Create a Document object Document doc = new Document(); //Load the sample Word file doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.docx"); //Get the first section Section section = doc.getSections().get(0); //Get the specific paragraph to add endnote Paragraph paragraph = section.getParagraphs().get(2); //Add an endnote Footnote endnote = paragraph.appendFootnote(FootnoteType.Endnote); //Set endnote text TextRange textRange = endnote.getTextBody().addParagraph().appendText("This is an endnote created by Spire.Doc."); //Set text format of endnote textRange.getCharacterFormat().setFontName("Arial"); textRange.getCharacterFormat().setFontSize(13f); textRange.getCharacterFormat().setTextColor(Color.RED); //Save to file doc.saveToFile("AddEndnote.docx", FileFormat.Docx_2013); } }
Insert and remove shapes in Excel in Java
This article will demonstrate how to insert and remove shapes in Excel file using Spire.XLS for Java.
Add shapes to Excel worksheet:
import com.spire.xls.*; import com.spire.xls.core.*; import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; public class addShapestoExcel { public static void main(String[] args) throws Exception { String output = "output/AddShapesToExcelSheet.xlsx"; //create a workbook. Workbook workbook = new Workbook(); //get the first worksheet. Worksheet sheet = workbook.getWorksheets().get(0); //add a triangle shape. IPrstGeomShape triangle = sheet.getPrstGeomShapes().addPrstGeomShape(2, 2, 100, 100, PrstGeomShapeType.Triangle); //fill the triangle with solid color. triangle.getFill().setForeColor( Color.YELLOW); triangle.getFill().setFillType( ShapeFillType.SolidColor); //add a heart shape. IPrstGeomShape heart = sheet.getPrstGeomShapes().addPrstGeomShape(2, 5, 100, 100, PrstGeomShapeType.Heart); //fill the heart with gradient color. heart.getFill().setForeColor(Color.RED); heart.getFill().setFillType(ShapeFillType.Gradient); //add an arrow shape with default color. IPrstGeomShape arrow = sheet.getPrstGeomShapes().addPrstGeomShape(10, 2, 100, 100, PrstGeomShapeType.CurvedRightArrow); //add a cloud shape. IPrstGeomShape cloud = sheet.getPrstGeomShapes().addPrstGeomShape(10, 5, 100, 100, PrstGeomShapeType.Cloud); //fill the cloud with custom picture BufferedImage image = ImageIO.read(new File("SpireXls.png")); cloud.getFill().customPicture(image, "SpireXls.png"); cloud.getFill().setFillType( ShapeFillType.Picture); //save to file. workbook.saveToFile(output, ExcelVersion.Version2013); } }
Output:
Remove a particular shape or all shapes from Excel worksheet:
import com.spire.xls.*; public class removeShape { public static void main(String[] args) throws Exception { //Load the sample file Workbook workbook = new Workbook(); workbook.loadFromFile("output/AddShapesToExcelSheet.xlsx"); //get the first worksheet. Worksheet sheet = workbook.getWorksheets().get(0); //delete the second shape in the worksheet sheet.getPrstGeomShapes().get(1).remove(); /* //delete all shapes in the worksheet for (int i = sheet.getPrstGeomShapes().getCount()-1; i >= 0; i--) { sheet.getPrstGeomShapes().get(i).remove(); }*/ //save to file. workbook.saveToFile("output/RemoveParticularShape.xlsx", ExcelVersion.Version2013); } }
Effective screenshot after remove the second shape from Excel worksheet:
Add different headers/footers for odd and even pages on Word
We have demonstrated how to add text and image header footer to Word document by using Spire.Doc for Java. This article will show you how to create different headers/footers for odd and even pages on Word document in Java applications.
import com.spire.doc.*; import com.spire.doc.documents.*; import com.spire.doc.fields.*; import java.awt.*; public class oddAndEvenHeaderFooter { public static void main(String[] args) throws Exception { String input = "multiPages.docx"; String output = "output/oddAndEvenHeaderFooter.docx"; //load the document Document doc = new Document(); doc.loadFromFile(input); //get the first section Section section = doc.getSections().get(0); //set the DifferentOddAndEvenPagesHeaderFooter property as true section.getPageSetup().setDifferentOddAndEvenPagesHeaderFooter(true); //add odd header Paragraph P3 = section.getHeadersFooters().getOddHeader().addParagraph(); TextRange OH = P3.appendText("Odd Header"); P3.getFormat().setHorizontalAlignment(HorizontalAlignment.Center); OH.getCharacterFormat().setFontName("Arial"); OH.getCharacterFormat().setFontSize(14); OH.getCharacterFormat().setTextColor(Color.BLUE); //add even header Paragraph P4 = section.getHeadersFooters().getEvenHeader().addParagraph(); TextRange EH = P4.appendText("Even Header from E-iceblue Using Spire.Doc"); P4.getFormat().setHorizontalAlignment(HorizontalAlignment.Center); EH.getCharacterFormat().setFontName("Arial"); EH.getCharacterFormat().setFontSize(14); EH.getCharacterFormat().setTextColor(Color.GREEN); //add odd footer Paragraph P2 = section.getHeadersFooters().getOddFooter().addParagraph(); TextRange OF = P2.appendText("Odd Footer"); P2.getFormat().setHorizontalAlignment(HorizontalAlignment.Center); OF.getCharacterFormat().setFontName("Arial"); OF.getCharacterFormat().setFontSize(14); OF.getCharacterFormat().setTextColor(Color.BLUE); //add even footer Paragraph P1 = section.getHeadersFooters().getEvenFooter().addParagraph(); TextRange EF = P1.appendText("Even Footer from E-iceblue Using Spire.Doc"); EF.getCharacterFormat().setFontName("Arial"); EF.getCharacterFormat().setFontSize(14); P1.getFormat().setHorizontalAlignment(HorizontalAlignment.Center); EF.getCharacterFormat().setTextColor(Color.GREEN); //save the document doc.saveToFile(output, FileFormat.Docx); } }
Output:
Create PowerPoint Chart from Excel Data in Java
This article shows you how to create a chart in PowerPoint using the data from an existing Excel document. This solution relies on Spire.Office.jar. Please download the latest version from here and add it as a dependency in your project.
Below is a screenshot of the Excel document.
import com.spire.presentation.FileFormat; import com.spire.presentation.Presentation; import com.spire.presentation.SlideSizeType; import com.spire.presentation.charts.ChartStyle; import com.spire.presentation.charts.ChartType; import com.spire.presentation.charts.IChart; import com.spire.xls.Workbook; import com.spire.xls.Worksheet; import java.awt.geom.Rectangle2D; public class CreateChartFromExcelData { public static void main(String[] args) throws Exception { //Create a Presentation object Presentation presentation = new Presentation(); presentation.getSlideSize().setType(SlideSizeType.SCREEN_16_X_9); //Add a clustered column chart to slide Rectangle2D rect = new Rectangle2D.Float(200, 100, 550, 320); IChart chart = presentation.getSlides().get(0).getShapes().appendChart(ChartType.COLUMN_CLUSTERED,rect); //Clear the default dummy data chart.getChartData().clear(0,0,5,5 ); //Load an existing Excel file to Workbook object Workbook wb = new Workbook(); wb.loadFromFile("C:\\Users\\Administrator\\Desktop\\data.xlsx"); //Get the first worksheet Worksheet sheet = wb.getWorksheets().get(0); //Import data from the sheet to chart table for (int r = 0; r < sheet.getAllocatedRange().getRowCount(); r++) { for (int c = 0; c < sheet.getAllocatedRange().getColumnCount(); c++) { chart.getChartData().get(r,c).setValue(sheet.getCellRange(r+1, c+1).getValue2()); } } //Add chart title chart.getChartTitle().getTextProperties().setText("Male/Female Ratio Per Dept."); chart.getChartTitle().getTextProperties().isCentered(true); chart.getChartTitle().setHeight(25f); chart.hasTitle(true); //Set the series label chart.getSeries().setSeriesLabel(chart.getChartData().get("B1","C1")); //Set the category labels chart.getCategories().setCategoryLabels(chart.getChartData().get("A2","A5")); //Set the series values chart.getSeries().get(0).setValues(chart.getChartData().get("B2","B5")); chart.getSeries().get(1).setValues(chart.getChartData().get("C2", "C5")); //Apply built-in chart style chart.setChartStyle(ChartStyle.STYLE_11); //Set overlap chart.setOverLap(-50); //Set gap width chart.setGapWidth(200); //Save to file presentation.saveToFile("output/Chart.pptx", FileFormat.PPTX_2013); } }
Save PowerPoint Shapes as Images in Java
This article shows you how to export shapes in a specific slide as images using Spire.Presentation for Java. Below is a screenshot of the sample PowerPoint document.
import com.spire.presentation.ISlide; import com.spire.presentation.Presentation; import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; public class SaveShapeAsImage { public static void main(String[] args) throws Exception { //Create a Presentation object Presentation presentation = new Presentation(); //Load the sample PowerPoint file presentation.loadFromFile("C:\\Users\\Administrator\\Desktop\\chart and table.pptx"); //Get the first slide ISlide slide = presentation.getSlides().get(0); //Declare a BufferedImage variable BufferedImage image; //Loop through the shapes in the slide for (int i = 0; i < slide.getShapes().getCount(); i++) { //Save the specific shape as image data image = slide.getShapes().saveAsImage(i); //Write data to png file File file = new File(String.format("ToImage-%d.png", i)); ImageIO.write(image, "PNG", file); } } }
Output
Java: Add or Delete Digital Signatures in Excel
A digital signature is an electronic signature with encrypted information that helps verify the authenticity of messages, software and digital documents. They are commonly used in software distribution, financial transactions, contract management software, and other situations that require forgery or tampering detection. When generating an Excel report, you may need to add a digital signature to make it look more authentic and official. In this article, you will learn how to add or delete digital signatures in Excel in Java using Spire.XLS for Java.
Install Spire.XLS for Java
First of all, 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.4.1</version> </dependency> </dependencies>
Add a Digital Signature to Excel in Java
You can add a digital signature to protect the integrity of an Excel file. Once the digital signature is added, the file becomes read-only to discourage further editing. If someone makes changes to the file, the digital signature will become invalid immediately.
Spire.XLS for Java provides the addDigitalSignature method of Workbook class to add digital signatures to an Excel file. The detailed steps are as follows:
- Instantiate a Workbook instance.
- Load an Excel file using Workbook.loadFromFile() method.
- Instantiate a CertificateAndPrivateKey instance with the specified certificate (.pfx) file path and the password of the .pfx file.
- Add a digital signature to the file using Workbook.addDigitalSignature(CertificateAndPrivateKey, String, Date) method.
- Save the result file using Workbook.saveToFile() method.
- Java
import com.spire.xls.ExcelVersion; import com.spire.xls.Workbook; import com.spire.xls.digital.CertificateAndPrivateKey; import java.util.Date; public class AddDigitalSignature { public static void main(String []args) throws Exception { //Create a Workbook instance Workbook workbook=new Workbook(); //Load an Excel file workbook.loadFromFile("Sample.xlsx"); //Add a digital signature to the file CertificateAndPrivateKey cap = new CertificateAndPrivateKey("Test.pfx","e-iceblue"); workbook.addDigitalSignature(cap, "e-iceblue",new Date()); //Save the result file workbook.saveToFile("AddDigitalSignature.xlsx", ExcelVersion.Version2013); } }
Delete Digital Signature from Excel in Java
Spire.XLS for Java provides the removeAllDigitalSignatures method of Workbook class for developers to remove digital signatures from an Excel file. The detailed steps are as follows:
- Initialize an instance of the Workbook class.
- Load an Excel file using Workbook.loadFromFile() method.
- Remove all digital signatures from the file using Workbook.removeAllDigitalSignatures() method.
- Save the result file using Workbook.saveToFile() method.
- Java
import com.spire.xls.ExcelVersion; import com.spire.xls.Workbook; public class DeleteDigitalSignature { public static void main(String []args) throws Exception { //Create a Workbook instance Workbook workbook = new Workbook(); //Load an Excel file workbook.loadFromFile("AddDigitalSignature.xlsx"); //Remove all digital signatures in the file workbook.removeAllDigitalSignatures(); //Save the result file workbook.saveToFile("RemoveDigitalSignature.xlsx", ExcelVersion.Version2013); } }
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.