Initial Commit
This commit is contained in:
246
SplitPdfConsole/Pdf.cs
Normal file
246
SplitPdfConsole/Pdf.cs
Normal file
@@ -0,0 +1,246 @@
|
||||
using PdfSharp.Pdf;
|
||||
using PdfSharp.Pdf.IO;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace SplitPdf
|
||||
{
|
||||
public class Pdf
|
||||
{
|
||||
public static List<string> splittedVorgänge = new List<string>();
|
||||
|
||||
public static void GenerateNewImportFiles(List<string> vorgänge)
|
||||
{
|
||||
WriteNewImportFiles(vorgänge);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Schreibt neue Importfiles, falls in einer der Importfiles ein geteilter Vorgang gefunden wurde
|
||||
/// </summary>
|
||||
/// <param name="vorgänge"></param>
|
||||
public static void WriteNewImportFiles(List<string> vorgänge)
|
||||
{
|
||||
string newImportFilePath = string.Empty;
|
||||
DirectoryInfo info;
|
||||
|
||||
info = new DirectoryInfo(Program.importFilesPath);
|
||||
var files = info.GetFiles();
|
||||
|
||||
StringBuilder builder = new StringBuilder();
|
||||
|
||||
bool found = false;
|
||||
string line = string.Empty;
|
||||
|
||||
for (int i = 0; i < vorgänge.Count; i++)
|
||||
{
|
||||
if (splittedVorgänge.Count != 0)
|
||||
{
|
||||
string addedTeile = string.Empty;
|
||||
string vorgang = string.Concat(vorgänge[i], ".pdf");//Dateiname in Importdatei
|
||||
|
||||
try
|
||||
{
|
||||
foreach (var file in files) //CM-File und Inpro-File
|
||||
{
|
||||
//Letzte ID in Textdatei
|
||||
int lastId = Convert.ToInt32(File.ReadLines(file.FullName).Last().Split(';').FirstOrDefault());
|
||||
|
||||
builder.Clear();
|
||||
|
||||
Console.WriteLine("checking import... {0}", file.Name);
|
||||
using (StreamReader sr = new StreamReader(file.FullName))
|
||||
{
|
||||
while (!sr.EndOfStream)
|
||||
{
|
||||
line = sr.ReadLine();
|
||||
|
||||
//ist Vorgang in aktueller Zeile enthalten?
|
||||
if (line.Contains(vorgang))
|
||||
{
|
||||
//Zerteile Zeile und überspringe letztes Element, da Dateiname
|
||||
string cuttedLine = string.Join(';', line.Split(';').SkipLast(1));
|
||||
addedTeile = AddSplittedVorgänge(cuttedLine, lastId, vorgang);
|
||||
found = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
builder.AppendLine(line);
|
||||
|
||||
//Wenn Ende erreicht, dann Teile anhängen
|
||||
if (sr.EndOfStream)
|
||||
{
|
||||
builder.AppendLine(addedTeile);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (found)
|
||||
{
|
||||
using (StreamWriter sw = new StreamWriter(file.FullName))
|
||||
{
|
||||
sw.Write("");
|
||||
sw.WriteLine(builder.ToString().Trim());
|
||||
builder.Clear();
|
||||
found = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.InnerException);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sucht in liste der geteilten Dateien nach aktuellem Vorgang und inkrementiert letzte ID aus Importdatei entsprechend der Anzahl an Teilen.
|
||||
/// Entfernt gleichzeitig Vorgang aus Liste geteilter Vorgänge.
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <param name="lastId"></param>
|
||||
/// <param name="vorgang"></param>
|
||||
/// <returns></returns>
|
||||
//Werden als letztes an Datei angehängt
|
||||
private static string AddSplittedVorgänge(string source, int lastId, string vorgang)
|
||||
{
|
||||
string result = string.Empty;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
source = string.Join(';', source.Split(';').Skip(1));
|
||||
lastId++;
|
||||
foreach (var splittedVorgang in splittedVorgänge.Where(x => x.Contains(vorgang.Split('.').FirstOrDefault())))
|
||||
{
|
||||
sb.AppendLine(string.Concat(lastId, ";", source, ";", splittedVorgang));
|
||||
lastId++;
|
||||
}
|
||||
|
||||
var temp = splittedVorgänge.Where(x => !x.Contains(vorgang.Split('.').FirstOrDefault()));
|
||||
splittedVorgänge = temp.ToList();
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
public static bool SplitBySize(string sourceFile, long limit, ref int teil)
|
||||
{
|
||||
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
|
||||
teil = 0;
|
||||
int part = 1;
|
||||
|
||||
try
|
||||
{
|
||||
PdfDocument input = PdfReader.Open(sourceFile, PdfDocumentOpenMode.Import);
|
||||
PdfDocument output = new PdfDocument();
|
||||
|
||||
|
||||
string filename = Path.GetFileNameWithoutExtension(sourceFile);
|
||||
bool fileCreated = false;
|
||||
|
||||
string newFile = String.Format("{0} - Teil {1}.pdf", filename, part);
|
||||
|
||||
//string newFilePath = Path.Combine(Path.GetDirectoryName(sourceFile), newFile);
|
||||
string newFilePath = Path.Combine(@"C:\temp_out", newFile);
|
||||
splittedVorgänge.Add(newFile); //Fülle Liste mit splitted Vorgängen
|
||||
|
||||
for (int i = 0; i < input.PageCount; i++)
|
||||
{
|
||||
output.AddPage(input.Pages[i]);
|
||||
output.Save(newFilePath);
|
||||
|
||||
if (i == input.PageCount - 1)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
FileInfo info = new FileInfo(newFilePath);
|
||||
|
||||
if (info.Length <= limit)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
part++;
|
||||
newFile = String.Format("{0} - Teil {1}.pdf", filename, part);
|
||||
splittedVorgänge.Add(newFile); //Fülle Liste mit splitted Vorgängen
|
||||
//newFilePath = Path.Combine(Path.GetDirectoryName(sourceFile), newFile);
|
||||
newFilePath = Path.Combine(@"C:\temp_out", newFile);
|
||||
output = new PdfDocument();
|
||||
fileCreated = true;
|
||||
}
|
||||
}
|
||||
return fileCreated;
|
||||
|
||||
}
|
||||
catch (ArgumentOutOfRangeException ex)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#region obsolete
|
||||
//Zu Zwecken falls mehrere Dateien geteilt werden müssen, die zu einem Vorgang gehören z.B. Westfalendamm 100 (2GB groß)
|
||||
//public static bool SplitBySize(string sourceFile, long limit, ref int teil)
|
||||
//{
|
||||
// System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
|
||||
// int part = 1;
|
||||
// if(teil !=0)
|
||||
// {
|
||||
// part = Convert.ToInt32(++teil);
|
||||
// }
|
||||
// try
|
||||
// {
|
||||
// PdfDocument input = PdfReader.Open(sourceFile, PdfDocumentOpenMode.Import);
|
||||
// PdfDocument output = new PdfDocument();
|
||||
|
||||
// string filename = Path.GetFileNameWithoutExtension(sourceFile);
|
||||
// bool fileCreated = false;
|
||||
// string newFile = string.Empty;
|
||||
|
||||
// newFile = String.Format("{0} - Teil {1}.pdf", filename, part);
|
||||
// string newFilePath = Path.Combine(Path.GetDirectoryName(sourceFile), newFile);
|
||||
// splittedVorgänge.Add(newFile); //Fülle Liste mit splitted Vorgängen
|
||||
|
||||
// for (int i = 0; i < input.PageCount; i++)
|
||||
// {
|
||||
// output.AddPage(input.Pages[i]);
|
||||
// output.Save(newFilePath);
|
||||
|
||||
// if (i == input.PageCount - 1)
|
||||
// {
|
||||
// break;
|
||||
// }
|
||||
|
||||
// FileInfo info = new FileInfo(newFilePath);
|
||||
|
||||
// if (info.Length <= limit)
|
||||
// {
|
||||
// continue;
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// part++;
|
||||
// newFile = String.Format("{0} - Teil {1}.pdf", filename, part);
|
||||
// splittedVorgänge.Add(newFile); //Fülle Liste mit splitted Vorgängen
|
||||
// newFilePath = Path.Combine(Path.GetDirectoryName(sourceFile), newFile);
|
||||
// output = new PdfDocument();
|
||||
// fileCreated = true;
|
||||
// }
|
||||
// }
|
||||
// teil = part;
|
||||
// return fileCreated;
|
||||
|
||||
// }
|
||||
// catch (ArgumentOutOfRangeException ex)
|
||||
// {
|
||||
// throw;
|
||||
// }
|
||||
|
||||
//}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
117
SplitPdfConsole/Program.cs
Normal file
117
SplitPdfConsole/Program.cs
Normal file
@@ -0,0 +1,117 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Threading;
|
||||
using System.Linq;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SplitPdf
|
||||
{
|
||||
class Program
|
||||
{
|
||||
public static string importFilesPath = @"\\fsdeg002\DatenStA61_S\Bauvorhaben\Import";
|
||||
|
||||
//private static string sourcePdfFilesPath = @"C:\Test\Compressor_In";
|
||||
private static string sourcePdfFilesPath = @"C:\TEMP";
|
||||
|
||||
//private static string destinationPdfFilesPath = @"C:\Test\Compressor_Out";
|
||||
private static string destinationPdfFilesPath = @"C:\temp_out";
|
||||
|
||||
public static List<string> vorgänge = new List<string>();
|
||||
|
||||
const int LIMIT = 30000000;
|
||||
static async Task Main(string[] args)
|
||||
{
|
||||
try
|
||||
{
|
||||
await SplitPdfInSubDir(sourcePdfFilesPath);
|
||||
|
||||
//CopyFolderAndFiles(new DirectoryInfo(sourcePdfFilesPath), new DirectoryInfo(destinationPdfFilesPath));
|
||||
|
||||
//DeleteSourceFiles(new DirectoryInfo(sourcePdfFilesPath));
|
||||
|
||||
//Pdf.GenerateNewImportFiles(vorgänge);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public static async Task<bool> SplitPdfInSubDir(string path)
|
||||
{
|
||||
var info = new DirectoryInfo(path);
|
||||
var countDir = info.GetDirectories().Length;
|
||||
bool result = false;
|
||||
int teil = 0;
|
||||
|
||||
if (countDir != 0)
|
||||
{
|
||||
foreach (var dir in info.GetDirectories())
|
||||
{
|
||||
await SplitPdfInSubDir(dir.ToString());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var count = Directory.GetFiles(path, "*.pdf").Length;
|
||||
if (count != 0)
|
||||
{
|
||||
foreach (var file in Directory.GetFiles(path))
|
||||
{
|
||||
var fi = new FileInfo(file);
|
||||
vorgänge.Add(path.Split('\\').LastOrDefault());
|
||||
if (fi.Length < LIMIT)// || fi.FullName.Contains("Teil"))
|
||||
continue;
|
||||
Console.WriteLine("splitting... {0}", fi.Name);
|
||||
|
||||
await Task.Run(() =>
|
||||
{
|
||||
result = Pdf.SplitBySize(file, LIMIT, ref teil);
|
||||
});
|
||||
|
||||
File.Delete(fi.FullName);
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static void CopyFolderAndFiles(DirectoryInfo source, DirectoryInfo target)
|
||||
{
|
||||
Directory.CreateDirectory(target.FullName);
|
||||
|
||||
if (source.GetFiles().Length > 1)
|
||||
{
|
||||
foreach (FileInfo fi in source.GetFiles().Where(x => x.FullName.Contains("Teil")))
|
||||
{
|
||||
Console.WriteLine("copying... {0}", fi.Name);
|
||||
fi.CopyTo(Path.Combine(target.FullName, fi.Name), true);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (FileInfo fi in source.GetFiles())
|
||||
{
|
||||
fi.CopyTo(Path.Combine(target.FullName, fi.Name), true);
|
||||
}
|
||||
}
|
||||
//Directories
|
||||
foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
|
||||
{
|
||||
DirectoryInfo nextTargetSubDir = target.CreateSubdirectory(diSourceSubDir.Name);
|
||||
|
||||
CopyFolderAndFiles(diSourceSubDir, nextTargetSubDir);
|
||||
}
|
||||
}
|
||||
|
||||
public static void DeleteSourceFiles(DirectoryInfo source)
|
||||
{
|
||||
foreach (var item in source.GetDirectories())
|
||||
{
|
||||
Directory.Delete(item.FullName, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!--
|
||||
https://go.microsoft.com/fwlink/?LinkID=208121.
|
||||
-->
|
||||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup>
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>Any CPU</Platform>
|
||||
<PublishDir>C:\Test\SplitPdf</PublishDir>
|
||||
<PublishProtocol>FileSystem</PublishProtocol>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
<RuntimeIdentifier>win-x86</RuntimeIdentifier>
|
||||
<SelfContained>true</SelfContained>
|
||||
<PublishSingleFile>True</PublishSingleFile>
|
||||
<PublishReadyToRun>False</PublishReadyToRun>
|
||||
<PublishTrimmed>False</PublishTrimmed>
|
||||
</PropertyGroup>
|
||||
</Project>
|
||||
18
SplitPdfConsole/SplitPdf.csproj
Normal file
18
SplitPdfConsole/SplitPdf.csproj
Normal file
@@ -0,0 +1,18 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
<AssemblyName>SplitPdf</AssemblyName>
|
||||
<RootNamespace>SplitPdf</RootNamespace>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="PdfSharp" Version="1.50.5147" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Properties\PublishProfiles\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user