C#/VB.NET: Extract Text and Images from PowerPoint

Text and Image are the most commonly used elements in PowerPoint documents. In some cases, you may need to extract the text and images from a PowerPoint document so they can be used in another document. For such cases, this article will demonstrate how to extract text and images from a PowerPoint document using C# and VB.NET.

The following topics will be covered:

  • Extract Text from a Specific PowerPoint Slide in C# and VB.NET
  • Extract All Text from a PowerPoint Document in C# and VB.NET
  • Extract Images from a Specific PowerPoint Slide in C# and VB.NET
  • Extract All Images from a PowerPoint Document in C# and VB.NET

Installation

In order to deal with PowerPoint documents, I will be using Spire.Presentation for .NET API. The DLL files of Spire.Presentation for .NET API can be either downloaded from the official website or installed via NuGet by selecting Tools > NuGet Package Manager > Package Manager Console and then add the following code:

PM> Install-Package Spire.Presentation

Extract Text from a Specific PowerPoint Slide in C# and VB.NET

The following are the main steps to extract text from a specific PowerPoint Slide:

  • Create an instance of Presentation class.
  • Load a PowerPoint document using Presentation.LoadFromFile() method.
  • Get the desired slide by its index using Presentation.Slides[index] property.
  • Loop through the shapes on each slide using ISlide.Shapes collection.
  • If the current shape is an IAutoShape, loop through the paragraphs in the shape using IAutoShape.TextFrame.Paragraphs collection, then extract the text of each paragraph using TextParagraph.Text property and add them into a StringBuilder instance.
  • Finally, save the extracted text as file.

C#

using Spire.Presentation;
using System.IO;
using System.Text;

namespace ExtractSlideText
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Presentation instance
            Presentation presentation = new Presentation();

            //Load a PowerPoint document
            presentation.LoadFromFile("Input.pptx");

            //Get the first slide
            ISlide slide = presentation.Slides[0];

            //Create a StringBuilder instance
            StringBuilder sb = new StringBuilder();

            //Loop through the shapes on the slide
            foreach (IShape shape in slide.Shapes)
            {
                //If the shape is IAutoshape
                if (shape is IAutoShape)
                {
                    //Loop through the paragraphs in the shape
                    foreach (TextParagraph tp in (shape as IAutoShape).TextFrame.Paragraphs)
                    {
                        //Append the text of each paragraph to the StringBuilder instance
                        sb.AppendLine(tp.Text);
                    }
                }

            }

            //Write text to a .txt file
            File.WriteAllText("Result.txt", sb.ToString());
        }
    }
}

VB.NET

Imports Spire.Presentation
Imports System.IO
Imports System.Text

Namespace ExtractSlideText
    Friend Class Program
        Private Shared Sub Main(ByVal args As String())
            'Create a Presentation instance
            Dim presentation As Presentation = New Presentation()

            'Load a PowerPoint document
            presentation.LoadFromFile("Input.pptx")

            'Get the first slide
            Dim slide As ISlide = presentation.Slides(0)

            'Create a StringBuilder instance
            Dim sb As StringBuilder = New StringBuilder()

            'Loop through the shapes on the slide
            For Each shape As IShape In slide.Shapes
                'If the shape is IAutoshape
                If TypeOf shape Is IAutoShape Then
                    'Loop through the paragraphs in the shape
                    For Each tp As TextParagraph In TryCast(shape, IAutoShape).TextFrame.Paragraphs
                        'Append the text of each paragraph to the StringBuilder instance
                        sb.AppendLine(tp.Text)
                    Next
                End If
            Next

            'Write text to a .txt file
            Call File.WriteAllText("Result.txt", sb.ToString())
        End Sub
    End Class
End Namespace

Extract All Text from a PowerPoint Document in C# and VB.NET

The code to extract all text from a PowerPoint document is very similar to the above code. The difference is that you need to loop through the slides in the PowerPoint document (instead of getting a desired slide) using Presentation.Slides collection.

C#

using Spire.Presentation;
using System.IO;
using System.Text;

namespace ExtractPptText
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Presentation object
            Presentation presentation = new Presentation();

            //Load the sample PowerPoint file
            presentation.LoadFromFile("Input.pptx");

            //Create a StringBuilder object
            StringBuilder sb = new StringBuilder();

            //Loop through the slides
            foreach (ISlide slide in presentation.Slides)
            {
                sb.AppendLine("slide" + slide.SlideNumber);
                //Loop through the shapes
                foreach (IShape shape in slide.Shapes)
                {
                    //Detect if a shape is IAutoshape
                    if (shape is IAutoShape)
                    {
                        //Loop through the paragraphs of a shape
                        foreach (TextParagraph tp in (shape as IAutoShape).TextFrame.Paragraphs)
                        {
                            //Append text of a paragraph to the string builder
                            sb.AppendLine(tp.Text);
                        }
                    }

                }

            }

            //Write text to txt file
            File.WriteAllText("Result.txt", sb.ToString());
        }
    }
}

VB.NET

Imports Spire.Presentation
Imports System.IO
Imports System.Text

Namespace ExtractPptText
    Friend Class Program
        Private Shared Sub Main(ByVal args As String())
            'Create a Presentation object
            Dim presentation As Presentation = New Presentation()

            'Load the sample PowerPoint file
            presentation.LoadFromFile("Input.pptx")

            'Create a StringBuilder object
            Dim sb As StringBuilder = New StringBuilder()

            'Loop through the slides
            For Each slide As ISlide In presentation.Slides
                sb.AppendLine("slide" & slide.SlideNumber)
                'Loop through the shapes
                For Each shape As IShape In slide.Shapes
                    'Detect if a shape is IAutoshape
                    If TypeOf shape Is IAutoShape Then
                        'Loop through the paragraphs of a shape
                        For Each tp As TextParagraph In TryCast(shape, IAutoShape).TextFrame.Paragraphs
                            'Append text of a paragraph to the string builder
                            sb.AppendLine(tp.Text)
                        Next
                    End If
                Next
            Next

            'Write text to txt file
            Call File.WriteAllText("Result.txt", sb.ToString())
        End Sub
    End Class
End Namespace

Extract Images from a Specific PowerPoint Slide in C# and VB.NET

On a PowerPoint slide, an image can be added as a shape or a slide background. To extract images, you need to process the shapes and the background of the slide.

You can refer to the following steps:

  • Create an instance of Presentation class.
  • Load a PowerPoint document using Presentation.LoadFromFile() method.
  • Get the desired slide by its index using Presentation.Slides[index] property.
  • If the slide’s fill type is picture, then extract the background image using ISlide.SlideBackground.Fill.PictureFill.Picture.EmbedImage.Image.Save() method.
  • Loop through the shapes on the slide.
  • If the current shape is an IAutoShape, and its fill type is picture, extract the image in the shape using IAutoShape.Fill.PictureFill.Picture.EmbedImage.Image.Save() method.
  • If the current shape is a SlidePicture, extract the image in the shape using SlidePicture.PictureFill.Picture.EmbedImage.Image.Save() method.

