В приведенном ниже фрагменте кода я прошу пользователя ввести путь к каталогу, чтобы указать целевой файл .pdf для преобразования.Тем не менее, я хотел бы иметь возможность конвертировать пакет .pdf файлов одновременно.Как я мог сделать это?Скажем, у пользователя есть 100 .pdf файлов в пути к каталогу, каждый с разными именами файлов.Каков наилучший способ изменить мой код, чтобы иметь возможность пакетного преобразования всех файлов .pdf одновременно?
Console.WriteLine("PDF to Excel conversion requires a user directory path");
Console.WriteLine(@"c:\Users\username\Desktop\FolderName\FileName.pdf");
Console.WriteLine("Your Directory Path: ");
var userPath = Console.ReadLine();
string pathToPdf = userPath;
string pathToExcel = Path.ChangeExtension(pathToPdf, ".xls");
// Converting PDF to Excel file
SautinSoft.PdfFocus f = new SautinSoft.PdfFocus();
// 'true' = convert data to spreadsheet (tabular and textual)
// 'false' = skip textual data and convert only tabular (tables)
f.ExcelOptions.ConvertNonTabularDataToSpreadsheet = true;
// 'true' = preserve the original page layout
// 'false' = place tables before text
f.ExcelOptions.PreservePageLayout = true;
f.OpenPdf(pathToPdf);
if (f.PageCount > 0)
{
int result = f.ToExcel(pathToExcel);
// open an excel workbook
if (result == 0)
{
System.Diagnostics.Process.Start(pathToExcel);
}
}
Редактировать: Ниже вы видите мою попытку написать программу, используя метод каталога Брэдли, показанный ниже.
static void Main(string[] args)
{
Console.WriteLine("Welcome. I am Textron's PDF to Excel converter.");
Console.WriteLine("\n - Create a folder with all your .pdf files to be converted");
Console.WriteLine("\n - You must define your directory path");
Console.WriteLine(@" For Example ==> c:\Users\Username\Desktop\YourFolder");
Console.WriteLine("\n Your directory: ");
var userPath = Console.ReadLine();
foreach (string file in Directory.EnumerateFiles(userPath, "*.pdf"))
{
string excelPath = Path.ChangeExtension(userPath, ".xls");
// Converting PDF to Excel filetype
SautinSoft.PdfFocus f = new SautinSoft.PdfFocus();
// 'true' = convert data to spreadsheet (tabular and textual)
// 'false' = skip textual data and convert only tabular (tables)
f.ExcelOptions.ConvertNonTabularDataToSpreadsheet = true;
f.OpenPdf(userPath);
if (f.PageCount > 0)
{
int result = f.ToExcel(excelPath);
// open an excel workbook
if (result == 0)
{
System.Diagnostics.Process.Start(excelPath);
}
}
}
}