C#: Move or Delete Worksheets in Excel

2024-06-24 08:49:00 Written by Koohji

When working with Excel files that contain multiple worksheets, you may sometimes find that some of the worksheets are no longer necessary or need to be organized in a different way. In these cases, you have the option of moving or deleting the worksheets to better manage your spreadsheet. In this article, you will learn how to move or delete worksheets 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

Move a Worksheet in Excel in C#

Spire.XLS for .NET provides the Worksheet.MoveWorksheet(int destIndex) method to move an Excel worksheet to a desired position by specifying the index. The following are the detailed steps:

  • Create a Workbook instance.
  • Load an Excel file using Workbook.LoadFromFile() method.
  • Get a specific worksheet using Workbook.Worksheet[] property.
  • Move the worksheet to another position using Worksheet.MoveWorksheet() method.
  • Save the result file using Workbook.SaveToFile() method.
  • C#
using Spire.Xls;

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

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

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

            //Move the worksheet to the 3rd position
            sheet.MoveWorksheet(2);

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

C#: Move or Delete Worksheets in Excel

Delete a Worksheet in Excel in C#

You can remove a specific worksheet from Excel by sheet index or name using the Workbook.Worksheets.RemoveAt(int index) or Workbook.Worksheets.Remove(string sheetName) method. The following are the detailed steps:

  • Create a Workbook instance.
  • Load an Excel file using Workbook.LoadFromFile() method.
  • Delete a specific worksheet from the file using Workbook.Worksheets.RemoveAt() or Workbook.Worksheets.Remove() method.
  • Save the result file using Workbook.SaveToFile() method.
  • C#
using Spire.Xls;

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

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

            //Remove a specific worksheet by index
            workbook.Worksheets.RemoveAt(0);
            //Remove a specific worksheet by name
            //workbook.Worksheets.Remove("Summary");

            //Save the result file
            workbook.SaveToFile("DeleteWorksheet.xlsx", ExcelVersion.Version2016);

        }
    }
}

C#: Move or Delete Worksheets 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.

Merge Multiple PDF Files into One in WPF

2012-07-04 08:29:11 Written by Koohji

Before we start our topic, let us first play a game. At the top of this page, there are nine categories. When we move our mouse on .NET, a dropdown list which shows all e-iceblue .NET products is displayed as the picture below:

Merge PDF Documents

Imagine, if we demonstrate every product on .NET out of the dropdown list on the top, what will this page be? The webpage will be rambling and disordered. As a result, visitors will hardly search what they want and finally have to leave disappointedly.

By the same token, if we do not merge PDF files of similar content to one document, all the information of various classes will mix together. We have no choice but to spend much time on finding the right files. While our work turns more efficient when we use PDF merge function. Furthermore, opening more PDF files at the same time always annoys people in that the windows are mistakenly switched. Finally, merge PDF does not mean gathering tens of PDF files into one. More reasonable choice is to amalgamate PDF files of few pages into single one. Those of numerous pages should be combined with merged PDF files in one folder for the convenience of reading.

Spire.PDF for WPF, as a professional PDF component, is a best hand for users to merge multiple PDF files on WPF application with C#, VB.NET. The whole procedure can be performed by below three simple steps.

Step 1. Create a C#/VB.NET Project

  1. Free download and correctly install Spire.PDF for WPF on system.
  2. Create “PDF merge” project in WPF Application with C#/VB.NET.
  3. Add a button in MainWindow.
  4. Add Spire.Pdf.Wpf.dll and System.Drawing as references.

Step 2 Merge PDF Files into One