C#

using Spire.Presentation;

namespace ExtractSlideImages
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create an instance of Presentation class
            Presentation ppt = new Presentation();
            //Load a PowerPoint document
            ppt.LoadFromFile("Input.pptx");

            //Get the first slide
            ISlide slide = ppt.Slides[0];

            //Extract image from slide background
            //If the slide has image background
            if (slide.SlideBackground.Fill.FillType == Spire.Presentation.Drawing.FillFormatType.Picture)
            {
                //Extract the image
                slide.SlideBackground.Fill.PictureFill.Picture.EmbedImage.Image.Save(string.Format("Background.png"));
            }

            //Extract image from shape
            //Loop through the shapes on the slide
            for (int i = 0; i < slide.Shapes.Count; i++)
            {
                IShape s = slide.Shapes[i];
                //If the shape is IAutoShape
                if (s is IAutoShape)
                {
                    IAutoShape autoShape = s as IAutoShape;
                    //If the shape is filled with image
                    if (autoShape.Fill.FillType == Spire.Presentation.Drawing.FillFormatType.Picture)
                    {
                        //Extract the image
                        autoShape.Fill.PictureFill.Picture.EmbedImage.Image.Save(string.Format("Shape{0}.png", i));
                    }
                }
                //If the shape is SlidePicture
                if (s is SlidePicture)
                {
                    //Extract the image
                    SlidePicture ps = s as SlidePicture;
                    ps.PictureFill.Picture.EmbedImage.Image.Save(string.Format("Picture{0}.png", i));
                }
            }
        }
    }
}

VB.NET

Imports Spire.Presentation

Namespace ExtractSlideImages
    Friend Class Program
        Private Shared Sub Main(ByVal args As String())
            'Create an instance of Presentation class
            Dim ppt As Presentation = New Presentation()
            'Load a PowerPoint document
            ppt.LoadFromFile("Input.pptx")

            'Get the first slide
            Dim slide As ISlide = ppt.Slides(0)

            'Extract image from slide background
            'If the slide has image background
            If slide.SlideBackground.Fill.FillType Is Spire.Presentation.Drawing.FillFormatType.Picture Then
                'Extract the image
                slide.SlideBackground.Fill.PictureFill.Picture.EmbedImage.Image.Save(String.Format("Background.png"))
            End If

            'Extract image from shape
            'Loop through the shapes on the slide
            For i As Integer = 0 To slide.Shapes.Count - 1
                Dim s As IShape = slide.Shapes(i)
                'If the shape is IAutoShape
                If TypeOf s Is IAutoShape Then
                    Dim autoShape As IAutoShape = TryCast(s, IAutoShape)
                    'If the shape is filled with image
                    If autoShape.Fill.FillType Is Spire.Presentation.Drawing.FillFormatType.Picture Then
                        'Extract the image
                        autoShape.Fill.PictureFill.Picture.EmbedImage.Image.Save(String.Format("Shape{0}.png", i))
                    End If
                End If
                'If the shape is SlidePicture
                If TypeOf s Is SlidePicture Then
                    'Extract the image
                    Dim ps As SlidePicture = TryCast(s, SlidePicture)
                    ps.PictureFill.Picture.EmbedImage.Image.Save(String.Format("Picture{0}.png", i))
                End If
            Next
        End Sub
    End Class
End Namespace

Extract All Images from a PowerPoint Document in C# and VB.NET

The following are the steps to extract all images from a PowerPoint document:

  •  Create an instance of Presentation class.
  • Load a PowerPoint document using Presentation.LoadFromFile() method.
  • Loop through the images in the document using Presentation.Images.Count property.
  • Save each image to file using Presentation.Images[index].Image.Save() method.

C#

using Spire.Presentation;
using System.Drawing;

namespace ExtractPptImages
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create an instance of Presentation class
            Presentation ppt = new Presentation();
            //Load a PowerPoint document
            ppt.LoadFromFile("Input.pptx");

            //Loop through the images in the document
            for (int i = 0; i < ppt.Images.Count; i++)
            {
                //Save each image to file
                ppt.Images[i].Image.Save(string.Format("Images{0}.png", i));
            }
        }
    }
}

VB.NET

Imports Spire.Presentation

Namespace ExtractPptImages
    Friend Class Program
        Private Shared Sub Main(ByVal args As String())
            'Create an instance of Presentation class
            Dim ppt As Presentation = New Presentation()
            'Load a PowerPoint document
            ppt.LoadFromFile("Input.pptx")

            'Loop through the images in the document
            For i As Integer = 0 To ppt.Images.Count - 1
                'Save each image to file
                ppt.Images(i).Image.Save(String.Format("Images{0}.png", i))
            Next
        End Sub
    End Class
End Namespace

See More

Product PageDocumentationExamplesForumTemporary License

Add, Verify or Remove Digital Signature in PowerPoint in Java

A digital signature is an electronic, encrypted, stamp of authentication on digital information such as e-mail messages or electronic documents. It can help the recipient verify if the document content has been altered or not since it was signed. If any changes are made, the signature will become invalid immediately. In this article, I will demonstrate how to add, verify or remove digital signature in PowerPoint using Java.

Add Dependencies

In order to deal with PowerPoint documents, I will be using Spire.Presentation for Java API. The API’s jar can be either downloaded from the official website or installed from Maven by adding the following code to your maven-based 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>5.1.7</version>   
    </dependency>   
</dependencies>

Add Digital Signature to PowerPoint in Java

The following are the steps to add a digital signature to a PowerPoint document:

  • Create an instance of Presentation class.
  • Load the PowerPoint document using Presentation.loadFromFile(String filePath) method.
  • Add a digital signature to the document using Presentation.addDigitalSignature(String pfxPath, String password, String comments, Date signTime) method.
  • Save the result document using Presentation.saveToFile(String filePath, FileFormat fileFormat) method.
import com.spire.presentation.FileFormat;
import com.spire.presentation.Presentation;

import java.util.Date;

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

        //Add a digital signature
        String pfxPath = "Certificate.pfx";
        String password = "123456"; //The password of the .pfx file
        String comment = "Modification is not allowed";
        presentation.addDigitalSignature(pfxPath, password, comment, new Date());

        //Save the result to file
        presentation.saveToFile("AddSignature.pptx", FileFormat.PPTX_2013);
    }
}

Output:

Add digital signature to PowerPoint in Java

Verify Digital Signature in PowerPoint in Java

The following are the steps to verify digital signature in a PowerPoint document:

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

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

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

Output:

Verify digital signature in PowerPoint in Java

Remove Digital Signature from PowerPoint in Java

