In addition to supporting adding background color to a whole Word document, Spire.Doc for Java can set background color for specified paragraph or text of a Word document. This article will demonstrate how to do it in Java programmatically.

Using the code

import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.*;
import java.awt.*;

public class SetParagraphShading {
    public static void main(String[] args) {
        //Load a Word document sample
        Document document = new Document();
        document.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample.docx");

        //Get a specified paragraph from the sample
        Paragraph paragaph = document.getSections().get(0).getParagraphs().get(1);
        //Set background color for the paragraph.
        paragaph.getFormat().setBackColor(Color.yellow);

        //Get specified text from the sample
        paragaph = document.getSections().get(0).getParagraphs().get(6);
        TextSelection selection = paragaph.find("Spire.Doc for Java", true, false);
        //Set background color for the text 
        TextRange range = selection.getAsOneRange();
        range.getCharacterFormat().setTextBackgroundColor(Color.pink);

        //Saving the resulting document 
        document.saveToFile("output/setParagraphShading.docx", FileFormat.Docx_2013);
    }
}

Output

Java Set background color for specified paragraph or text of a Word document

Wednesday, 28 October 2020 02:44

Create a Line Chart in PowerPoint in Java

This article demonstrates how to create a line chart in a PowerPoint document using Spire.Presentation for Java.

import com.spire.presentation.FileFormat;
import com.spire.presentation.Presentation;
import com.spire.presentation.SlideSizeType;
import com.spire.presentation.charts.ChartLegendPositionType;
import com.spire.presentation.charts.ChartType;
import com.spire.presentation.charts.IChart;

import java.awt.geom.Rectangle2D;

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

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

        //Insert a line chart 
        Rectangle2D.Double rect = new   Rectangle2D.Double(100, 50, 600, 430);
        IChart chart = presentation.getSlides().get(0).getShapes().appendChart(ChartType.LINE, rect);

        //Set chart title 
        chart.getChartTitle().getTextProperties().setText("Product Trends by Month");
        chart.getChartTitle().getTextProperties().isCentered(true);
        chart.getChartTitle().setHeight(30);
        chart.hasTitle(true);

        //Set axis title 
        chart.getPrimaryCategoryAxis().getTitle().getTextProperties().setText("Month");
        chart.getPrimaryCategoryAxis().hasTitle(true);
        chart.getPrimaryValueAxis().getTitle().getTextProperties().setText("Sales Volume");
        chart.getPrimaryValueAxis().hasTitle(true);

        //Write data to chart as chart data 
        chart.getChartData().get(0,0).setText("Month");
        chart.getChartData().get(1,0).setText("Jan");
        chart.getChartData().get(2,0).setText("Feb");
        chart.getChartData().get(3,0).setText("Mar");
        chart.getChartData().get(4,0).setText("Apr");
        chart.getChartData().get(5,0).setText("May");
        chart.getChartData().get(6,0).setText("Jun");

        chart.getChartData().get(0,1).setText("Desktops");
        chart.getChartData().get(1,1).setNumberValue(80);
        chart.getChartData().get(2,1).setNumberValue(45);
        chart.getChartData().get(3,1).setNumberValue(25);
        chart.getChartData().get(4,1).setNumberValue(20);
        chart.getChartData().get(5,1).setNumberValue(10);
        chart.getChartData().get(6,1).setNumberValue(5);

        chart.getChartData().get(0,2).setText("Laptops");
        chart.getChartData().get(1,2).setNumberValue(30);
        chart.getChartData().get(2,2).setNumberValue(25);
        chart.getChartData().get(3,2).setNumberValue(35);
        chart.getChartData().get(4,2).setNumberValue(50);
        chart.getChartData().get(5,2).setNumberValue(45);
        chart.getChartData().get(6,2).setNumberValue(55);

        chart.getChartData().get(0,3).setText("Tablets");
        chart.getChartData().get(1,3).setNumberValue(10);
        chart.getChartData().get(2,3).setNumberValue(15);
        chart.getChartData().get(3,3).setNumberValue(20);
        chart.getChartData().get(4,3).setNumberValue(35);
        chart.getChartData().get(5,3).setNumberValue(60);
        chart.getChartData().get(6,3).setNumberValue(95);

        //Set series labels 
        chart.getSeries().setSeriesLabel(chart.getChartData().get("B1", "D1"));

        //Set categories labels 
        chart.getCategories().setCategoryLabels(chart.getChartData().get("A2", "A7"));

        //Assign data to series values 
        chart.getSeries().get(0).setValues(chart.getChartData().get("B2", "B7"));
        chart.getSeries().get(1).setValues(chart.getChartData().get("C2", "C7"));
        chart.getSeries().get(2).setValues(chart.getChartData().get("D2", "D7"));

        //Display values in data labels 
        chart.getSeries().get(0).getDataLabels().setLabelValueVisible(true);
        chart.getSeries().get(1).getDataLabels().setLabelValueVisible(true);
        chart.getSeries().get(2).getDataLabels().setLabelValueVisible(true);

        //Set chart legend position 
        chart.getChartLegend().setPosition(ChartLegendPositionType.TOP);

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

