Tuesday, 24 November 2020 06:14

Link to a Specific Slide in Java

This article demonstrates how to add a hyperlink that links to a specific slide within the presentation by using Spire.Presnetation for Java.

import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;

import java.awt.geom.Rectangle2D;

public class LinkToSpecificSlide {

    public static void main(String[] args) throws Exception {

        //Create a Presentation object
        Presentation presentation = new Presentation();
        presentation.getSlideSize().setType(SlideSizeType.SCREEN_16_X_9);

        //Append a slide to it (there are two slides in the presentation including the default one)
        presentation.getSlides().append();

        //Get the first slide
        ISlide firstSlide = presentation.getSlides().get(0);

        //Add a shape to it
        IAutoShape shape = firstSlide.getShapes().appendShape(ShapeType.RECTANGLE,new Rectangle2D.Float(10, 50, 200, 50));
        shape.getFill().setFillType(FillFormatType.NONE);
        shape.getLine().setFillType(FillFormatType.NONE);

        //Add text to shape
        shape.getTextFrame().setText("Jump to the second slide");

        //Set a hyperlink for the shape, linking to the second slide 
        ClickHyperlink hyperlink = new ClickHyperlink(presentation.getSlides().get(1));
        shape.setClick(hyperlink);
        shape.getTextFrame().getTextRange().setClickAction(hyperlink);

        //Save to another file
        presentation.saveToFile("LinkToSlide.pptx", FileFormat.PPTX_2013);
    }
}

Link to a Specific Slide in Java

This article will show you how to add lines to Excel worksheets through two points. We could set the point’s location via relative location and Absolute location in pixels.

C#
using Spire.Xls;
using Spire.Xls.Core.Spreadsheet.Shapes;
using System.Drawing;

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

        {
            //Initiate a Workbook object and get the first worksheet
            Workbook workbook = new Workbook();
            Worksheet worksheet = workbook.Worksheets[0];
            //Add a line with relative location
            XlsLineShape line = worksheet.TypedLines.AddLine() as XlsLineShape;

            //set the column index of the starting point
            line.LeftColumn = 2;
            line.LeftColumnOffset = 2;
            line.TopRow = 5;
            line.TopRowOffset = 10;

            //set the column index of the end point
            line.RightColumn = 10;
            line.RightColumnOffset = 10;
            line.BottomRow =5;
            line.BottomRowOffset = 10; 

            //Set the color
            line.Color = Color.Red;


            //Add a line with Absolute location in pixels
            XlsLineShape line1 = worksheet.TypedLines.AddLine() as XlsLineShape;
            //Set the start point and end point
            line1.StartPoint = new Point(20, 30);
            line1.EndPoint = new Point(200, 30);

            //Set the color
            line1.Color = Color.Blue;
            
            workbook.SaveToFile("Addlines.xlsx", ExcelVersion.Version2013);
            workbook.Dispose();
        }
    }
 }
VB.NET
Imports Spire.Xls
Imports Spire.Xls.Core.Spreadsheet.Shapes
Imports System.Drawing

Namespace Word
    
    Class Program
        
        Private Shared Sub Main(ByVal args() As String)
            'Initiate a Workbook object and get the first worksheet
            Dim workbook As Workbook = New Workbook
            Dim worksheet As Worksheet = workbook.Worksheets(0)
            'Add a line with relative location
            Dim line As XlsLineShape = CType(worksheet.TypedLines.AddLine,XlsLineShape)
            'set the column index of the starting point
            line.LeftColumn = 2
            line.LeftColumnOffset = 2
            line.TopRow = 5
            line.TopRowOffset = 10
            'set the column index of the end point
            line.RightColumn = 10
            line.RightColumnOffset = 10
            line.BottomRow = 5
            line.BottomRowOffset = 10
            'Set the color
            line.Color = Color.Red
            'Add a line with Absolute location in pixels
            Dim line1 As XlsLineShape = CType(worksheet.TypedLines.AddLine,XlsLineShape)
            'Set the start point and end point
            line1.StartPoint = New Point(20, 30)
            line1.EndPoint = New Point(200, 30)
            'Set the color
            line1.Color = Color.Blue
            workbook.SaveToFile("Addlines.xlsx", ExcelVersion.Version2013)
            workbook.Dispose
        End Sub 
    End Class
End Namespace

Effective screenshot:

C#/VB.NET Add lines to Excel worksheets through two points

This article demonstrates how to add Trendline to an Excel chart and read the equation of the Trendline using Spire.XLS for Java.

Add Trendline

import com.spire.xls.*;
import com.spire.xls.core.IChartTrendLine;

import java.awt.*;

