We have already demonstrated how to use Spire.PDF to add multiple layers to PDF file and delete layer in PDF in C#. We can also toggle the visibility of a PDF layer while creating a new page layer with the help of Spire.PDF. In this section, we're going to demonstrate how to toggle the visibility of layers in new PDF document in C#.

Step 1: Create a new PDF document and add a new page to the PDF document.

PdfDocument pdf = new PdfDocument();
PdfPageBase page = pdf.Pages.Add();

Step 2: Add a layer named "Blue line" to the PDF page and set the layer invisible.

PdfPageLayer layer = page.PageLayers.Add("Blue line", false);
layer.Graphics.DrawLine(new PdfPen(PdfBrushes.Blue, 1), new PointF(0, 30), new PointF(300, 30));

Step 3: Add a layer named "Ellipse" to the PDF page and set the layer visible.

layer = page.PageLayers.Add("Ellipse", true);
PdfPen pen = new PdfPen(Color.Green, 1f);
PdfBrush brush = new PdfSolidBrush(Color.Green);
layer.Graphics.DrawEllipse(pen, brush, 50, 70, 200, 60);

Step 4: Save the document to file.

pdf.SaveToFile("LayerVisibility.pdf", FileFormat.PDF);

Effective screenshot after toggle the visibility of PDF layer:

How to toggle the visibility of PDF layer in C#

Full codes:

using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;


namespace LayerVisibility
{
    class Program
    {
        static void Main(string[] args)
        {
            PdfDocument pdf = new PdfDocument();
            PdfPageBase page = pdf.Pages.Add();

            PdfLayer layer = pdf.Layers.AddLayer("Green line", PdfVisibility.Off);
            PdfPen pen = new PdfPen(Color.Green, 1f);
            PdfCanvas pcA = layer.CreateGraphics(page.Canvas);
            pcA.DrawLine(pen,new PointF(0, 30), new PointF(300, 30));

            layer = pdf.Layers.AddLayer("Ellipse", PdfVisibility.On);
            PdfPen pen2 = new PdfPen(Color.Green, 1f);
            PdfBrush brush2 = new PdfSolidBrush(Color.Green);
            PdfCanvas pcB = layer.CreateGraphics(page.Canvas);
            pcB.DrawEllipse(pen2, brush2, 50, 70, 200, 60);


            pdf.SaveToFile("LayerVisibility.pdf", FileFormat.PDF);
        }
    }
}

Add Tab Stops to Word Paragraphs in C#

2017-09-18 09:28:27 Written by Koohji

Tab stops are markers placed on the ruler that define how text or numbers are aligned on a line. To add tab stops to a paragraph in Microsoft Word, we need to open the Tabs dialog box and then set the tab stop position, alignment and leader as shown below.

Add Tab Stops to Word Paragraphs in C#

This article elaborates how to add tab stops to paragraphs in word document programmatically using Spire.Doc.

Detail steps:

Step 1: Instantiate a Document object and add a section to it.

Document document = new Document();
Section section = document.AddSection(); 

Step 2: Add paragraph 1 to the section.

Paragraph paragraph1 = section.AddParagraph();

Step 3: Add tab stops to paragraph 1.

//Add tab and set its position (in points)
Tab tab = paragraph1.Format.Tabs.AddTab(28);
//Set tab alignment
tab.Justification = TabJustification.Left;
//move to next tab and append text
paragraph1.AppendText("\tWashing Machine");

//Add another tab and set its position (in points)
tab = paragraph1.Format.Tabs.AddTab(280);
//Set tab alignment
tab.Justification = TabJustification.Left;
//Specify tab leader type
tab.TabLeader = TabLeader.Dotted;
//move to next tab and append text
paragraph1.AppendText("\t$650"); 

Step 4: Add paragraph 2 to the section.

Paragraph paragraph2 = section.AddParagraph();

Step 5: Add tab stops to paragraph 2.

