using iTextSharp.text.pdf; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; using Document = iTextSharp.text.Document; using CliWrap; namespace ConsoleApp1 { class Program { static void Main(/*string[] args*/) { string[] fileExtension = { ".xlsx", ".xls", ".ods", ".odt", ".doc", ".docx", ".pdf" }; Console.WriteLine($"start convert"); var inputFiles = new List(); var resultFile = "C:\\Test\\result.pdf"; inputFiles.Add("11112222.docx"); inputFiles.Add("11117777.pdf"); inputFiles.Add("22223333.xls"); inputFiles.Add("33334444.xlsx"); var listOutNames = new List(); var filteredFilesNames = inputFiles .Distinct() .Where(f => fileExtension.Any(fe => f.ToLower().EndsWith(fe))) .ToList(); foreach (var FileName in inputFiles) { var outputFile = Path.ChangeExtension(FileName, ".pdf"); var outFile = StartConvertProcessAsync(FileName, outputFile); Console.WriteLine($"convert file - {FileName}"); Console.ReadLine(); listOutNames.Add(outFile.Result.ToString()); } Console.WriteLine("merged files"); Console.ReadLine(); DoMerged(listOutNames, resultFile); } public static void DoMerged(IEnumerable inputFiles, string outFile) { using (FileStream stream = new FileStream(outFile, FileMode.Create)) using (Document doc = new Document()) using (PdfCopy pdf = new PdfCopy(doc, stream)) { doc.Open(); PdfReader? reader = null; PdfImportedPage? page = null; inputFiles.ToList().ForEach(file => { reader = new PdfReader(file); for (int i = 0; i < reader.NumberOfPages; i++) { page = pdf.GetImportedPage(reader, i + 1); pdf.AddPage(page); } pdf.FreeReader(reader); reader.Close(); File.Delete(file); }); } } //public static void StartConvertProcess(string inputFileName, string outFileName) //{ // using (Process pdfprocess = new Process()) // { // pdfprocess.StartInfo.UseShellExecute = true; // //pdfprocess.StartInfo.LoadUserProfile = true; // pdfprocess.StartInfo.FileName = "soffice"; // pdfprocess.StartInfo.Arguments = $"--headless --convert-to pdf {inputFileName} --outdir {outFileName}"; // pdfprocess.StartInfo.WorkingDirectory = "/usr/bin"; // pdfprocess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; // pdfprocess.Start(); // if (!pdfprocess.WaitForExit(1000 * 60 * 1)) // { // pdfprocess.Kill(); // } // pdfprocess.Close(); // } //} private static async Task StartConvertProcessAsync(string inputFileName, string outFileName) { //string outPath = "/home/eddie/Test/OutFiles"; string outPath = "C:\\Test\\OutFiles"; var result = Cli.Wrap("C:\\Program Files\\LibreOffice\\program\\soffice.exe") .WithArguments($"-headless -convert-to pdf C:\\Test\\InFiles\\{inputFileName} -outdir {outPath}"); await result.ExecuteAsync(); var outFile = $"{outPath}\\{outFileName}"; return outFile; } public static async Task GetConverteAndMergedFileAsync(IEnumerable filesNames, string resultPath) { string[] fileExtension = { ".xlsx", ".xls", ".ods", ".odt", ".doc", ".docx", ".pdf" }; //var filteredFilesNames = filesNames.Distinct(); var filteredFilesNames = filesNames .Distinct() .Where(f => fileExtension.Any(fe => f.ToLower().EndsWith(fe))) .ToList(); var listFileNames = filteredFilesNames .ToList() .Select(o => new { inputFile = o, convertedFile = Path.ChangeExtension(o, ".pdf") }); foreach (var excelFileName in listFileNames) { await StartConvertProcessAsync(excelFileName.inputFile, excelFileName.convertedFile); Console.WriteLine($"convert file - {excelFileName.inputFile}"); Console.ReadLine(); } Console.WriteLine("merged files"); Console.ReadLine(); DoMerged(listFileNames.Select(c => c.convertedFile), resultPath); } } }