Wednesday, 04 September 2019 07:38

Set 3D effect for the text in PowerPoint in C#

With the help of Spire.Presentation for .NET, we could set 3D effect to the shapes and text in the PowerPoint document to make it attractive. We have already demonstrated how to use Spire.Presentation to set 3D format for shapes. In this article, I will introduce how to create three dimensional effects text in PowerPoint in C#.

using Spire.Presentation;
using Spire.Presentation.Drawing;
using System.Drawing;

namespace 3DEffectForText
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a new presentation object
            Presentation presentation = new Presentation();

            //Get the first slide
            ISlide slide = presentation.Slides[0];

            //Append a new shape to slide and set the line color and fill type
            IAutoShape shape = slide.Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(30, 40, 600, 200));
            shape.ShapeStyle.LineColor.Color = Color.White;
            shape.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.None;

            //Add text to the shape
            shape.AppendTextFrame("This demo shows how to add 3D effect text to Presentation slide");

            //set the color of text in shape
            TextRange textRange = shape.TextFrame.TextRange;
            textRange.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.Solid;
            textRange.Fill.SolidColor.Color = Color.Green;

            //set the Font of text in shape
            textRange.FontHeight = 30;
            textRange.LatinFont = new TextFont("Gulim");


            //Set 3D effect for text
            shape.TextFrame.TextThreeD.ShapeThreeD.PresetMaterial = PresetMaterialType.Matte;
            shape.TextFrame.TextThreeD.LightRig.PresetType = PresetLightRigType.Sunrise;
            shape.TextFrame.TextThreeD.ShapeThreeD.TopBevel.PresetType = BevelPresetType.Circle;
            shape.TextFrame.TextThreeD.ShapeThreeD.ContourColor.Color = Color.Green;
            shape.TextFrame.TextThreeD.ShapeThreeD.ContourWidth = 3;                    
          
            //Save the document to file.
            presentation.SaveToFile("3DEffectForText_result.pptx", FileFormat.Pptx2010);                                                        
        }
    }
}

Effective screenshot for 3-D effect text on Presentation slides:

Set 3D effect for the text in PowerPoint in C#

Alternative text (Alt Text) can help people with vision or cognitive impairments understand shapes, pictures or other graphical content. This article demonstrates how to set and get the alternative text of a shape in a PowerPoint document using Spire.Presentation for Java.

Set alternative text

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

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

public class SetAltText {
    public static void main(String[] args) throws Exception {
        //instantiate a Presentation object
        Presentation ppt = new Presentation();throws Exception 

        //add a shape to the first slide
        IAutoShape shape = ppt.getSlides().get(0).getShapes().appendShape(ShapeType.TRIANGLE, new Rectangle2D.Double(115, 130, 100, 100));
        shape.getFill().setFillType(FillFormatType.SOLID);
        shape.getFill().getSolidColor().setColor(Color.orange);
        shape.getShapeStyle().getLineColor().setColor(Color.white);

        //set alt text (title and description) for the shape
        shape.setAlternativeTitle("Triangle");
        shape.setAlternativeText("This is a triangle.");

        //save the resultant document
        ppt.saveToFile("Output.pptx", FileFormat.PPTX_2013);
    }
}

Set and Get Alternative Text (Alt Text) of PowerPoint Shapes in Java

Get alternative text

import com.spire.presentation.*;

public class GetAltText {
    public static void main(String[] args) throws Exception {
        //load PowerPoint document
        Presentation ppt = new Presentation();
        ppt.loadFromFile("Output.pptx");

        //get the first shape in the first slide
        IShape shape = ppt.getSlides().get(0).getShapes().get(0);

        //get the alt text (title and description) of the shape
        String altTitle = shape.getAlternativeTitle();
        String altDescription = shape.getAlternativeText();

        System.out.println("Title: " + altTitle);
        System.out.println("Description: " + altDescription);
    }
}

Set and Get Alternative Text (Alt Text) of PowerPoint Shapes in Java

Tuesday, 03 September 2019 06:59

Replace an Image in PowerPoint in Java

This article demonstrates how to replace an existing image in a PowerPoint document with a new image by using Spire.Presentation for Java.

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

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;

public class ReplaceImage {

    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\\input.pptx");

