forked from ddrilling/AsbCloudServer
173 lines
7.2 KiB
C#
173 lines
7.2 KiB
C#
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} не может быть добавлен в список файлов для конвертации и слияния в общий файл программы бурения. Не поддерживаемый формат файла");
|
||
// }
|
||
|
||
// }
|
||
// }
|
||
if (OperatingSystem.IsWindows())
|
||
{
|
||
Console.WriteLine("win");
|
||
Console.ReadLine();
|
||
}
|
||
if (OperatingSystem.IsLinux())
|
||
{
|
||
Console.WriteLine("linux");
|
||
Console.ReadLine();
|
||
}
|
||
|
||
}
|
||
|
||
public static void DoMerged(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++)
|
||
{
|
||
PdfImportedPage page = pdf.GetImportedPage(reader, i + 1);
|
||
pdf.AddPage(page);
|
||
}
|
||
pdf.FreeReader(reader);
|
||
reader.Close();
|
||
};
|
||
}
|
||
|
||
private static (string programFile, string programArg) getOptionsStartupProcess(string inputFileName, string resultFileDir)
|
||
{
|
||
(string programFile, string programArg) startupOptions;
|
||
if (OperatingSystem.IsWindows())
|
||
{
|
||
startupOptions.programFile = "C:\\Program Files\\LibreOffice\\program\\soffice.exe";
|
||
startupOptions.programArg = $"-headless -convert-to pdf {inputFileName} --outdir {resultFileDir}";
|
||
return startupOptions;
|
||
}
|
||
if (OperatingSystem.IsLinux())
|
||
{
|
||
startupOptions.programFile = "/usr/bin/soffice";
|
||
startupOptions.programArg = $"--headless --convert-to pdf {inputFileName} --outdir {resultFileDir}";
|
||
return (startupOptions);
|
||
}
|
||
|
||
throw new NotSupportedException("Вызов процесса в текущей операционной системе не возможен");
|
||
}
|
||
|
||
//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)
|
||
{
|
||
|
||
var progrAndArg = getOptionsStartupProcess(inputFileName, 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);
|
||
|
||
}
|
||
}
|
||
}
|