Extracting tables from PDFs and converting them into Excel format offers numerous advantages, such as enabling data manipulation, analysis, and visualization in a more versatile and familiar environment. This task is particularly valuable for researchers, analysts, and professionals dealing with large amounts of tabular data. In this article, you will learn how to extract tables from PDF to Excel in C# and VB.NET using Spire.Office for .NET.

Install Spire.Office for .NET

To begin with, you need to add the Spire.Pdf.dll and the Spire.Xls.dll included in the Spire.Office for.NET package as references in your .NET project. Spire.PDF is responsible for extracting data from PDF tables, and Spire.XLS is responsible for creating an Excel document based on the data obtained from PDF.

The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.Office

Extract Tables from PDF to Excel in C#, VB.NET

Spire.PDF for .NET offers the PdfTableExtractor.ExtractTable(int pageIndex) method to extract tables from a specific page of a searchable PDF document. The text of a specific cell can be accessed using PdfTable.GetText(int rowIndex, int columnIndex) method. This value can be then written to a worksheet through Worksheet.Range[int row, int column].Value property offered by Spire.XLS for .NET.  The following are the detailed steps.

  • Create an instance of PdfDocument class.
  • Load the sample PDF document using PdfDocument.LoadFromFile() method.
  • Extract tables from a specific page using PdfTableExtractor.ExtractTable() method.
  • Get text of a certain table cell using PdfTable.GetText() method.
  • Create a Workbook object.
  • Write the cell data obtained from PDF into a worksheet through Worksheet.Range.Value property.
  • Save the workbook to an Excel file using Workbook.SaveTofile() method.

The following code example extracts all tables from a PDF document and writes each of them into an individual worksheet within a workbook.

  • C#
  • VB.NET
using Spire.Pdf;
using Spire.Pdf.Utilities;
using Spire.Xls;

namespace ExtractTablesToExcel
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument object
            PdfDocument doc = new PdfDocument();

            //Load the sample PDF file
            doc.LoadFromFile(@"C:\Users\Administrator\Desktop\table.pdf");

            //Create a Workbook object
            Workbook workbook = new Workbook();

            //Clear default worksheets
            workbook.Worksheets.Clear();

            //Initialize an instance of PdfTableExtractor class
            PdfTableExtractor extractor = new PdfTableExtractor(doc);

            //Declare a PdfTable array 
            PdfTable[] tableList = null;

            int sheetNumber = 1;

            //Loop through the pages 
            for (int pageIndex = 0; pageIndex < doc.Pages.Count; pageIndex++)
            {
                //Extract tables from a specific page
                tableList = extractor.ExtractTable(pageIndex);

                //Determine if the table list is null
                if (tableList != null && tableList.Length > 0)
                {
                    //Loop through the table in the list
                    foreach (PdfTable table in tableList)
                    {
                        //Add a worksheet
                        Worksheet sheet = workbook.Worksheets.Add(String.Format("sheet{0}", sheetNumber));

                        //Get row number and column number of a certain table
                        int row = table.GetRowCount();
                        int column = table.GetColumnCount();

                        //Loop though the row and colunm 
                        for (int i = 0; i < row; i++)
                        {
                            for (int j = 0; j < column; j++)
                            {
                                //Get text from the specific cell
                                string text = table.GetText(i, j);

                                //Write text to a specified cell
                                sheet.Range[i + 1, j + 1].Value = text;
                            }
              
                        }
                        sheetNumber++;
                    }
                }
            }

            //Save to file
            workbook.SaveToFile("ToExcel.xlsx", ExcelVersion.Version2013);
        }
    }
}

C#/VB.NET: Extract Tables from PDF to 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.

C#/VB.NET: Create Column Charts in Word

2023-07-05 01:15:47 Written by Koohji

A clustered column chart and a stacked column chart are two variations of the column chart. The clustered column chart enables simple comparison of values across different categories, whereas the stacked column chart displays both the total value of different categories and the proportion of each individual component. In this article, you will learn how to create clustered or stacked column charts in Word in C# and VB.NET using Spire.Doc for .NET.

Install Spire.Doc for .NET

To begin with, you need to add the DLL files included in the Spire.Doc 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.Doc

Create a Clustered Column Chart in Word in C#, VB.NET

