forked from ddrilling/AsbCloudServer
74 lines
3.3 KiB
C#
74 lines
3.3 KiB
C#
using iTextSharp.text;
|
|
using iTextSharp.text.pdf;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using CliWrap;
|
|
using System.Threading.Tasks;
|
|
using System.Threading;
|
|
|
|
namespace AsbCloudInfrastructure.Services.DrillingProgram.Convert
|
|
{
|
|
#nullable enable
|
|
sealed internal class ConvertToPdf
|
|
{
|
|
private static readonly string[] filesExtensions = { ".xlsx", ".xls", ".ods", ".odt", ".doc", ".docx", ".pdf" };
|
|
|
|
private static void MergeFiles(IEnumerable<string> inputFiles, string outFile)
|
|
{
|
|
using var stream = new FileStream(outFile, FileMode.Create);
|
|
using var doc = new Document();
|
|
using var pdf = new PdfCopy(doc, stream);
|
|
doc.Open();
|
|
var inputFilesList = inputFiles.ToList();
|
|
foreach (var file in inputFilesList)
|
|
{
|
|
var reader = new PdfReader(file);
|
|
for (int i = 0; i < reader.NumberOfPages; i++)
|
|
{
|
|
pdf.AddPage(pdf.GetImportedPage(reader, i + 1));
|
|
}
|
|
pdf.FreeReader(reader);
|
|
reader.Close();
|
|
};
|
|
}
|
|
|
|
private static async Task StartConvertProcessAsync(string inputFileName, string resultFileDir,CancellationToken token)
|
|
{
|
|
//var result = Cli.Wrap("/usr/bin/soffice")
|
|
// .WithArguments($"--headless --convert-to pdf {inputFileName} --outdir {outFileName}");
|
|
var command = Cli.Wrap("C:\\Program Files\\LibreOffice\\program\\soffice.exe")
|
|
.WithArguments($"-headless -convert-to pdf {inputFileName} --outdir {resultFileDir}");
|
|
await command.ExecuteAsync(token);
|
|
}
|
|
|
|
public static async Task GetConverteAndMergedFileAsync(IEnumerable<string> files, string resultPath, string convertedFilesDir,CancellationToken token)
|
|
{
|
|
var badFiles = files.Where(f => !filesExtensions.Contains(Path.GetExtension(f)));
|
|
if (badFiles.Any())
|
|
{
|
|
throw new FileFormatException($"Файлы: {string.Join(", ", badFiles)} - неподдерживаемого формата. " +
|
|
$"Они не могут быть добавлены в список файлов для конвертации и слияния в общий файл программы бурения.");
|
|
}
|
|
var listFiles = files
|
|
.Distinct()
|
|
.Select(f => new {
|
|
inputFile = f,
|
|
convertedFile = Path.Combine(convertedFilesDir,"pdf",Path.ChangeExtension(Path.GetFileName(f), ".pdf"))
|
|
})
|
|
.ToList();
|
|
foreach (var file in listFiles)
|
|
{
|
|
var fileExt = Path.GetExtension(file.inputFile);
|
|
if (fileExt != ".pdf")
|
|
{
|
|
await StartConvertProcessAsync(file.inputFile, Path.GetDirectoryName(file.convertedFile)!,token);
|
|
}
|
|
}
|
|
MergeFiles(listFiles.Select(c => c.convertedFile), resultPath);
|
|
Directory.Delete(Path.Combine(convertedFilesDir,"pdf"),true);
|
|
}
|
|
}
|
|
#nullable disable
|
|
}
|