Conversion

Conversion (36)

C#/VB.NET: Convert Word to Excel

Wednesday, 14 December 2022 01:30 Written by support iceblue

Word and Excel are two completely different file types. Word documents are used to write essays, letters or create reports, while Excel documents are used to save data in tabular form, make charts, or perform mathematical calculations. It is not recommended to convert a complex Word document to an Excel spreadsheet because Excel can hardly render content according to its original layout in Word.

However, if your Word document is primarily composed of tables and you want to analyze the table data in Excel, you can use Spire.Office for .NET to convert Word to Excel while maintaining good readability.

Install Spire.Office for .NET

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

Convert Word to Excel in C# and VB.NET

This scenario actually uses two libraries in the Spire.Office package. They’re Spire.Doc for .NET and Spire.XLS for .NET. The former is used to read and extract content from a Word document, and the latter is used to create an Excel document and write data in the specific cells. To make this code example easy to understand, we created the following three custom methods that preform specific functions.

  • ExportTableInExcel() - Export data from a Word table to specified Excel cells.
  • CopyContentInTable() - Copy content from a table cell in Word to an Excel cell.
  • CopyTextAndStyle() - Copy text with formatting from a Word paragraph to an Excel cell.

The following steps demonstrate how to export data from an entire Word document to a worksheet using Spire.Office for .NET.

  • Create a Document object to load a Word file.
  • Create a Worbbook object and add a worksheet named "WordToExcel" to it.
  • Traverse though all the sections in the Word document, traverse through all the document objects under a certain section, and then determine if a document object is a paragraph or a table.
  • If the document object is a paragraph, write the paragraph in a specified cell in Excel using CoypTextAndStyle() method.
  • If the document object is a table, export the table data from Word to Excel cells using ExportTableInExcel() method.
  • Auto fit the row height and column width in Excel so that the data within a cell will not exceed the bound of the cell.
  • Save the workbook to an Excel file using Workbook.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using Spire.Xls;
using System;
using System.Drawing;

namespace ConvertWordToExcel
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document object
            Document doc = new Document();

            //Load a Word file
            doc.LoadFromFile(@"C:\Users\Administrator\Desktop\Invoice.docx");

            //Create a Workbook object
            Workbook wb = new Workbook();

            //Remove the default worksheets
            wb.Worksheets.Clear();

            //Create a worksheet named "WordToExcel"
            Worksheet worksheet = wb.CreateEmptySheet("WordToExcel");
            int row = 1;
            int column = 1;

            //Loop through the sections in the Word document
            foreach (Section section in doc.Sections)
            {
                //Loop through the document object under a certain section
                foreach (DocumentObject documentObject in section.Body.ChildObjects)
                {
                    //Determine if the object is a paragraph
                    if (documentObject is Paragraph)
                    {
                        CellRange cell = worksheet.Range[row, column];
                        Paragraph paragraph = documentObject as Paragraph;
                        //Copy paragraph from Word to a specific cell
                        CopyTextAndStyle(cell, paragraph);
                        row++;
                    }

                    //Determine if the object is a table
                    if (documentObject is Table)
                    {
                        Table table = documentObject as Table;
                        //Export table data from Word to Excel
                        int currentRow = ExportTableInExcel(worksheet, row, table);
                        row = currentRow;
                    }
                }
            }

            //Auto fit row height and column width
            worksheet.AllocatedRange.AutoFitRows();
            worksheet.AllocatedRange.AutoFitColumns();

            //Wrap text in cells
            worksheet.AllocatedRange.IsWrapText = true;

            //Save the workbook to an Excel file
            wb.SaveToFile("WordToExcel.xlsx", ExcelVersion.Version2013);
        }

        //Export data from Word table to Excel cells
        private static int ExportTableInExcel(Worksheet worksheet, int row, Table table)
        {
            CellRange cell;
            int column;
            foreach (TableRow tbRow in table.Rows)
            {
                column = 1;
                foreach (TableCell tbCell in tbRow.Cells)
                {
                    cell = worksheet.Range[row, column];
                    cell.BorderAround(LineStyleType.Thin, Color.Black);
                    CopyContentInTable(tbCell, cell);
                    column++;
                }
                row++;
            }
            return row;
        }

        //Copy content from a Word table cell to an Excel cell
        private static void CopyContentInTable(TableCell tbCell, CellRange cell)
        {
            Paragraph newPara = new Paragraph(tbCell.Document);
            for (int i = 0; i < tbCell.ChildObjects.Count; i++)
            {
                DocumentObject documentObject = tbCell.ChildObjects[i];
                if (documentObject is Paragraph)
                {
                    Paragraph paragraph = documentObject as Paragraph;
                    foreach (DocumentObject cObj in paragraph.ChildObjects)
                    {
                        newPara.ChildObjects.Add(cObj.Clone());
                    }
                    if (i < tbCell.ChildObjects.Count - 1)
                    {
                        newPara.AppendText("\n");
                    }
                }
            }
            CopyTextAndStyle(cell, newPara);
        }

        //Copy text and style of a paragraph to a cell
        private static void CopyTextAndStyle(CellRange cell, Paragraph paragraph)
        {
            RichText richText = cell.RichText;
            richText.Text = paragraph.Text;
            int startIndex = 0;
            foreach (DocumentObject documentObject in paragraph.ChildObjects)
            {
                if (documentObject is TextRange)
                {
                    TextRange textRange = documentObject as TextRange;
                    string fontName = textRange.CharacterFormat.FontName;
                    bool isBold = textRange.CharacterFormat.Bold;
                    Color textColor = textRange.CharacterFormat.TextColor;
                    float fontSize = textRange.CharacterFormat.FontSize;
                    string textRangeText = textRange.Text;
                    int strLength = textRangeText.Length;
                    ExcelFont font = cell.Worksheet.Workbook.CreateFont();
                    font.Color = textColor;
                    font.IsBold = isBold;
                    font.Size = fontSize;
                    font.FontName = fontName;
                    int endIndex = startIndex + strLength;
                    richText.SetFont(startIndex, endIndex, font);
                    startIndex += strLength;
                }
                if (documentObject is DocPicture)
                {
                    DocPicture picture = documentObject as DocPicture;
                    cell.Worksheet.Pictures.Add(cell.Row, cell.Column, picture.Image);
                    cell.Worksheet.SetRowHeightInPixels(cell.Row, 1, picture.Image.Height);
                }
            }
            switch (paragraph.Format.HorizontalAlignment)
            {
                case HorizontalAlignment.Left:
                    cell.Style.HorizontalAlignment = HorizontalAlignType.Left;
                    break;
                case HorizontalAlignment.Center:
                    cell.Style.HorizontalAlignment = HorizontalAlignType.Center;
                    break;
                case HorizontalAlignment.Right:
                    cell.Style.HorizontalAlignment = HorizontalAlignType.Right;
                    break;
            }
        }
    }
}