To add a chart to a Word document, use Paragraph.AppenChart(ChartType chartType, float width, float height) method. The ChartType enumeration includes various chart types predefined in MS Word. The following are the steps to add a clustered column chart in Word using Spire.Doc for .NET.

  • Create a Document object.
  • Add a section and a paragraph.
  • Add a column chart to the paragraph using Paragraph.AppendChart() method.
  • Add series to the chart using Chart.Series.Add() method.
  • Set the chart title through Chart.Tilte.Text property.
  • Save the document to a Word file using Document.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields.Shapes.Charts;
using Spire.Doc.Fields;

namespace CreateColumnChart
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document object
            Document document = new Document();

            //Add a section
            Section section = document.AddSection();

            //Add a paragraph
            Paragraph paragraph = section.AddParagraph();

            //Add a column chart
            ShapeObject shape = paragraph.AppendChart(ChartType.Column, 450, 250);

            //Get the chart
            Chart chart = shape.Chart;

            //Clear the default data
            chart.Series.Clear();

            //Add a series including series name, category names, and series values to chart
            chart.Series.Add("June",
                new[] { "Cuba", "Mexico", "France", "Germany" },
                new double[] { 5000, 8000, 9000, 8500 });

            //Add another series
            chart.Series.Add("July",
            new[] { "Cuba", "Mexico", "France", "Germany" },
            new double[] { 3000, 5000, 7000, 6000 });

            //Set the chart title
            chart.Title.Text = "Sales by Country";

            //Set the number format of the Y-axis 
            chart.AxisY.NumberFormat.FormatCode = "#,##0";

            //Set the legend position
            chart.Legend.Position = LegendPosition.Bottom;

            //Save to file
            document.SaveToFile("ClusteredColumnChart.docx", FileFormat.Docx2019);
        }
    }
}

C#/VB.NET: Create Column Charts in Word

Create a Stacked Column Chart in Word in C#, VB.NET

A stacked column chart can be created pretty much in the same way as a clustered column chart. The only difference is you have to change the chart type from Column to ColumnStacked.

  • Create a Document object.
  • Add a section and a paragraph.
  • Add a stacked column chart to the paragraph using Paragraph.AppendChart() method.
  • Add series to the chart using Chart.Series.Add() method.
  • Set the chart title through Chart.Tilte.Text property.
  • Save the document to a Word file using Document.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields.Shapes.Charts;
using Spire.Doc.Fields;

namespace StackedColumnChart
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document object
            Document document = new Document();

            //Add a section
            Section section = document.AddSection();

            //Add a paragraph
            Paragraph paragraph = section.AddParagraph();

            //Add a stacked column chart
            ShapeObject shape = paragraph.AppendChart(ChartType.ColumnStacked, 450, 250);

            //Get the chart
            Chart chart = shape.Chart;

            //Clear the default data
            chart.Series.Clear();

            //Add a series including series name, category names, and series values to chart
            chart.Series.Add("Store A",
                new[] { "Diet Coke", "Mountain Dew", "Diet Pesi", "Cherry Coke" },
                new double[] { 2500, 4600, 2800, 5100 });

            //Add another series
            chart.Series.Add("Store B",
            new[] { "Diet Coke", "Mountain Dew", "Diet Pesi", "Cherry Coke" },
            new double[] { 4100, 3200, 3800, 4000 });

            //Set the chart title
            chart.Title.Text = "Store Wise Soda Soft Drink Sales";

            //Set the number format of the Y-axis 
            chart.AxisY.NumberFormat.FormatCode = "#,##0";

            //Set the legend position
            chart.Legend.Position = LegendPosition.Bottom;

            //Save to file
            document.SaveToFile("StackedColumnChart.docx", FileFormat.Docx2019);
        }
    }
}

C#/VB.NET: Create Column Charts in Word

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.

Named ranges in Excel are valuable tools that empower you to assign meaningful names to specific cells or ranges within your spreadsheets. Instead of relying on traditional cell references like A1:B10, named ranges allow you to reference data by their logical names, making your formulas more intelligible and easier to understand and maintain. This article will demonstrate how to create, edit or delete named ranges in Excel in Java using Spire.XLS for Java.

Install Spire.XLS for Java

First of all, you're required to add the Spire.Xls.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.xls</artifactId>
        <version>15.12.15</version>
    </dependency>
</dependencies>

Create a Named Range in Excel in Java