The following are the steps to remove all digital signatures from a PowerPoint document:

  • Create an instance of Presentation class.
  • Load a PowerPoint document using Presentation.loadFromFile(String filePath) method.
  • Detect if the document is digitally signed or not using Presentation.isDigitallySigned() method. If the result is true, remove all digital signatures using Presentation.removeAllDigitalSignatures() method.
  • Save the result document using Presentation.saveToFile(String filePath, FileFormat fileFormat) method.
import com.spire.presentation.FileFormat;
import com.spire.presentation.Presentation;

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

        //Load the PowerPoint document
        presentation.loadFromFile("AddSignature.pptx");

        //Determine if the document is digitally signed
        if (presentation.isDigitallySigned())
        {
            //Remove all digital signatures
            presentation.removeAllDigitalSignatures();
        }

        //Save the result document
        presentation.saveToFile("RemoveSignature.pptx", FileFormat.PPTX_2013);
    }
}

Output:

Remove digital signature in PowerPoint in Java

See More

Product Page | Tutorials  | Examples | Forum | Customized Demo | Temporary License

C#/VB.NET: Read or Extract Text and Images from PDF

Most PDF documents may contain text and images. In some cases, you might need to extract the text or images from a PDF document for further processing. For such cases, this article will demonstrate how to read or extract text and images from PDF in C# and VB.NET.

The following topics will be covered:

  • Read or Extract Text from a Specific PDF Page in C# and VB.NET
  • Read or Extract Text from a Specific Rectangular Area of a PDF Page in C# and VB.NET
  • Read or Extract Text from a PDF File in C# and VB.NET
  • Read or Extract Images from a Specific PDF Page in C# and VB.NET
  • Read or Extract Images from a PDF File in C# and VB.NET

Installation

To deal with PDF files, we will need to use a third-party library. This article uses Spire.PDF for .NET, which is a powerful and multifunctional API for creating, manipulating, converting and printing PDF files.

The DLL files of Spire.PDF for .NET can be either downloaded from the official website or installed via NuGet by selecting Tools > NuGet Package Manager > Package Manager Console and then add the following code:

PM> Install-Package Spire.PDF

Read or Extract Text from a Specific PDF Page in C# and VB.NET

The ExtractText() method of PdfPageBase class is used to extract text from a particular PDF page.

Below are the steps to perform this task:

  • Create a PdfDocument instance.
  • Load a PDF file using PdfDocument.LoadFromFile() method.
  • Get the desired page by its index using PdfDocument.Pages[index] property.
  • Extract text from the page using PdfPageBase.ExtractText() method.
  • Create a StringBuilder instance and append the extracted text to it using StringBuilder.Append() method.
  • Save the text to a .txt file using File.WriteAllText() method.

C#

using Spire.Pdf;
using System;
using System.IO;
using System.Text;

namespace ExtractTextFromPage
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument instance
            PdfDocument doc = new PdfDocument();
            //Load a pdf file
            doc.LoadFromFile("Input.pdf");

            //Get the first page
            PdfPageBase page = doc.Pages[0];

            //Extract text from the page 
            String text = page.ExtractText();

            //Save the text to a txt file
            StringBuilder content = new StringBuilder();
            content.Append(text);            
            File.WriteAllText("Extract.txt", content.ToString());
        }
    }
}

VB.NET

Imports Spire.Pdf
Imports System.IO
Imports System.Text

Namespace ExtractTextFromPage
    Friend Class Program
        Private Shared Sub Main(ByVal args As String())
            'Create a PdfDocument instance
            Dim doc As PdfDocument = New PdfDocument()
            'Load a pdf file
            doc.LoadFromFile("Input.pdf")

            'Get the first page
            Dim page As PdfPageBase = doc.Pages(0)

            'Extract text from the page 
            Dim text As String = page.ExtractText()

            'Save the text to a txt file
            Dim content As StringBuilder = New StringBuilder()
            content.Append(text)
            Call File.WriteAllText("Extract.txt", content.ToString())
        End Sub
    End Class
End Namespace

Read or Extract Text from a Specific Rectangular Area of a PDF Page in C# and VB.NET

It’s possible to extract text from a specific rectangular area of a PDF page using another overloaded ExtractText() method, by passing a System.Drawing.RectangleF object. You can refer to the following code example.

C#

using Spire.Pdf;
using System;
using System.IO;
using System.Text;

namespace ExtractTextFromRectangularArea
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument instance
            PdfDocument doc = new PdfDocument();
            //Load a pdf file
            doc.LoadFromFile("Input.pdf");

            //Get the first page
            PdfPageBase page = doc.Pages[0];

            //Create a System.Drawing.RectangleF instance
            System.Drawing.RectangleF rect = new System.Drawing.RectangleF();
            //Extract text from a specific rectangular area in the page 
            String text = page.ExtractText(rect);

            //Save the text to a txt file
            StringBuilder content = new StringBuilder();
            content.Append(text);
            File.WriteAllText("Extract.txt", content.ToString());
        }
    }
}

VB.NET

Imports Spire.Pdf
Imports System
Imports System.IO
Imports System.Text

Namespace ExtractTextFromRectangularArea
    Friend Class Program
        Private Shared Sub Main(ByVal args As String())
            'Create a PdfDocument instance
            Dim doc As PdfDocument = New PdfDocument()
            'Load a pdf file
            doc.LoadFromFile("Input.pdf")

            'Get the first page
            Dim page As PdfPageBase = doc.Pages(0)

            'Create a System.Drawing.RectangleF instance
            Dim rect As Drawing.RectangleF = New Drawing.RectangleF()
            'Extract text from a specific rectangular area in the page 
            Dim text As String = page.ExtractText(rect)

            'Save the text to a txt file
            Dim content As StringBuilder = New StringBuilder()
            content.Append(text)
            Call File.WriteAllText("Extract.txt", content.ToString())
        End Sub
    End Class
End Namespace

Read or Extract Text from a PDF File in C# and VB.NET

The above examples show how to extract text from a specific page. If you want to extract text from a whole PDF file, use the following code.

C#

using Spire.Pdf;
using System;
using System.IO;
using System.Text;

namespace ExtractTextFromPDF
{
    class Program
    {
        static void Main(string[] args)
        {       
            //Create a PdfDocument instance
            PdfDocument doc = new PdfDocument();
            //Load a pdf file
            doc.LoadFromFile("Input.pdf");

            //Create a StringBuilder instance
            StringBuilder content = new StringBuilder();

            //Loop through the pages
            foreach (PdfPageBase page in doc.Pages)
            {
                //Extract text from each page 
                String text = page.ExtractText();
                //Append text to the StringBuilder
                content.Append(text);
            }

            //Save the text to a .txt file
            File.WriteAllText("Extract.txt", content.ToString());
        }
    }
}

VB.NET

Imports Spire.Pdf
Imports System.IO
Imports System.Text

