In people's daily life, we can open a PDF document by right clicking the open option as well as using C#, VB.NET or other programming languages. Both methods are available as long as you have a PDF Document, but for PDF itself, it has no viewing function, thus, we need to use PDF Viewer to help us view it. This article is designed to open a PDF Document with C#, VB.NET via PDF Viewer by two methods.

Spire. PDFViewer is designed for viewing PDF files from .NET application. It does NOT require Adobe Read or any other 3rd party software/library installed on system. By using Spire.PDFViewer, we can do this job easily. Please just follow the below procedure.

Step 1: Create a new project

  • Freely Download Spire.PDFViewer
  • Create a new project in Visual Studio and add a toolScript in Form1
  • Set its target Framework to be .NET Framework 4
  • Add Spire.PdfViewer. Forms as reference in Project. And add using at the top of the method. Please see below:
[C#]
using System.IO; 
using Spire.PdfViewer.Forms;
[VB.NET]
Imports Sytem. IO
Imports Spire.PdfViewer.Forms

Step 2: Open a PDF Document with C#, VB.NET via Spire.PDFViewer

Method one: This method is to directly load a PDF file from system, then open it.

[C#]
        public partial class Form1 : Form
    {
       
        public Form1()
        {
            InitializeComponent();
            
        }

        private void Form1_Load(object sender, EventArgs e)
        {
         
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            string pdfDoc = @"D:\michelle\e-iceblue\Spire.Office.pdf";
            if (File.Exists(pdfDoc))
            {
                this.pdfDocumentViewer1.LoadFromFile(pdfDoc);
            }

        }
    }
 }
[VB.NET]
Public Partial Class Form1
		Inherits Form

		Public Sub New()

			InitializeComponent()
		End Sub

		Private Sub Form1_Load(sender As Object, e As EventArgs)

		End Sub

		Private Sub toolStripButton1_Click(sender As Object, e As EventArgs)
			Dim pdfDoc As String = "D:\michelle\e-iceblue\Spire.Office.pdf"
			If File.Exists(pdfDoc) Then
				Me.pdfDocumentViewer1.LoadFromFile(pdfDoc)
			End If

		End Sub
	End Class
End Namespace

Method Two: This method allows you to choose the PDF file you want to open in a dialog box from your computer.

[C#]
public partial class Form1 : Form
 {
       
        public Form1()
        {
            InitializeComponent();
            
        }

        private void Form1_Load(object sender, EventArgs e)
        {
         
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "PDF document (*.pdf)|*.pdf";
            DialogResult result = dialog.ShowDialog();
            if (result == DialogResult.OK)
            {
                try
                {
                    string pdfFile = dialog.FileName;
                    this.pdfDocumentViewer1.LoadFromFile(pdfFile);
                }
                catch (Exception exe)
                {
                    MessageBox.Show(exe.Message, "Spire.PdfViewer Demo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

            }

        }
   }
[VB.NET]
Public Partial Class Form1
	Inherits Form

	Public Sub New()

		InitializeComponent()
	End Sub

	Private Sub Form1_Load(sender As Object, e As EventArgs)

	End Sub

	Private Sub toolStripButton1_Click(sender As Object, e As EventArgs)
		Dim dialog As New OpenFileDialog()
		dialog.Filter = "PDF document (*.pdf)|*.pdf"
		Dim result As DialogResult = dialog.ShowDialog()
		If result = DialogResult.OK Then
			Try
				Dim pdfFile As String = dialog.FileName
				Me.pdfDocumentViewer1.LoadFromFile(pdfFile)
			Catch exe As Exception
				MessageBox.Show(exe.Message, "Spire.PdfViewer Demo", MessageBoxButtons.OK, MessageBoxIcon.[Error])

			End Try
		End If

	End Sub
End Class

Step 3: Launch the file

Press F5, you can see Form1 display itself as picture below:

Then click "open" in the Form. When you use method one, you can see the PDF document content shows in the Form1. Also you can set the size of the form according to your own preference. When you use method two, you can choose the PDF Document by yourself in a dialog box. And then preview it in Form1.

Note: I set the default name of toolScript to be "open".

Effective Screenshot:

C#/VB.NET: Add Worksheets to Excel

2022-04-15 08:25:00 Written by Koohji

When working with an existing Excel file or creating an Excel file from scratch, we may need to add one or more worksheets to record data. In this article, we will demonstrate how to add worksheets to Excel in C# and VB.NET using Spire.XLS for .NET library.

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 DLLs files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.XLS

Add a Worksheet to an Existing Excel file in C# and VB.NET

The following are the steps to add a worksheet to an existing Excel file:

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

namespace AddWorksheet
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Workbook instance
            Workbook workbook = new Workbook();
            //Load an Excel file
            workbook.LoadFromFile("Sample.xlsx");

            //Add a worksheet 
            Worksheet sheet = workbook.Worksheets.Add("New_Sheet");

            //Add data to cell (1, 1)
            sheet.Range[1, 1].Value = "New Sheet";

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

C#/VB.NET: Add Worksheets to Excel

Add a Worksheet to a New Excel File in C# and VB.NET

The following steps show how to create a new Excel file and add a worksheet to it:

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

namespace AddWorksheetToNewExcel
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Workbook instance
            Workbook workbook = new Workbook();
            //Clear the default worksheets
            workbook.Worksheets.Clear();
            
            //Add a worksheet with name
            Worksheet sheet = workbook.Worksheets.Add("Sheet1");

            //Add data to cell (1, 1)
            sheet.Range[1, 1].Value = "Sheet 1";

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

C#/VB.NET: Add Worksheets to Excel

Add Multiple Worksheets to a New Excel File in C# and VB.NET

The following steps show how to create a new Excel file and add 3 worksheets to it:

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

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

            //Add 3 worksheets 
            workbook.CreateEmptySheets(3);

            //Loop through the worksheets
            for (int i = 0; i < workbook.Worksheets.Count; i++)
            {
                Worksheet sheet = workbook.Worksheets[i];
                //Add data to cell (1, 1) in each worksheet
                sheet.Range[1, 1].Value = "Sheet " + (i + 1);
            }

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

C#/VB.NET: Add Worksheets 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.

Finding specific text or phrases in a long Word document can be a bit of a hassle. Fortunately, MS Word provides the "Find" function to locate specific content in a document quickly. You can also highlight the found content with a background color to ensure that they won't be overlooked by the readers. This article will demonstrate how to programmatically find and highlight text in a Word document 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 DLLs files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.Doc

Find and Highlight Text in a Word Document

The detailed steps are as follows.

  • Create a Document instance
  • Load a sample Word document using Document.LoadFromFile() method.
  • Find all matching text in the document using Document.FindAllString(string matchString, bool caseSensitive, bool wholeWord) method.
  • Loop through all matching text in the document.
  • Get the text range of a specific matching text using TextSelection.GetAsOneRange() method, and then set its highlight color using TextRange.CharacterFormat.HighlightColor property.
  • Save the result file using Document.SaveToFile() method.
  • C#
  • VB.NET
using System.Drawing;
using Spire.Doc;
using Spire.Doc.Documents;

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

            //Load a sample Word document 
            document.LoadFromFile("input.docx");

            //Find all matching text in the document
            TextSelection[] text = document.FindAllString("transcendentalism", false, true);

            //Loop through all matching text and set highlight color for them
            foreach (TextSelection seletion in text)
            {
                seletion.GetAsOneRange().CharacterFormat.HighlightColor = Color.Yellow;
            }

            //Save the result file
            document.SaveToFile("FindHighlight.docx", FileFormat.Docx);
        }
    }
}

C#/VB.NET: Find and Highlight Text 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.

page 288