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
Wednesday, 07 January 2015 08:30

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

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
Tuesday, 13 August 2024 08:49

C#: Expand or Collapse Bookmarks in PDF

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.

Wednesday, 31 December 2014 07:04

Create PDF Document Thumbnail via PDFViewer

PDF document thumbnails, appearing in the left-hand corner of a PDF reader, provide miniature previews of document pages. The 'Thumbnail View' pane allows you to view page thumbnails of all the pages included in the PDF document and you can simply click on the thumbnail to quickly travel to that page.

This article presents how to create a PDF document thumbnail view in your own .NET PDF viewer via Spire.PDFViewer. Detailed steps are as below:

Step 1: Create a new project in Windows Forms Application.

Step 2: Add Spire.PDFViewer control to VS Toolbox.

Create PDF Document Thumbnail via PDFViewer

Step 3: Design Form1.

  • Add a ToolStrip to Form1. Insert two Buttons, one TextBox and one Label to ToolStrip.
  • Drag PdfDocumentThumbnail and PdfDocumentViewer control from Toolbox to Form1.
  • Set properties of the Buttons, Labels, TextBox, PdfDocumentThumbnail and PdfDocumentViewer as below.

Tools

Properties

As

ToolStripButton1 Name btnOpen
DisplayStyle Image
ToolStripButton2 Name btnThumbnailRatio
DisplayStyle Image
ToolStripLabel1 Name toolStripLabel1
DisplayStyle Text
Text Ratio
ToolStripTextBox Name txtThumbnailRatio
PdfDocumentThumbnail Name pdfDocumentThumbnail1
Dock Fill
PdfDocumentViewer Name pdfDocumentViewer1
Dock Full

Step 4: Set code for btnOpen and btnThumbnailRatio.

1) Create a OpenFileDailog in btnOpen click event which enables you to open a file with correct type.

    private void btnOpen_Click(object sender, EventArgs e)
 {
     OpenFileDialog dialog = new OpenFileDialog();
     dialog.Filter = "PDF document(*.pdf)|*.pdf";
     DialogResult result = dialog.ShowDialog();
     if (result == DialogResult.OK)
     {
         string pdfDocument = dialog.FileName;
         this.pdfDocumentViewer1.LoadFromFile(pdfDocument);
     }

 }

2) Set zoom ratio for thumbnail view mode.

  private void btnThumbnailRatio_Click(object sender, EventArgs e)
  {
      if (this.pdfDocumentViewer1.IsDocumentLoaded)
     {
          int percent = 0;
          bool isNumeric = int.TryParse(this.txtThumbnailRatio.Text, out percent);
          if (isNumeric)
          {
              this.pdfDocumentThumbnail1.ZoomPercent =Math.Abs(percent);
          }
      }
  }

Run this program and open an existing PDF document, you'll get following output:

Create PDF Document Thumbnail via PDFViewer

Monday, 29 December 2014 08:07

Formatting text in Excel in C#

Spire.XLS can help developers to write rich text into the spreadsheet easily, such as set the text in bold, italic, and set the colour and font for them. We have already shown you how to set Subscript and Superscript in Excel files by using Spire.XLS. This article will focus on demonstrate how to formatting text in Excel sheet in C#.

Spire.Xls offers ExcelFont class and developers can format the text of cells easily. By using RichText, we can add the text into cells and set font for it. Here comes to the steps of how to write rich text into excel cells.

Step 1: Create an excel document and get its first worksheet.

Workbook workbook = new Workbook();
Worksheet sheet = workbook.Worksheets[0];

Step 2: Create the fonts and sent the format for each font.

//create a font and set the format to bold.
ExcelFont fontBold = workbook.CreateFont();
fontBold.IsBold = true;

//create a font and set the format to underline.              
ExcelFont fontUnderline = workbook.CreateFont();
fontUnderline.Underline = FontUnderlineType.Single;

//create a font and set the format to italic.            
ExcelFont fontItalic = workbook.CreateFont();
fontItalic.IsItalic = true;

//create a font and set the color to green.            
ExcelFont fontColor = workbook.CreateFont();
fontColor.KnownColor = ExcelColors.Green;

Step 3: Set the font for specified cell range.

RichText richText = sheet.Range["A1"].RichText;
richText.Text="It is in Bold";
richText.SetFont(0, richText.Text.Length-1, fontBold);
  
richText = sheet.Range["A2"].RichText;
richText.Text = "It is underlined";
richText.SetFont(0, richText.Text.Length-1, fontUnderline);

richText = sheet.Range["A3"].RichText;
richText.Text = "It is Italic";
richText.SetFont(0, richText.Text.Length - 1, fontItalic);

richText = sheet.Range["A4"].RichText;
richText.Text = "It is Green";
richText.SetFont(0, richText.Text.Length - 1, fontColor);

Step 4: Save the document to file.

workbook.SaveToFile("Sample.xls",ExcelVersion.Version97to2003);

Effective screenshot of writing rich text into excel worksheet:

Formatting text in Excel in C#