        //add an image to the image collection
        String imagePath = "C:\\Users\\Administrator\\Desktop\\Microsoft-PowerPoint-logo.jpg";
        BufferedImage bufferedImage = ImageIO.read(new FileInputStream(imagePath));
        IImageData image = presentation.getImages().append(bufferedImage);

        //get the shape collection from the first slide
        ShapeCollection shapes = presentation.getSlides().get(0).getShapes();

        //loop through the shape collection
        for (int i = 0; i < shapes.getCount(); i++) {

            //determine if a shape is a picture
            if (shapes.get(i) instanceof SlidePicture) {

                //fill the shape with a new image
               ((SlidePicture) shapes.get(i)).getPictureFill().getPicture().setEmbedImage(image);
            }
        }
        
        //save to file
        presentation.saveToFile("output/ReplaceImage.pptx", FileFormat.PPTX_2013);
    }
}

Replace an Image in PowerPoint in Java

Friday, 30 August 2019 08:38

Add and Delete Layers in PDF in Java

Spire.PDF for Java supports adding many types of layers such as line, image, string, rectangle to PDF document as well as deleting layers from PDF document. In this article, we'll show you how to add and delete Layers in a PDF document using Spire.PDF for Java.

Add layers

import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.graphics.PdfBrushes;
import com.spire.pdf.graphics.PdfCanvas;
import com.spire.pdf.graphics.PdfPen;
import com.spire.pdf.graphics.layer.PdfLayer;

import java.awt.geom.Point2D;

public class AddLayers {
    public static void main(String[] args){
        //instantiate a PdfDocument object
        PdfDocument pdf = new PdfDocument();
        //add a page
        PdfPageBase page = pdf.getPages().add();
        
        //add 3 layers to the page
        PdfLayer layer = pdf.getLayers().addLayer("red line1");
        PdfCanvas canvas1 = layer.createGraphics(pdf.getPages().get(0).getCanvas());
        canvas1.drawLine(new PdfPen(PdfBrushes.getRed(), 1), new Point2D.Float(50, 350), new Point2D.Float(200, 350));
        layer = pdf.getLayers().addLayer("blue line1");
        PdfCanvas canvas2 = layer.createGraphics(pdf.getPages().get(0).getCanvas());
        canvas2.drawLine(new PdfPen(PdfBrushes.getBlue(), 1), new Point2D.Float(50, 450), new Point2D.Float(200, 450));
        layer = pdf.getLayers().addLayer("green line1");
        PdfCanvas canvas3 = layer.createGraphics(pdf.getPages().get(0).getCanvas());
        canvas3.drawLine(new PdfPen(PdfBrushes.getGreen(), 1), new Point2D.Float(50,550), new Point2D.Float(200, 550));

        //save the resultant document
        pdf.saveToFile("output/addLayers.pdf");
        pdf.close();
    }
}

Add and Delete Layers in PDF in Java

Delete layers

import com.spire.pdf.PdfDocument;

public class DeleteLayers {
    public static void main(String[] args){
        //load a PDF document
        PdfDocument pdf = new PdfDocument();
        pdf.loadFromFile("output/addLayers.pdf");

        //remove a layer by name
        pdf.getLayers().removeLayer("red line1");

        //save the resultant document
        pdf.saveToFile("output/deleteLayer.pdf");
        pdf.close();
    }
}

Add and Delete Layers in PDF in Java

This article demonstrates how to add a hyperlink to an image in a PowerPint slide using Spire.Presentation for Java.

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

import java.awt.geom.Rectangle2D;

public class AddHyperlinkToImage {

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

        //create a Presentation object
        Presentation presentation = new Presentation();

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

        //add an image to slide
        String imaPath = "C:\\Users\\Administrator\\Desktop\\logo.png";
        Rectangle2D.Float rect = new Rectangle2D.Float(50, 50, 220, 60);
        IEmbedImage image = slide.getShapes().appendEmbedImage(ShapeType.RECTANGLE, imaPath, rect);

        //set the line of image shape to none
        image.getLine().setFillType(FillFormatType.NONE);

        //add a hyperlink to image
        ClickHyperlink hyperlink = new ClickHyperlink("https://www.e-iceblue.com");
        image.setClick(hyperlink);

