SlucovaniRozpisek/PomocneMetody.cs
2025-04-09 15:19:58 +02:00

138 lines
4.9 KiB
C#

using System.IO;
using System.Windows;
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
using PdfSharp.Drawing;
public static class PomocneMetody
{
public static bool ObsahujePdf(string cestaSlozky)
{
if (string.IsNullOrWhiteSpace(cestaSlozky) || !Directory.Exists(cestaSlozky))
{
throw new ArgumentException("Neplatná cesta ke složce.", nameof(cestaSlozky));
}
return Directory.GetFiles(cestaSlozky, "*.pdf").Length > 0;
}
public static List<string> NactiPdfZeSlozky(string cestaSlozky)
{
if (string.IsNullOrWhiteSpace(cestaSlozky) || !Directory.Exists(cestaSlozky))
{
throw new ArgumentException("Neplatná cesta ke složce.", nameof(cestaSlozky));
}
return Directory.GetFiles(cestaSlozky, "*.pdf")
.Select(f => Path.GetFileName(f) ?? string.Empty)
.Where(f => !string.IsNullOrEmpty(f))
.ToList();
}
public static List<string> NactiPdfNazvyBezKoncovky(string cestaSlozky)
{
if (string.IsNullOrWhiteSpace(cestaSlozky) || !Directory.Exists(cestaSlozky))
{
throw new ArgumentException("Neplatná cesta ke složce.", nameof(cestaSlozky));
}
return Directory.GetFiles(cestaSlozky, "*.pdf")
.Select(f => Path.GetFileNameWithoutExtension(f) ?? string.Empty)
.Where(f => !string.IsNullOrEmpty(f))
.ToList();
}
public static string NormalizujCestu(string cesta)
{
if (string.IsNullOrWhiteSpace(cesta))
{
throw new ArgumentException("Neplatná cesta ke složce.", nameof(cesta));
}
return cesta.EndsWith(Path.DirectorySeparatorChar) ? cesta : cesta + Path.DirectorySeparatorChar;
}
public static void KopirujSoubor(string zdrojovySoubor, string cilovySoubor, bool prepsatExistujici)
{
if (string.IsNullOrWhiteSpace(zdrojovySoubor) || string.IsNullOrWhiteSpace(cilovySoubor))
{
throw new ArgumentException("Neplatná cesta ke zdrojovému nebo cílovému souboru.");
}
if (!File.Exists(zdrojovySoubor))
{
throw new FileNotFoundException("Zdrojový soubor neexistuje.", zdrojovySoubor);
}
try
{
if (File.Exists(cilovySoubor))
{
if (prepsatExistujici)
{
File.Copy(zdrojovySoubor, cilovySoubor, true);
}
}
else
{
File.Copy(zdrojovySoubor, cilovySoubor);
}
}
catch (IOException ex)
{
MessageBox.Show($"Chyba při kopírování souboru {zdrojovySoubor}: {ex.Message}", "Chyba", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
public static void SloucitPdf(string pdfJedna, string pdfDva, string vystupniPdf, bool prepsatExistujici)
{
if (!File.Exists(pdfJedna) || !File.Exists(pdfDva))
{
throw new FileNotFoundException("Jeden nebo oba vstupní PDF soubory neexistují.");
}
if (File.Exists(vystupniPdf) && !prepsatExistujici)
{
throw new IOException("Výstupní PDF již existuje a přepsání není povoleno.");
}
try
{
using (PdfDocument docJedna = PdfReader.Open(pdfJedna, PdfDocumentOpenMode.Modify))
{
var strankaJedna = docJedna.Pages[0];
double sirkaJedna = strankaJedna.Width.Value;
double vyskaJedna = strankaJedna.Height.Value;
using (PdfDocument docDva = PdfReader.Open(pdfDva, PdfDocumentOpenMode.Import))
{
if (docDva.PageCount > 0)
{
XPdfForm form = XPdfForm.FromFile(pdfDva);
form.PageNumber = 1;
var strankaDva = docDva.Pages[0];
double sirkaDva = strankaDva.Width.Value;
double vyskaDva = strankaDva.Height.Value;
XRect pozice = new XRect(sirkaJedna - sirkaDva, vyskaJedna - vyskaDva, sirkaDva, vyskaDva);
using (XGraphics gfx = XGraphics.FromPdfPage(strankaJedna, XGraphicsPdfPageOptions.Prepend))
{
gfx.DrawImage(form, pozice);
}
docJedna.Save(vystupniPdf);
}
else
{
MessageBox.Show("Druhý PDF dokument neobsahuje žádné stránky.", "Chyba", MessageBoxButton.OK, MessageBoxImage.Warning);
}
}
}
}
catch (IOException ex)
{
MessageBox.Show($"Chyba při práci s PDF soubory: {ex.Message}", "Chyba", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}