You can use the Workbook.getNameRanges().add(String name) method provided by Spire.XLS for Java to add a named range to an Excel workbook. Once the named range is added, you can define the cell or range of cells it refers to using the INamedRange.setRefersToRange(IXLSRange range) method. The detailed steps are as follows:

  • Initialize an instance of the Workbook class.
  • Load an Excel workbook using the Workbook.loadFromFile() method.
  • Add a named range to the workbook using the Workbook.getNameRanges().add(String name) method.
  • Get a specific worksheet in the workbook using the Workbook.getWorksheets().get(int index) method.
  • Set the cell range that the named range refers to using the INamedRange.setRefersToRange(IXLSRange range) method.
  • Save the result file using the Workbook.saveToFile() method.
  • Java
import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
import com.spire.xls.core.INamedRange;

public class CreateNamedRange {
    public static void main(String[] args) {
        //Initialize an instance of the Workbook class
        Workbook workbook = new Workbook();
        //Load an Excel workbook
        workbook.loadFromFile("Sample.xlsx");

        //Add a named range to the workbook
        INamedRange namedRange = workbook.getNameRanges().add("Amount");

        //Get a specific worksheet in the workbook
        Worksheet sheet = workbook.getWorksheets().get(0);

        //Set the cell range that the named range references
        namedRange.setRefersToRange(sheet.getCellRange("D2:D5"));

        //Save the result file to a specific location
        String result = "CreateNamedRange.xlsx";
        workbook.saveToFile(result, ExcelVersion.Version2013);
        workbook.dispose();
    }
}

Java: Create, Edit or Delete Named Ranges in Excel

Edit an Existing Named Range in Excel in Java

After you've created a named range, you may want to modify its name or adjust the cells it refers to. The following are the detailed steps:

  • Initialize an instance of the Workbook class.
  • Load an Excel workbook using the Workbook.loadFromFile() method.
  • Get a specific named range in the workbook using the Workbook.getNameRanges().get(int index) method.
  • Modify the name of the named range using the INamedRange.setName(String name) method.
  • Modify the cells that the named range refers to using the INamedRange.setRefersToRange(IXLSRange range) method.
  • Save the result file using the Workbook.saveToFile() method.
  • Java
import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
import com.spire.xls.core.INamedRange;

public class ModifyNamedRange {
    public static void main(String[] args) {
        //Initialize an instance of the Workbook class
        Workbook workbook = new Workbook();
        //Load an Excel workbook
        workbook.loadFromFile("CreateNamedRange.xlsx");

        //Get a specific named range in the workbook
        INamedRange namedRange = workbook.getNameRanges().get(0);

        //Change the name of the named range
        namedRange.setName("MonitorAmount");

        //Set the cell range that the named range references
        namedRange.setRefersToRange(workbook.getWorksheets().get(0).getCellRange("D2"));

        //Save the result file to a specific location
        String result = "ModifyNamedRange.xlsx";
        workbook.saveToFile(result, ExcelVersion.Version2013);
        workbook.dispose();
    }
}

Java: Create, Edit or Delete Named Ranges in Excel

Delete a Named Range from Excel in Java

If you have made significant changes to the structure or layout of your spreadsheet, it might be necessary to delete a named range that is no longer relevant or accurate. The detailed steps are as follows:

  • Initialize an instance of the Workbook class.
  • Load an Excel workbook using the Workbook.loadFromFile() method.
  • Remove a specific named range by its index or name using the Workbook.getNameRanges().removeAt(int index) or Workbook.getNameRanges().remove(string name) method.
  • Save the result file using the Workbook.saveToFile() method.
  • Java
import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;

public class DeleteNamedRange {
    public static void main(String[] args) {
        //Initialize an instance of the Workbook class
        Workbook workbook = new Workbook();
        //Load an Excel workbook
        workbook.loadFromFile("CreateNamedRange.xlsx");

        //Remove a specific named range by its index
        workbook.getNameRanges().removeAt(0);

        //Remove a specific named range by its name
        //workbook.getNameRanges().remove("Amount");

        //Save the result file to a specific location
        String result = "RemoveNamedRange.xlsx";
        workbook.saveToFile(result, ExcelVersion.Version2013);
        workbook.dispose();
    }
}

Java: Create, Edit or Delete Named Ranges 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.

page 80