//Add tab and set its position (in points)
tab = paragraph2.Format.Tabs.AddTab(28);
//Set tab alignment
tab.Justification = TabJustification.Left;
//move to next tab and append text
paragraph2.AppendText("\tRefrigerator");

//Add another tab and set its position (in points)
tab = paragraph2.Format.Tabs.AddTab(280);
//Set tab alignment
tab.Justification = TabJustification.Left;
//Specify tab leader type
tab.TabLeader = TabLeader.NoLeader;
//move to next tab and append text
paragraph2.AppendText("\t$800"); 

Step 6: Save and close the document object.

document.SaveToFile("Tab.docx", FileFormat.Docx2013);
document.Close();

Screenshot:

Add Tab Stops to Word Paragraphs in C#

Full code:

using Spire.Doc;
using Spire.Doc.Documents;
namespace AddTapStops
{
 class Program
    {

     static void Main(string[] args)
     {
         //Instantiate a Document object
         Document document = new Document();
         //Add a section
         Section section = document.AddSection();

         //Add paragraph 1
         Paragraph paragraph1 = section.AddParagraph();

         //Add tab and set its position (in points)
         Tab tab = paragraph1.Format.Tabs.AddTab(28);
         //Set tab alignment
         tab.Justification = TabJustification.Left;
         //move to next tab and append text
         paragraph1.AppendText("\tWashing Machine");

         //Add another tab and set its position (in points)
         tab = paragraph1.Format.Tabs.AddTab(280);
         //Set tab alignment
         tab.Justification = TabJustification.Left;
         //Specify tab leader type
         tab.TabLeader = TabLeader.Dotted;
         //move to next tab and append text
         paragraph1.AppendText("\t$650");

         //Add paragraph 2
         Paragraph paragraph2 = section.AddParagraph();

         //Add tab and set its position (in points)
         tab = paragraph2.Format.Tabs.AddTab(28);
         //Set tab alignment
         tab.Justification = TabJustification.Left;
         //move to next tab and append text
         paragraph2.AppendText("\tRefrigerator"); //move to next tab and append text

         //Add another tab and set its position (in points)
         tab = paragraph2.Format.Tabs.AddTab(280);
         //Set tab alignment
         tab.Justification = TabJustification.Left;
         //Specify tab leader type
         tab.TabLeader = TabLeader.NoLeader;
         //move to next tab and append text
         paragraph2.AppendText("\t$800");

         //Save and close the document object            
         document.SaveToFile("Tab.docx", FileFormat.Docx2013);
         document.Close();

     }

    }
}

With Spire.XLS for .NET, we could easily add different kinds of charts to Excel worksheet, such as Pie Chart, Column Chart, bar chart, line chart, radar chart, Doughnut chart, pyramid chart, etc. In this article, we'll show you how to remove chart from Excel worksheet by using Spire.XLS.

Below is a sample document which contains a chart and table from the Excel worksheet, then we'll remove the chart from the slide.

How to remove chart from Excel worksheet in C#, VB.NET

C# Code Snippet of how to remove the chart from Excel:

Step 1: Create an instance of Excel workbook and load the document from file.

Workbook workbook = new Workbook();
workbook.LoadFromFile("Sample.xlsx");

Step 2: Get the first worksheet from the workbook.

Worksheet sheet = workbook.Worksheets[0];

Step 3: Get the first chart from the first worksheet.

IChartShape chart = sheet.Charts[0];

Step 4: Remove the chart.

chart.Remove();

Step 5: Save the document to file.

workbook.SaveToFile("RemoveChart.xlsx");

Effective screenshot:

How to remove chart from Excel worksheet in C#, VB.NET

Full code:

