MS Excel contains a set of Windows Forms controls that can be used to Worksheet to host item. Spire.XLS also provides programmers similar features to add controls on Worksheet at runtime without installing any other control program. In this article, I'll introduce you how to insert TextBox, CheckBox and RadioButton into Worksheet via Spire.XLS in C#, VB.NET.

Detailed Steps:

Step 1: Download Spire.XLS and reference dll file to your VS project.

Step 2: Use Spire.Xls.Core as namespace, which contains all the interfaces like ITextBoxShap, ICheckBox, IRadioButton and etc.

Step 3: Initialize a new instance of Workbook and create a Worksheet in it.

Workbook wb = new Workbook();
Worksheet ws = wb.Worksheets[0];

Step 4: Insert a TextBox at specified location and input the display text.

ITextBoxShape textbox = ws.TextBoxes.AddTextBox(2, 2, 15, 100);
textbox.Text = "Hello World";

Step 5: Insert three CheckBox into Worksheet at different locations. Set the CheckState and display text.

ICheckBox cb = ws.CheckBoxes.AddCheckBox(4, 2, 15, 100);
cb.CheckState = CheckState.Checked;
cb.Text = "Check Box 1";

cb = ws.CheckBoxes.AddCheckBox(4, 4, 15, 100);
cb.CheckState = CheckState.Checked;
cb.Text = "Check Box 2";

cb = ws.CheckBoxes.AddCheckBox(4, 6, 15, 100);
cb.CheckState = CheckState.Checked;
cb.Text = "Check Box 3";

Step 6: Insert three RadioButton and set the related properties.

IRadioButton rb = ws.RadioButtons.Add(6, 2, 15, 100);
rb.Text = "Option 1";

rb = ws.RadioButtons.Add(8, 2, 15, 100);
rb.CheckState = CheckState.Checked;
rb.Text = "Option 2";

rb = ws.RadioButtons.Add(10, 2, 15, 100);
rb.Text = "Option 3";

Step 7: Save the file.

ws.DefaultRowHeight = 15;
wb.SaveToFile("Result.xlsx", ExcelVersion.Version2010);

Output:

How to Insert Controls to Worksheet in C#, VB.NET

Full Code:

[C#]
using Spire.Xls;
using Spire.Xls.Core;
namespace InsertControl
{
    class Program
    {
        static void Main(string[] args)
        {
            Workbook wb = new Workbook();
            Worksheet ws = wb.Worksheets[0];

            ITextBoxShape textbox = ws.TextBoxes.AddTextBox(2, 2, 15, 100);
            textbox.Text = "Hello World";

            ICheckBox cb = ws.CheckBoxes.AddCheckBox(4, 2, 15, 100);
            cb.CheckState = CheckState.Checked;
            cb.Text = "Check Box 1";

            cb = ws.CheckBoxes.AddCheckBox(4, 4, 15, 100);
            cb.CheckState = CheckState.Checked;
            cb.Text = "Check Box 2";

            cb = ws.CheckBoxes.AddCheckBox(4, 6, 15, 100);
            cb.CheckState = CheckState.Checked;
            cb.Text = "Check Box 3";

            IRadioButton rb = ws.RadioButtons.Add(6, 2, 15, 100);
            rb.Text = "Option 1";

            rb = ws.RadioButtons.Add(8, 2, 15, 100);
            rb.CheckState = CheckState.Checked;
            rb.Text = "Option 2";

            rb = ws.RadioButtons.Add(10, 2, 15, 100);
            rb.Text = "Option 3";

            ws.DefaultRowHeight = 15;
            wb.SaveToFile("Result.xlsx", ExcelVersion.Version2010);
        }
    }
}
[VB.NET]
Imports Spire.Xls
Imports Spire.Xls.Core
Namespace InsertControl
	Class Program
		Private Shared Sub Main(args As String())
			Dim wb As New Workbook()
			Dim ws As Worksheet = wb.Worksheets(0)

			Dim textbox As ITextBoxShape = ws.TextBoxes.AddTextBox(2, 2, 15, 100)
			textbox.Text = "Hello World"

			Dim cb As ICheckBox = ws.CheckBoxes.AddCheckBox(4, 2, 15, 100)
			cb.CheckState = CheckState.Checked
			cb.Text = "Check Box 1"

			cb = ws.CheckBoxes.AddCheckBox(4, 4, 15, 100)
			cb.CheckState = CheckState.Checked
			cb.Text = "Check Box 2"

			cb = ws.CheckBoxes.AddCheckBox(4, 6, 15, 100)
			cb.CheckState = CheckState.Checked
			cb.Text = "Check Box 3"

			Dim rb As IRadioButton = ws.RadioButtons.Add(6, 2, 15, 100)
			rb.Text = "Option 1"

			rb = ws.RadioButtons.Add(8, 2, 15, 100)
			rb.CheckState = CheckState.Checked
			rb.Text = "Option 2"

			rb = ws.RadioButtons.Add(10, 2, 15, 100)
			rb.Text = "Option 3"

			ws.DefaultRowHeight = 15
			wb.SaveToFile("Result.xlsx", ExcelVersion.Version2010)
		End Sub
	End Class
End Namespace

Sparkline is a tiny chart that can be inserted in cells to represent the trends in a series of values. Spire.XLS enables programmers to select a data cell range and display sparkline in another cell, usually next to data range. This article gives an example in C# and VB.NET to show how this purpose is achieved with a few lines of code.

Code Snippet:

Step 1: Create a new Workbook and load the sample file.

Workbook wb = new Workbook();
wb.LoadFromFile("sample.xlsx");

Step 2: Get the Worksheet from Workbook.

Worksheet ws = wb.Worksheets[0];

Step 3: Set SparklineType as line and apply line sparkline to SparklineCollection.

SparklineGroup sparklineGroup = ws.SparklineGroups.AddGroup(SparklineType.Line);
SparklineCollection sparklines = sparklineGroup.Add();

Step 4: Call SparklineCollection.Add(DateRange, ReferenceRange) mothed get data from a range of cells and display sparkline chart inside another cell, e.g., a sparkline in E2 shows the trend of values from A2 to D2.

sparklines.Add(ws["A2:D2"], ws["E2"]);
sparklines.Add(ws["A3:D3"], ws["E3"]);
sparklines.Add(ws["A4:D4"], ws["E4"]);
sparklines.Add(ws["A5:D5"], ws["E5"]);

Step 5: Save the file.

wb.SaveToFile("output.xlsx",ExcelVersion.Version2010);

Output:

How to Insert Sparkline in Excel in C#, VB.NET

Full Code:

[C#]
using Spire.Xls;
namespace InsertSparkline
{
    class Program
    {

        static void Main(string[] args)
        {

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

            SparklineGroup sparklineGroup = ws.SparklineGroups.AddGroup(SparklineType.Line);
            SparklineCollection sparklines = sparklineGroup.Add();
            sparklines.Add(ws["A2:D2"], ws["E2"]);
            sparklines.Add(ws["A3:D3"], ws["E3"]);
            sparklines.Add(ws["A4:D4"], ws["E4"]);
            sparklines.Add(ws["A5:D5"], ws["E5"]);

            wb.SaveToFile("output.xlsx", ExcelVersion.Version2010);

        }
    }
}
[VB.NET]
Imports Spire.Xls
Namespace InsertSparkline
	Class Program

		Private Shared Sub Main(args As String())

			Dim wb As New Workbook()
			wb.LoadFromFile("sample.xlsx")
			Dim ws As Worksheet = wb.Worksheets(0)

			Dim sparklineGroup As SparklineGroup = ws.SparklineGroups.AddGroup(SparklineType.Line)
			Dim sparklines As SparklineCollection = sparklineGroup.Add()
			sparklines.Add(ws("A2:D2"), ws("E2"))
			sparklines.Add(ws("A3:D3"), ws("E3"))
			sparklines.Add(ws("A4:D4"), ws("E4"))
			sparklines.Add(ws("A5:D5"), ws("E5"))

			wb.SaveToFile("output.xlsx", ExcelVersion.Version2010)

		End Sub
	End Class
End Namespace

C#: Expand or Collapse Bookmarks in PDF

2024-08-13 08:49:00 Written by Koohji

In PDF documents, the functionality to expand or collapse bookmarks helps provide a more organized and easier to navigate layout. When bookmarks are expanded, the entire hierarchy of bookmarks is visible at once, giving a comprehensive view of the document's structure and aiding in comprehension of its flow. Conversely, the option to collapse bookmarks is beneficial for users who prefer a less detailed view, as it allows them to focus on a specific part of the document without being distracted by the entire hierarchy. This article will introduce how to expand or collapse bookmarks in PDF in C# using Spire.PDF for .NET.

Install Spire.PDF for .NET

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

Expand or Collapse a Specific Bookmark in PDF in C#

Spire.PDF for .NET provides the PdfBookmark.ExpandBookmark property to expand or collapse a specified bookmark in PDF. The following are the detailed steps.

  • Create a PdfDocument object.
  • Load a PDF file using PdfDocument.LoadFromFile() method.
  • Get a specified bookmark in the PDF file using PdfDocument.Bookmarks[] property.
  • Expand the bookmark by setting the PdfBookmark.ExpandBookmark property to true. Or set it to false to collapses the bookmark.
  • Save the result PDF file using PdfDocument.SaveToFile() method.
  • C#
using Spire.Pdf;
using Spire.Pdf.Bookmarks;

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

            //Load a PDF file
            pdf.LoadFromFile("AnnualReport.pdf");

            //Get a specified bookmark
            PdfBookmark bookmarks = pdf.Bookmarks[3];

            //Expand the bookmark
            bookmarks.ExpandBookmark = true;
            //collapse the bookmark
            //bookmarks.ExpandBookmark = false;


            //Save the result file
            pdf.SaveToFile("ExpandSpecifiedBookmark.pdf");
        }
    }
}

C#: Expand or Collapse Bookmarks in PDF

Expand or Collapse All Bookmarks in PDF in C#

You can also iterate through all the bookmarks in a PDF file, and then expand or collapse each bookmark through the PdfBookmark.ExpandBookmark property. The following are the detailed steps.

  • Create a PdfDocument object.
  • Load a PDF file using PdfDocument.LoadFromFile() method.
  • Iterate through all bookmarks in the PDF file.
  • Expand each bookmark by setting the PdfBookmark.ExpandBookmark property to true. Or set it to false to collapses each bookmark.
  • Save the result PDF file using PdfDocument.SaveToFile() method.
  • C#
using Spire.Pdf;
using Spire.Pdf.Bookmarks;

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

            //Load a PDF file
            pdf.LoadFromFile("AnnualReport.pdf");

            //Loop through all bookmarks and expand them
            foreach (PdfBookmark bookmark in pdf.Bookmarks)
            {
               bookmark.ExpandBookmark = true;
            }
            //Save the result file
            pdf.SaveToFile("ExpandAllBookmarks.pdf");
        }
    }
}

C#: Expand or Collapse Bookmarks in PDF

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 250