public class AddTrendline {
    public static void main(String[] args){
        //Create a Workbook instance
        Workbook workbook = new Workbook();
        //Load the Excel file
        workbook.loadFromFile("test.xlsx");

        //Get the first chart in the first worksheet
        Chart chart = workbook.getWorksheets().get(0).getCharts().get(0);

        //Add a Trendline to the first series of the chart
        IChartTrendLine trendLine = chart.getSeries().get(0).getTrendLines().add(TrendLineType.Linear);

        //Set Trendline name
        trendLine.setName("Linear(Series1)");
        //Set line type and color
        trendLine.getBorder().setPattern(ChartLinePatternType.DashDot);
        trendLine.getBorder().setColor(Color.blue);
        //Set forward and backward value
        trendLine.setForward(0.5);
        trendLine.setBackward(0.5);
        //Set intercept value
        trendLine.setIntercept(5);

        //Display equation on chart
        trendLine.setDisplayEquation(true);
        //Display R-Squared value on chart
        trendLine.setDisplayRSquared(true);

        //Save the result file
        workbook.saveToFile("AddTrendline.xlsx", ExcelVersion.Version2013);
    }
}

Add Trendline to Chart and Read Trendline Equation in Excel in Java

Read Trendline equation

import com.spire.xls.Chart;
import com.spire.xls.Workbook;
import com.spire.xls.core.IChartTrendLine;

public class ReadEquationOfTrendline {
    public static void main(String[] args){
        //Create a Workbook instance
        Workbook workbook = new Workbook();
        //Load the Excel file
        workbook.loadFromFile("AddTrendline.xlsx");

        //Get the first chart in the first worksheet
        Chart chart = workbook.getWorksheets().get(0).getCharts().get(0);

        //Read the equation of the first series of the chart
        IChartTrendLine trendLine = chart.getSeries().get(0).getTrendLines().get(0);
        String equation = trendLine.getFormula();
        System.out.println("The equation is: " + equation);
    }
}

Add Trendline to Chart and Read Trendline Equation in Excel in Java

Spire.Doc for Java supports embedding an external file (Word, Excel, PowerPoint, PDF, picture, video, etc.) in Word documents as an OLE object. This article gives you an example of how to insert a PDF file into a Word document.

import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.OleObjectType;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.DocOleObject;
import com.spire.doc.fields.DocPicture;

public class InsertOLE {

    public static void main(String[] args) {

        //Create a Document object and load a Word document
        Document doc = new Document();
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\source.docx");

        //Get the last section
        Section section = doc.getLastSection();

        //Add a paragraph
        Paragraph par = section.addParagraph();

        //Load an image which will be inserted to Word document representing the embedded file
        DocPicture pdfIcon = new DocPicture(doc);
        pdfIcon.loadImage("C:\\Users\\Administrator\\Desktop\\pdf-icon.jpg");

        //Insert a PDF file to the Word document as an OLE object
        par.appendOleObject("C:\\Users\\Administrator\\Desktop\\report.pdf", pdfIcon, OleObjectType.Adobe_Acrobat_Document);

        //Save to another file
        doc.saveToFile("EmbedDocument.docx", FileFormat.Docx_2013);
    }
}

Insert an External File into Word Documents in Java

This article will show you how to accept/reject the tracked changes on Excel workbook in C#/VB.NET with the help of Spire.XLS.

C#
using Spire.Xls;

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

        {
            Workbook workbook = new Workbook();
            workbook.LoadFromFile("Sample.xlsx");

            if (workbook.HasTrackedChanges)
            {
                workbook.AcceptAllTrackedChanges();
                //workbook.RejectAllTrackedChanges();
            }
                          
            workbook.SaveToFile("Result.xlsx", FileFormat.Version2013);
        }
    }
 }
VB.NET
Imports Spire.Xls

Namespace TrackChanges_XLS
    
    Class Program
        
        Private Shared Sub Main(ByVal args() As String)
            Dim workbook As Workbook = New Workbook
            workbook.LoadFromFile("Sample.xlsx")
            If workbook.HasTrackedChanges Then
                workbook.AcceptAllTrackedChanges
                'workbook.RejectAllTrackedChanges();
            End If
            
            workbook.SaveToFile("Result.xlsx", FileFormat.Version2013)
        End Sub
    End Class
End Namespace

This tutorial will demonstrate how to set font and background color for Excel textbox using Spire.XLS for Java.

The following is a screenshot of a sample document:

Set Font and Background Color for Excel Textbox in Java

Using the code

import java.awt.*;
import com.spire.xls.*;
import com.spire.xls.core.spreadsheet.shapes.*;

