This article demonstrates how to extract OLE objects from an Excel document using Spire.XLS for Java.

The following screenshot shows the input excel document we used for demonstration:

Extract OLE Objects from an Excel Document in 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:

Extract OLE Objects from an Excel Document 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.");
        }
    }
}

Detect if an Excel Document is Password Protected in Java

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:

Java remove text or image watermark from presentation slides

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 remove text or image watermark from 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:

Java insert image watermark to presentation slides

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:

Java insert image watermark to presentation slides

Wednesday, 27 May 2020 07:48

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:

Convert Text to Columns in Excel in Java

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:

Convert Text to Columns in Excel in Java

Wednesday, 27 May 2020 01:57

Sort Excel Data in Java

This article demonstrates how to sort data within a cell range using Spire.XLS for Java.

import com.spire.xls.Worksheet;
import com.spire.xls.Workbook;
import com.spire.xls.OrderBy;
import com.spire.xls.ExcelVersion;

public class SortData {

    public static void main(String[] args) {

        //Create a Workbook object
        Workbook workbook = new Workbook();

        //Load the sample Excel file
        workbook.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.xlsx");

        //Get the first worksheet
        Worksheet sheet = workbook.getWorksheets().get(0);

        //Specify the column index and the sort mode. The column index starts from 0.
        workbook.getDataSorter().getSortColumns().add(1,OrderBy.Ascending);

        //Specify the range to sort
        workbook.getDataSorter().sort(sheet.getCellRange("A1:F9"));

        //Save the document
        workbook.saveToFile("SortData.xlsx", ExcelVersion.Version2016);
    }
}

Sort Excel Data in Java

Monday, 25 May 2020 07:11

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);
    }
}

Hide Gridlines in Excel Chart in Java

This article demonstrrates how to accept or reject all tracked changes in a Word document using Spire.XLS for Java.

Sample Document

Accept or Reject Tracked Changes in Word in Java

import com.spire.doc.Document;
import com.spire.doc.FileFormat;

public class AcceptOrRejectChanges {

    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");

        //Accept all changes in the entire document
        doc.acceptChanges();
        
        ////Reject all changes in the entire document
        //doc.rejectChanges();
        
        //Save the document
        doc.saveToFile("AcceptAllChanges.docx", FileFormat.Docx);
    }
}

Output

Accept or Reject Tracked Changes in Word in Java

Tuesday, 19 May 2020 08:10

Convert SVG to PDF in C#/VB.NET

With Spire.PDF for .NET, we could save PDF to SVG easily. Starts from Spire.PDF V 6.5.6, it supports to convert SVG image to PDF. This article will demonstrate how to add a SVG image to PDF file and convert SVG to PDF.

Convert SVG to PDF

Spire.PDF offers a method of doc.LoadFromSvg() to load the SVG directly and doc.SaveToFile()method to save to PDF file.

Firstly, view the Sample SVG image:

Convert SVG to PDF in C#/VB.NET

C#
using Spire.Pdf;

namespace SVGtoPDF
{
    class Program
    {
        static void Main(string[] args)
        {
            PdfDocument doc = new PdfDocument();
            doc.LoadFromSvg("Sample.svg");

            doc.SaveToFile("Result.pdf", FileFormat.PDF);  

        }
    }
}
VB.NET
Imports Spire.Pdf

Namespace SVGtoPDF
    
    Class Program
        
        Private Shared Sub Main(ByVal args() As String)
            Dim doc As PdfDocument = New PdfDocument
            doc.LoadFromSvg("Sample.svg")
            doc.SaveToFile("Result.pdf", FileFormat.PDF)
        End Sub
    End Class
End Namespace

Effective screenshot after converting SVG to PDF:

Convert SVG to PDF in C#/VB.NET

Add SVG image to PDF file

Besides convert the SVG directly to PDF, it also supports to add an existing SVG image to PDF file. It also supports to set the position and size of the SVG image after adding it to PDF. Please check the steps as below:

C#
using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;