Namespace ExtractTextFromPDF
    Friend Class Program
        Private Shared Sub Main(ByVal args As String())
            'Create a PdfDocument instance
            Dim doc As PdfDocument = New PdfDocument()
            'Load a pdf file
            doc.LoadFromFile("Input.pdf")

            'Create a StringBuilder instance
            Dim content As StringBuilder = New StringBuilder()

            'Loop through the pages
            For Each page As PdfPageBase In doc.Pages
                'Extract text from each page 
                Dim text As String = page.ExtractText()
                'Append text to the StringBuilder
                content.Append(text)
            Next

            'Save the text to a .txt file
            Call File.WriteAllText("Extract.txt", content.ToString())
        End Sub
    End Class
End Namespace

Read or Extract Images from a Specific PDF Page in C# and VB.NET

The following are the steps to extract images from a specific PDF page:

  • Create a PdfDocument instance.
  • Load a PDF file using PdfDocument.LoadFromFile() method.
  • Get the desired page by its index using PdfDocument.Pages[index] property.
  • Extract images from the page into an Image array using PdfPageBase.ExtractImages() method.
  • If any image is found, loop through the array, save each image to disk using Image.Save() method.

C#

using Spire.Pdf;
using System.Drawing;

namespace ExtractImageFromPage
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument instance
            PdfDocument doc = new PdfDocument();
            //Load a pdf file
            doc.LoadFromFile("Input.pdf");

            //Get the first page
            PdfPageBase page = doc.Pages[0];
            //Extract images from the page into an Image array
            Image[] images = page.ExtractImages();

            //If any image is found
            if (images != null && images.Length > 0)
            {
                //Loop through the array
                for (int i = 0; i < images.Length; i++)
                {
                    //Save each image to disk
                    Image image = images[i];
                    image.Save("image" + (i + 1).ToString() + ".png", System.Drawing.Imaging.ImageFormat.Png);
                }
            }
        }
    }
}

VB.NET

Imports Spire.Pdf

Namespace ExtractImageFromPage
    Friend Class Program
        Private Shared Sub Main(ByVal args As String())
            'Create a PdfDocument instance
            Dim doc As PdfDocument = New PdfDocument()
            'Load a pdf file
            doc.LoadFromFile("Input.pdf")

            'Get the first page
            Dim page As PdfPageBase = doc.Pages(0)
            'Extract images from the page into an Image array
            Dim images As Image() = page.ExtractImages()

            'If any image is found
            If images IsNot Nothing AndAlso images.Length > 0 Then
                'Loop through the array
                For i = 0 To images.Length - 1
                    'Save each image to disk
                    Dim image As Image = images(i)
                    image.Save("image" & (i + 1).ToString() & ".png", Drawing.Imaging.ImageFormat.Png)
                Next
            End If
        End Sub
    End Class
End Namespace

Read or Extract Images from a PDF File in C# and VB.NET

If you want to extract images from a whole PDF file, use the following code.

C#

using Spire.Pdf;
using System.Drawing;

namespace ExtractTextFromPDF
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument instance
            PdfDocument doc = new PdfDocument();
            //Load a pdf file
            doc.LoadFromFile("Input.pdf");

            Image[] images = null;
            //Loop through the pages
            foreach (PdfPageBase page in doc.Pages)
            {
                //Extract images from each page into an Image array
                images = page.ExtractImages();
            }

            //If any image is found
            if (images != null && images.Length > 0)
            {
                //Loop through the array
                for (int i = 0; i < images.Length; i++)
                {
                    //Save each image to disk
                    Image image = images[i];
                    image.Save("image" + (i + 1).ToString() + ".png", System.Drawing.Imaging.ImageFormat.Png);
                }
            }
        }
    }
}

VB.NET

Imports Spire.Pdf

Namespace ExtractTextFromPDF
    Friend Class Program
        Private Shared Sub Main(ByVal args As String())
            'Create a PdfDocument instance
            Dim doc As PdfDocument = New PdfDocument()
            'Load a pdf file
            doc.LoadFromFile("Input.pdf")
            Dim images As Image() = Nothing
            'Loop through the pages
            For Each page As PdfPageBase In doc.Pages
                'Extract images from each page into an Image array
                images = page.ExtractImages()
            Next

            'If any image is found
            If images IsNot Nothing AndAlso images.Length > 0 Then
                'Loop through the array
                For i = 0 To images.Length - 1
                    'Save each image to disk
                    Dim image As Image = images(i)
                    image.Save("image" & (i + 1).ToString() & ".png", Drawing.Imaging.ImageFormat.Png)
                Next
            End If
        End Sub
    End Class
End Namespace

See More

Product Page 丨 Documentation 丨 Examples 丨 Forum 丨 Temporary License 丨

C#/VB.NET: Convert Excel Worksheets to Images (PNG, JPEG, EMF, SVG)

In certain circumstances, you may need to convert Excel worksheets to images, for example, generating thumbnails for the worksheets. In this article, I will demonstrate how to convert Excel worksheet to image using C# and VB.NET. The following topics will be covered:

  • Convert Worksheet to PNG or JPEG in C# and VB.NET
  • Convert Worksheet to EMF in C# and VB.NET
  • Convert Worksheet to SVG in C# and VB.NET
  • Convert Specific Cells of a Worksheet to Image in C# and VB.NET

Installation

In order to achieve the conversion, I will be using Spire.XLS for .NET, which is an easy to use and feature-rich library for creating, editing, converting and printing Excel files.

The DLL files of Spire.XLS for .NET API can be either downloaded from the official website or installed via NuGet by selecting Tools > NuGet Package Manager > Package Manager Console and then add the following code:

PM> Install-Package Spire.XLS

Convert Worksheet to PNG or JPEG in C# and VB.NET

The following are the steps to convert an Excel worksheet to PNG or JPEG images:

  • Create a Workbook instance.
  • Load an Excel file using Workbook.LoadFromFile() method.
  • Get the worksheet you want to convert using Workbook.Worksheets[index] property.
  • Convert the worksheet to PNG/JPEG using XlsWorksheet.SaveToImage() method.

C#

using Spire.Xls;

namespace WorksheetToPngOrJpeg
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Workbook instance
            Workbook workbook = new Workbook();
            //Load an Excel file
            workbook.LoadFromFile("Sample.xlsx");

            //Get the first worksheet
            Worksheet sheet = workbook.Worksheets[0];
            //Save to jpeg
            sheet.SaveToImage("ToJPEG.jpg");
            //Save to png
            //sheet.SaveToImage("ToPNG.png");
            workbook.Dispose();
        }
    }
}

VB.NET

Imports Spire.Xls

Namespace WorksheetToPngOrJpeg
    Friend Class Program
        Private Shared Sub Main(ByVal args As String())
            'Create a Workbook instance
            Dim workbook As Workbook = New Workbook()
            'Load an Excel file
            workbook.LoadFromFile("Sample.xlsx")

            'Get the first worksheet
            Dim sheet As Worksheet = workbook.Worksheets(0)
            'Save to jpeg
            sheet.SaveToImage("ToJPEG.jpg")
            'Save to png
            'sheet.SaveToImage("ToPNG.png");
            workbook.Dispose()
        End Sub
    End Class