public class SetFontAndBackground {
    public static void main(String[] args) {
        //Load an Excel sample
        Workbook workbook = new Workbook();
        workbook.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample.xlsx");

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

        //Get the textbox which will be edited
        XlsTextBoxShape shape = (XlsTextBoxShape) sheet.getTextBoxes().get(0);

        //Set the font and background color for the textbox
        //Set font
        ExcelFont font = workbook.createFont();
        font.setFontName("Calibri");
        font.setSize(14);
        font.isBold(false);
        font.setColor(Color.MAGENTA);
        (new RichText(shape.getRichText())).setFont(0, shape.getText().length() - 1, font);

        //Set background color
        shape.getFill().setFillType(ShapeFillType.SolidColor);
        shape.getFill().setForeKnownColor(ExcelColors.Yellow);

        //Save to file
        workbook.saveToFile("output/setFontAndBackgroundForTextBox.xlsx", ExcelVersion.Version2013);
    }
}

Output

Set Font and Background Color for Excel Textbox in Java

Tuesday, 10 November 2020 07:16

Java set Excel print page margins

This article demonstrates how to set Excel page margins before printing the Excel worksheets in Java applications. By using Spire.XLS for Java, we could set top margin, bottom margin, left margin, right margin, header margin, and footer margin. Please note that the unit for margin is inch on Spire.XLS for Java while On Microsoft Excel, it is cm (1 inch=2.54 cm).

import com.spire.xls.*;

public class setMargins {
    public static void main(String[] args) {

        String outputFile="output/setMarginsOfExcel.xlsx";

        //Load the sample document from file
        Workbook workbook = new Workbook();
        workbook.loadFromFile("Sample.xlsx";);

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

        //Get the PageSetup object of the first worksheet.
        PageSetup pageSetup = sheet.getPageSetup();

        //Set the page margins of bottom, left, right and top.
        pageSetup.setBottomMargin(2);
        pageSetup.setLeftMargin(1);
        pageSetup.setRightMargin(1);
        pageSetup.setTopMargin(3);
        
        //Set the margins of header and footer.
        pageSetup.setHeaderMarginInch(2);
        pageSetup.setFooterMarginInch(2);

        //Save to file.
        workbook.saveToFile(outputFile, ExcelVersion.Version2013);

    }
}

Output:

Java set Excel print page margins

This article demonstrates how to add and delete digital signature in an Excel file using Spire.XLS for .NET.

Add digital signature

C#
using System;
using System.Security.Cryptography.X509Certificates;
using Spire.Xls;
using Spire.Xls.Core.MergeSpreadsheet.Interfaces;

namespace Add_Digital_Signature
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Workbook instance
            Workbook workbook = new Workbook();
            //Load an Excel file
            workbook.LoadFromFile("Sample.xlsx");

            //Add digital signature to the file
            X509Certificate2 cert = new X509Certificate2("gary.pfx", "e-iceblue");
            DateTime certtime = new DateTime(2020, 7, 1, 7, 10, 36);
            IDigitalSignatures signature = workbook.AddDigitalSignature(cert, "e-iceblue", certtime);
            
            //Save the result file
            workbook.SaveToFile("AddDigitalSignature.xlsx", FileFormat.Version2013);
        }
    }
}
VB.NET
Imports System
Imports System.Security.Cryptography.X509Certificates
Imports Spire.Xls
Imports Spire.Xls.Core.MergeSpreadsheet.Interfaces

Namespace Add_Digital_Signature
    Class Program
        Private Shared Sub Main(ByVal args As String())
            Dim workbook As Workbook = New Workbook()
            workbook.LoadFromFile("Sample.xlsx")
            Dim cert As X509Certificate2 = New X509Certificate2("gary.pfx", "e-iceblue")
            Dim certtime As DateTime = New DateTime(2020, 7, 1, 7, 10, 36)
            Dim signature As IDigitalSignatures = workbook.AddDigitalSignature(cert, "e-iceblue", certtime)
            workbook.SaveToFile("AddDigitalSignature.xlsx", FileFormat.Version2013)
        End Sub
    End Class
End Namespace

 

Add and Delete Digital Signature in Excel in C#, VB.NET

Delete digital signature

C#
using Spire.Xls;

namespace Delete_Digital_Signature
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Workbook instance
            Workbook workbook = new Workbook();
            //Load an Excel file
            workbook.LoadFromFile("AddDigitalSignature.xlsx");

            //Remove all the digital signatures in the file
            workbook.RemoveAllDigitalSignatures();

            //Save the result file
            workbook.SaveToFile("RemoveDigitalSignature.xlsx", FileFormat.Version2013);
        }
    }
}
VB.NET
Imports Spire.Xls

