Wednesday, 11 September 2019 09:38

Group Shapes in PowerPoint in Java

This article demonstrates how to group shapes in a PowerPoint document using Spire.Presentation for Java.

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

import java.awt.geom.Rectangle2D;
import java.util.ArrayList;

public class GroupShapes {
    public static void main(String[] args) throws Exception {
        //create a PowerPoint document
        Presentation ppt = new Presentation();
        //get the first slide
        ISlide slide = ppt.getSlides().get(0);

        //add a rectangle shape to the slide
        IShape rectangle = slide.getShapes().appendShape(ShapeType.RECTANGLE, new Rectangle2D.Double(20,100,200,40));
        rectangle.getFill().setFillType(FillFormatType.SOLID);
        rectangle.getFill().getSolidColor().setKnownColor(KnownColors.GOLD);
        rectangle.getLine().setWidth(0.1f);

        //add a ribbon shape to the slide
        IShape ribbon = slide.getShapes().appendShape(ShapeType.RIBBON_2, new Rectangle2D.Double(60, 75, 120, 80));
        ribbon.getFill().setFillType(FillFormatType.SOLID);
        ribbon.getFill().getSolidColor().setKnownColor(KnownColors.PURPLE);
        ribbon.getLine().setWidth(0.1f);

        //add the shapes to a list
        ArrayList list = new ArrayList();
        list.add((Shape)rectangle);
        list.add((Shape)ribbon);

        //group the shapes
        ppt.getSlides().get(0).groupShapes(list);

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

Group Shapes in PowerPoint in Java

Wednesday, 11 September 2019 03:29

Java Create Barcode to Word Document

This article demonstrates how to add barcode to a word document in Java applications with the help of Spire.Doc for Java. Firstly, please ensure that the barcode font has been install on the system. The installed font could be found from C:\Windows\Fonts.

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

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

        // Create Document
        Document document = new Document();
        //Add a section
        Section section = document.addSection();
        //Add  s paragraph to the section
        Paragraph para = section.addParagraph();

        //Add barcode and set format
        TextRange tr = para.appendText("H63TWX11072");
        tr.getCharacterFormat().setFontName("C39HrP60DlTt");
        tr.getCharacterFormat().setFontSize(80f);

        //Save the document to file
        document.saveToFile("Barcode.docx", FileFormat.Docx);
    }
}

Effective screenshot after add barcode to Word:

Java create Barcode to Word document

Tuesday, 10 September 2019 07:51

Replace Text in PowerPoint in Java

This article demonstrates how to replace text in an exising PowerPoint document with new text using Spire.Presentation for Java.

import com.spire.presentation.*;

import java.util.HashMap;
import java.util.Map;

public class ReplaceText {

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

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

        //load the template file
        presentation.loadFromFile("C:\\Users\\Administrator\\Desktop\\input.pptx");

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

        //create a Map object
        Map map = new HashMap();

        //add several pairs of keys and values to the map
        map.put("#name#","John Smith");
        map.put("#age#","28");
        map.put("#address#","Oklahoma City, United States");
        map.put("#tel#","333 123456");
        map.put("#email#","johnsmith@outlook.com");

        //replace text in the slide
        replaceText(slide,map);

        //save to another file
        presentation.saveToFile("output/ReplaceText.pptx", FileFormat.PPTX_2013);
    }

    /**
     * Replace text within a slide
     * @param slide Specifies the slide where the replacement happens
     * @param map Where keys are existing strings in the document and values are the new strings to replace the old ones
     */
    public static void replaceText(ISlide slide, Map map) {

        for (Object shape : slide.getShapes()
        ) {
            if (shape instanceof IAutoShape) {

                for (Object paragraph : ((IAutoShape) shape).getTextFrame().getParagraphs()
                ) {
                    ParagraphEx paragraphEx = (ParagraphEx)paragraph;
                    for (String key : map.keySet()
                    ) {
                        if (paragraphEx.getText().contains(key)) {

                            paragraphEx.setText(paragraphEx.getText().replace(key, map.get(key)));
                         }
                    }
                }
            }
        }
    }
}

Replace Text in PowerPoint in Java

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