End Namespace

Convert Worksheet to EMF in C# and VB.NET

The following are the steps to convert an Excel worksheet to EMF image:

  • Create a Workbook instance.
  • Load an Excel file using Workbook.LoadFromFile() method.
  • Get the worksheet you want to convert using Workbook.Worksheets[index] property.
  • Create a FileStream instance for the converted EMF image.
  • Convert the worksheet to EMF using XlsWorksheet.ToEMFStream(Stream stream, int firstRow, int firstColumn, int lastRow, int lastColumn, EmfType emfType) method.

C#

using Spire.Xls;
using System.IO;

namespace WorksheetToEMF
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Workbook instance
            Workbook workbook = new Workbook();
            //Load an Excel file
            workbook.LoadFromFile("Sample.xlsx");

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

            //Save the worksheet to EMF
            FileStream fs = new FileStream("ToEMF.emf", FileMode.Create);
            sheet.ToEMFStream(fs, 1, 1, sheet.LastRow, sheet.LastColumn, System.Drawing.Imaging.EmfType.EmfOnly);
            fs.Flush();
            fs.Close();
            workbook.Dispose();
        }
    }
}

VB.NET

Imports Spire.Xls
Imports System.IO

Namespace WorksheetToEMF
    Friend Class Program
        Private Shared Sub Main(ByVal args As String())
            'Create a Workbook instance
            Dim workbook As Workbook = New Workbook()
            'Load an Excel file
            workbook.LoadFromFile("Sample.xlsx")

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

            'Save the worksheet to EMF
            Dim fs As FileStream = New FileStream("ToEMF.emf", FileMode.Create)
            sheet.ToEMFStream(fs, 1, 1, sheet.LastRow, sheet.LastColumn, Drawing.Imaging.EmfType.EmfOnly)
            fs.Flush()
            fs.Close()
            workbook.Dispose()
        End Sub
    End Class
End Namespace

Convert Worksheet to SVG in C# and VB.NET

The following are the steps to convert an Excel worksheet to SVG image:

  • Create a Workbook instance.
  • Load an Excel file using Workbook.LoadFromFile() method.
  • Get the worksheet you want to convert using Workbook.Worksheets[index] property.
  • Create a FileStream instance for the converted SVG image.
  • Convert the worksheet to SVG using XlsWorksheet.ToSVGStream(Stream stream, int firstRow, int firstColumn, int lastRow, int lastColumn) method.

C#

using Spire.Xls;
using System.IO;

namespace WorksheetToSVG
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Workbook instance
            Workbook workbook = new Workbook();
            //Load an Excel file
            workbook.LoadFromFile("Sample.xlsx");

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

            //Save the worksheet to SVG
            FileStream fs = new FileStream("ToSVG.svg", FileMode.Create);
            sheet.ToSVGStream(fs, 1, 1, sheet.LastRow, sheet.LastColumn);
            fs.Flush();
            fs.Close();
            workbook.Dispose();
        }
    }
}

VB.NET

Imports Spire.Xls
Imports System.IO

Namespace WorksheetToSVG
    Friend Class Program
        Private Shared Sub Main(ByVal args As String())
            'Create a Workbook instance
            Dim workbook As Workbook = New Workbook()
            'Load an Excel file
            workbook.LoadFromFile("Sample.xlsx")

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

            'Save the worksheet to SVG
            Dim fs As FileStream = New FileStream("ToSVG.svg", FileMode.Create)
            sheet.ToSVGStream(fs, 1, 1, sheet.LastRow, sheet.LastColumn)
            fs.Flush()
            fs.Close()
            workbook.Dispose()
        End Sub
    End Class
End Namespace

Convert Specific Cells of a Worksheet to Image in C# and VB.NET

The following are the steps to convert specific cells of a worksheet to image:

  • Create a Workbook instance.
  • Load an Excel file using Workbook.LoadFromFile() method.
  • Get the worksheet you want to convert using Workbook.Worksheets[index] property.
  • Convert specific cells of the worksheet to PNG image using XlsWorksheet.ToImage(int firstRow, int firstColumn, int lastRow, int lastColumn).Save(string filename, ImageFormat format) method.

C#

using Spire.Xls;

namespace SpecificCellsToImage
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Workbook instance
            Workbook workbook = new Workbook();
            //Load an Excel file
            workbook.LoadFromFile("Sample.xlsx");

            //Get the first worksheet
            Worksheet sheet = workbook.Worksheets[0];
            //Save specific cells to PNG image
            sheet.ToImage(1, 1, 6, 3).Save("SpecificCellsToImage.png", System.Drawing.Imaging.ImageFormat.Png);

            workbook.Dispose();
        }
    }
}

VB.NET

Imports Spire.Xls

Namespace SpecificCellsToImage
    Friend Class Program
        Private Shared Sub Main(ByVal args As String())

            'Create a Workbook instance
            Dim workbook As Workbook = New Workbook()
            'Load an Excel file
            workbook.LoadFromFile("Sample.xlsx")

            'Get the first worksheet
            Dim sheet As Worksheet = workbook.Worksheets(0)
            'Save specific cells to PNG image
            sheet.ToImage(1, 1, 6, 3).Save("SpecificCellsToImage.png", Drawing.Imaging.ImageFormat.Png)
            workbook.Dispose()
        End Sub
    End Class
End Namespace

Output after converting specific cells to image:

Convert Specific Cells to Image using C# and VB.NET

See More

Product Page | Documentation | Examples | Temporary License | Forum |

Add or Delete Page Breaks in Excel in Java

Page breaks in Excel are dividers that break a large worksheet into separate pages for printing. By default, Excel inserts automatic page breaks according to the paper size and margin settings etc. But you can divide the worksheet at specific places you want by inserting manual page breaks. In this article, I will demonstrate how to add or delete manual page breaks in Excel in Java using Free Spire.XLS for Java API.

Contents Summary:

  • Add Page Breaks to Excel in Java
  • Delete a Specific Page Break from Excel in Java
  • Delete All Page Breaks from Excel in Java

Add Dependencies

To begin with, you need to add needed dependencies for including Free Spire.XLS for Java library into your Java project. You can either download the library’s jar from the official website or install it from Maven by adding the following code to your maven-based 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.xls.free</artifactId>    
        <version>3.9.1</version>    
    </dependency>    
</dependencies>

Add Page Breaks to Excel in Java

You can add two types of page breaks: Horizontal page break and vertical page break.

  • Create an instance of Workbook class.
  • Load an Excel file using Workbook.loadFromFile() method.
  • Get the desired worksheet by its index using Workbook.getWorksheets().get() method.
  • Specify the cells where you want to add page breaks to using Worksheet.getRange().get() method.
  • Add horizontal and vertical page breaks to the cells using Worksheet.getHPageBreaks().add() and Worksheet.getVPageBreaks().add() methods.
  • Set the sheet view mode to ViewMode.Preview using Worksheet.setViewMode() method.
  • Save the result file using Workbook.saveToFile() method.