Create a Line Chart in PowerPoint in Java

This article demonstrates how to use Spire.XLS for Java to split Excel text or numbers in one cell into multiple columns by delimiters. The delimiter characters could be Space ( ), Comma (,) Semicolon(;) etc.

import com.spire.xls.*;

public class splitDataIntoMultipleColumns {
    public static void main(String[] args) {
        //Load the sample document from file
        Workbook workbook = new Workbook();
        workbook.loadFromFile("Sample.xlsx");

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

        //Split data into separate columns by the delimiter characters of space.
        String[] splitText = null;
        String text = null;
        for (int i = 1; i < sheet.getLastRow(); i++)
        {
            text = sheet.getRange().get(i + 1, 1).getText();
            splitText = text.split(" ");
            for (int j = 0; j < splitText.length; j++)
            {
                sheet.getRange().get(i + 1, 1 + j + 1).setText(splitText[j]);
            }
        }
        //Save to file
        workbook.saveToFile("Result.xlsx", ExcelVersion.Version2013);
    }
}

Output:

Java split one cell contents into multiple columns in Excel

We have already introduced how to add animation effect to shape in PowerPoint, in this article, we will introduce how to add animation effect to paragraph in PowerPoint using Spire.Presentation for Java.

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

import java.awt.*;
import java.awt.geom.Rectangle2D;

public class AddAnimationOnParagraph {
    public static void main(String[] args) throws Exception {
        //Create a Presentation instance
        Presentation ppt = new Presentation();

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

        //Add a shape to the slide
        IAutoShape shape = slide.getShapes().appendShape(ShapeType.RECTANGLE, new Rectangle2D.Double(150, 150, 450, 100));
        shape.getFill().setFillType(FillFormatType.SOLID);
        shape.getFill().getSolidColor().setColor(Color.gray);
        shape.getShapeStyle().getLineColor().setColor(Color.white);
        shape.appendTextFrame("This demo shows how to apply animation on paragraph in PPT document.");

        //Add animation effect to the first paragraph in the shape
        AnimationEffect animation = shape.getSlide().getTimeline().getMainSequence().addEffect(shape, AnimationEffectType.FLOAT);
        animation.setStartEndParagraphs(0, 0);

        //Save the result document
        ppt.saveToFile("AddAnimationOnPara.pptx", FileFormat.PPTX_2013);
        ppt.dispose();
    }
}

Output:

Add Animation Effect to Paragraph in PowerPoint in Java

Tuesday, 20 October 2020 07:03

Create Pivot Chart in Excel in Java

This article demonstrates how to create pivot chart in an Excel file in Java using Spire.XLS for Java.

The input Excel file:

Create Pivot Chart in Excel in Java

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

public class CreatePivotChart {
    public static void main(String[] args) {
        //Load the Excel file
        Workbook workbook = new Workbook();
        workbook.loadFromFile("Sample.xlsx");

        //Get the first worksheet
        Worksheet sheet = workbook.getWorksheets().get(0);
        //get the first pivot table in the worksheet
        IPivotTable pivotTable = sheet.getPivotTables().get(0);

        //Add a clustered column chart based on the pivot table data to the second worksheet
        Chart chart = workbook.getWorksheets().get(1).getCharts().add(ExcelChartType.ColumnClustered, pivotTable);
        //Set chart position
        chart.setTopRow(2);
        chart.setBottomRow(15);
        //Set chart title
        chart.setChartTitle("Total");

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

Output:

Create Pivot Chart in Excel in Java

Monday, 19 October 2020 08:37

Create Bubble Chart in Excel in C#/VB.NET

This article will show you how to use Spire.XLS for .NET to create a bubble chart in Excel in C# and VB.NET.

C#
using Spire.Xls;

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

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

            //Add a worksheet and set name 
            workbook.CreateEmptySheets(1);
            Worksheet sheet = workbook.Worksheets[0];
            sheet.Name = "Chart data";
            //Initialize chart and set its type
            Chart chart = sheet.Charts.Add(ExcelChartType.Bubble);
            
            //Set the position of the chart in the worksheet
            chart.LeftColumn = 1;
            chart.RightColumn = 10;
            chart.TopRow = 1;
            chart.BottomRow = 20;

            //Set title for the chart and values
            Spire.Xls.Charts.ChartSerie cs1 = chart.Series.Add("Bubble Chart");
            cs1.EnteredDirectlyValues = new object[] { 2.2, 5.6 };
            cs1.EnteredDirectlyCategoryLabels = new object[] { 1.1, 4.4 };
            cs1.EnteredDirectlyBubbles = new object[] { 3, 6 };

            //Save the document to file
            workbook.SaveToFile("Output.xlsx", ExcelVersion.Version2010);
        }
    }
    
}
VB.NET
Imports Spire.Xls