[C#]
using Spire.Xls;
using Spire.Xls.Core;
namespace RemoveChart
{

    class Program
    {

        static void Main(string[] args)
        {
            Workbook workbook = new Workbook();
            workbook.LoadFromFile("Sample.xlsx");
            Worksheet sheet = workbook.Worksheets[0];
            IChartShape chart = sheet.Charts[0];
            chart.Remove();
            workbook.SaveToFile("RemoveChart.xlsx");

        }

    }
}
[VB.NET]
Imports Spire.Xls
Imports Spire.Xls.Core
Namespace RemoveChart

	Class Program

		Private Shared Sub Main(args As String())
			Dim workbook As New Workbook()
			workbook.LoadFromFile("Sample.xlsx")
			Dim sheet As Worksheet = workbook.Worksheets(0)
			Dim chart As IChartShape = sheet.Charts(0)
			chart.Remove()
			workbook.SaveToFile("RemoveChart.xlsx")

		End Sub

	End Class
End Namespace

In Word, textbox can contain multiple elements such as text, image and table. This article demonstrates how to insert table into word textbox, and read and delete existing table from word textbox using Spire.Doc.

Insert table

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
namespace InsertTable
{
    class Program
    {

        static void Main(string[] args)
        {

            //Create a Document instance
            Document document = new Document();

            //Add a section
            Section section = document.AddSection();
            //Add a paragraph to the section
            Paragraph paragraph = section.AddParagraph();

            //Add textbox to the paragraph
            TextBox textbox = paragraph.AppendTextBox(300, 100);

            //Add text to textbox
            Paragraph textboxParagraph = textbox.Body.AddParagraph();
            TextRange textboxRange = textboxParagraph.AppendText("Table 1");
            textboxRange.CharacterFormat.FontName = "Arial";

            //Insert table to textbox
            Table table = textbox.Body.AddTable(true);
            //Specify the number of rows and columns of the table
            table.ResetCells(4, 4);

            string[,] data = new string[,]  
{  
{"Name","Age","Gender","ID" },  
{"John","28","Male","0023" },  
{"Steve","30","Male","0024" },  
{"Lucy","26","female","0025" }  
};

            //Add data to table 
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    TextRange tableRange = table[i, j].AddParagraph().AppendText(data[i, j]);
                    tableRange.CharacterFormat.FontName = "Arial";
                }
            }

            //Apply style to table
            table.ApplyStyle(DefaultTableStyle.LightGridAccent3);

            //Save the document
            document.SaveToFile("Output.docx", FileFormat.Docx2013);

        }
    }
}

Insert, Read and Delete Table from Word Textbox in C#

Read table

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.IO;
using System.Text;
namespace ReadTable
{
    class Program
    {

        static void Main(string[] args)
        {
            //Create a Document instance and load the word document
            Document document = new Document("Output.docx");

            //Get the first textbox
            TextBox textbox = document.TextBoxes[0];

            //Get the first table in the textbox
            Table table = textbox.Body.Tables[0] as Table;

            StringBuilder sb = new StringBuilder();

            //Loop through the paragraphs of the table and extract text to a .txt file
            foreach (TableRow row in table.Rows)
            {
                foreach (TableCell cell in row.Cells)
                {
                    foreach (Paragraph paragraph in cell.Paragraphs)
                    {
                        sb.AppendLine(paragraph.Text);
                    }
                }
            }
            File.WriteAllText("text.txt", sb.ToString());

        }
    }
}

Insert, Read and Delete Table from Word Textbox in C#

Delete table

using Spire.Doc;
using Spire.Doc.Fields;
namespace DeleteTable
{
    class Program
    {

        static void Main(string[] args)
        {
            //Create a Document instance and load the word document
            Document document = new Document("Output.docx");

            //Get the first textbox
            TextBox textbox = document.TextBoxes[0];

            //Remove the first table from the textbox
            textbox.Body.Tables.RemoveAt(0);

            //Save the document
            document.SaveToFile("RemoveTable.docx", FileFormat.Docx2013);


        }
    }
}

Insert, Read and Delete Table from Word Textbox in C#

When creating charts from scratch in PowerPoint slide, an Excel sheet with some dummy data will automatically be generated. The dummy data can be overwritten with the data from data source as well as from an existing Excel file. This article demonstrates how we can create chart in PowerPoint using the data in Excel file.