import com.spire.xls.*;

public class AddPageBreaks {
    public static void main(String []args) {
        //Create a Workbook instance
        Workbook workbook = new Workbook();
        //Load an Excel file
        workbook.loadFromFile("Sample.xlsx");

        //Get the first worksheet
        Worksheet sheet = workbook.getWorksheets().get(0);

        //Specify the cells where you want to add page breaks to
        CellRange cell1 = sheet.getRange().get("A10");
        CellRange cell2 = sheet.getRange().get("F1");

        //Add a horizontal page break
        sheet.getHPageBreaks().add(cell1);

        //Add a vertical page break
        sheet.getVPageBreaks().add(cell2);

        //Set view mode to Preview in order to view the page breaks
        sheet.setViewMode(ViewMode.Preview);

        //Save the result file
        workbook.saveToFile("AddPageBreaks.xlsx", ExcelVersion.Version2013);
    }
}
Add page breaks to Excel using Java

Delete a Specific Page Break from Excel in Java

The following are the steps to delete a specific horizontal or vertical page break from an Excel worksheet:

  • Create an instance of Workbook class.
  • Load an Excel file using Workbook.loadFromFile() method.
  • Get the desired worksheet by its index using Workbook.getWorksheets().get() method.
  • Delete a specific horizontal or vertical page break from the worksheet by its index using Worksheet.getHPageBreaks().removeAt() or Worksheet.getVPageBreaks().removeAt() method.
  • Save the result file using Workbook.saveToFile() method.
import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;

public class DeleteASpecificPageBreak {
    public static void main(String []args) {
        //Create a Workbook instance
        Workbook workbook = new Workbook();
        //Load an Excel file
        workbook.loadFromFile("AddPageBreaks.xlsx");

        //Get the first worksheet
        Worksheet sheet = workbook.getWorksheets().get(0);

        //Delete the first horizontal page break
        sheet.getHPageBreaks().removeAt(0);
        //Delete the first vertical page break
        sheet.getVPageBreaks().removeAt(0);

        //Save the result file
        workbook.saveToFile("DeleteASpecificPageBreaks.xlsx", ExcelVersion.Version2013);
    }
}

Delete All Page Breaks from Excel in Java

To delete all the page breaks, you can follow the steps below:

  • Create an instance of Workbook class.
  • Load an Excel file using Workbook.loadFromFile() method.
  • Get the desired worksheet by its index using Workbook.getWorksheets().get() method.
  • Delete all the horizontal and vertical page breaks from the worksheet using Worksheet.getHPageBreaks().clear() and Worksheet.getVPageBreaks().clear() methods.
  • Save the result file using Workbook.saveToFile() method.
import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;

public class DeleteAllPageBreaks {
    public static void main(String []args) {
        //Create a Workbook instance
        Workbook workbook = new Workbook();
        //Load an Excel file
        workbook.loadFromFile("AddPageBreaks.xlsx");

        //Get the first worksheet
        Worksheet sheet = workbook.getWorksheets().get(0);

        //Delete all horizontal page breaks
        sheet.getHPageBreaks().clear();
        //Delete all vertical page breaks
        sheet.getVPageBreaks().clear();

        //Save the result file
        workbook.saveToFile("DeleteAllPageBreaks.xlsx", ExcelVersion.Version2013);
    }
}

Note: Deleting the manual page breaks will reset the worksheet to display the automatic page breaks (In Page Break Preview, dashed lines are page breaks Excel automatically added. Solid lines are page breaks that were added manually).

Delete page breaks from Excel using Java

See More

Product Page | Tutorials | Forum |

Set Background Color and Background Image for PowerPoint Slides in C#

Microsoft PowerPoint allows you to set background for PowerPoint slides. You can choose to set background color or background image as per your requirements. In this article, I will demonstrate how to set solid background color, gradient background color and background image for PowerPoint slides in C#, along with how to set background for master slides in C# using Spire.Presentation for .NET library.

Installation

The DLL files of Spire.Presentation for .NET API can be either downloaded from the official website or installed via NuGet by selecting Tools > NuGet Package Manager > Package Manager Console and then adding the following code:

PM> Install-Package Spire.Presentation

Set Solid Background Color for PowerPoint Slides using C#

The following are the steps to set solid background color for a PowerPoint slide:

  • Create an instance of Presentation class.
  • Load a PowerPoint document using Presentation.LoadFromFile() method.
  • Get the desired slide by its index using Presentation.Slides[index] property.
  • Set ISlide.SlideBackground.Type property as BackgroundType.Custom.
  • Set ISlide.SlideBackground.Fill.FillType property as FillFormatType.Solid.
  • Set ISlide.SlideBackground.Fill.SolidColor.Color property as Color.Green or other color you like.
  • Save the result document using Presentation.SaveToFile() method.
using Spire.Presentation;
using Spire.Presentation.Drawing;
using System.Drawing;

namespace SetSolidBackgroundColor
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Presentation instance
            Presentation ppt = new Presentation();
            //Load a PowerPoint document
            ppt.LoadFromFile("Input.pptx");

            //Get the first slide
            ISlide slide = ppt.Slides[0];

            //Set the background color for the first slide as green
            slide.SlideBackground.Type = BackgroundType.Custom;
            slide.SlideBackground.Fill.FillType = FillFormatType.Solid;
            slide.SlideBackground.Fill.SolidColor.Color = Color.Green;

            //Save the result document
            ppt.SaveToFile("SolidBackground.pptx", FileFormat.Pptx2013);
        }
    }
}
Set solid background color for PowerPoint slides using C#

Set Gradient Background Color for PowerPoint Slides using C#

The following are the steps to set gradient background color for a PowerPoint slide:

  • Create an instance of Presentation instance.
  • Load a PowerPoint document using Presentation.LoadFromFile() method.
  • Get the desired slide by its index using Presentation.Slides[index] property.
  • Set ISlide.SlideBackground.Type property as BackgroundType.Custom.
  • Set ISlide.SlideBackground.Fill.FillType property as FillFormatType.Gradient.
  • Apply Gradient background color for the slide using ISlide.SlideBackground.Fill.Gradient.GradientStops.Append() method.
  • Save the result document using Presentation.SaveToFile() method.
using Spire.Presentation;
using Spire.Presentation.Drawing;

namespace SetGradientBackgroundColor
{
    class Program
    {
        static void Main(string[] args)
        {

            //Create a Presentation instance
            Presentation ppt = new Presentation();
            //Load a PowerPoint document
            ppt.LoadFromFile("Input.pptx");

            //Get the first slide
            ISlide slide = ppt.Slides[0];

            //Apply Gradient background color for the first slide
            slide.SlideBackground.Type = BackgroundType.Custom;
            slide.SlideBackground.Fill.FillType = FillFormatType.Gradient;
            slide.SlideBackground.Fill.Gradient.GradientStops.Append(0f, KnownColors.White);
            slide.SlideBackground.Fill.Gradient.GradientStops.Append(1f, KnownColors.MediumSeaGreen);

            //Save the document
            ppt.SaveToFile("GradientBackground.pptx", FileFormat.Pptx2013);
        }
    }
}
Set gradient background color for PowerPoint slides using C#