        //save the file
        presentation.saveToFile("output/ImageHyperLink.pptx", FileFormat.PPTX_2013);
    }
}

Add Hyperlinks to Images in PowerPoint in Java

Tuesday, 27 August 2019 07:22

Add arrow line to Excel worksheet in C#

With Spire.XLS for .NET, developers can easily use C# to add shapes to Excel worksheet. From version 9.8.11, Spire.XLS supports to add arrow lines to Excel worksheet. The following sample will show you how to insert arrow line, double Arrow, Elbow Arrow, Elbow Double-Arrow, Curved Arrow and Curved Double-Arrow to Excel worksheet in C#.

using Spire.Xls;
using System.Drawing;

namespace Add_Lines_to_Excel
{
    class Program
    {
        static void Main(string[] args)
        {
            //Initiate a Workbook object and get the first worksheet
            Workbook workbook = new Workbook();
            Worksheet sheet = workbook.Worksheets[0];

            //Add a Double Arrow and fill the line with solid color
            var line = sheet.TypedLines.AddLine();
            line.Top = 10;
            line.Left = 20;
            line.Width = 100;
            line.Height = 0;
            line.Color = Color.Blue;
            line.BeginArrowHeadStyle = ShapeArrowStyleType.LineArrow;
            line.EndArrowHeadStyle = ShapeArrowStyleType.LineArrow;
            
            //Add an Arrow and fill the line with solid color
            var line_1 = sheet.TypedLines.AddLine();
            line_1.Top = 50;
            line_1.Left = 30;
            line_1.Width = 100;
            line_1.Height = 100;
            line_1.Color = Color.Red;
            line_1.BeginArrowHeadStyle = ShapeArrowStyleType.LineNoArrow;
            line_1.EndArrowHeadStyle = ShapeArrowStyleType.LineArrow;

            //Add an Elbow Arrow Connector
            Spire.Xls.Core.Spreadsheet.Shapes.XlsLineShape line3 = sheet.TypedLines.AddLine() as Spire.Xls.Core.Spreadsheet.Shapes.XlsLineShape;
            line3.LineShapeType = LineShapeType.ElbowLine;
            line3.Width = 30;
            line3.Height = 50;
            line3.EndArrowHeadStyle = ShapeArrowStyleType.LineArrow;
            line3.Top = 100;
            line3.Left = 50;
            
            //Add an Elbow Double-Arrow Connector
            Spire.Xls.Core.Spreadsheet.Shapes.XlsLineShape line2 = sheet.TypedLines.AddLine() as Spire.Xls.Core.Spreadsheet.Shapes.XlsLineShape;
            line2.LineShapeType = LineShapeType.ElbowLine;
            line2.Width = 50;
            line2.Height = 50;
            line2.EndArrowHeadStyle = ShapeArrowStyleType.LineArrow;
            line2.BeginArrowHeadStyle = ShapeArrowStyleType.LineArrow;
            line2.Left = 120;
            line2.Top = 100;

            //Add a Curved Arrow Connector 
            line3 = sheet.TypedLines.AddLine() as Spire.Xls.Core.Spreadsheet.Shapes.XlsLineShape;
            line3.LineShapeType = LineShapeType.CurveLine;
            line3.Width = 30;
            line3.Height = 50;
            line3.EndArrowHeadStyle = ShapeArrowStyleType.LineArrowOpen;
            line3.Top = 100;
            line3.Left = 200;

            //Add a Curved Double-Arrow Connector
            line2 = sheet.TypedLines.AddLine() as Spire.Xls.Core.Spreadsheet.Shapes.XlsLineShape;
            line2.LineShapeType = LineShapeType.CurveLine;
            line2.Width = 30;
            line2.Height = 50;
            line2.EndArrowHeadStyle = ShapeArrowStyleType.LineArrowOpen;
            line2.BeginArrowHeadStyle = ShapeArrowStyleType.LineArrowOpen;
            line2.Left = 250;
            line2.Top = 100;

            //Save the file
            workbook.SaveToFile("AddLines.xlsx", ExcelVersion.Version2013);

        }
    }
}

Add arrow line to Excel worksheet in C#

Tuesday, 27 August 2019 06:07

