I would like to know if it is possible to convert all doc and docx files (don't know the names) from one folder and transfer them to the other folder as a PDF format. One doc = one PDF and so on.
I used the following code (please see below).
I get error (NullReferenceException was unhandled) on:
doc.SaveToFile(fileName, FileFormat.PDF);
- Code: Select all
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Spire.Doc;
namespace Convert
{
class Program
{
static void Main(string[] args)
{
string dir = @"C:\Source\";
string des = @"C:\Destination\";
string[] docs = Directory.GetFiles(dir, "*.txt");
string[] docxs = Directory.GetFiles(dir, "*.rtf");
foreach (string fileName in docs)
{
Document doc = new Document();
var newPath = Path.ChangeExtension(fileName, "pdf");
var savePath = Path.Combine(des, Path.GetFileName(newPath));
doc.SaveToFile(fileName, FileFormat.PDF);
}
foreach (string fileName in docxs)
{
Document docx = new Document();
var newPath = Path.ChangeExtension(fileName, "pdf");
var savePath = Path.Combine(des, Path.GetFileName(newPath));
docx.SaveToFile(fileName, FileFormat.PDF);
}
}
}
}
Thank you for any help.