Set Background Image for PowerPoint Slides using C#

The following are the steps to set gradient background color for a PowerPoint slide:

  • Create an instance of Presentation class.
  • Load a PowerPoint document using Presentation.LoadFromFile() method.
  • Get the desired slide by its index using Presentation.Slides[index] property.
  • Set ISlide.SlideBackground.Type property as BackgroundType.Custom.
  • Set ISlide.SlideBackground.Fill.FillType property as FillFormatType.Picture.
  • Set ISlide.SlideBackground.Fill.PictureFill.FillType property as PictureFillType.Stretch.
  • Load an image into a System.Drawing.Image object.
  • Append the image to the presentation using Presentation.Images.Append(Image image) method.
  • Set the image as slide background using ISlide.SlideBackground.Fill.PictureFill.Picture.EmbedImage property.
  • Save the result document using Presentation.SaveToFile() method.
using Spire.Presentation;
using Spire.Presentation.Drawing;
using System.Drawing;

namespace SetBackgroundImage
{
    class Program
    {
        static void Main(string[] args)
        {

            //Create a Presentation instance
            Presentation ppt = new Presentation();
            //Load a PowerPoint document
            ppt.LoadFromFile("Input.pptx");

            //Get the first slide
            ISlide slide = ppt.Slides[0];

            //Set background image for the first slide
            slide.SlideBackground.Type = BackgroundType.Custom;
            slide.SlideBackground.Fill.FillType = FillFormatType.Picture;
            slide.SlideBackground.Fill.PictureFill.FillType = PictureFillType.Stretch;
            Image img = Image.FromFile("background.jpg");
            IImageData image = ppt.Images.Append(img);
            slide.SlideBackground.Fill.PictureFill.Picture.EmbedImage = image;

            //Save the result document
            ppt.SaveToFile("ImageBackground.pptx", FileFormat.Pptx2013);
        }
    }
}
Set background image for PowerPoint slides using C#

Set Background for Master Slides using C#

The above code examples show how to set background for normal slides. If you want to set background for master slides, follow the steps below.

  • Create an instance of Presentation class.
  • Load a PowerPoint document using Presentation.LoadFromFile() method.
  • Get the master slide using Presentation.Masters[index] property.
  • Set IMasterSlide.SlideBackground.Type property as BackgroundType.Custom.
  • Set IMasterSlide.SlideBackground.Fill.FillType property as FillFormatType.Solid.
  • Set IMasterSlide.SlideBackground.Fill.SolidColor.Color property as Color.LightSalmon or other color you like.
  • Save the result document using Presentation.SaveToFile() method.
using Spire.Presentation;
using Spire.Presentation.Drawing;
using System.Drawing;

namespace SetMasterBackground
{
    class Program
    {
        static void Main(string[] args)
        {

            //Create a Presentation instance
            Presentation presentation = new Presentation();
            //Load a PowerPoint document
            presentation.LoadFromFile("Input.pptx");

            //Get the master slide
            IMasterSlide masterSlide = presentation.Masters[0];

            //Set solid background color for the master slide
            masterSlide.SlideBackground.Type = BackgroundType.Custom;
            masterSlide.SlideBackground.Fill.FillType = FillFormatType.Solid;
            masterSlide.SlideBackground.Fill.SolidColor.Color = Color.LightSalmon;

            //Save the result document
            presentation.SaveToFile("MasterBackground.pptx", FileFormat.Pptx2013);
        }
    }
}
Set background for master slides using C#

See More

Product Page | Tutorials | Demo | Examples | Forum | 

C#/VB.NET: Convert Excel to CSV and Vice Versa

CSV (Comma Separated Values) files are plain text files that contain data with comma separated values. They are commonly used by spreadsheet programs such as Microsoft Excel and OpenOffice. In certain cases, you might need to do conversions between Excel and CSV. In this article, I will demonstrate how to convert Excel to CSV and vice versa in C# and VB.NET using Spire.XLS for .NET library.

Installation

The DLL files of Spire.XLS for .NET API can be either downloaded from the official website or installed via NuGet by selecting Tools > NuGet Package Manager > Package Manager Console and then adding the following code:

PM> Install-Package Spire.XLS

Convert Excel to CSV using C# and VB.NET

The SaveToFile method of the XlsWorksheet class is used to convert an Excel worksheet to CSV. The following are the steps to convert an Excel worksheet to CSV:

  • Create an instance of Workbook class.
  • Load an Excel file using Workbook.LoadFromFile() method.
  • Get the desired worksheet by its index using Workbook.Worksheets[index] property.
  • Save the worksheet as CSV using XlsWorksheet.SaveToFile() method. You can choose one of the following overloads:
    • SaveToFile(string fileName, string separator)
    • SaveToFile(string fileName, string separator, Encoding encoding)
    • SaveToFile(string fileName, string separator, bool retainHiddenData)

Convert an Excel Worksheet to CSV using C#

using Spire.Xls;
using System.Text;

namespace ConvertAnWorksheetToCsv
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create an instance of Workbook class
            Workbook workbook = new Workbook();
            //Load an Excel file
            workbook.LoadFromFile("Sample.xlsx");

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

            //Save the worksheet as CSV
            sheet.SaveToFile("ExcelToCSV.csv", ",", Encoding.UTF8);
        }
    }
}

Convert an Excel Worksheet to CSV using VB.NET

Imports Spire.Xls
Imports System.Text

Namespace ConvertAnWorksheetToCsv
    Friend Class Program
        Private Shared Sub Main(ByVal args As String())
            'Create an instance of Workbook class
            Dim workbook As Workbook = New Workbook()
            'Load an Excel file
            workbook.LoadFromFile("Sample.xlsx")

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

            'Save the worksheet as CSV
            sheet.SaveToFile("ExcelToCSV.csv", ",", Encoding.UTF8)
        End Sub
    End Class
End Namespace
Convert Excel worksheet to CSV using C# and VB.NET

The above code saves only the first worksheet in the Excel file as CSV. If you want to save multiple worksheets in the Excel file as separate CSV files, use the following code.

Convert Multiple Worksheets to Separate CSV Files using C#

using Spire.Xls;
using System.Text;

namespace ConvertMultipleWorksheetsToCsv
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create an instance of Workbook class
            Workbook workbook = new Workbook();
            //Load an Excel file
            workbook.LoadFromFile("Sample.xlsx");

            //Loop through the worksheets in the Excel file
            for (int i = 0; i < workbook.Worksheets.Count; i++)
            {
                Worksheet sheet = workbook.Worksheets[i];
                //Save each worksheet as a separate CSV
                sheet.SaveToFile("Output/ExcelToCSV_" + i +".csv", ",", Encoding.UTF8);
            }
        }
    }
}

