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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user