Namespace Delete_Digital_Signature
    Class Program
        Private Shared Sub Main(ByVal args As String())
            Dim workbook As Workbook = New Workbook()
            workbook.LoadFromFile("AddDigitalSignature.xlsx")
            workbook.RemoveAllDigitalSignatures()
            workbook.SaveToFile("RemoveDigitalSignature.xlsx", FileFormat.Version2013)
        End Sub
    End Class
End Namespace

The output Excel file after deleting digital signature:

Add and Delete Digital Signature in Excel in C#, VB.NET

This article demonstrates how to set spacing between paragraphs and how to set line spacing within a paragraph using Spire.Doc for Java.

Set Paragraph Spacing

import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;

public class SetParagraphSpacing {

    public static void main(String[] args) {

        //Create a Document instance
        Document doc = new Document();

        //Add a section
        Section section = doc.addSection();

        //Add a paragraph
        Paragraph para = section.addParagraph();
        para.appendText("Spire.Doc for Java is a professional Java Word API that enables Java applications to " +
                "manipulate Word documents without using Microsoft Office.");

        //Set paragraph after spacing
        para.getFormat().setAfterSpacing(20f);

        //Add another paragraph
        para = section.addParagraph();
        para.appendText("A plenty of Word document processing tasks can be performed by Spire.Doc for Java, "+
                "such as creating, comparing, reading, editing, converting and printing Word documents, "+
                "inserting image, adding header and footer, creating table and adding form field.");
        
        //Save the document
        doc.saveToFile("output/SetParagraphSpacing.docx", FileFormat.Docx_2013);
    }
}

Set Paragraph Spacing and Line Spacing in Word in Java

Set Line Spacing

import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;

public class SetLineSpacing {

    public static void main(String[] args) {

        //Create a Document instance
        Document doc = new Document();

        //Add a section
        Section section = doc.addSection();

        //Add a paragraph
        Paragraph para = section.addParagraph();
        para.appendText("Spire.Doc for Java is a professional Java Word API that enables Java applications to " +
                "manipulate Word documents without using Microsoft Office. A plenty of Word document " +
                "processing tasks can be performed by Spire.Doc for Java, such as creating, comparing, " +
                "reading, editing, converting and printing Word documents, inserting image, adding header " +
                "and footer, creating table and adding form field.");

        //Set line spacing
        para.getFormat().setLineSpacing(30f);

        //Save the document
        doc.saveToFile("SetLineSpacing.docx", FileFormat.Docx_2013);
    }
}

Set Paragraph Spacing and Line Spacing in Word in Java

Tuesday, 03 November 2020 08:31

Remove Header and Footer in Word in Java

There can be 3 types of headers and footers in a Word document, i.e. on first page, on odd pages and on even pages. This article demonstrates how to remove all the headers and footers on first page, on odd pages and on even pages in a section of a Word document using Spire.Doc for Java.

Remove Header

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.HeaderFooterType;

public class RemoveHeader {
    public static void main(String[] args){
        //Create a Document instance
        Document doc = new Document();
        //Load a sample Word document
        doc.loadFromFile("Sample.docx");

        //Get the first section
        Section section = doc.getSections().get(0);

        //Remove the header on the first page
        HeaderFooter header = section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.Header_First_Page);
        if (header != null)
            header.getChildObjects().clear();
        
        //Remove the header on the odd pages
        header = section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.Header_Odd);
        if (header != null)
            header.getChildObjects().clear();
        
        //Remove the header on the even pages
        header = section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.Header_Even);
        if (header != null)
            header.getChildObjects().clear();

        //Save the result document
        doc.saveToFile("RemoveHeader.docx", FileFormat.Docx_2013);
    }
}

Remove Footer

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.HeaderFooterType;

public class RemoveFooter {
    public static void main(String[] args){
        //Create a Document instance
        Document doc = new Document();
        //Load a sample Word document
        doc.loadFromFile("Sample.docx");

        //Get the first section
        Section section = doc.getSections().get(0);

        //Remove footer on the first page
        HeaderFooter footer = section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.Footer_First_Page);
        if (footer != null)
            footer.getChildObjects().clear();

        //Remove footer on the odd pages
        footer = section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.Footer_Odd);
        if (footer != null)
            footer.getChildObjects().clear();

        //Remove footer on the even pages
        footer = section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.Footer_Even);
        if (footer != null)
            footer.getChildObjects().clear();

        //Save the result document
        doc.saveToFile("RemoveFooter.docx", FileFormat.Docx_2013);
    }
}
Page 4 of 132