Convert Multiple Worksheets to Separate CSV Files using VB.NET

Imports Spire.Xls
Imports System.Text

Namespace ConvertMultipleWorksheetsToCsv
    Friend Class Program
        Private Shared Sub Main(ByVal args As String())
            'Create an instance of Workbook class
            Dim workbook As Workbook = New Workbook()
            'Load an Excel file
            workbook.LoadFromFile("Sample.xlsx")

            'Loop through the worksheets in the Excel file
            For i As Integer = 0 To workbook.Worksheets.Count - 1
                Dim sheet As Worksheet = workbook.Worksheets(i)
                'Save each worksheet as a separate CSV
                sheet.SaveToFile("Output/ExcelToCSV_" & i & ".csv", ",", Encoding.UTF8)
            Next
        End Sub
    End Class
End Namespace

Convert CSV to Excel using C# and VB.NET

The following are the steps to convert a CSV to Excel:

  • Create an instance of Workbook class.
  • Load a CSV file using Workbook.LoadFromFile(string fileName, string separator, int startRow, int startColumn) method.
  • Loop through the worksheets in the CSV file.
  • In the loop, access the used range of the current worksheet using Worksheet.AllocatedRange property, and set ignore error option using CellRange.IgnoreErrorOptions property. Then autofit columns and rows using CellRange.AutoFitColumns() and CellRange.AutoFitRows() methods.
  • Save the CSV to Excel using Workbook.saveToFile(string fileName, ExcelVersion version) method.

Convert CSV to Excel using C#

using Spire.Xls;

namespace ConvertCsvToExcel
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create an instance of Workbook class
            Workbook workbook = new Workbook();

            //Load a CSV file
            workbook.LoadFromFile(@"ExcelToCSV.csv", ",", 1, 1);

            //Loop through the worksheets in the CSV file
            for (int i = 0; i < workbook.Worksheets.Count; i++)
            {
                Worksheet sheet = workbook.Worksheets[i];
                //Access the used range in each worksheet
                CellRange usedRange = sheet.AllocatedRange;
                //Ignore errors when saving numbers in the used range with text
                usedRange.IgnoreErrorOptions = IgnoreErrorType.NumberAsText;
                //Autofit columns and rows
                usedRange.AutoFitColumns();
                usedRange.AutoFitRows();
            }

            //Save the result file
            workbook.SaveToFile("CSVToExcel.xlsx", ExcelVersion.Version2013);
        }
    }
}

Convert CSV to Excel using VB.NET

Imports Spire.Xls

Namespace ConvertCsvToExcel
    Friend Class Program
        Private Shared Sub Main(ByVal args As String())
            'Create an instance of Workbook class
            Dim workbook As Workbook = New Workbook()

            'Load a CSV file
            workbook.LoadFromFile("ExcelToCSV.csv", ",", 1, 1)

            'Loop through the worksheets in the CSV file
            For i As Integer = 0 To workbook.Worksheets.Count - 1
                Dim sheet As Worksheet = workbook.Worksheets(i)
                'Access the used range in each worksheet
                Dim usedRange As CellRange = sheet.AllocatedRange
                'Ignore errors when saving numbers in the used range with text
                usedRange.IgnoreErrorOptions = IgnoreErrorType.NumberAsText
                'Autofit columns and rows
                usedRange.AutoFitColumns();
                usedRange.AutoFitRows();
            Next

            'Save the result file
            workbook.SaveToFile("CSVToExcel.xlsx", ExcelVersion.Version2013)
        End Sub
    End Class
End Namespace
Convert CSV to Excel using C# and VB.NET

See More

Product Page | Documentation | Examples | Forum |

Split a PDF into Multiple Files in Java

You might need to split a PDF into more than one file in certain circumstances, like if you have a big PDF and you want to send just a few pages to others. In this article, I will introduce how to split a PDF file into multiple files programmatically using Java.

In the following section, you will learn two scenarios to split a PDF file, they are:

  • Split a PDF File by Each Page
  • Split a PDF File by Page Ranges

Add Dependencies

In order to split PDF files, I will be using Spire.PDF for Java, which is an easy-to-use and multi-functional PDF library.

The jar file of Spire.PDF for Java can be downloaded from this link or installed from Maven by adding the following code to your maven-based 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.pdf</artifactId>
        <version>5.1.0</version>
    </dependency>
</dependencies>

Split a PDF File by Each Page in Java

To split a PDF file by each page, you just need to add two lines of code. The following are the steps to split a PDF file by each page:

  • Load a PDF file using PdfDocument class.
  • Split each page of the PDF into a separate file using the PdfDocument.split(String destFilePattern, int startNumber) method.

The following code example shows how to split a 5 pages PDF file by each page in Java:

import com.spire.pdf.PdfDocument;

public class SplitPdfByEachPage {
    public static void main(String []args){
        //Load a PDF file using PdfDocument class
        PdfDocument doc = new PdfDocument("Sample.pdf");

        //Split every page of the PDF into a separate file
        doc.split("Output/Split-{0}.pdf", 1);
        doc.close();
    }
}

Output:

Split a PDF file by each page in Java

Split a PDF File by Page Ranges in Java

Sometimes, you might want to split a PDF file by page ranges instead of each page. Let’s follow the following steps to split a PDF file by page ranges:

  • Load a PDF file using PdfDocument class.
  • Create a new PDF file using PdfDocument class
  • Import desired pages of the source PDF file to the new PDF file using PdfDocument.insertPageRange(PdfDocument doc, int startIndex, int endIndex) method.
  • Save the result PDF file using PdfDocument.saveToFile() method.

The following code shows how to split a 5 pages PDF file into two files – one with 2 pages, another with 3 pages:

import com.spire.pdf.PdfDocument;

public class SplitPdfByCertainPages {
    public static void main(String []args){
        //Load a PDF file using PdfDocument class
        PdfDocument doc = new PdfDocument("sample.pdf");

        //Create a new PDF file
        PdfDocument newDoc1 = new PdfDocument();
        //Import 1-2 pages of the source PDF to the new PDF
        newDoc1.insertPageRange(doc, 0, 1);
        //Save the PDF file
        newDoc1.saveToFile("Output/Doc1.pdf");

        //Create a new PDF file
        PdfDocument newDoc2 = new PdfDocument();
        //Import 3-5 pages of the source PDF to the new PDF
        newDoc2.insertPageRange(doc, 2, 4);
        //Save the PDF file
        newDoc2.saveToFile("Output/Doc2.pdf");
    }
}

Output:

Split a PDF file by certain pages in Java

Conclusion

In this article, I have demonstrated how to split PDF files in Java using Spire.PDF for Java library. Besides splitting PDF, the library can also be used to merge, convert, print, protect PDF files and many more. You can explore more about it by visiting the documentation.

Design a site like this with WordPress.com
Get started