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.3.2</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.
Set Table Alignment in Slides in Java
This article demonstrates how to horizontally and vertically align a table in a slide using Spire.Presentation for Java.
Below is a screenshot of the sample document.
import com.spire.presentation.FileFormat; import com.spire.presentation.ITable; import com.spire.presentation.Presentation; import com.spire.presentation.ShapeAlignmentEnum; public class AlignTable { public static void main(String[] args) throws Exception { //Create a Presentation object Presentation presentation = new Presentation(); //Load the sample PowerPoint document contain presentation.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pptx"); //Declare a ITable variable ITable table = null; //Loop through the shapes in the first slide for (Object shape: presentation.getSlides().get(0).getShapes() ) { //Check if shape is an instance of ITable if (shape instanceof ITable) { //Convert shape to table table =(ITable)shape; } } //Horizontally align table to center table.setShapeAlignment(ShapeAlignmentEnum.ShapeAlignment.AlignCenter); //Vertically align table to middle table.setShapeAlignment(ShapeAlignmentEnum.ShapeAlignment.AlignMiddle); //Save to file presentation.saveToFile("AlignTable.pptx", FileFormat.PPTX_2013); } }
Java: Copy Slides Between Two PowerPoint Documents
Suppose there are two PowerPoint documents, and you want to copy a certain slide from one document to a specified location of the other. Manual copying and pasting is an option, but the quicker and more efficient way is to use Java codes for automatic operation. This article will show you how to programmatically copy slides between two different PowerPoint documents using Spire.Presentation for Java.
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>
Copy Slides Between Two PowerPoint Documents
The following are detailed steps to copy a slide from one PowerPoint document to a specified position or the end of the other document.
- Create a Presentation object and load one sample document using Presentation.loadFromFile() method.
- Create another Presentation object and load the other sample document using Presentation.loadFromFile() method.
- Get a specific slide of document one using Presentation.getSlides().get() method and insert its copy into the specified position of document two using Presentation.getSlides().insert() method.
- Get another specific slide of document one using Presentation.getSlides().get() method and add its copy to the end of document two using Presentation.getSlides().append() method.
- Save the document two to another file using Presentation.saveToFile() method.
- Java
import com.spire.presentation.FileFormat; import com.spire.presentation.Presentation; public class CopySlidesBetweenPPT { public static void main(String[] args) throws Exception { //Create a Presentation object to load one sample document Presentation pptOne= new Presentation(); pptOne.loadFromFile("C:\\Users\\Test1\\Desktop\\sample1.pptx"); //Create another Presentation object to load the other sample document Presentation pptTwo = new Presentation(); pptTwo.loadFromFile("C:\\Users\\Test1\\Desktop\\sample2.pptx"); //Insert the specific slide from document one into the specified position of document two pptTwo.getSlides().insert(0,pptOne.getSlides().get(0)); //Append the specific slide of document one to the end of document two pptTwo.getSlides().append(pptOne.getSlides().get(3)); //Save the document two to another file pptTwo.saveToFile("output/CopySlidesBetweenPPT.pptx", FileFormat.PPTX_2013); } }
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.
Add multiple watermarks in presentation slides
Spire.Presentation supports to insert text watermark and image watermark to PowerPoint document. This article will show you how to use Spire.Presentation to add multiple watermarks to the presentation slides in C#/VB.NET.
using Spire.Presentation; using Spire.Presentation.Drawing; using System; using System.Drawing; using System.Windows.Forms; namespace WatermarkDemo { class Program { static void Main(string[] args) { //Create a PPT document and load file Presentation presentation = new Presentation(); presentation.LoadFromFile("Sample.pptx"); //Get the size of the watermark string Font font = new Font("Arial", 20); String watermarkText = "E-iceblue"; SizeF size = TextRenderer.MeasureText("E-iceblue", font); float x = 30; float y = 80; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { //Define a rectangle range RectangleF rect = new RectangleF(x, y, size.Width, size.Height); //Add a rectangle shape with a defined range IAutoShape shape = presentation.Slides[0].Shapes.AppendShape(Spire.Presentation.ShapeType.Rectangle, rect); //Set the style of the shape shape.Fill.FillType = FillFormatType.None; shape.ShapeStyle.LineColor.Color = Color.White; shape.Rotation = -45; shape.Locking.SelectionProtection = true; shape.Line.FillType = FillFormatType.None; //Add text to the shape shape.TextFrame.Text = watermarkText; TextRange textRange = shape.TextFrame.TextRange; //Set the style of the text range textRange.Fill.FillType = FillFormatType.Solid; textRange.Fill.SolidColor.Color = Color.FromArgb(120, Color.HotPink); textRange.EastAsianFont = new TextFont(font.Name); textRange.FontHeight = font.Size; x += (100 + size.Width); } x = 30; y += (100 + size.Height); } //Save the document presentation.SaveToFile("Watermark_result.pptx", FileFormat.Pptx2010); } } }
Imports Spire.Presentation Imports Spire.Presentation.Drawing Imports System Imports System.Drawing Imports System.Windows.Forms Namespace WatermarkDemo Class Program Private Shared Sub Main(ByVal args() As String) 'Create a PPT document and load file Dim presentation As Presentation = New Presentation presentation.LoadFromFile("Sample.pptx") 'Get the size of the watermark string Dim font As Font = New Font("Arial", 20) Dim watermarkText As String = "E-iceblue" Dim size As SizeF = TextRenderer.MeasureText("E-iceblue", font) Dim x As Single = 30 Dim y As Single = 80 Dim i As Integer = 0 Do While (i < 3) Dim j As Integer = 0 Do While (j < 3) 'Define a rectangle range Dim rect As RectangleF = New RectangleF(x, y, size.Width, size.Height) 'Add a rectangle shape with a defined range Dim shape As IAutoShape = presentation.Slides(0).Shapes.AppendShape(Spire.Presentation.ShapeType.Rectangle, rect) 'Set the style of the shape shape.Fill.FillType = FillFormatType.None shape.ShapeStyle.LineColor.Color = Color.White shape.Rotation = -45 shape.Locking.SelectionProtection = true shape.Line.FillType = FillFormatType.None 'Add text to the shape shape.TextFrame.Text = watermarkText Dim textRange As TextRange = shape.TextFrame.TextRange 'Set the style of the text range textRange.Fill.FillType = FillFormatType.Solid textRange.Fill.SolidColor.Color = Color.FromArgb(120, Color.HotPink) textRange.EastAsianFont = New TextFont(font.Name) textRange.FontHeight = font.Size x = (x + (100 + size.Width)) j = (j + 1) Loop x = 30 y = (y + (100 + size.Height)) i = (i + 1) Loop 'Save the document presentation.SaveToFile("Watermark_result.pptx", FileFormat.Pptx2010) End Sub End Class End Namespace
Output:
Apply a Shadow Effect to Text in PowerPoint in Java
This article demonstrates how to apply a shadow effect to the text in a PowerPoint slide using Spire.Presentation for Java.
import com.spire.presentation.*; import com.spire.presentation.drawing.FillFormatType; import com.spire.presentation.drawing.OuterShadowEffect; import java.awt.*; import java.awt.geom.Rectangle2D; public class SetShadowEffect { 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); //Add a rectangle to slide IAutoShape shape = slide.getShapes().appendShape(ShapeType.RECTANGLE,new Rectangle2D.Float(50,80,500,100)); shape.getFill().setFillType(FillFormatType.NONE); shape.getLine().setFillType(FillFormatType.NONE); //Set text of the shape shape.appendTextFrame("Text shading on slide"); //Set font style shape.getTextFrame().getTextRange().setFontHeight(38f); shape.getTextFrame().getTextRange().setLatinFont(new TextFont("Arial Black")); shape.getTextFrame().getTextRange().getFill().setFillType(FillFormatType.SOLID); shape.getTextFrame().getTextRange().getFill().getSolidColor().setColor(Color.BLACK); //Create a OuterShadowEffect object OuterShadowEffect outerShadow= new OuterShadowEffect(); //Set the shadow effect outerShadow.setBlurRadius(0); outerShadow.setDirection(50); outerShadow.setDistance(10); outerShadow.getColorFormat().setColor(Color.orange); //Apply shadow effect to text shape.getTextFrame().getTextRange().getEffectDag().setOuterShadowEffect(outerShadow); //Save to file presentation.saveToFile("output/AddShadow.pptx", FileFormat.PPTX_2013); } }
Java remove the formulas but keep the values on Excel worksheet
This article will demonstrate how to use Spire.XLS for Java to remove the formulas but keep the values on the Excel worksheet.
Firstly, view the original Excel:
import com.spire.xls.*; public class Test { public static void main(String[] args) throws Exception { String inputFile = "Sample.xlsx"; String outputFile="output/removeFormulasButKeepValues_result.xlsx"; //Create a workbook. Workbook workbook = new Workbook(); //Load the file from disk. workbook.loadFromFile(inputFile); //Loop through worksheets. for (Worksheet sheet : (Iterable) workbook.getWorksheets()) { //Loop through cells. for (CellRange cell : (Iterable) sheet.getRange()) { //If the cell contains formula, get the formula value, clear cell content, and then fill the formula value into the cell. if (cell.hasFormula()) { Object value = cell.getFormulaValue(); cell.clear(ExcelClearOptions.ClearContent); cell.setValue(value.toString()); } } } //Save to file workbook.saveToFile(outputFile, ExcelVersion.Version2013); } }
Output:
Detect if the PDF file is password protected in C#/VB.NET
This article demonstrates how to detect whether a PDF file is encrypted or not by using Spire.PDF for .NET. Spire.PDF offers a method named IsPasswordProtected(string fileName) which returns a boolean value. If the value is true, means the PDF is encrypted with password, otherwise it's not.
using Spire.Pdf; using System; namespace PdfDemo { class Program { static void Main(string[] args) { string fileName = "Sample.pdf"; bool value = PdfDocument.IsPasswordProtected(fileName); Console.WriteLine(value); Console.ReadKey(); } } }
Imports Spire.Pdf Imports System Namespace PdfDemo Class Program Private Shared Sub Main(ByVal args() As String) Dim fileName As String = "Sample.pdf" Dim value As Boolean = PdfDocument.IsPasswordProtected(fileName) Console.WriteLine(value) Console.ReadKey End Sub End Class End Namespace
After running the project, we get the Output that shows the PDF file is password protected:
Java add multiple image watermarks to Word document
We have demonstrated how to use Spire.Doc for Java to add multiple text watermarks to word document. This article will show you how to add multiple image watermarks to the Word document with the help of Spire.Doc for Java.
import com.spire.doc.Document; import com.spire.doc.FileFormat; import com.spire.doc.HeaderFooter; import com.spire.doc.Section; import com.spire.doc.documents.Paragraph; import com.spire.doc.documents.TextWrappingStyle; import com.spire.doc.fields.DocPicture; public class WordImageWatermark { public static void main(String[] args) throws Exception { //Load the sample file Document doc=new Document(); doc.loadFromFile("Sample.docx"); //Load the image DocPicture picture = new DocPicture(doc); picture.loadImage("Logo.png"); //Set the text wrapping style picture.setTextWrappingStyle(TextWrappingStyle.Behind); for (int n = 0; n < doc.getSections().getCount(); n++) { Section section = doc.getSections().get(n); //Get the head of section HeaderFooter header = section.getHeadersFooters().getHeader(); Paragraph paragrapg1; if(header.getParagraphs().getCount()>0){ paragrapg1=header.getParagraphs().get(0); }else { //Add the header to the paragraph paragrapg1 = header.addParagraph(); } for (int p = 0; p < 3; p++) { for (int q = 0; q < 2; q++) { //copy the image and add it to many places picture = (DocPicture)picture.deepClone(); picture.setVerticalPosition(100 + 200 * p); picture.setHorizontalPosition(50 + 210 * q); paragrapg1.getChildObjects().add(picture); } } } //Save the document to file doc.saveToFile("Result.docx", FileFormat.Docx_2013); } }
Output: