page 140

Replace Text with Image in Word in Java

2019-11-20 07:45:55 Written by Koohji

This article demonstrates how to replace selected text in a Word document with an image using Spire.Doc for Java.

import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.documents.TextSelection;
import com.spire.doc.fields.DocPicture;
import com.spire.doc.fields.TextRange;

public class ReplaceTextWithImage {

    public static void main(String[] args) {

        //Load a sample Word file
        Document document = new Document();
        document.loadFromFile("C:\\Users\\Administrator\\Desktop\\input.docx");

        //Find the string 'E-iceblue' in the document
        TextSelection[] selections = document.findAllString("E-iceblue", true, true);

        //Replace the string with an image
        int index = 0;
        TextRange range = null;
        for (Object obj : selections) {

            TextSelection textSelection = (TextSelection)obj;
            DocPicture pic = new DocPicture(document);
            pic.loadImage("C:\\Users\\Administrator\\Desktop\\e-iceblue-logo.png");
            range = textSelection.getAsOneRange();
            index = range.getOwnerParagraph().getChildObjects().indexOf(range);
            range.getOwnerParagraph().getChildObjects().insert(index,pic);
            range.getOwnerParagraph().getChildObjects().remove(range);
        }

        //Save the document
        document.saveToFile("output/ReplaceTextWithImage.docx", FileFormat.Docx_2013);
    }
}

Replace Text with Image in Word in Java

A trendline is a line superimposed on a chart revealing the overall direction of the data. Spire.Presentation for Java supports adding six different types of trendlines to chart, i.e. linear, logarithmic, polynomial, power, exponential and moving average.

The below example demonstrates how to use Spire.Presentation for Java to add a linear trendline to a chart.

import com.spire.presentation.FileFormat;
import com.spire.presentation.ISlide;
import com.spire.presentation.Presentation;
import com.spire.presentation.charts.IChart;
import com.spire.presentation.charts.ITrendlines;
import com.spire.presentation.charts.TrendlineSimpleType;

public class AddTrendlineToChart {
    public static void main(String[] args) throws Exception {
        //create a Presentation instance
        Presentation ppt = new Presentation();
        //load the PowerPoint document
        ppt.loadFromFile("Chart.pptx");

        //get the first slide
        ISlide slide = ppt.getSlides().get(0);
        //get the chart on the slide
        IChart chart = (IChart)slide.getShapes().get(0);

        //add a linear trendline to the first series of the chart
        ITrendlines trendLine = chart.getSeries().get(0).addTrendLine(TrendlineSimpleType.LINEAR);
        //display equation
        trendLine.setdisplayEquation(true);

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

Output:

Add Trendline to Chart in PowerPoint in Java

Mark as Final means that the presentation slide is final edition and the author doesn’t want any changes on the document. With Spire.Presentation for Java, we can protect the presentation slides by setting the password. This article demonstrates how to mark a presentation as final by setting the document property MarkAsFinal as true.

import com.spire.presentation.FileFormat;
import com.spire.presentation.Presentation;

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

        //Create a PPT document and load file
        Presentation presentation = new Presentation();
        presentation.loadFromFile("Sample.pptx");
       
        //Set the document property MarkAsFinal as true
        presentation.getDocumentProperty().set("_MarkAsFinal", true);

        //Save the document to file
        presentation.saveToFile("output/MarkasFinal.pptx", FileFormat.PPTX_2010);
    }
}

Effective screenshot after mark as final for presentation:

Java protect presentation slides by setting the property with mark as final

page 140

Coupon Code Copied!

Christmas Sale

Celebrate the season with exclusive savings

Save 10% Sitewide

Use Code:

View Campaign Details