Conversion

  • Demo
  • C# source
  • VB.Net source
This demo shows you how to convert a Word document (doc/docx) to PDF, HTML, Image, XPS, RTF and other file format.
No Matter How Big or Small Your Project is,
Any technical question related to our product, contact us at support@e-iceblue.com.
Any question related to the purchase of product, contact us at sales@e-iceblue.com.
If you don't find the demo you want, please contact support@e-iceblue.com for the free customized demo just for you.
using System;
using System.Drawing;
using System.Drawing.Imaging;

using Spire.Doc;
using Spire.Doc.Documents;

namespace DemoOnlineCode
{
    class Convertors
    {
        public void demoConversion(String docFile, string format)
        {
            Document document = new Document(docFile, FileFormat.Auto);
            ConvertFormat(document, format);
        }

        private void ConvertFormat(Document document, string format)
        {
            switch (format)
            {
                case "PDF":
                    document.SaveToFile("demo.pdf", FileFormat.PDF);
                    break;

                case "XPS":
                    document.SaveToFile("demo.xps", FileFormat.XPS);
                    break;

                case "Image":
                    Image[] Images = document.SaveToImages(ImageType.Bitmap);
                    if (Images != null && Images.Length > 0)
                    {
                        if (Images.Length == 1)
                        {
                            Images[0].Save("demo.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
                        }
                        else
                        {
                            for (int i = 0; i < Images.Length; i++)
                            {
                                String fileName = String.Format("Image-{0}.png", i);
                                Images[i].Save(fileName, ImageFormat.Png);
                            }
                        }
                    }
                    break;
                case "RTF":
                    document.SaveToFile("demo.rtf", FileFormat.Rtf);
                    break;
                case "TIFF":
                    Image[] TiffImage = document.SaveToImages(ImageType.Bitmap);
                  
                    Image result = JoinTiffImages(TiffImage, EncoderValue.CompressionLZW);
                    result.Save("demo.tiff", ImageFormat.Tiff);
                    break;
                case "HTML":
                    document.SaveToFile("demo.html", FileFormat.Html);
                    break;
            }
        }

        private static Image JoinTiffImages(Image[] images, EncoderValue compressEncoder)
        {

            //use the save encoder
            Encoder enc = Encoder.SaveFlag;
            EncoderParameters ep = new EncoderParameters(2);
            ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.MultiFrame);
            ep.Param[1] = new EncoderParameter(Encoder.Compression, (long)compressEncoder);

            Image pages = null;
            int frame = 0;
            ImageCodecInfo info = GetEncoderInfo("image/tiff");

            foreach (Image img in images)
            {
                if (frame == 0)
                {
                    pages = img;
                    pages.Save("demo.tiff", info, ep);
                }
                else
                {
                    //save the intermediate frames
                    ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage);

                    pages.SaveAdd(img, ep);

                }

                if ((frame == images.Length - 1) && frame != 0)
                {
                    //flush and close.
                    ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.Flush);
                    pages.SaveAdd(ep);
                }

                frame++;
            }
            return pages;
        }

        private static ImageCodecInfo GetEncoderInfo(string mimeType)
        {
            ImageCodecInfo[] encoders = ImageCodecInfo.GetImageEncoders();
            for (int j = 0; j < encoders.Length; j++)
            {
                if (encoders[j].MimeType == mimeType)
                    return encoders[j];
            }

            throw new Exception(mimeType + " mime type not found in ImageCodecInfo");
        }
    }

}
Imports System.Drawing
Imports System.Drawing.Imaging

Imports Spire.Doc
Imports Spire.Doc.Documents

Namespace DemoOnlineCode
    Class Convertors
        Public Sub demoConversion(docFile As [String], format As String)
            Dim document As New Document(docFile, FileFormat.Auto)
            ConvertFormat(document, format)
        End Sub

        Private Sub ConvertFormat(document As Document, format As String)
            Select Case format
                Case "PDF"
                    document.SaveToFile("demo.pdf", FileFormat.PDF)
                    Exit Select

                Case "XPS"
                    document.SaveToFile("demo.xps", FileFormat.XPS)
                    Exit Select

                Case "Image"
                    Dim Images As Image() = document.SaveToImages(ImageType.Bitmap)
                    If Images IsNot Nothing AndAlso Images.Length > 0 Then
                        If Images.Length = 1 Then
                            Images(0).Save("demo.bmp", System.Drawing.Imaging.ImageFormat.Bmp)
                        Else
                            For i As Integer = 0 To Images.Length - 1
                                Dim fileName As [String] = [String].Format("Image-{0}.png", i)
                                Images(i).Save(fileName, ImageFormat.Png)
                            Next
                        End If
                    End If
                    Exit Select
                Case "RTF"
                    document.SaveToFile("demo.rtf", FileFormat.Rtf)
                    Exit Select
                Case "TIFF"
                    Dim TiffImage As Image() = document.SaveToImages(ImageType.Bitmap)

                    Dim result As Image = JoinTiffImages(TiffImage, EncoderValue.CompressionLZW)
                    result.Save("demo.tiff", ImageFormat.Tiff)
                    Exit Select
                Case "HTML"
                    document.SaveToFile("demo.html", FileFormat.Html)
                    Exit Select
            End Select
        End Sub

        Private Shared Function JoinTiffImages(images As Image(), _
                                               compressEncoder As EncoderValue) As Image

            'use the save encoder
            Dim enc As Encoder = Encoder.SaveFlag
            Dim ep As New EncoderParameters(2)
            ep.Param(0) = New EncoderParameter(enc, CLng(EncoderValue.MultiFrame))
            ep.Param(1) = New EncoderParameter(Encoder.Compression, CLng(compressEncoder))

            Dim pages As Image = Nothing
            Dim frame As Integer = 0
            Dim info As ImageCodecInfo = GetEncoderInfo("image/tiff")

            For Each img As Image In images
                If frame = 0 Then
                    pages = img
                    pages.Save("demo.tiff", info, ep)
                Else
                    'save the intermediate frames
                    ep.Param(0) = New EncoderParameter(enc, CLng(EncoderValue.FrameDimensionPage))
                    pages.SaveAdd(img, ep)
                End If

                If (frame = images.Length - 1) AndAlso frame <> 0 Then
                    'flush and close.
                    ep.Param(0) = New EncoderParameter(enc, CLng(EncoderValue.Flush))
                    pages.SaveAdd(ep)
                End If

                frame += 1
            Next
            Return pages
        End Function

        Private Shared Function GetEncoderInfo(mimeType As String) As ImageCodecInfo
            Dim encoders As ImageCodecInfo() = ImageCodecInfo.GetImageEncoders()
            For j As Integer = 0 To encoders.Length - 1
                If encoders(j).MimeType = mimeType Then
                    Return encoders(j)
                End If
            Next

            Throw New Exception(mimeType & " mime type not found in ImageCodecInfo")
        End Function
    End Class

End Namespace