namespace AddSVGImagetoPDF
{
    class Program
    {
        static void Main(string[] args)
        {

            PdfDocument doc1 = new PdfDocument();
            doc1.LoadFromSvg("Sample.svg");

            PdfDocument doc2 = new PdfDocument();
            doc2.LoadFromFile("Sample2.pdf");

            PdfTemplate template = doc1.Pages[0].CreateTemplate();


            //template.Draw(doc2.Pages[0].Canvas, new PointF());

            //set the position and size of SVG image
            doc2.Pages[0].Canvas.DrawTemplate(doc1.Pages[0].CreateTemplate(), new PointF(80, 80), new SizeF(300, 300));

            doc2.SaveToFile("Result0.pdf", FileFormat.PDF);   


        }
    }
}
VB.NET
Imports Spire.Pdf
Imports Spire.Pdf.Graphics
Imports System.Drawing

Namespace AddSVGImagetoPDF
    
    Class Program
        
        Private Shared Sub Main(ByVal args() As String)
            Dim doc1 As PdfDocument = New PdfDocument
            doc1.LoadFromSvg("Sample.svg")
            Dim doc2 As PdfDocument = New PdfDocument
            doc2.LoadFromFile("Sample2.pdf")
            Dim template As PdfTemplate = doc1.Pages(0).CreateTemplate
            'template.Draw(doc2.Pages[0].Canvas, new PointF());
            'set the posion and size of SVG image
            doc2.Pages(0).Canvas.DrawTemplate(doc1.Pages(0).CreateTemplate, New PointF(80, 80), New SizeF(300, 300))
            doc2.SaveToFile("Result0.pdf", FileFormat.PDF)
        End Sub
    End Class
End Namespace

Effective screenshot after adding a SVG image to PDF file:

Convert SVG to PDF in C#/VB.NET

Tuesday, 19 May 2020 01:13

Create a PDF Portfolio in C#

A PDF Portfolio allows us to put multiple types of files (for example, text documents, email messages, spreadsheets and PowerPoint presentations) together into a master PDF file. The files in a PDF Portfolio can retain their individual identities. We can open, read, edit, and format each component file independently of the other component files.

Spire.PDF for .NET supports creating a PDF Portfolio, adding files and folders to the PDF Portfolio. The following example demonstrates how to create a PDF Portfolio and add files to it using Spire.PDF for .NET.

using System;
using Spire.Pdf;

namespace CreatePDFPortfolio
{
    class Program
    {
        static void Main(string[] args)
        {
            String[] files = new String[] { "Instruction.pdf", "Sales Report.docx", "Sample.xlsx" };
            //Create a PdfDocument instance
            using (PdfDocument doc = new PdfDocument())
            {
                //Create a PDF Portfolio and add files to it
                for (int i = 0; i < files.Length; i++)
                {
                    doc.Collection.AddFile(files[i]);
                    
                }
                //Save the result file
                doc.SaveToFile("PortfolioWithFiles.pdf", FileFormat.PDF);
                doc.Dispose();
            }
        }
    }
}

Create a PDF Portfolio in C#

The following example shows how to create a PDF Portfolio and add folders to it using Spire.PDF for .NET.

using System;
using Spire.Pdf;
using Spire.Pdf.Collections;

namespace CreatePDFPortfolio
{
    class Program
    {
        static void Main(string[] args)
        {
            String[] files = new String[] { "Instruction.pdf", "Sales Report.docx", "Sample.xlsx" };
            //Create a PdfDocument instance
            using (PdfDocument doc = new PdfDocument())
            {
                //Create a PDF Portifolio, add folders to it and add files to the folders
                for (int i = 0; i < files.Length; i++)
                {                                       
                    PdfFolder folder = doc.Collection.Folders.CreateSubfolder("Folder" + i);
                    folder.AddFile(files[i]);
                }
                //Save the result file
                doc.SaveToFile("PortfolioWithFolders.pdf", FileFormat.PDF);
                doc.Dispose();
            }
        }
    }
}

Create a PDF Portfolio in C#

Page 1 of 121