C#/VB.NET: Convert Word 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.

C#/VB.NET: Convert ODT to PDF

Tuesday, 20 September 2022 01:05 Written by support iceblue

ODT files are OpenDocument Text files created with word processing programs such as free OpenOffice Writer. Like DOCX files, ODT files can contain content like text, images, objects and styles, but they may not be readable by some people who don't have the appropriate application installed. If you plan to share an ODT file, it's best to convert it to pdf so everyone can access it. In this article, we will explain how to convert ODT to PDF in C# and VB.NET 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 DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.Doc

Convert ODT to PDF using C# and VB.NET

The following are the steps to convert an ODT file to PDF:

  • Create an instance of Document class.
  • Load an ODT file using Document.LoadFromFile() method.
  • Convert the ODT file to PDF using Document.SaveToFile(string fileName, FileFormat fileFormat) method.
  • C#
  • VB.NET
using Spire.Doc;

namespace ConvertOdtToPdf
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //Create a Document instance
            Document doc = new Document();
            //Load an ODT file
            doc.LoadFromFile("Sample.odt");

            //Save the ODT file to PDF
            doc.SaveToFile("OdtToPDF.pdf", FileFormat.PDF);
        }
    }
}

C#/VB.NET: Convert ODT to 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.

Compared with Word document format, pictures are more convenient to share and preview across platforms, because they do not require MS Word to be installed on machines. Moreover, converting Word to images can preserve the original appearance of the document, which is useful when further modifications are not desired. In this article, you will learn how to convert Word documents to images in C# and VB.NET 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 DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.Doc

Convert Word to JPG in C#, VB.NET

Spire.Doc for .NET offers the Document.SaveToImages() method to convert a whole Word document into individual Bitmap or Metafile images. Then, a Bitmap or Metafile image can be saved as a BMP, EMF, JPEG, PNG, GIF, or WMF format file. The following are the steps to convert a Word document to JPG images using this library.

  • Create a Document object.
  • Load a Word document using Document.LoadFromFile() method.
  • Convert the document to Bitmap images using Document.SaveToImages() method.
  • Loop through the image collection to get the specific one and save it as a JPG file.
  • C#
  • VB.NET
