page 106

C#/VB.NET: Replace a Picture in Excel

2021-11-05 01:15:01 Written by Koohji

Sometimes after you have finished an Excel workbook, you may need to replace some of the existing pictures with better ones for the purpose of making the workbook more appealing and persuasive. In this tutorial, you will learn how to replace a picture in Excel using Spire.XLS for .NET.

Install Spire.XLS for .NET

To begin with, you need to add the DLL files included in the Spire.XLS for .NET package as references in your .NET project. The DLLs files can be either downloaded from this link or installed via NuGet.

  • Package Manager
PM> Install-Package Spire.XLS

Replace a Picture in Excel

The following are the detailed steps to replace a picture with another one using Spire.XLS for .NET.

  • C#
  • VB.NET
using Spire.Xls;
using Spire.Xls.Collections;
using System.Drawing;

namespace ReplacePictureinExcel
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Workbook instance 
            Workbook workbook = new Workbook();
            //Load the Excel file
            workbook.LoadFromFile (“Input.xls”);

            //Get the first sheet
            Worksheet sheet = workbook.Worksheets[0];

            //Get Excel picture collection
            PicturesCollection pictureCollection = sheet.Pictures;

            //Get the first picture from the collection 
            ExcelPicture excelPicture = pictureCollection[0];
            
            // Creates an Image from the specified file.
            excelPicture.Picture = Image.FromFile (image);

            //Save the document
            workbook.SaveToFile("ReplaceImage.xlsx", ExcelVersion.Version2013);
        }
    } 
}
Imports Spire.Xls
Imports Spire.Xls.Collections
Imports System.Drawing

Namespace ReplacePictureinExcel
	Class Program
		Private Shared Sub Main(args As String())

			'Create a Workbook instance 
			Dim workbook As New Workbook()
			'Load the Excel file
			workbook.LoadFromFile(Input.xls)

			'Get the first sheet
			Dim sheet As Worksheet = workbook.Worksheets(0)

			'Get Excel picture collection
			Dim pictureCollection As PicturesCollection = sheet.Pictures

			'Get the first picture from the collection 
			Dim excelPicture As ExcelPicture = pictureCollection(0)

			' Creates an Image from the specified file.
			excelPicture.Picture = Image.FromFile(image)

			'Save the document
			workbook.SaveToFile("ReplaceImage.xlsx", ExcelVersion.Version2013)
		End Sub
	End Class
End Namespace

The original file:

C#/VB.NET: Replace a Picture in Excel

The generated file:

C#/VB.NET: Replace a Picture in Excel

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

PowerPoint documents signed with digital signatures can help recipients check if they have been altered since they were signed. If any changes are made, the signatures will become invalid immediately. Therefore, before you edit a PowerPoint document, you should check if it has been digitally signed or not. In this article, you will learn how to achieve this task programmatically in Java using Spire.Presentation for Java.

Install Spire.Presentation for Java

First of all, you're required to add the Spire.Presentation.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.presentation</artifactId>
        <version>11.1.1</version>
    </dependency>
</dependencies>

Verify if a PowerPoint Document is Digitally Signed

Spire.Presentation for Java provides the Presentation.isDigitallySigned() method to detect if a PowerPoint document is digitally signed or not. If the method returns true, then it means the document is digitally signed.

The following are the detailed steps to implement this function:

  • Create a Presentation instance.
  • Load a PowerPoint document using Presentation.loadFromFile() method.
  • Detect if the document is digitally signed or not using Presentation.isDigitallySigned() method.
  • Java
import com.spire.presentation.Presentation;

public class VerifyIfPPTisDigitallySigned {
    public static void main(String []args) throws Exception {
        //Create a Presentation instance
        Presentation ppt = new Presentation();
        //Load a PowerPoint document
        ppt.loadFromFile("Sample.pptx");

        //Verify if the document is digitally signed or not
        if (ppt.isDigitallySigned()) {
            System.out.println("This document is digitally signed");
        } else {
            System.out.println("This document is not digitally signed");
        }
    }
}

Java: Verify if a PowerPoint Document is Digitally Signed

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

When an image appears over the text, the text will be covered by the image and will be unable to be displayed. At this point, you can set the transparency of the picture in order to show the text above the image. This article is to demonstrate how to insert an image into a specific slide position in PowerPoint and set its transparency using Spire.Presentation for Java.

Install Spire.Presentation for Java

First of all, you're required to add the Spire.Presentation.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

  • Package Manager
<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.presentation</artifactId>
        <version>11.1.1</version>
    </dependency>
</dependencies>

Set Transparency for Images in PowerPoint

A number of classes and methods are involved in this code example, so a table shown as below is provided to make it easier for you to learn about them.

Name Description
IAutoShape Interface Represents a shape.
ShapeList Class Represents a collection of shapes.
PictureFillFormat Class Represents a picture fill style.
ShapeList.appendShape(ShapeType.shapeType, Rectangle2D.Double) Method Adds a new shape to list.
IAutoShape.getLine().setFillType(FillFormatType.value)  Method Sets the fill format type of the line.
IAutoShape.getFill().setFillType(FillFormatType.value) Method Sets the fill format type of a shape.
IAutoShape.getFill().getPictureFill() Method Gets the picture fill format.
PictureFillFormat.setFillType(PictureFillType.value) Method Sets the picture fill mode.
PictureFillFormat.getPicture().setUrl(java.lang.String value) Method Sets the image's URL for a picture fill.
PictureFillFormat.getPicture().setTransparency() Method Sets the transparency of a picture fill.

The following are some steps to set transparency for images in PowerPoint:

  • Create a Presentation instance and load a PowerPoint sample document using Presentation.loadFromFile() method.
  • Get a specified slide using Presentation.getSlides().get() method, and insert a shape to the specified position of the slide using ShapeList.appendShape(ShapeType.shapeType, Rectangle2D.Double) method.
  • Fill the shape with an image and set the fill format type using IAutoShape.getFill().setFillType(FillFormatType.value) method.
  • Get the picture fill format using IAutoShape.getFill().getPictureFill() method.
  • Set the picture fill mode using PictureFillFormat.setFillType(PictureFillType.value) method, set the image’s URL using PictureFillFormat.getPicture().setUrl(java.lang.String value) method, and set the transparency of the image using PictureFillFormat.getPicture().setTransparency() method.
  • Save the document using Presentation.saveToFile() method.
  • Java
import com.spire.presentation.*;
import com.spire.presentation.drawing.*;
import java.awt.geom.Rectangle2D;

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

        //Load a PowerPoint sample document
        presentation.loadFromFile("C:\\Users\\Test1\\Desktop\\sample.pptx");

        //Insert a shape to the specified position of the first slide
        Rectangle2D.Double rect1 = new   Rectangle2D.Double(50, 130, 275, 150);
        IAutoShape shape =  presentation.getSlides().get(1).getShapes().appendShape(ShapeType.RECTANGLE, rect1);

        //Fill the shape with an image
        shape.getLine().setFillType(FillFormatType.NONE);//Sets the fill format type
        shape.getFill().setFillType(FillFormatType.PICTURE);//Sets the type of filling
        shape.getFill().getPictureFill().getPicture().setUrl("https://cdn.e-iceblue.com/C:\\Users\\Test1\\Desktop\\Picture.png");//Sets the linked image's URL
        shape.getFill().getPictureFill().setFillType(PictureFillType.STRETCH);//Sets the picture fill mode

        //Set transparency of the image
        shape.getFill().getPictureFill().getPicture().setTransparency(50);

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

Java: Set Transparency for Images in PowerPoint

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

page 106