Converts a one file to a file with another format, see supported UseOffice..::.eDirection

Namespace:  SautinSoft
Assembly:  UseOffice (in UseOffice.dll) Version: 4.3.12.15

Syntax

      
 C#  Visual Basic 
public int ConvertFile(
	string inputFile,
	string outputFile,
	UseOffice..::.eDirection convertingDirection
)
Public Function ConvertFile ( _
	inputFile As String, _
	outputFile As String, _
	convertingDirection As UseOffice..::.eDirection _
) As Integer

Parameters

inputFile
String
A full local path to a file which you want to convert
outputFile
String
A full local path to create output file
convertingDirection
UseOffice..::.eDirection
Desired converting direction

Return Value

0 - Converting successfully
1 - Can't open input file. Check that you are using full local path to input file, URL and relative path are not supported
2 - Can't create output file. Please check that you have permissions to write by this path or probably this path already used by another application
3 - Converting failed, please contact with our Support Team
4 - MS Office isn't installed. The component requires that any of these versions of MS Office should be installed: 2000, XP, 2003, 2007 or 2010

Remarks



Examples

CopyHow to convert Word Excel PowerPoint files to PDF files in C#
using System;
using System.IO;
using System.Collections;

namespace Sample
{
    class Test
    {

        static void Main(string[] args)
        {

            //Convert Word Excel PowerPoint to PDF files
            //If you need more information about UseOffice .Net email us at:
            //support[at]sautinsoft.com        


            SautinSoft.UseOffice u = new SautinSoft.UseOffice();

            //directory which contains Word, Excel, PowerPoint files: *.doc, *.docx, *.rtf, *.txt, *.xls, *.xlsx, *.csv, *.ppt, *.pptx
            string directoryWithFiles = Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory(),@"..\..\..\..\..\..\Testing files\"));

            //Prepare UseOffice .Net, loads MS Word, Excel, PowerPoint into memory
            int ret = u.InitOffice();


            //Return values:
            //0 - Loading successfully
            //1 - Can't load MS Excel (Word and PowePoint are loaded successfully)
            //10 - Can't load MS Word (Excel and PowerPoint are loaded successfully)
            //11 - Can't load MS Word and Excel (PowerPoint loaded successfully)
            //100 - Can't load MS PowerPoint (Excel and Word are loaded successfully)
            //101 - Can't load MS Excel and PowerPoint (Word loaded successfully)
            //110 - Can't load PowerPoint and Word (Excel loaded successfully)
            //111 - Can't load MS Office 

            if (ret==111)
                return;

            string [] filters = null;

            switch (ret)
            {
                case 0    : filters = new string []{"*.doc", "*.docx", "*.rtf", "*.txt", "*.xls", "*.xlsx", "*.csv", "*.ppt", "*.pptx"}; break;
                case 1    : filters = new string []{"*.doc", "*.docx", "*.rtf", "*.txt", "*.ppt", "*.pptx"}; break;
                case 10    : filters = new string []{"*.xls", "*.xlsx", "*.csv", "*.ppt", "*.pptx"}; break;
                case 11    : filters = new string []{"*.ppt", "*.pptx"}; break;
                case 100: filters = new string []{"*.doc", "*.docx", "*.rtf", "*.txt", "*.xls", "*.xlsx", "*.csv"}; break;
                case 101: filters = new string []{"*.doc", "*.docx", "*.rtf", "*.txt"}; break;
                case 110: filters = new string []{"*.xls", "*.xlsx", "*.csv"}; break;
                default    : return;
            }

            //Convert all Word, Excel, PorwerPoint files to PDF from specifed directory

            //1. Get list of MS Office files from directory


            ArrayList documents = new ArrayList();


            foreach(string filter in filters)
            {
                documents.AddRange(Directory.GetFiles(directoryWithFiles, filter));
            }


            //2. Convert to PDF
            string ext ="";
            string outPutFile = "";
            for (int i=0; i<documents.Count; i++)
            {
                SautinSoft.UseOffice.eDirection direction = SautinSoft.UseOffice.eDirection.DOC_to_PDF;
                ext = Path.GetExtension((string)documents[i]).ToLower();

                if (ext.IndexOf("doc")>0)
                    direction = SautinSoft.UseOffice.eDirection.DOC_to_PDF;
                else if (ext.IndexOf("rtf")>0)
                    direction = SautinSoft.UseOffice.eDirection.RTF_to_PDF;            
                else if (ext.IndexOf("txt")>0)
                    direction = SautinSoft.UseOffice.eDirection.RTF_to_PDF;
                else if (ext.IndexOf("xls")>0)
                    direction = SautinSoft.UseOffice.eDirection.XLS_to_PDF;
                else if (ext.IndexOf("csv")>0)
                    direction = SautinSoft.UseOffice.eDirection.XLS_to_PDF;
                else if (ext.IndexOf("ppt")>0)
                    direction = SautinSoft.UseOffice.eDirection.PPT_to_PDF;

                outPutFile = Path.Combine(Path.GetDirectoryName((string)documents[i]),Path.GetFileNameWithoutExtension((string)documents[i]));
                outPutFile+=".pdf";                
                u.ConvertFile((string)documents[i],outPutFile, direction);
            }

            u.CloseOffice();            

        }
    }
}
CopyHow to convert Word Excel PowerPoint files to PDF files in VB.Net
Imports System
Imports System.IO
Imports System.Text
Imports System.Collections