using Spire.Doc;
using Spire.Doc.Documents;
using System;
using System.Drawing;
using System.Drawing.Imaging;

namespace ConvertWordToJPG
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document object
            Document doc = new Document();

            //Load a Word document
            doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Template.docx");

            //Convert the whole document into individual images 
            Image[] images = doc.SaveToImages(ImageType.Bitmap);

            //Loop through the image collection
            for (int i = 0; i < images.Length; i++)
            {
                //Save the image to a JPEG format file
                string outputfile = String.Format("‪Image-{0}.jpg", i);‬‬
                images[i].Save("C:\\Users\\Administrator\\Desktop\\Images\\" + outputfile, ImageFormat.Jpeg);
            }
        }
    }
}

Convert Word to SVG in C#, VB.NET

Using Spire.Doc for .NET, you can save a Word document as a queue of byte arrays. Each byte array can then be written as a SVG file. The detailed steps to convert Word to SVG are as follows.

  • Create a Document object.
  • Load a Word file using Document.LoadFromFile() method.
  • Save the document as a queue of byte arrays using Document.SaveToSVG() method.
  • Loop through the items in the queue to get a specific byte array.
  • Write the byte array to a SVG file.
  • C#
  • VB.NET
using Spire.Doc;
using System;
using System.Collections.Generic;
using System.IO;

namespace CovnertWordToSVG
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document object
            Document doc = new Document();

            //Load a Word document
            doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Template.docx");

            //Save the document as a queue of byte arrays
            Queue<byte[]> svgBytes = doc.SaveToSVG();

            //Loop through the items in the queue
            for (int i = 0; i < svgBytes.Count; i++)
            {
                //Convert the queue to an array
                byte[][] bytes = svgBytes.ToArray();

                //Specify the output file name
                string outputfile = String.Format("‪Image-{0}.svg", i);‬‬

                //Write the byte[] in a SVG format file
                FileStream fs = new FileStream("C:\\Users\\Administrator\\Desktop\\Images\\" + outputfile, FileMode.Create);
                fs.Write(bytes[i], 0, bytes[i].Length);
                fs.Close();           
            }   
        }
    }
}

Convert Word to PNG with Customized Resolution in C#, VB.NET

An image with higher resolution is generally more clear. You can customize the image resolution while converting Word to PNG by following the following steps.

  • Create a Document object.
  • Load a Word file using Document.LoadFromFile() method.
  • Convert the document to Bitmap images using Document.SaveToImages() method.
  • Loop through the image collection to get the specific one.
  • Call the custom method ResetResolution() to reset the image resolution.
  • Save the image as a PNG file.
  • C#
  • VB.NET
using Spire.Doc;
using System;
using System.Drawing;
using System.Drawing.Imaging;
using Spire.Doc.Documents;

namespace ConvertWordToPng
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document object
            Document doc = new Document();

            //Load a Word document
            doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Template.docx");

            //Convert the whole document into individual images 
            Image[] images = doc.SaveToImages(ImageType.Metafile);

            //Loop through the image collection
            for (int i = 0; i < images.Length; i++)
            {
                //Reset the resolution of a specific image
                Image newimage = ResetResolution(images[i] as Metafile, 150);

                //Save the image to a PNG format file
                string outputfile = String.Format("‪Image-{0}.png", i);‬‬
                newimage.Save("C:\\Users\\Administrator\\Desktop\\Images\\" + outputfile, ImageFormat.Png);
            }
        }

        //Set the image resolution by the ResetResolution() method
        public static Image ResetResolution(Metafile mf, float resolution)
        {
            int width = (int)(mf.Width * resolution / mf.HorizontalResolution);
            int height = (int)(mf.Height * resolution / mf.VerticalResolution);
            Bitmap bmp = new Bitmap(width, height);
            bmp.SetResolution(resolution, resolution);
            using (Graphics g = Graphics.FromImage(bmp))
            {
                g.DrawImage(mf, Point.Empty);
            }
            return bmp;
        }
    }
}

C#/VB.NET: Convert Word to Images (JPG, PNG and SVG)

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.

It is possible to perform Word to PDF conversion in Azure apps such as Azure Web apps and Azure Functions apps using Spire.Doc for .NET. In this article, you can see the code example to achieve this function with Spire.Doc for .NET.

The input Word document:

C#, VB.NET Convert Word to PDF in Azure Apps

Step 1: Install Spire.Doc NuGet Package as a reference to your project from NuGet.org.

C#, VB.NET Convert Word to PDF in Azure Apps

Step 2: Add the following code to convert Word to PDF.

C#
//Create a Document instance
Document document = new Document(false);
//Load the Word document
document.LoadFromFile(@"sample.docx");

