DD.WellWorkover.Cloud/AsbCloudInfrastructure/Services/DrillingProgram/Convert/ConvertToPdf.cs

73 lines
3.0 KiB
C#
Raw Normal View History

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