Insert HTML String in PowerPoint in Java

This article demonstrates how to render text with simple HTML tags to formatted text in a presentation slide by using Spire.Presentation for Java.

import com.spire.presentation.FileFormat;
import com.spire.presentation.IAutoShape;
import com.spire.presentation.Presentation;
import com.spire.presentation.ShapeType;
import com.spire.presentation.drawing.FillFormatType;

import java.awt.geom.Rectangle2D;

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

        //create a Presentation object
        Presentation ppt = new Presentation();
        
        //add a shape to the first slide
        IAutoShape shape = ppt.getSlides().get(0).getShapes().appendShape(ShapeType.RECTANGLE, new Rectangle2D.Float(50, 50, 400, 100));
        shape.getFill().setFillType(FillFormatType.NONE);

        //clear the default paragraph
        shape.getTextFrame().getParagraphs().clear();

        //define html string
        String htmlString = "<ul>" +
                            "<li style=\"color:blue\">Spire.Presentation for Java</li>" +
                            "<li style=\"color:green\">Spire.PDF for Java</li>" +
                            "<li style=\"color:gray\">Spire.Doc for Java</li>" +
                            "<li style=\"color:red\">Spire.Barcode for Java</li>" +
                            "</ul>";

        //insert html string in the shape
        shape.getTextFrame().getParagraphs().addFromHtml(htmlString);

        //save to file
        ppt.saveToFile("output/InsertHtml.pptx", FileFormat.PPTX_2013);
    }
}

Insert HTML String in PowerPoint in Java

Thursday, 22 August 2019 09:54

Create Table of Contents in Word in Java

This article demonstrates how to create table of contents in a Word document using Spire.Doc for Java.

The following example shows how to create a table of contents with default appearance that includes all text formatted with built-in styles Heading 1, Heading 2, Heading 3, and page numbers right-aligned with tab leaders.

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

import java.awt.*;

public class TableofContents {
    public static void main(String[] args){
        //instantiate a Document object
        Document doc = new Document();
        //add a section
        Section section = doc.addSection();

        //add a paragraph
        Paragraph para = section.addParagraph();
        TextRange tr = para.appendText("Table of Contents");
        //set font size and text color
        tr.getCharacterFormat().setFontSize(11);
        tr.getCharacterFormat().setTextColor(Color.blue);       
        //set the space after the paragraph
        para.getFormat().setAfterSpacing(10);

        //add a paragraph
        para = section.addParagraph();
        //add a table of contents with default appearance by specifying lower heading level and upper heading level. The heading level range must be from 1 to 9.
        para.appendTOC(1, 3);

        //add a new section
        section = doc.addSection();
        //add a paragraph
        para = section.addParagraph();
        para.appendText("Heading 1");
        //apply Heading 1 style to the paragraph
        para.applyStyle(BuiltinStyle.Heading_1);
        section.addParagraph();

        //add a paragraph
        para = section.addParagraph();
        para.appendText("Heading 2");
        //apply Heading 2 style to the paragraph
        para.applyStyle(BuiltinStyle.Heading_2);
        section.addParagraph();

        //add a paragraph
        para = section.addParagraph();
        para.appendText("Heading 3");
        //apply Heading 3 style to the paragraph
        para.applyStyle(BuiltinStyle.Heading_3);
        section.addParagraph();

        //update Table of Contents
        doc.updateTableOfContents();

        //save the resultant document
        doc.saveToFile("createTableOfContents.docx", FileFormat.Docx);
    }
}

Output:

Create Table of Contents in Word in Java

We can also create a custom table of contents and determine what entries to appear in the table of contents by using TOC switches. The following example shows how to create a custom table of contents that includes all text formatted with built-in styles Heading 1, Heading 2 and Heading 3 but omits page numbers from heading levels 1-3.

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

import java.awt.*;

