How to Wrap Text in Excel Cell with C#/VB.NET

INTRODUCTION

Generally speaking, if information in one cell is a long sentence or formed with several sentences, it will be displayed within one line by default and the contents over the width of cell will not be displayed if there are data in next cell. In order to have all the contents presented, Excel provides users with function to wrap text in cell.

After setting to wrap text, contents in one cell will be put in several lines according to cell size. When merging cells, this function is often used because contents in merged cell may be a summary of data information.

In this post, I want to share the method to wrap text in Excel with C#/VB.NET.

BACKGROUND

There is an Excel file which saves students information of one class. In Additional column, length of some information is longer than column width so that I will wrap text for these cells. And then set format for cells in which text has been wrapped.

Original File

In order to realize this function more easily and quickly, I use a .NET Excel component, Spire.XLS for .NET in this example. Therefore, at the beginning, I add its dll file as reference in my project.

STEPS

I. Load Excel file which I want to wrap text from computer and initialize worksheet.

C#

            Workbook workbook = new Workbook();
            workbook.LoadFromFile(@”E:\work\Documents\student info.xlsx”);
            Worksheet sheet = workbook.Worksheets[0];

VB.NET

            Dim workbook As New Workbook()
            workbook.LoadFromFile(“E:\work\Documents\student info.xlsx”)
            Dim sheet As Worksheet = workbook.Worksheets(0)

II. Confirm cell range and assign true value for sheet.Range[].IsWrapText.

C#

            sheet.Range[“G3”].IsWrapText = true;
            sheet.Range[“G11”].IsWrapText = true;

VB.NET

            sheet.Range(“G3”).IsWrapText = True
            sheet.Range(“G11”).IsWrapText = True

III. Set format for cells in which text has been wrapped, including background color. Then, set row height and column width to make the appearance better.

C#

            sheet.Range[“G3”].Style.Color = Color.SpringGreen;
            sheet.Range[“G11”].Style.Color = Color.DeepSkyBlue;

            sheet.Range[“G11”].RowHeight = 30;
            sheet.Range[“G11”].ColumnWidth = 40;

VB.NET

            sheet.Range(“G3”).Style.Color = Color.SpringGreen
            sheet.Range(“G11”).Style.Color = Color.DeepSkyBlue

            sheet.Range(“G11”).RowHeight = 30
            sheet.Range(“G11”).ColumnWidth = 40

IV. Save this document with another name by using workbook.SaveToFile() method. Then launch it.

C#

            workbook.SaveToFile(“wraptext.xlsx”, ExcelVersion.Version2010);
            System.Diagnostics.Process.Start(“wraptext.xlsx”);

VB.NET

            workbook.SaveToFile(“wraptext.xlsx”, ExcelVersion.Version2010)
            System.Diagnostics.Process.Start(“wraptext.xlsx”)

RESULT

Freely Download Spire.XLS for .NET

Advertisements

How to Extract Image from Word with C#/VB.NET

In order to make contents to be more vivid and appealed, people would like to insert images in Word document. For example, if one document introduces one book, the author will insert book cover in paragraphs.

Although these images are often used to decorate document, some are very helpful to explain contents in other documents if the same information appears in two documents. In this post, I want to introduce a method about how to extract image from Word document by using C#/VB.NET.

In this example, I prepare a Word document which includes two images. I will extract and save them. Then, launch one of them.

Also, I use a component, Spire.Doc to realize this function quickly. So I add its dll file as reference in project.

Detailed Steps Shown as Following:           

At the beginning, we need to use the following namespace.

C#

using System;
using System.Drawing;
using System.Collections;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Collections;
using Spire.Doc.Interface;
using Spire.Doc.Fields;

VB.NET

Imports System
Imports System.Drawing
Imports System.Collections
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Collections
Imports Spire.Doc.Interface
Imports Spire.Doc.Fields 

Step 1. Load document and then set index for following image name setting.

C#

            Document document = new Document(@”E:\work\Documents\Antarctic.docx”);
            int index = 0;

VB.NET

            Dim document As New Document(“E:\work\Documents\Antarctic.docx”)
            Dim index As Integer = 0

Step 2. Use foreach sentence to get sections in document, paragraphs in section and child objects in paragraph.