//Create a ToPdfParameterList instance
ToPdfParameterList ps = new ToPdfParameterList
{
    UsePSCoversion = true
};
//Save Word document to PDF using PS conversion
document.SaveToFile("ToPdf.pdf", ps);
VB.NET
Private Sub SurroundingSub()
    Dim document As Document = New Document(false)
    document.LoadFromFile("sample.docx")
    Dim ps As ToPdfParameterList = New ToPdfParameterList With {
        .UsePSCoversion = True
    }
    document.SaveToFile("ToPdf.pdf", ps)
End Sub

The Output PDF document:

C#, VB.NET Convert Word to PDF in Azure Apps

Start from version 7.3.3, Spire.Doc supports keeping Word headings as PDF bookmarks when converting Word to PDF. This article demonstrates how to use Spire.Doc to accomplish this functionality.

The input Word document:

Keep Word Headings as PDF Bookmarks When Converting Word to PDF in C#

Example code

using Spire.Doc;

namespace Keep_word_headings_as_pdf_bookmarks
{
    class Program
    {
        static void Main(string[] args)
        {
            Document doc = new Document(false);
            doc.LoadFromFile("Input.docx");

            ToPdfParameterList topdf = new ToPdfParameterList();
            topdf.CreateWordBookmarks = true;
            topdf.CreateWordBookmarksUsingHeadings = true;

            doc.SaveToFile("Output.pdf", topdf);
        }
    }
}

The output PDF document:

Keep Word Headings as PDF Bookmarks When Converting Word to PDF in C#

Convert Word to PCL

Tuesday, 05 March 2019 09:02 Written by support iceblue

PCL File is Digital printed document created in the Printer Command Language (more commonly referred to as PCL) page description language. From v7.1.19, Spire.Doc supports to convert word document to PCL. There are many kinds of standard for PCL document; the PCL here refers to PCL 6 (PCL 6 Enhanced or PCL XL). This article will show you how to save word document to PCL in C# and VB.NET by only three lines of codes.

