page 203

Spire.XLS provides a class named Workbook that represents an Excel workbook. This class contains a method named IsPasswordProtected(string fileName) which returns a boolean value. If the value is true, means the workbook is encrypted with password, otherwise it's not.

This is an Excel workbook protected with password:

Detect if an Excel Workbook is Password Protected in C#, VB.NET

Now refer below code to detect if the Excel workbook is password protected:

[C#]
using System;
using Spire.Xls;

namespace Detect_if_workbook_is_password_protected
{
    class Program
    {
        static void Main(string[] args)
        {
            string fileName = "E:\\Program Files\\Sample.xlsx";
            //Detect if the Excel workbook is password protected.
            bool value = Workbook.IsPasswordProtected(fileName);
            Console.WriteLine(value);
            Console.ReadKey();
        }
    }
}
[VB.NET]
Imports Spire.Xls

Namespace Detect_if_workbook_is_password_protected
	Class Program
		Private Shared Sub Main(args As String())
			Dim fileName As String = "E:\Program Files\Sample.xlsx"
			'Detect if the Excel workbook is password protected.
			Dim value As Boolean = Workbook.IsPasswordProtected(fileName)
			Console.WriteLine(value)
			Console.ReadKey()
		End Sub
	End Class
End Namespace

After running the project, we get the Output that shows the workbook is password protected:

Detect if an Excel Workbook is Password Protected in C#, VB.NET

There can be many types of titles such as centered title, subtitle and title on PowerPoint slides. This article demonstrates how to get such titles from all the slides of a PowerPoint document by using Spire.Presentation and C#.

This is a PowerPoint document which contains a centered title and a subtitle on the first slide and a title on the second slide.

How to Get the Titles of All Slides with C#

Follow below steps to get the titles:

Step 1: Instantiate a Presentation object and load the PowerPoint document.

Presentation ppt = new Presentation();
ppt.LoadFromFile("test.pptx");

Step 2: Instantiate a list of IShape objects.

List<IShape> shapelist = new List<IShape>();

Step 3: Loop through all sildes in the document and all shapes on each slide, add the shape which placeholder type is title or centered title or subtitle to the list.

foreach (ISlide slide in ppt.Slides)
{
    foreach (IShape shape in slide.Shapes)
    {
        if (shape.Placeholder != null)
        {
            switch (shape.Placeholder.Type)
            {
                case PlaceholderType.Title:
                    shapelist.Add(shape);
                    break;
                case PlaceholderType.CenteredTitle:
                    shapelist.Add(shape);
                    break;
                case PlaceholderType.Subtitle:
                    shapelist.Add(shape);
                    break;
            }
        }
    }
}

Step 4: Loop through the list and print out the inner text of all shapes in the list.

for (int i = 0; i < shapelist.Count; i++)
{
    IAutoShape shape1 = shapelist[i] as IAutoShape;
    Console.WriteLine(shape1.TextFrame.Text);
}

Output:

How to Get the Titles of All Slides with C#

Full code:

using System;
using System.Collections.Generic;
using Spire.Presentation;

namespace Get_the_Titles_of_All_Slides
{
    class Program
    {
        static void Main(string[] args)
        {
            Presentation ppt = new Presentation();
            ppt.LoadFromFile("test.pptx");
            List<IShape> shapelist = new List<IShape>();
            foreach (ISlide slide in ppt.Slides)
            {
                foreach (IShape shape in slide.Shapes)
                {
                    if (shape.Placeholder != null)
                    {
                        switch (shape.Placeholder.Type)
                        {
                            case PlaceholderType.Title:
                                shapelist.Add(shape);
                                break;
                            case PlaceholderType.CenteredTitle:
                                shapelist.Add(shape);
                                break;
                            case PlaceholderType.Subtitle:
                                shapelist.Add(shape);
                                break;
                        }
                    }
                }
            }
            for (int i = 0; i < shapelist.Count; i++)
            {
                IAutoShape shape1 = shapelist[i] as IAutoShape;
                Console.WriteLine(shape1.TextFrame.Text);
            }
            Console.ReadKey();
        }
    }
}

Every time when we change the slide's size, we need to reset the size and position for the shape to ensure the shape on the slide shows orderly. This tutorial will focus on demonstrating how to reset the size and position for the shape in C#.

Firstly, please view the original shape:

Reset the size and position for the shape on presentation slides in C#

Code snippet of how to set the size and position of the shape:

Step 1: Create a new presentation document and load the document from file.

Presentation presentation = new Presentation();
presentation.LoadFromFile("Sample.pptx");

Step 2: Define the original slide size.

float currentHeight = presentation.SlideSize.Size.Height;
float currentWidth = presentation.SlideSize.Size.Width;

Step 3: Change the slide size as A3.

presentation.SlideSize.Type = SlideSizeType.A3;

Step 4: Define the new slide size.

float newHeight = presentation.SlideSize.Size.Height;
float newWidth = presentation.SlideSize.Size.Width;

Step 5: Define the ratio from the old and new slide size.

float ratioHeight = newHeight / currentHeight;
float ratioWidth = newWidth / currentWidth;

Step 6: Reset the size and position of the shape on the slide.

foreach (ISlide slide in presentation.Slides)
{
    foreach (IShape shape in slide.Shapes)
    {
        //Reset the shape size
        shape.Height = shape.Height * ratioHeight;
        shape.Width = shape.Width * ratioWidth;

        //Reset the shape position
        shape.Left = shape.Left * ratioHeight;
        shape.Top = shape.Top * ratioWidth;

    }

Step 7: Save the document to file.

presentation.SaveToFile("resize.pptx", FileFormat.Pptx2010);

Effective screenshot after reset the size and position of the shape on the slide:

Reset the size and position for the shape on presentation slides in C#

Full codes:

using Spire.Presentation;

namespace ResetSizeandPosition
{

    class Program
    {

        static void Main(string[] args)
        {
            {
                Presentation presentation = new Presentation();
                presentation.LoadFromFile("Sample.pptx");

                float currentHeight = presentation.SlideSize.Size.Height;
                float currentWidth = presentation.SlideSize.Size.Width;

                presentation.SlideSize.Type = SlideSizeType.A3;

                float newHeight = presentation.SlideSize.Size.Height;
                float newWidth = presentation.SlideSize.Size.Width;

                float ratioHeight = newHeight / currentHeight;
                float ratioWidth = newWidth / currentWidth;

                foreach (ISlide slide in presentation.Slides)
                {
                    foreach (IShape shape in slide.Shapes)
                    {
                        shape.Height = shape.Height * ratioHeight;
                        shape.Width = shape.Width * ratioWidth;

                        shape.Left = shape.Left * ratioHeight;
                        shape.Top = shape.Top * ratioWidth;
                    }

                    presentation.SaveToFile("resize.pptx", FileFormat.Pptx2010);
                }
            }

        }
    }
}
page 203