2023-01-13 17:28:04 +05:00
using iTextSharp.text.pdf ;
2022-05-05 10:06:15 +05:00
using System ;
2022-10-11 09:02:53 +05:00
using System.Collections.Generic ;
2022-06-17 13:20:48 +05:00
using System.IO ;
2022-10-06 13:49:20 +05:00
using System.Linq ;
using System.Threading.Tasks ;
2023-01-13 17:28:04 +05:00
using Document = iTextSharp . text . Document ;
using CliWrap ;
2021-10-26 17:22:32 +05:00
namespace ConsoleApp1
2021-04-02 17:28:07 +05:00
{
class Program
2021-10-03 20:08:17 +05:00
{
2022-10-06 13:49:20 +05:00
static void Main ( /*string[] args*/ )
{
2023-01-27 18:16:25 +05:00
// 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);
2022-10-11 17:04:26 +05:00
2023-01-27 18:16:25 +05:00
//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} не может быть добавлен в список файлов для конвертации и слияния в общий файл программы бурения. Н е поддерживаемый формат файла");
// }
2023-01-17 11:11:40 +05:00
2023-01-27 18:16:25 +05:00
// }
// }
if ( OperatingSystem . IsWindows ( ) )
2023-01-13 17:28:04 +05:00
{
2023-01-27 18:16:25 +05:00
Console . WriteLine ( "win" ) ;
2023-01-13 17:28:04 +05:00
Console . ReadLine ( ) ;
}
2023-01-27 18:16:25 +05:00
if ( OperatingSystem . IsLinux ( ) )
2023-01-17 05:14:04 +05:00
{
2023-01-27 18:16:25 +05:00
Console . WriteLine ( "linux" ) ;
Console . ReadLine ( ) ;
2023-01-17 05:14:04 +05:00
}
2023-01-27 18:16:25 +05:00
2023-01-13 17:28:04 +05:00
}
2022-11-03 16:57:41 +05:00
2023-01-13 17:28:04 +05:00
public static void DoMerged ( 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 ( ) ;
var inputFilesList = inputFiles . ToList ( ) ;
foreach ( var file in inputFilesList )
2023-01-13 17:28:04 +05:00
{
2023-01-18 11:31:58 +05:00
var reader = new PdfReader ( file ) ;
for ( int i = 0 ; i < reader . NumberOfPages ; i + + )
2023-01-13 17:28:04 +05:00
{
2023-01-18 11:31:58 +05:00
PdfImportedPage page = pdf . GetImportedPage ( reader , i + 1 ) ;
pdf . AddPage ( page ) ;
}
pdf . FreeReader ( reader ) ;
reader . Close ( ) ;
} ;
2023-01-13 17:28:04 +05:00
}
2022-10-11 17:04:26 +05:00
2023-01-27 18:16:25 +05:00
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 ( "Вызов процесса в текущей операционной системе не возможен" ) ;
}
2023-01-13 17:28:04 +05:00
//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 )
{
2023-01-27 18:16:25 +05:00
var progrAndArg = getOptionsStartupProcess ( inputFileName , outFileName ) ;
2023-01-13 17:28:04 +05:00
//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" ) ;
2022-10-11 09:02:53 +05:00
Console . ReadLine ( ) ;
2023-01-13 17:28:04 +05:00
DoMerged ( listFileNames . Select ( c = > c . convertedFile ) , resultPath ) ;
2022-10-06 13:49:20 +05:00
}
2022-06-17 13:20:48 +05:00
}
2021-04-02 17:28:07 +05:00
}