1. Load three PDF documents from system.
[C#]
            //pdf document list
            String[] files = new String[]
            {
                @"D:\e-iceblue\Spire.PDF\Demos\Data\DigitalSignature.pdf",
                @"D:\e-iceblue\Spire.PDF\Demos\Data\Sample2.pdf",
                @"D:\e-iceblue\Spire.PDF\Demos\Data\ImageWaterMark.pdf"                            
            };
[VB.NET]
'pdf document list
Dim files As [String]() = New [String]() {"D:\e-iceblue\Spire.PDF\Demos\Data\DigitalSignature.pdf", "D:\e-iceblue\Spire.PDF\Demos\Data\Sample2.pdf", "D:\e-iceblue\Spire.PDF\Demos\Data\ImageWaterMark.pdf"}
          

Step 2. Open the three PDF files in a new document and import all pages into a new one

[C#]
           //open pdf documents            
            PdfDocument[] docs = new PdfDocument[files.Length];
            for (int i = 0; i < files.Length; i++)
            {
                docs[i] = new PdfDocument(files[i]);
            }
            //append document
            docs[0].AppendPage(docs[1]);
            //import pages
            for (int i = 0; i < docs[2].Pages.Count; i = i + 2)
            {
                docs[0].InsertPage(docs[2], i);
            }
[VB.NET]
	'open pdf documents            
	Dim docs As PdfDocument() = New PdfDocument(files.Length - 1) {}
	For i As Integer = 0 To files.Length - 1
		docs(i) = New PdfDocument(files(i))
	Next
	'append document
	docs(0).AppendPage(docs(1))
	'import pages
	Dim i As Integer = 0
	While i < docs(2).Pages.Count
		docs(0).InsertPage(docs(2), i)
		i = i + 2
	End While
          

Step 3. Save and Preview the Merged PDF File

[C#]
            docs[0].SaveToFile("MergeDocuments.pdf");
            foreach (PdfDocument doc in docs)
            {
                doc.Close();
            }
            PDFDocumentViewer("MergeDocuments.pdf");
[VB.NET]
docs(0).SaveToFile("MergeDocuments.pdf")
For Each doc As PdfDocument In docs
	doc.Close()
Next
PDFDocumentViewer("MergeDocuments.pdf")

Effective Screeshot

Merge PDF Documents

Spire.PDF is a PDF document creation component that enables your .NET/Silverlight/WPF applications to read, write and manipulate PDF documents without using Adobe Acrobat.

Hyperlinks in PDF are a valuable feature that enables readers to effortlessly access a given webpage. By including hyperlinks in a PDF, it becomes easier to offer readers supplementary information about the document or direct them to relevant resources. When a reader clicks on a hyperlink, the corresponding page opens immediately in the browser. In this article, we will demonstrate how to add hyperlinks to existing text in a PDF document through .NET programs 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

Insert a Hyperlink to Existing Text in PDF with C#/VB.NET

Hyperlinks in PDF documents are inserted to the page as annotation elements. Adding a hyperlink to existing text in a PDF document requires locating the text first. Once the location has been obtained, a PdfLinkAnnotation object with a specified URL action can be created and added to the position. The detailed steps are as follows:

  • Create an object of PdfDocument class and load a PDF file using PdfDocument.LoadFromFile() method.
  • Get the first page using PdfDocument.Pages property.
  • Create an object of PdfTextFinder class and set the finder options using PdfTextFinder.Options.Parameter property.
  • Find the specified text in the page using PdfTextFinder.Find() method and get the third occurrence.
  • Loop through the text bounds of the specified occurrence (because the text being searched may span multiple lines and have more than one bound, the retrieved text bounds are stored in a list to accommodate this variability).
  • Create a PdfURIAction object to specify the URL to link to, and then create a PdfLinkAnnotation object within the text bound. Set the border width and border color using properties under PdfLinkAnnotation class.
  • Get the page's annotation collection using PdfAnnotationCollection.Create(page) method and insert the hyperlink using its Add(PdfLinkAnnotation) method.
  • Save the PDF file using PdfDocument.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Pdf;
using Spire.Pdf.Actions;
using Spire.Pdf.Interactive.Annotations;
using Spire.Pdf.Texts;
using System;
using System.Collections.Generic;
using System.Drawing;
using TextFindParameter = Spire.Pdf.Texts.TextFindParameter;

namespace ChangeHyperlink
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //Create an object of PdfDocument class
            PdfDocument pdf = new PdfDocument();

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

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

            //Create an object of PdfTextFinder and set the finder options
            PdfTextFinder finder = new PdfTextFinder(page);
            finder.Options.Parameter = TextFindParameter.IgnoreCase;

            //Find the specified text in the page and get the third occurrence
            List collection = finder.Find("climate change");
            PdfTextFragment fragment = collection[2];

            //Loop through the text bounds of the specified occurrence
            foreach (RectangleF bounds in fragment.Bounds)
            {
                //Create a hyperlink annotation
                PdfURIAction action = new PdfURIAction("https://en.wikipedia.org/wiki/Climate_change");
                PdfLinkAnnotation url = new PdfLinkAnnotation(page, bounds, action);
                //Set the border width of the hyperlink annotation
                url.Border.Width = 1f;
                //Set the color of the border
                url.Color = Color.Blue;
                //Add the hyperlink annotation to the page
                PdfAnnotationCollection.Create(page).Add(url);
            }

            //Save the PDF file
            pdf.SaveToFile("AddHyperlinks.pdf");
            pdf.Dispose();
        }
    }
}

C#/VB.NET: Insert Hyperlinks into Existing Text 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 326