C#

            foreach (Section section in document.Sections)
            {
                foreach (Paragraph paragraph in section.Paragraphs)
                {
                    foreach (DocumentObject docObject in paragraph.ChildObjects)
                    {

VB.NET
            For Each section As Section In document.Sections
                For Each paragraph As Paragraph In section.Paragraphs
                    For Each docObject As DocumentObject In paragraph.ChildObjects

Step 3. Use if sentence to judge object type in paragraphs. If the type is image, get it and set image name format with String.Format(“Image-{0}.png”, index) to make that image name can be changed by order automatically. Finally, save image by using picture.Image.Save() method. There are two parameters passed to this method, image name and image format.

C#

                        if (docObject.DocumentObjectType == DocumentObjectType.Picture)
                        {
                            DocPicture picture = docObject as DocPicture;
                            //Name the image
                            String imageName = String.Format(“Image-{0}.png”, index);
                            //Save the image.
                            picture.Image.Save(imageName, System.Drawing.Imaging.ImageFormat.Png);
                            index++;
                        }
                    }
                }
            }

VB.NET

                        If docObject.DocumentObjectType = DocumentObjectType.Picture Then
                            Dim picture As DocPicture = TryCast(docObject, DocPicture)
                            ‘Name the image
                            Dim imageName As String = String.Format(“Image-{0}.png”, index)
                            ‘Save the image.
                            picture.Image.Save(imageName, System.Drawing.Imaging.ImageFormat.Png)
                            index += 1
                        End If
                    Next docObject
                Next paragraph
            Next section

Step 4. Launch one of the images which are extracted from Word. The image I will launch is the first image.

C#

            System.Diagnostics.Process.Start(“Image-0.png”);

VB.NET

            System.Diagnostics.Process.Start(“Image-0.png”)

Result Shown as Following:

___________________________________________________________________________________________

Click Here to LEARN MORE about Spire.Doc

Click Here to DOWNLOAD Spire.Doc

Spire.Office also can be used to realize this function.

How to Set Image Quality When Converting Word to PDF with C#/VB.NET

If one Word document has several images, the size of PDF document will be very large which converting from this Word document. Sometimes, uses will be troubled because of the large size PDF document. In this post, I will introduce a method to make document size smaller through changing image quality when converting Word to PDF by using C#/VB.NET.

At the beginning, we need to learn something about image quality. Image quality is a characteristic of an image that measures perceived image degradation. That means image will be compared to an ideal or perfect status.

In my example, I use a component, Spire.Doc in this method. So, I add its dll file as reference in my project.

Detailed Steps Shown as Following:           

Step 1, Load Document

Declare a document and load document which I want to set image quality and convert to PDF by using document.LoadFromFile() method.

C#

            Document document = new Document();

            document.LoadFromFile(@”E:\work\Documents\welcome.docx”);

VB.NET

            Dim document As New Document()

            document.LoadFromFile(“E:\work\Documents\welcome.docx”)

Step 2, Set Image Quality

Assign value for document.JPEGQuality. The default quality is percentage 80. If you want to have smaller size, you can set the percentage to be lower than 80.

C#

            document.JPEGQuality= 50;

VB.NET

            document.JPEGQuality = 50

Step 3, Convert Word to PDF

Save document as PDF by using document.SaveToFile() method and then launch this converted file.

C#

            document.SaveToFile(“ImageQuantity.PDF”, FileFormat.PDF);

            System.Diagnostics.Process.Start(“ImageQuantity.PDF”);

VB.NET

            document.SaveToFile(“ImageQuality.PDF”, FileFormat.PDF)

            System.Diagnostics.Process.Start(“ImageQuality.PDF”)

Note: if original document has several images, this sentence: document.JPEGQuality= 50; is used to set all image quality.

Result Shown as Following:

Comparing with the original Word document, PDF which is converted with MS Word and PDF document which converted with setting quality, we can find ImageQuality.PDF has smaller size.

____________________________________________________________________________________________

Click Here to LEARN MORE about Spire.Doc

Click Here to DOWNLOAD Spire.Doc

 Spire.Office also can be used to realize this function.

How to Export PDF Page AS Image with C#/VB.NET

Actually, PDF becomes more and more popular at present. Because of its wonderful reading effect, many people use it to create e-books or magazines. Although PDF document is very convenient for reading, printing and sending, someone may want to convert PDF to other format for some special requirements or just convert some of pages in PDF as other format.

In this post, I want to introduce a method about how to export pages in PDF as image by using C#/VB.NET.

As is known, a PDF document may have several pages. The example I will show focuses on exporting one page in document as image. The image format can be set by own when saving image.

Also, a component, Spire.PDFViewer is used for realizing this function. So, I have added its dll file as reference in project.

Detailed Steps Shown as Following:           

Step 1, Create a Window Form Project and Design Form

