Extract OLE Objects from an Excel Document in Java
This article demonstrates how to extract OLE objects from an Excel document using Spire.XLS for Java.
import com.spire.xls.*; import com.spire.xls.core.IOleObject; import java.io.*; public class ExtractOLEObjects { public static void main(String[] args){ //Create a Workbook instance Workbook workbook = new Workbook(); //Load the Excel document workbook.loadFromFile("OLEObjectsExample.xlsx"); //Get the first worksheet Worksheet sheet = workbook.getWorksheets().get(0); //Extract ole objects if (sheet.hasOleObjects()) { for (int i = 0; i < sheet.getOleObjects().size(); i++) { IOleObject object = sheet.getOleObjects().get(i); OleObjectType type = sheet.getOleObjects().get(i).getObjectType(); switch (type) { //Word document case WordDocument: byteArrayToFile(object.getOleData(), "output/extractOLE.docx"); break; //PowerPoint document case PowerPointSlide: byteArrayToFile(object.getOleData(), "output/extractOLE.pptx"); break; //PDF document case AdobeAcrobatDocument: byteArrayToFile(object.getOleData(), "output/extractOLE.pdf"); break; //Excel document case ExcelWorksheet: byteArrayToFile(object.getOleData(), "output/extractOLE.xlsx"); break; } } } } public static void byteArrayToFile(byte[] datas, String destPath) { File dest = new File(destPath); try (InputStream is = new ByteArrayInputStream(datas); OutputStream os = new BufferedOutputStream(new FileOutputStream(dest, false));) { byte[] flush = new byte[1024]; int len = -1; while ((len = is.read(flush)) != -1) { os.write(flush, 0, len); } os.flush(); } catch (IOException e) { e.printStackTrace(); } } }
The following screenshot shows the extracted OLE documents:
Detect if an Excel Document is Password Protected in Java
This article demonstrates how to detect whether an Excel document is password protected or not using Spire.XLS for Java.
import com.spire.xls.Workbook; public class DetectProtectedOrNot { public static void main(String[] args) { //Get the file path String filePath= "C:\\Users\\Administrator\\Desktop\\sample.xlsx"; //Detect whether the workbook is password protected or not Boolean isProtected = Workbook.bookIsPasswordProtected(filePath); //Print results if (isProtected) { System.out.print("The document is password protected."); } else { System.out.print("The document is not protected."); } } }
Java remove text or image watermark from presentation slides
This article will demonstrate how to use Spire.Presentaion for Java to remove text watermark and image watermark from the presentation slides.
Firstly, view the sample PowerPoint document with text and image watermark:
import com.spire.presentation.*; import com.spire.presentation.drawing.*; public class removeTextOrImageWatermark { public static void main(String[] args) throws Exception { //Load the sample document Presentation presentation = new Presentation(); presentation.loadFromFile("Sample.pptx"); //Remove text watermark by removing the shape which contains the string "E-iceblue". 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 IAutoShape) { IAutoShape shape = (IAutoShape)presentation.getSlides().get(i).getShapes().get(j); if (shape.getTextFrame().getText().contains("E-iceblue")) { presentation.getSlides().get(i).getShapes().remove(shape); } } } } //Remove image watermark for (int i = 0; i < presentation.getSlides().getCount(); i++) { presentation.getSlides().get(i).getSlideBackground().getFill().setFillType(FillFormatType.NONE); } //Save to file. presentation.saveToFile("removeTextOrImageWatermark.pptx";, FileFormat.PPTX_2013); } }
Effective screenshot after removing text or image watermark:
Java insert image watermark to presentation slides
We have introduced how to use Spire.Presentaion for Java to add text watermark to PowerPoint document. This article will demonstrate how to add image watermark to presentation slides in java applications.
Firstly view the sample PowerPoint document:
import com.spire.presentation.*; import com.spire.presentation.drawing.*; import javax.imageio.ImageIO; import java.io.File; public class addImageWatermark { public static void main(String[] args) throws Exception { //Create a PowerPoint document. Presentation presentation = new Presentation(); //Load the file from disk. presentation.loadFromFile("Sample.pptx"); //Get the image you want to add as image watermark. File file =new File("logo.png"); IImageData image = presentation.getImages().append(ImageIO.read(file)); //Set the properties of SlideBackground, and then fill the image as watermark. presentation.getSlides().get(0).getSlideBackground().setType(BackgroundType.CUSTOM); presentation.getSlides().get(0).getSlideBackground().getFill().setFillType(FillFormatType.PICTURE); presentation.getSlides().get(0).getSlideBackground().getFill().getPictureFill().setFillType(PictureFillType.STRETCH); presentation.getSlides().get(0).getSlideBackground().getFill().getPictureFill().getPicture().setEmbedImage(image); String result = "addImageWatermark.pptx"; //Save to file. presentation.saveToFile(result, FileFormat.PPTX_2013); } }
Effective screenshot after adding image watermark to PowerPoint document:
Convert Text to Columns in Excel in Java
This article demonstrates how to convert text to columns in Excel using Spire.XLS for Java. The following screenshot shows the sample Excel file before converting:
import com.spire.xls.ExcelVersion; import com.spire.xls.Workbook; import com.spire.xls.Worksheet; public class ConvertTextToColumns { public static void main(String[] args){ //Create a workbook instance Workbook workbook = new Workbook(); //Load the Excel file workbook.loadFromFile("Template.xlsx"); //Get the first worksheet Worksheet sheet = workbook.getWorksheets().get(0); //Convert text into columns by the delimited characters of space String[] splitText = null; String text = null; for (int i = 1; i < sheet.getLastRow()+1; i++) { text = sheet.getRange().get(i, 1).getText(); splitText = text.split(" "); for (int j = 0; j < splitText.length; j++) { sheet.getRange().get(i, 1 + j + 1).setText(splitText[j]); } } //Save the result file workbook.saveToFile("ConvertTextToColumns.xlsx", ExcelVersion.Version2013); } }
The following screenshot shows the output Excel file after converting:
Java: Sort Data in Excel
The sort function in Excel allows you to arrange the text in alphabetical order, sort the numbers from smallest to largest or largest to smallest, and dates from oldest to latest or latest to oldest, etc. In this article, you will learn how to programmatically sort numbers in a cell range 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>
Sort Data in Excel
The detailed steps are as follows:
- Create a Workbook instance and load a sample Excel document using Workbook.loadFromFile() method.
- Get a specified worksheet using Workbook.getWorksheets().get() method.
- Get a sort fields collection using Workbook.getDataSorter().getSortColumns() method, and then specify the column that need to be sorted and the sort mode in the collection using SortColumns.add() method.
- Sort the data in the specified cell range of the collection using Workbook.getDataSorter().sort() method.
- Save the document to file using Workbook.saveToFile() method.
- Java
import com.spire.xls.*; public class SortData { public static void main(String[] args) { //Create a Workbook instance Workbook workbook = new Workbook(); //Load the sample Excel document workbook.loadFromFile("sample.xlsx"); //Get the first worksheet Worksheet sheet = workbook.getWorksheets().get(0); //Specify the column that need to be sorted and the sort mode (ascending or descending) workbook.getDataSorter().getSortColumns().add(0, SortComparsionType.Values, OrderBy.Ascending); //Sort data in the specified cell range workbook.getDataSorter().sort(sheet.getCellRange("A1:D10")); //Save the document to file workbook.saveToFile("SortData.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.
Hide Gridlines in Excel Chart in Java
This article demonstrates how to hide gridlines in an Excel chart using Spire.XLS for Java.
import com.spire.xls.*; public class HideGridlinesInChart { public static void main(String[] args) { //Create a Workbook object Workbook workbook = new Workbook(); //Load an Excel file that contains data for creating chart workbook.loadFromFile("C:\\Users\\Administrator\\Desktop\\data.xlsx"); //Get the first worksheet Worksheet sheet = workbook.getWorksheets().get(0); //Add a column chart Chart chart = sheet.getCharts().add(ExcelChartType.ColumnClustered); chart.setChartTitle("Column Chart"); //Set the chart data range chart.setDataRange(sheet.getCellRange("A1:C5")); chart.setSeriesDataFromRange(false); //Set the chart position chart.setLeftColumn(1); chart.setTopRow(6); chart.setRightColumn(8); chart.setBottomRow(19); //Hide the grid lines of chart chart.getPrimaryValueAxis().hasMajorGridLines(false); //Save the document workbook.saveToFile("HideGridlines.xlsx", ExcelVersion.Version2016); } }
Java: Accept or Reject Tracked Changes in Word
Track Changes is a built-in feature in Microsoft Word which allows you to see all changes that were made to the document, and you can decide whether to accept or reject those changes. It is very useful especially when you are collaborating with multiple people on the same contracts or school assignments. In this article, you will learn how to programmatically accept or reject all tracked changes in a Word document using Spire.Doc for Java.
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.4.6</version> </dependency> </dependencies>
Accept All Tracked Changes in a Word document
The detailed steps are as follows.
- Create a Document instance.
- Load a sample Word document using Document.loadFromFile() method.
- Accept all changes in the document using Document.acceptChanges() method.
- Save the document to another file using Document.saveToFile() method.
- Java
import com.spire.doc.Document; import com.spire.doc.FileFormat; public class AcceptTrackedChanges { public static void main(String[] args) { //Create a Document instance Document doc = new Document(); //Load the sample Word document doc.loadFromFile("test file.docx"); //Accept all changes in the document doc.acceptChanges(); //Save the document doc.saveToFile("AcceptAllChanges.docx", FileFormat.Docx); } }
Reject All Tracked Changes in a Word document
The detailed steps are as follows.
- Create a Document instance.
- Load a sample Word document using Document.loadFromFile() method.
- Reject all changes in the document using Document.rejectChanges() method.
- Save the document to another file using Document.saveToFile() method.
- Java
import com.spire.doc.Document; import com.spire.doc.FileFormat; public class RejectTrackedChanges { public static void main(String[] args) { //Create a Document instance Document doc = new Document(); //Load the sample Word document doc.loadFromFile("test file.docx"); //Reject all changes in the document doc.rejectChanges(); //Save the document doc.saveToFile("RejectAllChanges.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.
C#/VB.NET: Convert SVG to PDF
SVG is a file format for vector graphics, used to create images that can be scaled without loss of quality. However, PDF is more suitable for sharing and printing due to its support for high-quality printing, encryption, digital signatures, and other features. Converting SVG to PDF ensures good image display on different devices and environments, and better protects intellectual property. In this tutorial, we will show you how to convert SVG to PDF and how to add a SVG image to PDF in C# and VB.NET using Spire.PDF for .NET.
Install Spire.PDF for .NET
To begin with, you need to add the DLL files included in the Spire.PDF for .NET package as references in your .NET project. The DLLs files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.PDF
Convert SVG to PDF in C# and VB.NET
Spire.PDF for .NET provides the PdfDocument.SaveToFile(String, FileFormat) method, which allows users to save an SVG file as a PDF. The detailed steps are as follows.
- Create a PdfDocument object.
- Load a sample SVG file using PdfDocument.LoadFromFile() method.
- Convert the SVG file to PDF using PdfDocument.SaveToFile(String, FileFormat) method.
- C#
- VB.NET
using Spire.Pdf; namespace SVGtoPDF { class Program { static void Main(string[] args) { //Create a PdfDocument object PdfDocument doc = new PdfDocument(); //Load a sample SVG file doc.LoadFromSvg("Sample.svg"); //Save result document doc.SaveToFile("Result.pdf", FileFormat.PDF); doc.Dispose(); } } }
Add SVG image to PDF in C# and VB.NET
In addition to converting SVG to PDF directly, it also supports adding SVG image files to the specified locations in PDF. Please check the steps as below:
- Create a PdfDocument object and load an SVG file using PdfDocument. LoadFromSvg() method.
- Create a template based on the content of the SVG file using PdfDocument. Pages[].CreateTemplate() method.
- Get the width and height of the template on the page.
- Create another PdfDocument object and load a PDF file using PdfDocument.LoadFromFile() method.
- Draw the template with a custom size at a specified location using PdfDocument.Pages[].Canvas.DrawTemplate() method.
- Save to PDF file using PdfDocument.SaveToFile(String, FileFormat) method.
- C#
- VB.NET
using Spire.Pdf; using Spire.Pdf.Graphics; using System.Drawing; namespace AddSVGImagetoPDF { class Program { static void Main(string[] args) { //Create a PdfDocument object PdfDocument doc1 = new PdfDocument(); //Load an SVG file doc1.LoadFromSvg("C:\\Users\\Administrator\\Desktop\\sample.svg"); //Create a template based on the content of the SVG file PdfTemplate template = doc1.Pages[0].CreateTemplate(); //Get the width and height of the template float width = template.Width; float height = template.Height; //Create another PdfDocument object PdfDocument doc2 = new PdfDocument(); //Load a PDF file doc2.LoadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pdf"); //Draw the template with a custom size at a specified location doc2.Pages[0].Canvas.DrawTemplate(template, new PointF(0, 0), new SizeF(width * 0.8f, height * 0.8f)); //Save to PDF file doc2.SaveToFile("AddSvgToPdf.pdf", FileFormat.PDF); doc2.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.
C#/VB.NET: Create a PDF Portfolio
A PDF portfolio is a collection of files that can contain text documents, spreadsheets, emails, images, PowerPoint presentations and drawings. Although a PDF portfolio assembles different types of files into a single unit, each of the files in it retains their original formatting, resolutions and sizes. In this article, you will learn how to programmatically create a PDF portfolio using Spire.PDF for .NET.
Install Spire.PDF for .NET
To begin with, you need to add the DLL files included in the Spire.PDF for.NET package as references in your .NET project. The DLLs files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.PDF
Create a PDF Portfolio and Add Files to It
As a PDF portfolio is a collection of files, Spire.PDF for .NET allows you to create it easily using PdfDocument.Collection property. Then you can add files to the PDF portfolio using PdfCollection.AddFile() method. The detailed steps are as follows:
- Specify the files that need to be added to the PDF portfolio.
- Create PdfDocument instance.
- Create a PDF portfolio and add files to it using PdfDocument.Collection.AddFile() method.
- Save the result file using PdfDocument.SaveToFile() method.
- C#
- VB.NET
using System; using Spire.Pdf; namespace CreatePDFPortfolio { class Program { static void Main(string[] args) { // Specify the files String[] files = new String[] { "input.pdf", "sample.docx", "report.xlsx", "Intro.pptx", "logo.png" }; //Create a PdfDocument instance using (PdfDocument pdf = new PdfDocument()) { //Create a PDF portfolio and add files to it for (int i = 0; i < files.Length; i++) { pdf.Collection.AddFile(files[i]); } //Save the result file pdf.SaveToFile("PortfolioWithFiles.pdf", FileFormat.PDF); pdf.Dispose(); } } } }
Create a PDF Portfolio and Add Folders to It
After creating a PDF portfolio, Spire.PDF for .NET also allows you to create folders within the PDF portfolio to further manage the files. The detailed steps are as follows:
- Specify the files that need to be added to the PDF portfolio.
- Create PdfDocument instance.
- Create a PDF Portfolio using PdfDocument.Collection property.
- Add folders to the PDF portfolio using PdfCollection.Folders.CreateSubfolder() method, and then add files to the folders using PdfFolder.AddFile() method.
- Save the result file using PdfDocument.SaveToFile() method.
- C#
- VB.NET
using System; using Spire.Pdf; using Spire.Pdf.Collections; namespace CreatePDFPortfolio { class Program { static void Main(string[] args) { // Specify the files String[] files = new String[] { "input.pdf", "sample.docx", "report.xlsx", "Intro.pptx", "logo.png" }; //Create a PdfDocument instance using (PdfDocument pdf = new PdfDocument()) { //Create a PDF portfolio and add folders to it for (int i = 0; i < files.Length; i++) { PdfFolder folder = pdf.Collection.Folders.CreateSubfolder("Folder" + i); //Add files to the folders folder.AddFile(files[i]); } //Save the result file pdf.SaveToFile("PortfolioWithFolders.pdf", FileFormat.PDF); pdf.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.