Full codes:

using Spire.Xls;
namespace WriteRichtextinExcel
{
    class Program
    {
        static void Main(string[] args)
        {
            Workbook workbook = new Workbook();
            Worksheet sheet = workbook.Worksheets[0];

            ExcelFont fontBold = workbook.CreateFont();
            fontBold.IsBold = true;

            ExcelFont fontUnderline = workbook.CreateFont();
            fontUnderline.Underline = FontUnderlineType.Single;

            ExcelFont fontItalic = workbook.CreateFont();
            fontItalic.IsItalic = true;

            ExcelFont fontColor = workbook.CreateFont();
            fontColor.KnownColor = ExcelColors.Green;

            RichText richText = sheet.Range["A1"].RichText;
            richText.Text = "It is in Bold";
            richText.SetFont(0, richText.Text.Length - 1, fontBold);

            richText = sheet.Range["A2"].RichText;
            richText.Text = "It is underlined";
            richText.SetFont(0, richText.Text.Length - 1, fontUnderline);

            richText = sheet.Range["A3"].RichText;
            richText.Text = "It is Italic";
            richText.SetFont(0, richText.Text.Length - 1, fontItalic);

            richText = sheet.Range["A4"].RichText;
            richText.Text = "It is Green";
            richText.SetFont(0, richText.Text.Length - 1, fontColor);

            workbook.SaveToFile("Sample.xls", ExcelVersion.Version97to2003);

        }
    }
}
Thursday, 25 December 2014 08:32

How to Zoom Word Document using Spire.DocViewer

Zoom is a basic as well useful function in any text file reader. Users can chose to zoom out or zoom in of a document depending on how small the font is, or on their own view preference. In this article, I’ll introduce two ways to zoom Word file using Spire.DocViewer:

  • Zoom with a particular zoom mode
  • Zoom by entering a percentage

Now, I will explain more by creating a Windows Forms Application. Some steps about how to add DocVierwer control to toolbox and how to open Word file using Spire.DocViewer have been demonstrated previously. In the following section, I only add a MenuItem and a TextBox to Form1.

In the MenuItem, named Zoom, I create three sub-items which are used to save three particular zoom modes.

  • Default: View page in its original size.
  • Fit to page: When this option is selected, the document will be resized to match the dimensions of your view window.
  • Fit to width: When this option is selected, the document will best fit to width of window.

Code behind:

        private void defaultToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.docDocumentViewer1.ZoomMode = ZoomMode.Default;
        }
        private void fitToPageToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.docDocumentViewer1.ZoomMode = ZoomMode.FitPage;
        }
        private void fitToWidthToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.docDocumentViewer1.ZoomMode = ZoomMode.FitWidth;
        }

Another way to zoom in or out of Word document is enter a desired percentage in TextBox.

Code for TextBox:

      private void toolStripTextBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (Keys.Enter == e.KeyCode)
            {
                int p;
                if (int.TryParse(this.toolStripTextBox1.Text, out p))
                {
                    this.docDocumentViewer1.ZoomTo(p);
                }
            }
        }

Run the program, you can get following windows application.

Zoom Word Document using Spire.DocViewer

Fit to Width:

Zoom Word Document using Spire.DocViewer

Zoom with 50 percentages:

Zoom Word Document using Spire.DocViewer

Tuesday, 23 December 2014 08:15

Add underline text in PDF in C#

Adding text into the PDF files is one of the most important requirements for developers. With the help of Spire.PDF, developer can draw transform text, alignment text and rotate text in PDF files easily. This tutorial will show you how to add underline text in C#.

By using the method canvas.drawstring offered by Spire.PDF, developers can set the position, font, brush and style for the adding texts. With the PdfFontStyle, developers can set the style to underline, bold, italic, regular and strikeout. Here comes to the code snippet of how to add underline text in PDF.

Step 1: Create a new PDF document.

PdfDocument pdf = new PdfDocument();

Step 2: Add a new page to the PDF file.

PdfPageBase page = pdf.Pages.Add();

Step 3: Create a true type font with size 20f, underline style.

PdfTrueTypeFont font = new PdfTrueTypeFont(@"C:\WINDOWS\Fonts\CONSOLA.TTF", 20f, PdfFontStyle.Underline);

Step 4: Create a blue brush.

PdfSolidBrush brush = new PdfSolidBrush(Color.Blue);

Step 5: Draw the text string at the specified location with the specified Brush and Font objects.

page.Canvas.DrawString("Hello E-iceblue Support Team", font, brush, new PointF(10, 10));

Step 6: Save the PDF file.

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

Effective screenshot:

How to add underline text in PDF in C#

Full codes:

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


namespace AddUnderlinetext
{
    class Program
    {
        static void Main(string[] args)
        {
            PdfDocument pdf = new PdfDocument();
            PdfPageBase page = pdf.Pages.Add();
            PdfTrueTypeFont font = new PdfTrueTypeFont(@"C:\WINDOWS\Fonts\CONSOLA.TTF", 20f, PdfFontStyle.Underline);
            PdfSolidBrush brush = new PdfSolidBrush(Color.Blue);
            page.Canvas.DrawString("Hello E-iceblue Support Team", font, brush, new PointF(10, 10));
            pdf.SaveToFile("Result.pdf", FileFormat.PDF);
        }
    }
}