This solution requires Spire.Presneation.dll and Spire.Xls.dll to be added as references in project. Please download Spire.Office and reference the corresponding DLLs from it.

Here is the Excel sheet containing our sample data.

How to Create Chart Using Excel Data in PowerPoint in C#, VB.NET

Step 1: Create a Presentation document.

Presentation ppt = new Presentation();

Step 2: Append a column chart in the first slide.

RectangleF rect = new RectangleF(40, 100, 550, 320);
IChart chart = ppt.Slides[0].Shapes.AppendChart(ChartType.ColumnClustered, rect);

Step 3: Clear the default dummy data.

chart.ChartData.Clear(0, 0, 5, 5);

Step 4: Load an existing Excel file to Workbook instance and get the first worksheet.

Workbook wb = new Workbook();
wb.LoadFromFile("data.xlsx");
Worksheet sheet = wb.Worksheets[0];

Step 5: Import data from the worksheet to chart table.

for (int r = 0; r < sheet.AllocatedRange.RowCount; r++)
{
    for (int c = 0; c < sheet.AllocatedRange.ColumnCount; c++)
    {
        chart.ChartData[r, c].Value = sheet.Range[r + 1, c + 1].Value2;
    }
}

Step 6: Set the series label and categories labels.

chart.Series.SeriesLabel = chart.ChartData["B1", "B1"];
chart.Categories.CategoryLabels = chart.ChartData["A2","A13"];

Step 7: Set the series values.

chart.Series[0].Values = chart.ChartData["B2","B13"];

Step 8: Save the file.

ppt.SaveToFile("chart.pptx",Spire.Presentation.FileFormat.Pptx2013);

Output:

How to Create Chart Using Excel Data in PowerPoint in C#, VB.NET

Full Code:

[C#]
using System;
using Spire.Presentation;
using System.Drawing;
using Spire.Presentation.Charts;
using Spire.Xls;

namespace CreateChartFromExcelData
{
    class Program
    {
        static void Main(string[] args)
        {
            //initialize an instance of Presentation class
            Presentation ppt = new Presentation();
            RectangleF rect = new RectangleF(40, 100, 550, 320);
            IChart chart = ppt.Slides[0].Shapes.AppendChart(ChartType.ColumnClustered, rect);

            //clear the default dummy data
            chart.ChartData.Clear(0, 0, 5, 5);

            //load an existing Excel file to Workbook object
            Workbook wb = new Workbook();
            wb.LoadFromFile("data.xlsx");
            Worksheet sheet = wb.Worksheets[0];

            //import data from the sheet to chart table
            for (int r = 0; r < sheet.AllocatedRange.RowCount; r++)
            {
                for (int c = 0; c < sheet.AllocatedRange.ColumnCount; c++)
                {
                    chart.ChartData[r, c].Value = sheet.Range[r + 1, c + 1].Value2;
                }
            }

            //add chart title
            chart.ChartTitle.TextProperties.Text = "Monthly Sales Report";
            chart.ChartTitle.TextProperties.IsCentered = true;
            chart.ChartTitle.Height = 30;
            chart.HasTitle = true;

            //set the series label
            chart.Series.SeriesLabel = chart.ChartData["B1", "B1"];

            //set the category labels
            chart.Categories.CategoryLabels = chart.ChartData["A2","A13"];

            //set the series values
            chart.Series[0].Values = chart.ChartData["B2","B13"];

            //save the file
            ppt.SaveToFile("chart.pptx",Spire.Presentation.FileFormat.Pptx2013);
        }
    }
}
[VB.NET]
Imports Spire.Presentation
Imports System.Drawing
Imports Spire.Presentation.Charts
Imports Spire.Xls

Namespace CreateChartFromExcelData
    Class Program
        Private Shared Sub Main(args As String())
			'initialize an instance of Presentation class
			Dim ppt As New Presentation()

            Dim rect As New RectangleF(40, 100, 550, 320)

            Dim chart As IChart = ppt.Slides(0).Shapes.AppendChart(ChartType.ColumnClustered, rect)

			'clear the default dummy data
			chart.ChartData.Clear(0, 0, 5, 5)

			'load an existing Excel file to Workbook object
			Dim wb As New Workbook()

            wb.LoadFromFile("data.xlsx")
			Dim sheet As Worksheet = wb.Worksheets(0)

			'import data from the sheet to chart table
			For r As Integer = 0 To sheet.AllocatedRange.RowCount - 1
				For c As Integer = 0 To sheet.AllocatedRange.ColumnCount - 1

                    chart.ChartData(r, c).Value = sheet.Range(r + 1, c + 1).Value2
                Next

            Next

			'add chart title
			chart.ChartTitle.TextProperties.Text = "Monthly Sales Report"
			chart.ChartTitle.TextProperties.IsCentered = True
            chart.ChartTitle.Height = 30
			chart.HasTitle = True

			'set the series label

            chart.Series.SeriesLabel = chart.ChartData("B1", "B1")

			'set the category labels

            chart.Categories.CategoryLabels = chart.ChartData("A2", "A13")

			'set the series values

            chart.Series(0).Values = chart.ChartData("B2", "B13")

			'save the file
			ppt.SaveToFile("chart.pptx", Spire.Presentation.FileFormat.Pptx2013)
		End Sub

    End Class
End Namespace

With the help of Spire.XLS, we can set the conditional format the Excel cell in C# and VB.NET. We can also use Spire.XLS to remove the conditional format from a specific cell or the entire Excel worksheet. This article will demonstrate how to remove conditional format from Excel in C#.

Firstly, view the original Excel worksheet with conditional formats:

Remove conditional format from Excel in C#

Step 1: Create an instance of Excel workbook and load the document from file.

Workbook workbook = new Workbook();
workbook.LoadFromFile("Sample.xlsx");

Step 2: Get the first worksheet from the workbook.

Worksheet sheet = workbook.Worksheets[0];

Step 3: Remove the conditional formats from the specific Excel Cell B2.

sheet.Range["B2"].ConditionalFormats.Remove();

Step 4: Remove all the conditional formats from the whole Excel worksheet.

sheet.AllocatedRange.ConditionalFormats.Remove();

Step 5: Save the document to file.

workbook.SaveToFile("Result.xlsx", ExcelVersion.Version2010);

Remove the conditional format from a special Excel range B2:

Remove conditional format from Excel in C#

Remove all the conditional formats from the entire Excel worksheet:

Remove conditional format from Excel in C#

Full codes of how to remove the conditional formats from Excel worksheet:

using Spire.Xls;
namespace RemoveConditionalFormat
{
    class Program
    {
        static void Main(string[] args)
        {
            {

                Workbook workbook = new Workbook();
                workbook.LoadFromFile("Sample.xlsx");

                Worksheet sheet = workbook.Worksheets[0];

                //sheet.Range["B2"].ConditionalFormats.Remove();

                sheet.AllocatedRange.ConditionalFormats.Remove();

                workbook.SaveToFile("Result.xlsx", ExcelVersion.Version2010);
            }

        }
    }
}

In Spire.XLS, we can hide or show the headers of rows and columns by setting the RowColumnHeadersVisible property of XlsWorksheet class. This article elaborates the steps of how to accomplish this function using Spire.XLS.

The following screenshot shows the input file which contain one worksheet with row and column headers.

Hide or Show Row Column Headers in Excel with C#

Detail steps:

Step 1: Instantiate a Workbook instance and load the Excel file.

Workbook workbook = new Workbook();
workbook.LoadFromFile("Input.xlsx");

Step 2: Get the first worksheet.

Worksheet sheet = workbook.Worksheets[0];

Step 3: Hide or show the headers of rows and columns in the worksheet.

//Hide the headers of rows and columns
sheet.RowColumnHeadersVisible = false;

//Show the headers of rows and columns
//sheet.RowColumnHeadersVisible = true;

Step 4: Save the file.

workbook.SaveToFile("Output.xlsx");

The screenshot after hiding the row and column headers:

Hide or Show Row Column Headers in Excel with C#

Full code:

using Spire.Xls;
namespace ShowRowColumnHeader
{
    class Program
    {

        static void Main(string[] args)
        {
            // Instantiate a Workbook instance
            Workbook workbook = new Workbook();
            //Load the Excel file
            workbook.LoadFromFile("Input.xlsx");

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

            //Hide the headers of rows and columns
            sheet.RowColumnHeadersVisible = false;

            //Show the headers of rows and columns
            //sheet.RowColumnHeadersVisible = true;

            //Save the file
            workbook.SaveToFile("Output.xlsx");
        }
    }
}

In Excel, you can group related rows and columns to better organize and manage data. This feature is especially useful for simplifying complex spreadsheets, making data easier to navigate and analyze. When data is grouped, Excel provides the option to expand or collapse the groups, allowing users to control how much information is displayed at any given time. In this article, you will learn how to expand or collapse rows and columns in Excel in C# using Spire.XLS for .NET.

Install Spire.XLS for .NET

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

PM> Install-Package Spire.XLS

Expand Rows and Columns in Excel in C#

Expanding a group reveals all the detailed data within it. Using Spire.XLS for .NET, you can expand the grouped columns or rows through the Worksheet.Range[].ExpandGroup() method. The following are the detailed steps:

  • Create a Workbook instance.
  • Load an Excel file using Workbook.LoadFromFile() method.
  • Get a specified worksheet using Workbook.Worksheets[] property.
  • Expand the grouped rows using Worksheet.Range[].ExpandGroup(GroupByType.ByRows) method.
  • Expand the grouped columns using Worksheet.Range[].ExpandGroup(GroupByType.ByColumns) method.
  • Save the result file using Workbook.SaveToFile() method.
  • C#
using Spire.Xls;
namespace ExpandandExcelGroups
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create Workbook instance
            Workbook workbook = new Workbook();

            // Load an Excel file
            workbook.LoadFromFile("Grouped.xlsx");

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

            // Expand the grouped rows
            sheet.Range["A2:G6"].ExpandGroup(GroupByType.ByRows, ExpandCollapseFlags.ExpandParent);
            sheet.Range["A8:G13"].ExpandGroup(GroupByType.ByRows);

            // Expand the grouped columns
            sheet.Range["D2:G13"].ExpandGroup(GroupByType.ByColumns);

            // Save the result file
            workbook.SaveToFile("ExpandGroups.xlsx");
        }
    }
}