[C#]
using Spire.Doc;

namespace DOCPCL
{
    class Program
    {
        static void Main(string[] args)
        {
            //load the sample document
            Document doc = new Document();
            doc.LoadFromFile("Sample.docx", FileFormat.Docx2010);

            //save the document as a PCL file
            doc.SaveToFile("Result.pcl", FileFormat.PCL);



        }
    }
}
[VB.NET]
Imports Spire.Doc

Namespace DOCPCL
	Class Program
		Private Shared Sub Main(args As String())
			'load the sample document
			Dim doc As New Document()
			doc.LoadFromFile("Sample.docx", FileFormat.Docx2010)

			'save the document as a PCL file
			doc.SaveToFile("Result.pcl", FileFormat.PCL)



		End Sub
	End Class
End Namespace

How to convert Word to PostScript in C#

Thursday, 08 November 2018 07:59 Written by jie zou

PostScript is a page description language that is an industry standard for outputting high-resolution text and graphics. From Version 6.11.2, Spire.Doc supports to convert doc/docx to a postscript file in both WinForm app and ASP.NET app. This article will demonstrate how to convert word to PostScript in C# and VB.NET.

Firstly, view the sample word document:

How to convert Word to PostScript in C#

[C#]
using Spire.Doc;

namespace Word
{
    class Program
    {
        static void Main(string[] args)
        {            
            Document doc = new Document();
            doc.LoadFromFile("Sample.docx", FileFormat.Docx2010);
            doc.SaveToFile("Result.ps", FileFormat.PostScript);                 
        }
    }
}
[VB.NET]
Imports Spire.Doc
Namespace Word
    Class Program
        Private Shared Sub Main(ByVal args As String())
            Dim doc As Document = New Document()
            doc.LoadFromFile("Sample.docx", FileFormat.Docx2010)
            doc.SaveToFile("Result.ps", FileFormat.PostScript)
        End Sub
    End Class
End Namespace

Effective screenshot of the resulted PostScript file converted from .docx document:

How to convert Word to PostScript in C#

We already have the documentation introducing how to convert Word to EPUB. However, you may want to add a cover image to EPUB when creating an EPUB book from a Word document. The following code snippets will demonstrate the same.

Step 1: Create a Document instance and load a sample Word file.

Document doc = new Document();
doc.LoadFromFile("SampleWordFile.docx");

Step 2: Load a picture to DocPicture object.

DocPicture picture = new DocPicture(doc);
picture.LoadImage(Image.FromFile("CoverImage.jpg"));

Step 3: Add the picture to EPUB as cover image when creating EPUB from the Word document.

doc.SaveToEpub("output.epub", picture);

Output:

Add a Cover Image when Converting Word to EPUB in C#, VB.NET

Full Code:

[C#]
using Spire.Doc;
using Spire.Doc.Fields;
using System.Drawing;
namespace DOCTOEPUB
{
    class Program
    {
        static void Main(string[] args)
        {
            Document doc = new Document();
            doc.LoadFromFile("SampleWordFile.docx");

            DocPicture picture = new DocPicture(doc);
            picture.LoadImage(Image.FromFile("CoverImage.jpg"));
            doc.SaveToEpub("output.epub", picture);  
        } 
     }
}
[VB.NET]
Imports Spire.Doc
Imports Spire.Doc.Fields
Imports System.Drawing
Namespace DOCTOEPUB
	Class Program
		Private Shared Sub Main(args As String())
			Dim doc As New Document()
			doc.LoadFromFile("SampleWordFile.docx")

			Dim picture As New DocPicture(doc)
			picture.LoadImage(Image.FromFile("CoverImage.jpg"))
			doc.SaveToEpub("output.epub", picture)
		End Sub
	End Class
End Namespace

A file with the .ODT file extension is an OpenDocument Text document file. These files are most often created by the free OpenOffice Writer word processor program. ODT files are similar to the popular DOCX file format used with Microsoft Word. Both of the two file types can hold things like text, images, objects, and styles.

However, when you open an ODT document in Microsoft Word, the formatting of the ODT document may differ as a result of the two programs not sharing the same features. When converting ODT to DOCX or vice versa, the data and content will be converted successfully, but may not including the original formatting.

Following code snippets introduce how to convert ODT to DOC or DOCX and vice versa using Spire.Doc.

ODT to DOCX

To convert ODT to DOC, change the file extension and file format to .Doc in SaveToFile method.

[C#]
using Spire.Doc;

namespace ODTtoDOCX
{
    class Program
    {
        static void Main(string[] args)
        {
            Document doc = new Document();
            doc.LoadFromFile("SampleODTFile.odt");
            doc.SaveToFile("output.docx", FileFormat.Docx)

        }

 
    }
}
[VB.NET]
Imports Spire.Doc

Namespace ODTtoDOCX
	Class Program
		Private Shared Sub Main(args As String())
			Dim doc As New Document()
			doc.LoadFromFile("SampleODTFile.odt")
			doc.SaveToFile("output.docx", FileFormat.Docx)

		End Sub
	End Class
End Namespace

DOCX to ODT

To convert Doc to ODT, load a .Doc file format document when loading the source file.

[C#]
using Spire.Doc;
namespace DOCXtoODT
{
    class Program
    {
        static void Main(string[] args)
        {
            Document doc = new Document();
            doc.LoadFromFile("SampleODTFile.odt");
            doc.SaveToFile("output.docx", FileFormat.Docx);

        } 
    }
}
[VB.NET]
Imports Spire.Doc
Namespace DOCXtoODT
	Class Program
		Private Shared Sub Main(args As String())
			Dim doc As New Document()
			doc.LoadFromFile("SampleODTFile.odt")
			doc.SaveToFile("output.docx", FileFormat.Docx)

		End Sub
	End Class
End Namespace

SVG (Scalable Vector Graphics) is an image file format and it has the advantage of be edited and indexed easily. It is widely used for interactivity and animation. Starts from Spire.Doc V5.8, Spire.Doc offers a method doc.SaveToFile() to enable developers to save the word document to SVG file format in C# easily. This article will focus on demonstrate how to convert word document to SVG with the help of Spire.Doc by using a simple line of code in C#.

Firstly, please view the original word document:

How to Convert Word to SVG (Scalable Vector Graphics) in C#

Step 1: Create a C# project in visual studio, then add a reference to Spire.Doc.dll and use the following namespace.

using Spire.Doc;

Step 2: Create a word instance and load the source word document from file.

Document doc = new Document();
doc.LoadFromFile("Sample.docx");

Step 3: Save the document to SVG file.

doc.SaveToFile("result.svg", FileFormat.SVG);

After running the code, we'll get the effective screenshot of the following result SVG file from word document:

How to Convert Word to SVG (Scalable Vector Graphics) in C#

Full codes:

using Spire.Doc;

namespace wordconversion.Case
{
    class WordtoSVG
    {

         public WordtoSVG()
         {
        Document doc = new Document();
        doc.LoadFromFile("Sample.docx");

        doc.SaveToFile("result.svg", FileFormat.SVG);
         }

    }
}
Page 1 of 3