Built-in table styles are predefined formatting options that can be quickly applied to any table, greatly enhancing its appearance and readability. As same as MS PowerPoint, Spire.Presentation provides such a feature to give your table a professional look by applying a certain built-in table style. This article presents how this purpose can be achieved using Spire.Presentation with C#, VB.NET.

As we can see from below picture, plain tale is not that attractive before applying any style.

How to Apply Built-in Style to PowerPoint Table in C#, VB.NET

Code Snippet for Applying Table Style:

Step 1: Create an instance of presentation and load the test file.

Presentation ppt = new Presentation("test.pptx", FileFormat.Pptx2010);

Step 2: Get the table from presentation slide.

ITable table = ppt.Slides[0].Shapes[1] as ITable;

Step 3: Choose one table style from TableStylePreset and apply it to selected table.

table.StylePreset = TableStylePreset.MediumStyle2Accent2;

Step 4: Save the file.

ppt.SaveToFile("Result.pptx", FileFormat.Pptx2010);

Output:

How to Apply Built-in Style to PowerPoint Table in C#, VB.NET

Full Code:

[C#]
using Spire.Presentation;

namespace Apply_Built_in_Style
{

    class Program
    {

        static void Main(string[] args)
        {
            Presentation ppt = new Presentation("test.pptx", FileFormat.Pptx2010);
            ITable table = ppt.Slides[0].Shapes[1] as ITable;
            table.StylePreset = TableStylePreset.MediumStyle2Accent2;
            ppt.SaveToFile("Result.pptx", FileFormat.Pptx2010);

        }

    }
}
[VB.NET]
Imports Spire.Presentation

Namespace Apply_Built_in_Style

	Class Program

		Private Shared Sub Main(args As String())
			Dim ppt As New Presentation("test.pptx", FileFormat.Pptx2010)
			Dim table As ITable = TryCast(ppt.Slides(0).Shapes(1), ITable)
			table.StylePreset = TableStylePreset.MediumStyle2Accent2
			ppt.SaveToFile("Result.pptx", FileFormat.Pptx2010)

		End Sub

	End Class
End Namespace
Tuesday, 16 December 2014 08:59

How to remove auto filters in Excel

We have already shown you how to use the method ListObjects to filter the data and format it as a table. This article will focus on demonstrate how to remove the auto filters with the method AutoFilters offered by Spire.XLS. Developer can also use the method sheet.AutoFilters.Range to create the auto filters.

Step 1: Create an excel document and load the Excel with auto filers from file.

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

Step 2: Gets the first worksheet in the Excel file.

Worksheet sheet = workbook.Worksheets[0];

Step 3: Remove the auto filters.

sheet.AutoFilters.Clear();

Step 4: Save the document to file.

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

Screenshot for the Excel files with auto filters:

How to remove auto filters in Excel

The effective screenshot of removing the auto filters:

How to remove auto filters in Excel

Full codes:

using Spire.Xls;
namespace RemoveAutoFilter
{
    class Program
    {

      static void Main(string[] args)
        {
            Workbook workbook = new Workbook();
            workbook.LoadFromFile("Filter.xlsx");

            Worksheet sheet = workbook.Worksheets[0];
            sheet.AutoFilters.Clear();

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


        }
    }

This article has demonstrated how to create a Windows Forms Application to open and close Word file using Spire.DocViewer. Now, we are adding a print button to Form1 to fulfill the print feature for this Word document viewer.

Detailed Steps

Step 1: Add another button to Form1, and set the following properties for it in the Properties Window:

  • Name: btnPrint
  • DisplayStyle: Image
  • Image: Print.Properties.Resources.Print

Step 2: Double click the button to write code. In this button code, we firstly instantiate an object of System.Windows.Forms.PrintDialog and set some of its relative properties. Call ShowDialog method to make the print dialog visible.

    private void btnPrint_Click(object sender, EventArgs e)
        {
            //Show a Print Dialog
            PrintDialog dialog = new PrintDialog();
            dialog.AllowCurrentPage = true;
            dialog.AllowSomePages = true;
            dialog.UseEXDialog = true;
            DialogResult result = dialog.ShowDialog();
            //...
         }

Step 3: Set print parameters of DocDocumentViewer.PrintDialog and print the document.

            //...
                 if (result == DialogResult.OK)
            {
                try
                {
                    //Set print parameters.
                    this.docDocumentViewer1.PrintDialog = dialog;
                    //Gets the PrintDocument.
                    dialog.Document = docDocumentViewer1.PrintDocument;
                    dialog.Document.Print();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

Run the program, open an existing Word file and click 'Print' button, you'll get a dialog as below:

How to Print Word Document using Spire.DocViewer