Namespace BubbleChart
    
    Class Program
        
        Private Shared Sub Main(ByVal args() As String)
            'Create a new workbook
            Dim workbook As Workbook = New Workbook
            'Add a worksheet and set name 
            workbook.CreateEmptySheets(1)
            Dim sheet As Worksheet = workbook.Worksheets(0)
            sheet.Name = "Chart data"
            'Initialize chart and set its type
            Dim chart As Chart = sheet.Charts.Add(ExcelChartType.Bubble)
            'Set the position of the chart in the worksheet
            chart.LeftColumn = 1
            chart.RightColumn = 10
            chart.TopRow = 1
            chart.BottomRow = 20
            'Set title for the chart and values
            Dim cs1 As Spire.Xls.Charts.ChartSerie = chart.Series.Add("Bubble Chart")
            cs1.EnteredDirectlyValues = New Object() {2.2, 5.6}
            cs1.EnteredDirectlyCategoryLabels = New Object() {1.1, 4.4}
            cs1.EnteredDirectlyBubbles = New Object() {3, 6}
            'Save the document to file
            workbook.SaveToFile("Output.xlsx", ExcelVersion.Version2010)
        End Sub
    End Class
End Namespace

Effective screenshot of Excel Bubble chart:

Create Bubble Chart in Excel in C#/VB.NET

We have already demonstrated how to insert HTML formatted text to a Presentation slide by using Spire.Presentation for Java. This article will introduce the way to insert HTML with images to PowerPoint and each html tag will be added to the slide as a separate shape.

import com.spire.presentation.*;
import com.spire.presentation.collections.*;

public class AddHTMLWithImage {
    public static void main(String[] args) throws Exception {
        //Create an instance of presentation document
        Presentation ppt = new Presentation();
        //Get the shapes on the first slide.
        ShapeList shapes = ppt.getSlides().get(0).getShapes();
        //Add contents to shapes from HTML codes, which includes text and image.
        shapes.addFromHtml("<html><div><p>E-iceblue</p>" +
                "<p><img src='https://cdn.e-iceblue.com/C:\\Users\\Test1\\Desktop\\logo.png'/></p>" +
                "<p>Spire.Presentation for Java</p></html>");

        //Save the document
        String result = "output/insertHtmlWithImage.pptx";
        ppt.saveToFile(result, FileFormat.PPTX_2013);
    }
}

Insert HTML with images into PowerPoint in Java

This article demonstrates how to add hyperlinks to SmartArt Nodes in a PowerPoint document in C# and VB.NET using Spire.Presentation for .NET.

C#
using Spire.Presentation;
using Spire.Presentation.Diagrams;

namespace SmartArt
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Presentation instance
            Presentation ppt = new Presentation();
            //Load the PowerPoint document
            ppt.LoadFromFile("SmartArt.pptx");

            //Get the first slide
            ISlide slide = ppt.Slides[0];
           
            //Get the SmartArt
            ISmartArt smartArt = slide.Shapes[0] as ISmartArt;

            //Add hyperlink to the first node of the SmartArt to link to a web page
            smartArt.Nodes[0].Click = new ClickHyperlink("https://www.e-iceblue.com");
            //Add hyperlink to the first node of the SmartArt to link to a specific slide
            smartArt.Nodes[1].Click = new ClickHyperlink(ppt.Slides[1]);

            //Save the result document
            ppt.SaveToFile("Result.pptx", FileFormat.Pptx2013);
        }
    }
}
VB.NET
Imports Spire.Presentation
Imports Spire.Presentation.Diagrams

