DD.WellWorkover.Cloud/ConsoleApp1/Program.cs

142 lines
5.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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<string>();
var resultFile = "C:\\Test\\result.pdf";
inputFiles.Add("11112222.docx");
inputFiles.Add("11117777.pdf");
inputFiles.Add("22223333.xls");
inputFiles.Add("33334444.xlsx");
inputFiles.Add("33334444.tts");
var listOutNames = new List<string>();
var filteredFilesNames = inputFiles
.Distinct()
.Where(f => fileExtension.Any(fe => f.ToLower().EndsWith(fe)))
.ToList();
FileInfo fileInfo = new FileInfo(resultFile);
matchesExtensions(inputFiles);
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);
static void matchesExtensions(List<string> inputFiles)
{
string[] fileExtension = { ".xlsx", ".xls", ".ods", ".odt", ".doc", ".docx", ".pdf" };
foreach (var file in inputFiles)
{
var fileExt = Path.GetExtension(file);
if (fileExtension.All(fe => fileExt != fe))
{
throw new FileFormatException($"Файл с именем: {file} не может быть добавлен в список файлов для конвертации и слияния в общий файл программы бурения. Не поддерживаемый формат файла");
}
}
}
}
public static void DoMerged(IEnumerable<string> 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<string> 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<string> 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);
}
}
}