Thursday, 31 October 2024 06:06

C#: Convert Word to Markdown

Markdown, with its lightweight syntax, offers a streamlined approach to web content creation, collaboration, and document sharing, particularly in environments where tools like Git or Markdown-friendly editors are prevalent. By converting Word documents to Markdown files, users can enhance their productivity, facilitate easier version control, and ensure compatibility across different systems and platforms. In this article, we will explore the process of converting Word documents to Markdown files using Spire.Doc for .NET, providing simple C# code examples.

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 Markdown with C#

Using Spire.Doc for .NET, we can convert a Word document to a Markdown file by loading the document using Document.LoadFromFile() method and then convert it to a Markdown file using Document.SaveToFile(filename: String, FileFormat.Markdown) method. The detailed steps are as follows:

  • Create an instance of Document class.
  • Load a Word document using Document.LoadFromFile() method.
  • Convert the document to a Markdown file using Document.SaveToFile(filename: String, FileFormat.Markdown) method.
  • Release resources.
  • C#
using Spire.Doc;

namespace WordToMarkdown
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create an instance of Document class
            Document doc = new Document();

            // Load a Word document
            doc.LoadFromFile("Sample.docx");

            // Convert the document to a Markdown file
            doc.SaveToFile("output/WordToMarkdown.md", FileFormat.Markdown);
            doc.Dispose();
        }
    }
}

C#: Convert Word to Markdown

Convert Word to Markdown Without Images

When using Spire.Doc for .NET to convert Word documents to Markdown files, images are stored in Base64 encoding by default, which can increase the file size and affect compatibility. To address this, we can remove the images during conversion, thereby reducing the file size and enhancing compatibility.

The following steps outline how to convert Word documents to Markdown files without images:

  • Create an instance of Document class.
  • Load a Word document using Document.LoadFromFile() method.
  • Iterate through the sections and then the paragraphs in the document.
  • Iterate through the document objects in the paragraphs:
    • Get a document object through Paragraph.ChildObjects[] property.
    • Check if it’s an instance of DocPicture class. If it is, remove it using Paragraph.ChildObjects.Remove(DocumentObject) method.
  • Convert the document to a Markdown file using Document.SaveToFile(filename: String, FileFormat.Markdown) method.
  • Release resources.
  • C#
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;

namespace WordToMarkdownNoImage
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create an instance of Document class
            Document doc = new Document();

            // Load a Word document
            doc.LoadFromFile("Sample.docx");

            // Iterate through the sections in the document
            foreach (Section section in doc.Sections)
            {
                // Iterate through the paragraphs in the sections
                foreach (Paragraph paragraph in section.Paragraphs)
                {
                    // Iterate through the document objects in the paragraphs
                    for (int i = 0; i < paragraph.ChildObjects.Count; i++)
                    {
                        // Get a document object
                        DocumentObject docObj = paragraph.ChildObjects[i];
                        // Check if it is an instance of DocPicture class
                        if (docObj is DocPicture)
                        {
                            // Remove the DocPicture instance
                            paragraph.ChildObjects.Remove(docObj);
                        }
                    }
                }
            }

            // Convert the document to a Markdown file
            doc.SaveToFile("output/WordToMarkdownNoImage.md", FileFormat.Markdown);
            doc.Dispose();
        }
    }
}

C#: Convert Word to Markdown

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.

Published in Conversion
Wednesday, 10 April 2024 00:59

C#: Convert Markdown to Word and PDF

Markdown, as a lightweight markup language, is favored by programmers and technical document writers for its simplicity, readability, and clear syntax. However, in specific scenarios, there is often a need to convert Markdown documents into Word documents with rich formatting capabilities and control over the layout or to generate PDF files suitable for printing and easy viewing. This article is going to demonstrate how to convert Markdown content into Word documents or PDF files with Spire.Doc for .NET to meet various document processing requirements in different scenarios.

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 Markdown Files to Word Documents with C#

With Spire.Doc for .NET, we can load a Markdown file using Document.LoadFromFile(string fileName, FileFormat.Markdown) method and then convert it to other formats using Document.SaveToFile(string fileName, fileFormat FileFormat) method.

Since images in Markdown files are stored as links, directly converting a Markdown file to a Word document is suitable for Markdown files that do not contain images. If the file contains images, further processing of the images is required after conversion.

Here are the steps to convert a Markdown file to a Word document:

  • Create an instance of Document class.
  • Load a Markdown file using Document.LoadFromFile(string fileName, FileFormat.Markdown) method.
  • Convert the file to a Word document and save it using Document.SaveToFile(string fileName, FileFormat.Docx) method.
  • C#
using Spire.Doc;

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

            // Load a Markdown file
            doc.LoadFromFile("Sample.md", FileFormat.Markdown);

            // Convert the Markdown file to a Word document
            doc.SaveToFile("MarkdownToWord.docx", FileFormat.Docx);
            doc.Close();
        }
    }
}

C#: Convert Markdown to Word and PDF

Convert Markdown Files to PDF Files with C#

We can also directly convert Markdown files to PDF files by using the FileFormat.PDF Enum as the parameter. Here are the steps to convert a Markdown file to a PDF file:

  • Create an instance of Document class.
  • Load a Markdown file using Document.LoadFromFile(string fileName, FileFormat.Markdown) method.
  • Convert the file to a PDF file and save it using Document.SaveToFile(string fileName, FileFormat.Docx) method.
  • C#
using Spire.Doc;

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

            // Load a Markdown file
            doc.LoadFromFile("Sample.md", FileFormat.Markdown);

            // Convert the Markdown file to a PDF file
            doc.SaveToFile("MarkdownToPDF.pdf", FileFormat.PDF);
            doc.Close();
        }
    }
}

C#: Convert Markdown to Word and 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.

Published in Conversion
Wednesday, 14 December 2022 01:30

C#/VB.NET: Convert Word to Excel

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.

Published in Conversion
Tuesday, 20 September 2022 01:05

C#/VB.NET: Convert ODT to PDF

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.

Published in Conversion

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.

Published in Conversion

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

Published in Conversion
Tuesday, 05 March 2019 09:02

Convert Word to PCL

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
Published in Conversion
Thursday, 08 November 2018 07:59

How to convert Word to PostScript in C#

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#

Published in Conversion

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
Published in Conversion

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
Published in Conversion
Page 1 of 4