Because I need to use Spire.PDFViewer controls, so I need to add them into VS Toolbar as following:

Then, design form. Add a PDFDocumentViewer and a button. Change button text as Export PDF Page AS Image.

Step 2, Double Click Button to Write Code

Step 3, Load PDF Document

Because PDF document content will be shown in PDFDocumentViewer control, so use this.pdfDocumentViewer1.LoadFromFile() method.

C#

        private void Form1_Load(object sender, EventArgs e)
        {
            this.pdfDocumentViewer1.LoadFromFile(@”E:\work\Documents\Troubleshooting Guide Template.pdf”);
        }

VB.NET

        Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
            Me.pdfDocumentViewer1.LoadFromFile(“E:\work\Documents\Troubleshooting Guide Template.pdf”)
        End Sub

Step 4, Create Save Dialogue

Declare a new save dialog and define formats for filtering.

C#

        private void button1_Click(object sender, EventArgs e)
        {
            SaveFileDialog dialog = new SaveFileDialog();
            dialog.Filter = “JPG Format(*.jpg)|*.jpg|BMP Format(*.bmp)|*.bmp|PNG Format(*.png)|*.png|GIF Format(*.gif)|*.gif”;
            DialogResult result = dialog.ShowDialog();

VB.NET

        Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
            Dim dialog As New SaveFileDialog()
            dialog.Filter = “JPG Format(*.jpg)|*.jpg|BMP Format(*.bmp)|*.bmp|PNG Format(*.png)|*.png|GIF Format(*.gif)|*.gif”
            Dim result As DialogResult = dialog.ShowDialog()

Step 5, Export PDF Page AS Image

Declare a file name string and use if sentence to judge dialogue result. If result is OK, get current page and save this page as image. Finally, save this image.

C#

            if (result == DialogResult.OK)
            {
                int currentPage = this.pdfDocumentViewer1.CurrentPageNumber;
                Bitmap image = this.pdfDocumentViewer1.SaveAsImage(currentPage – 1);
                image.Save(fileName);
            }

VB.NET

            If result = DialogResult.OK Then
                Dim currentPage As Integer = Me.pdfDocumentViewer1.CurrentPageNumber
                Dim image As Bitmap = Me.pdfDocumentViewer1.SaveAsImage(currentPage – 1)
                image.Save(fileName)
            End If

Result Shown as Following:

___________________________________________________________________________________________

Click Here to LEARN MORE about Spire.PDFViewer

Click Here to DOWNLOAD Spire.PDFViewer

Spire.Office also can be used to realize this function.

How to Convert Word to TXT with C#/VB.NET

TXT Introduction

TXT is one windows attached text format, which is the most frequently used file format. It is used to the save text information and can be opened with several tools easily, for example, notepad, browser and so on.

Actually, there are a few formats for TXT file, for example, font style, size and color. Therefore, TXT file is suitable for saving plain text. Although TXT file doesn’t have many formats, it is still popular for easily to open and read.

Convert Word to TXT

Sometimes, because users need to have a plain text file for submitting to website, they may convert Word document to TXT.

In this post, I will introduce a method about how to convert Word to TXT by using C#/VB.NET.

Note: Spire.Doc is used in this example. If you want to use the following code, please download and install this component firstly. Then, add its dll file as reference in project.

Detailed Steps Shown as Following:           

I. Load file which I want to convert to TXT by using document.LoadFromFile() method. 

C#

            Document document = new Document();

            document.LoadFromFile(@”E:\work\Documents\Blues Introduction.docx”);

VB.NET

            Dim document As New Document()

            document.LoadFromFile(“E:\work\Documents\Blues Introduction.docx”)

II. Convert Word to TXT by using document.SaveToFile() method. There are two parameters passed to this method, file name format and file format. Extension of file name should be .txt and file format should be .Txt as well.

C#

            document.SaveToFile(“ToText.txt”, FileFormat.Txt);

VB.NET

            document.SaveToFile(“ToText.txt”, FileFormat.Txt)

III. Launch the saved txt file.

C#

            System.Diagnostics.Process.Start(“ToText.txt”);

VB.NET

            System.Diagnostics.Process.Start(“ToText.txt”)

Result Shown as Following:

_____________________________________________________________________________________________

Click Here to LEARN MORE about Spire.Doc

Click Here to DOWNLOAD Spire.Doc

Spire.Office also can be used to realize this function.

Related Posts:

Convert Word to EPub

Convert Word to HTML

Convert Word to XML

Convert Word to Image

Convert Word to PDF