using System.IO; using System.Windows; using PdfSharp.Pdf; using PdfSharp.Pdf.IO; using PdfSharp.Drawing; using System.Runtime.InteropServices; [ComVisible(false)] 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 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 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 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); throw new Exception($"Chyba při kopírování souboru {zdrojovySoubor}: {ex.Message}"); } } 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); throw new Exception("Druhý PDF dokument neobsahuje žádné stránky."); } } } } catch (IOException ex) { //MessageBox.Show($"Chyba při práci s PDF soubory: {ex.Message}", "Chyba", MessageBoxButton.OK, MessageBoxImage.Error); throw new Exception($"Chyba při práci s PDF soubory: {ex.Message}"); } } }