Module Module1
    Sub Main()
        'Convert Word Excel PowerPoint to PDF files
        'If you need more information about UseOffice .Net email us at:
        'support[at]sautinsoft.com        



        Dim u As New SautinSoft.UseOffice

        'directory which contains Word, Excel, PowerPoint files: *.doc, *.docx, *.rtf, *.txt, *.xls, *.xlsx, *.csv, *.ppt, *.pptx
        Dim directoryWithFiles As String = Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory(), "..\..\..\..\..\Testing files\"))

        'Prepare UseOffice .Net, loads MS Word, Excel, PowerPoint into memory
        Dim ret As Integer = u.InitOffice()


        'Return values:
        '0 - Loading successfully
        '1 - Can't load MS Excel (Word and PowePoint are loaded successfully)
        '10 - Can't load MS Word (Excel and PowerPoint are loaded successfully)
        '11 - Can't load MS Word and Excel (PowerPoint loaded successfully)
        '100 - Can't load MS PowerPoint (Excel and Word are loaded successfully)
        '101 - Can't load MS Excel and PowerPoint (Word loaded successfully)
        '110 - Can't load PowerPoint and Word (Excel loaded successfully)
        '111 - Can't load MS Office 


        If ret = 111 Then
            Return
        End If

        Dim filters() As String = Nothing

        Select Case ret
            Case 0
                filters = New String() {"*.doc", "*.docx", "*.rtf", "*.txt", "*.xls", "*.xlsx", "*.csv", "*.ppt", "*.pptx"}
            Case 1
                filters = New String() {"*.doc", "*.docx", "*.rtf", "*.txt", "*.ppt", "*.pptx"}
            Case 10
                filters = New String() {"*.xls", "*.xlsx", "*.csv", "*.ppt", "*.pptx"}
            Case 11
                filters = New String() {"*.ppt", "*.pptx"}
            Case 100
                filters = New String() {"*.doc", "*.docx", "*.rtf", "*.txt", "*.xls", "*.xlsx", "*.csv"}
            Case 101
                filters = New String() {"*.doc", "*.docx", "*.rtf", "*.txt"}
            Case 110
                filters = New String() {"*.xls", "*.xlsx", "*.csv"}
            Case Else
                Return
        End Select

        'Convert all Word, Excel, PorwerPoint files to PDF from specifed directory


        '1. Get list of MS Office files from directory



        Dim documents As New ArrayList


        For Each filter As String In filters
            documents.AddRange(Directory.GetFiles(directoryWithFiles, filter))
        Next filter


        '2. Convert to PDF
        Dim ext As String = ""
        Dim outPutFile As String = ""
        For i As Integer = 0 To documents.Count - 1
            Dim direction As SautinSoft.UseOffice.eDirection = SautinSoft.UseOffice.eDirection.DOC_to_PDF
            ext = Path.GetExtension(CStr(documents(i))).ToLower()

            If ext.IndexOf("doc") > 0 Then
                direction = SautinSoft.UseOffice.eDirection.DOC_to_PDF
            ElseIf ext.IndexOf("rtf") > 0 Then
                direction = SautinSoft.UseOffice.eDirection.RTF_to_PDF
            ElseIf ext.IndexOf("txt") > 0 Then
                direction = SautinSoft.UseOffice.eDirection.RTF_to_PDF
            ElseIf ext.IndexOf("xls") > 0 Then
                direction = SautinSoft.UseOffice.eDirection.XLS_to_PDF
            ElseIf ext.IndexOf("csv") > 0 Then
                direction = SautinSoft.UseOffice.eDirection.XLS_to_PDF
            ElseIf ext.IndexOf("ppt") > 0 Then
                direction = SautinSoft.UseOffice.eDirection.PPT_to_PDF
            End If

            outPutFile = Path.Combine(Path.GetDirectoryName(CStr(documents(i))), Path.GetFileNameWithoutExtension(CStr(documents(i))))
            outPutFile &= ".pdf"
            u.ConvertFile(CStr(documents(i)), outPutFile, direction)
        Next i

        u.CloseOffice()
    End Sub
End Module

See Also