Namespace SmartArt
    Class Program
        Private Shared Sub Main(ByVal args As String())
            Dim ppt As Presentation = New Presentation()
            ppt.LoadFromFile("SmartArt.pptx")
            Dim slide As ISlide = ppt.Slides(0)
            Dim smartArt As ISmartArt = TryCast(slide.Shapes(0), ISmartArt)
            smartArt.Nodes(0).Click = New ClickHyperlink("https://www.e-iceblue.com")
            smartArt.Nodes(1).Click = New ClickHyperlink(ppt.Slides(1))
            ppt.SaveToFile("Result.pptx", FileFormat.Pptx2013)
        End Sub
    End Class
End Namespace

Output:

Add Hyperlinks to SmartArt Nodes in PowerPoint in C#, VB.NET

We have demonstrated how to use Spire.Doc for Java to add text watermark and image watermark to word document. This article will show you how to add WordArt to the Word header to get the multiple watermarks on the Word document:

Insert multiple text watermarks to Word

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.ShapeLineStyle;
import com.spire.doc.documents.ShapeType;
import com.spire.doc.fields.ShapeObject;
import java.awt.*;

public class WordWatermark {

    public static void main(String[] args) {

        //Load the sample document
        Document doc = new Document();
        doc.loadFromFile("Sample.docx");
        //Add WordArt shape and set the size
        ShapeObject shape = new ShapeObject(doc, ShapeType.Text_Plain_Text);
        shape.setWidth(60);
        shape.setHeight(20);
        //Set the text, position and sytle for the wordart
        shape.setVerticalPosition(30);
        shape.setHorizontalPosition(20);
        shape.setRotation(315);
        shape.getWordArt().setText("Confidential");
        shape.setFillColor(Color.red);
        shape.setLineStyle(ShapeLineStyle.Single);
        shape.setStrokeColor(new Color(192, 192, 192, 255));
        shape.setStrokeWeight(1);

        Section section;
        HeaderFooter header;
        for (int n = 0; n < doc.getSections().getCount(); n++) {
            section = doc.getSections().get(n);
            //Get the header of section
            header = section.getHeadersFooters().getHeader();
            Paragraph paragraph1;
            for (int i = 0; i < 4; i++) {
                //Add the hearder to the paragraph
                paragraph1 = header.addParagraph();
                for (int j = 0; j < 3; j++) {
                    //copy the word are and add it to many places
                    shape = (ShapeObject) shape.deepClone();
                    shape.setVerticalPosition(50 + 150 * i);
                    shape.setHorizontalPosition(20 + 160 * j);
                    paragraph1.getChildObjects().add(shape);
                }
            }
        }
        //Save the document to file
        doc.saveToFile("Result.docx", FileFormat.Docx_2013);

    }
}

Effective screenshot:

Java insert multiple text watermarks to Word document

Tuesday, 29 September 2020 08:40

Java Convert HTML to PDF

This article will demonstrate how to use Spire.Doc for Java to convert HTML string and HTML file to PDF in Java applications.

HTML String to PDF

import com.spire.doc.*;
import java.io.*;

public class htmlStringToWord {

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

        String inputHtml = "InputHtml.txt";
        //Create a new document
        Document document = new Document();
        //Add a section
        Section sec = document.addSection();

        String htmlText = readTextFromFile(inputHtml);
        //add a paragraph and append html string.
        sec.addParagraph().appendHTML(htmlText);

        //Save to PDF
        document.saveToFile("HTMLstringToPDF.pdf", FileFormat.PDF);
    }
    public static String readTextFromFile(String fileName) throws IOException{
        StringBuffer sb = new StringBuffer();
        BufferedReader br = new BufferedReader(new FileReader(fileName));
        String content = null;
        while ((content = br.readLine()) != null) {
            sb.append(content);
        }
        return sb.toString();
    }
}

Java Convert HTML to PDF

Convert HTML file to PDF

import com.spire.doc.*;
import com.spire.doc.documents.XHTMLValidationType;

public class htmlFileToWord {

    public static void main(String[] args) throws Exception {
        // Load the sample HTML file
        Document document = new Document();
        document.loadFromFile("InputHtmlFile.html", FileFormat.Html, XHTMLValidationType.None);

        //Save to file
        document.saveToFile("Result.pdf",FileFormat.PDF);
    }
}

Effective screenshot:

Java Convert HTML to PDF

Page 1 of 127