using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Spire.Doc;
using Spire.Xls;
using Spire.Pdf;
using System.IO;
using System.Text.RegularExpressions;

namespace ConvertWordandExceltoPDF
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string path = @"../../Files";
            Document doc = null;
            Workbook workbook = null;

            try
            {
                string[] documents = Directory.GetFiles(path);
                Regex regexExcel = new Regex(@"^.*?\.(xls|xlsx)$");
                Regex regexWord = new Regex(@"^.*?\.(doc|docx)$");
                foreach (string file in documents)
                {
                    string filename = Path.GetFileNameWithoutExtension(file);
                    if (regexExcel.IsMatch(file.ToLower()))
                    {
                        //Convert Excel to Pdf
                        workbook = new Workbook();
                        workbook.LoadFromFile(file);
                        workbook.SaveToFile(path + "\\" + filename + ".pdf", Spire.Xls.FileFormat.PDF);
                    }
                    else if (regexWord.IsMatch(file.ToLower()))
                    {
                        //Convert Word to Pdf
                        doc = new Document(file);
                        doc.SaveToFile(path + "\\" + filename + ".pdf", Spire.Doc.FileFormat.PDF);
                    }
                }

            }
            catch (Exception ex)
            {

            }
            finally
            {
                MessageBox.Show("Succeed!");
            }

            Application.Exit();
        }
    }
}
