Printing: Replace PDFtoPrinter with PdfiumViewer

This commit is contained in:
2024-09-23 18:33:28 +02:00
parent b67857ae22
commit 94a6dd5312
7 changed files with 20 additions and 25 deletions

View File

@ -8,15 +8,12 @@ using System.Windows;
using System.Text.RegularExpressions;
using System.Linq;
using System.Net.Sockets;
using PdfiumViewer;
using System.Drawing.Printing;
namespace Elwig.Helpers.Printing {
public static class Pdf {
private static readonly string PdfToPrinter = new string[] { App.ExePath }
.Union(Environment.GetEnvironmentVariable("PATH")?.Split(';') ?? [])
.Select(x => Path.Combine(x, "PDFtoPrinter.exe"))
.Where(File.Exists)
.FirstOrDefault() ?? throw new FileNotFoundException("PDFtoPrinter executable not found");
private static readonly string WinziPrint = new string[] { App.ExePath }
.Union(Environment.GetEnvironmentVariable("PATH")?.Split(';') ?? [])
.Select(x => Path.Combine(x, "WinziPrint.exe"))
@ -91,21 +88,24 @@ namespace Elwig.Helpers.Printing {
});
}
public static async Task Print(string path, int copies = 1) {
public static async Task Print(string path, int copies = 1, bool doublePaged = false) {
await Print(path, new() {
Copies = (short)copies,
Collate = true,
Duplex = doublePaged ? Duplex.Vertical : Duplex.Simplex,
});
}
public static Task Print(string path, PrinterSettings settings) {
try {
var p = new Process() { StartInfo = new() {
FileName = PdfToPrinter,
CreateNoWindow = true,
UseShellExecute = false,
} };
p.StartInfo.ArgumentList.Add(path);
p.StartInfo.ArgumentList.Add("/s");
p.StartInfo.ArgumentList.Add($"copies={copies}");
p.Start();
await p.WaitForExitAsync();
using var doc = PdfDocument.Load(path);
using var printDoc = doc.CreatePrintDocument(PdfPrintMode.CutMargin);
printDoc.PrinterSettings = settings;
printDoc.Print();
} catch (Exception e) {
MessageBox.Show("Beim Drucken ist ein Fehler aufgetreten:\n\n" + e.Message, "Fehler beim Drucken", MessageBoxButton.OK, MessageBoxImage.Error);
}
return Task.CompletedTask;
}
}
}