public class TableofContents {
    public static void main(String[] args){
        //instantiate a Document object
        Document doc = new Document();
        //add a section
        Section section = doc.addSection();

        //add a paragraph
        Paragraph para = section.addParagraph();
        TextRange tr = para.appendText("Table of Contents");
        //set font size and text color
        tr.getCharacterFormat().setFontSize(11);
        tr.getCharacterFormat().setTextColor(Color.blue);
        //set the space after the paragraph
        para.getFormat().setAfterSpacing(10);

        //create a custom table of contents that omits page numbers from heading levels 1-3.
        TableOfContent toc = new TableOfContent(doc, "{\\o \"1-3\" \\n 1-3}");
        para = section.addParagraph();
        para.getItems().add(toc);
        para.appendFieldMark(FieldMarkType.Field_Separator);
        para.appendText("TOC");
        para.appendFieldMark(FieldMarkType.Field_End);
        doc.setTOC(toc);

        //add a new section
        section = doc.addSection();
        //add a paragraph
        para = section.addParagraph();
        para.appendText("Heading 1");
        //apply Heading 1 style to the paragraph
        para.applyStyle(BuiltinStyle.Heading_1);
        section.addParagraph();

        //add a paragraph
        para = section.addParagraph();
        para.appendText("Heading 2");
        //apply Heading 2 style to the paragraph
        para.applyStyle(BuiltinStyle.Heading_2);
        section.addParagraph();

        //add a paragraph
        para = section.addParagraph();
        para.appendText("Heading 3");
        //apply Heading 3 style to the paragraph
        para.applyStyle(BuiltinStyle.Heading_3);
        section.addParagraph();

        //update Table of Contents
        doc.updateTableOfContents();

        //save the resultant document
        doc.saveToFile("customTableOfContents.docx", FileFormat.Docx);

    }
}

Output:

Create Table of Contents in Word in Java

Wednesday, 21 August 2019 09:33

Java print a PowerPoint document

Spire.Presentation for Java supports to print the presentation slides in Java applications. This article will show you how to print PowerPoint documents from the following aspects:

  • Print all presentation slides with default printer
  • Select some slides from the PowerPoint document to print

Print PowerPoint document to a default printer and print all the presentation slides.

import com.spire.presentation.Presentation;
import com.spire.presentation.PresentationPrintDocument;

public class PrintPPT {
    public static void main(String[] args) throws Exception {
    String inputFile = "Sample.pptx";

    //Create a ppt document and load file
    Presentation presentation = new Presentation();
    presentation.loadFromFile(inputFile);

    //Print all the presentation slides with default printer
    PresentationPrintDocument document = new PresentationPrintDocument(presentation);
    document.print();
    presentation.dispose();

    }
}

Select some discontinuous slides from the PowerPoint document to print

import com.spire.presentation.Presentation;
import com.spire.presentation.PresentationPrintDocument;

public class PrintPPT {
    public static void main(String[] args) throws Exception {
        String inputFile = "Sample.pptx";

        //Create a ppt document and load file
        Presentation presentation = new Presentation();
        presentation.loadFromFile(inputFile);
        
        PresentationPrintDocument document = new PresentationPrintDocument(presentation);
        //Select the slides to print
        document.selectSlidesForPrint("1", "2-6");
        document.print();
        presentation.dispose();

        }
    }
Friday, 16 August 2019 01:05

Create Pivot Chart in Excel in C#

Starting from version 9.8.5, Spire.XLS supports creating pivot chart based on pivot table. This article is going to demonstrate how we can use Spire.XLS to implement this feature.

The input.xlsx Excel file:

Create Pivot Chart in Excel in C#

Sample Code

using Spire.Xls;
using Spire.Xls.Core;

namespace CreatePivotChart
{
    class Program
    {
        static void Main(string[] args)
        {
            //load the Excel file
            Workbook workbook = new Workbook();
            workbook.LoadFromFile("Input.xlsx");

            //get the first worksheet
            Worksheet sheet = workbook.Worksheets[0];
            //get the first pivot table in the worksheet
            IPivotTable pivotTable = sheet.PivotTables[0];

            //create a clustered column chart based on the pivot table
            Chart chart = sheet.Charts.Add(ExcelChartType.ColumnClustered, pivotTable);
            //set chart position
            chart.TopRow = 19;
            chart.BottomRow = 38;
            //set chart title
            chart.ChartTitle = "Pivot Chart";

            //save the resultant file 
            workbook.SaveToFile("CreatPivotChart.xlsx", ExcelVersion.Version2013);
        }
    }
}

Screenshot of the created pivot chart:

Create Pivot Chart in Excel in C#