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-13 17:28:04 +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" ) ;
2023-01-17 05:14:04 +05:00
inputFiles . Add ( "33334444.tts" ) ;
2022-10-11 17:04:26 +05:00
2023-01-13 17:28:04 +05:00
var listOutNames = new List < string > ( ) ;
var filteredFilesNames = inputFiles
. Distinct ( )
. Where ( f = > fileExtension . Any ( fe = > f . ToLower ( ) . EndsWith ( fe ) ) )
. ToList ( ) ;
2023-01-17 05:14:04 +05:00
matchesExtensions ( inputFiles ) ;
2023-01-13 17:28:04 +05:00
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 ) ;
2023-01-17 05:14:04 +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-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 )
{
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 ) ;
} ) ;
}
}
2022-10-11 17:04:26 +05:00
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 )
{
//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
}