Expand all the grouped rows and columns in Excel

Collapse Rows and Columns in Excel in C#

Collapsing a group hides the information, showing only the summary or parent data. To collapse rows or columns in Excel, you can use the Worksheet.Range[].CollapseGroup() method. The following are the detailed steps:

  • Create a Workbook instance.
  • Load an Excel file using Workbook.LoadFromFile() method.
  • Get a specified worksheet using Workbook.Worksheets[] property.
  • Collapse the rows using Worksheet.Range[].CollapseGroup(GroupByType.ByRows) method.
  • Collapse the columns using Worksheet.Range[].CollapseGroup(GroupByType.ByColumns) method.
  • Save the result file using Workbook.SaveToFile() method.
  • C#
using Spire.Xls;
namespace CollapseExcelGroups
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create Workbook instance
            Workbook workbook = new Workbook();

            // Load an Excel file
            workbook.LoadFromFile("ExpandGroups.xlsx");

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

            // Collapse rows
            sheet.Range["A2:A6"].CollapseGroup(GroupByType.ByRows);

            // Collapse Columns
            sheet.Range["D1:G1"].CollapseGroup(GroupByType.ByColumns);

            // Save the result file
            workbook.SaveToFile("CollapseGroups.xlsx");
        }
    }
}

Collapse the specified rows and columns 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 22