page 256

When sending a PowerPoint document containing a lot of media files and images to others for text proofreading, you may find that the transfer speed is quite slow because of the large file size. In such a case, it is better to extract the text from PowerPoint to MS Word or Notepad first, and then send only the text content. In addition, the extracted text content can also be archived or backed up for future reference. In this article, you will learn how to extract text from a PowerPoint Presentation in C# and VB.NET using Spire.Presentation for .NET.

Install Spire.Presentation for .NET

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

PM> Install-Package Spire.Presentation

Extract Text from PowerPoint Presentations in C# and VB.NET

To facilitate the sharing or delivery of text information in a PowerPoint document, text extraction is an operation occasionally required. The following are the steps to extract text from all presentation slides and save in a TXT file.

  • Initialize an instance of the Presentation class.
  • Load a sample PowerPoint document using Presentation.LoadFromFile() method.
  • Create a StringBuilder instance.
  • Iterate through each slide in the document, and then iterate through all the shapes in each slide.
  • Determine whether the shapes are of IAutoShape type. If yes, iterate through all the paragraphs in each shape and get the paragraph text using TextParagraph.Text property.
  • Append the extracted text to the StringBuilder instance using StringBuilder.AppendLine() method
  • Create a new txt file and write the extracted text to the file using File.WriteAllText() method.
  • C#
  • VB.NET
using Spire.Presentation;
using System.IO;
using System.Text;
namespace ExtractText
{
    class Program
    {
        static void Main(string[] args)
        {
            //Initialize an instance of the Presentation class
            Presentation presentation = new Presentation();

            //Load a sample PowerPoint document
            presentation.LoadFromFile("Island.pptx");
            //Create a StringBuilder instance
            StringBuilder sb = new StringBuilder();

            //Iterate through each slide in the document
            foreach (ISlide slide in presentation.Slides)
            {
                //Iterate through each shape in each slide
                foreach (IShape shape in slide.Shapes)
                {
                    //Check if the shape is of IAutoShape type
                    if (shape is IAutoShape)
                    {
                        //Iterate through all paragraphs in each shape
                        foreach (TextParagraph tp in (shape as IAutoShape).TextFrame.Paragraphs)
                        {
                            //Extract text and save to StringBuilder instance
                            sb.AppendLine(tp.Text);
                        }
                    }
                }
            }
            //Create a new txt file to save the extracted text
            File.WriteAllText("ExtractText.txt", sb.ToString());
        }
    }
}

C#/VB.NET: Extract Text from PowerPoint Presentations

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.

Hyperlinks can easily guide the readers to the place you want to point to and it displays large amount of information to readers. Spire.Presentation for .NET enables developers to insert hyperlinks in PowerPoint presentations. Developers can also modify the existing hyperlinks and replace with new link text or target URL. This section will show how to edit hyperlinks from presentation slides in C#. Firstly check the screenshot of the original hyperlink:

Modify hyperlinks in PowerPoint documents

Spire.Presentation offers a ClickAction class for users to edit the hyperlinks. You can call the function ClickHyperlink to edit the hyperlinks. Here comes to the steps.

Step 1: Load a PowerPoint documents with hyperlinks.

Presentation pre = new Presentation();
pre.LoadFromFile(@"..\..\sample.pptx");

Step 2: Find the hyperlinks you want to edit.

IAutoShape shape = (IAutoShape)pre.Slides[0].Shapes[2];

Step 3: Edit the link text and the target URL.

shape.TextFrame.TextRange.ClickAction.Address = "http://www.e-iceblue.com";
shape.TextFrame.TextRange.Text = "E-iceblue";

Step 4: Save the document.

pre.SaveToFile(@"..\..\result.pptx",FileFormat.Pptx2010);

Effective screenshot after edit the hyperlink on presentation slide:

Modify hyperlinks in PowerPoint documents

Full codes:

namespace EditHyperlink
{
    class Program
    {
        static void Main(string[] args)
        {
            Presentation pre = new Presentation();
            pre.LoadFromFile(@"..\..\sample.pptx");
            IAutoShape shape = (IAutoShape)pre.Slides[0].Shapes[2];
            shape.TextFrame.TextRange.ClickAction.Address = "http://www.e-iceblue.com";
            shape.TextFrame.TextRange.Text = "E-iceblue";
            pre.SaveToFile(@"..\..\result.pptx",FileFormat.Pptx2010);
            System.Diagnostics.Process.Start(@"..\..\result.pptx");
        }
    }
}

If you created a pretty Excel table and now want to publish it online as a web page, the simplest way is to export it to an old good HTML file. However a problem may occur if you just simply transform image in Excel to HTML code with a relative link (URL). This way, your web page may no longer display properly on client machines since the image can't be reached through that URL on client-side. In this article, we’re going to resolve this issue by embedding image in HTML code when converting Excel to HTML.

Here is an Excel table with some images embedded in.

Embed image in HTML when converting Excel to HTML

We're able to convert this Excel file to HTML by following below code snippet:

Step 1: Create a new instance of workbook.

Workbook book = new Workbook();
book.LoadFromFile("Book1.xlsx");

Step 2: Embed images into HTML code using Data URI scheme.

HTMLOptions options = new HTMLOptions();
options.ImageEmbedded = true;

Step 3: Save the worksheet to HTML.

book.Worksheets[0].SaveToHtml("sample.html", options);
System.Diagnostics.Process.Start("sample.html");

Output:

Embed image in HTML when converting Excel to HTML

HTML Code:

Since the HTML code is too long to be displayed here, we have to present it by a screenshot.

Embed image in HTML when converting Excel to HTML

Full C# Code:

using Spire.Xls;
using Spire.Xls.Core.Spreadsheet;
namespace CreateWorkbook
{
    class Program
    {
        static void Main(string[] args)
        {
            // create Workbook instance and load file
            Workbook book = new Workbook();
            book.LoadFromFile("Book1.xlsx");

            // embed image into html when converting
            HTMLOptions options = new HTMLOptions();
            options.ImageEmbedded = true;

            // save the sheet to html
            book.Worksheets[0].SaveToHtml("sample.html", options);
            System.Diagnostics.Process.Start("sample.html"); 

        }
    }
}
page 256