From be437cf5b08fa976485fb48ef3ab4b958454a1b0 Mon Sep 17 00:00:00 2001 From: David Shnayder Date: Wed, 14 Sep 2022 13:50:05 +0300 Subject: [PATCH] Performed testing and fixed all risen issues --- PdfTool/App.xaml | 2 +- PdfTool/Controller/ImageToPdfConverter.cs | 29 ++++++++++------------- PdfTool/Controller/PdfMerger.cs | 8 +------ PdfTool/MainWindow.xaml.cs | 5 +--- 4 files changed, 15 insertions(+), 29 deletions(-) diff --git a/PdfTool/App.xaml b/PdfTool/App.xaml index f24f645..f1f53aa 100644 --- a/PdfTool/App.xaml +++ b/PdfTool/App.xaml @@ -78,7 +78,7 @@ - + diff --git a/PdfTool/Controller/ImageToPdfConverter.cs b/PdfTool/Controller/ImageToPdfConverter.cs index 36b27e9..3c53d69 100644 --- a/PdfTool/Controller/ImageToPdfConverter.cs +++ b/PdfTool/Controller/ImageToPdfConverter.cs @@ -29,16 +29,14 @@ public async Task ConvertImagesAsync(string[] filePaths) { PdfDocument documents = new PdfDocument(); documents.Info.Title = Path.GetFileNameWithoutExtension(path); - var isVertical = true; PdfPage page = documents.AddPage(); XImage image = XImage.FromFile(path); if (image.PixelWidth > image.PixelHeight) { page.Orientation = PageOrientation.Landscape; - isVertical = false; } XGraphics gfx = XGraphics.FromPdfPage(page); - await DrawImage(gfx, image, isVertical, 0, 0, (int)page.Width, (int)page.Height); + await DrawImage(gfx, image, 0, 0, (int)page.Width, (int)page.Height); string resultPath = Path.ChangeExtension(path, ".pdf"); @@ -65,35 +63,32 @@ public async Task ConvertImagesAsync(string[] filePaths) { /// /// /// - private Task DrawImage(XGraphics gfx, XImage image, bool isVertical, int x, int y, int width, int height) { + private Task DrawImage(XGraphics gfx, XImage image, int x, int y, int width, int height) { if (!_settings.MaintainAspectRatio) { gfx.DrawImage(image, x, y, width, height); return Task.CompletedTask; } - - if (image.PixelHeight <= height && image.PixelWidth <= width) { //A4 height and width - gfx.DrawImage(image, x, y); // don't scale - return Task.CompletedTask; - } var area = width * height; var inner = image.PixelWidth / (double)image.PixelHeight * area; var Width = Math.Sqrt(inner); var Height = Width * image.PixelHeight / image.PixelWidth; - var ScaledMargin = isVertical ? width : height; - if (Width > ScaledMargin) { - var ratio = Width / ScaledMargin; + + if (Width > width) { + var ratio = Width / width; Width /= ratio; Height /= ratio; } - if (isVertical) { - x += (int)((width - Width) / 2); + + if (Height < height) { y += (int)((height - Height) / 2); - } else { - x += (int)((height - Width) / 2); - y += (int)((width - Height) / 2); } + + if (Width < width) { + x += (int)((width - Width) / 2); + } + gfx.DrawImage(image, x, y, (int)Width, (int)Height);//scale to A4 return Task.CompletedTask; } diff --git a/PdfTool/Controller/PdfMerger.cs b/PdfTool/Controller/PdfMerger.cs index 75b6ec7..6a7ffec 100644 --- a/PdfTool/Controller/PdfMerger.cs +++ b/PdfTool/Controller/PdfMerger.cs @@ -36,12 +36,6 @@ public async Task MergeDocumentsAsync(string[] filePaths, string request var filePath = Path.Combine(directory!, newFileName); var outputPath = Path.ChangeExtension(filePath, ".pdf"); - if (File.Exists(outputPath)) { - fileName += "(1)"; - filePath = Path.Combine(directory!, fileName); - outputPath = Path.ChangeExtension(filePath, ".pdf"); - } - PdfDocument document = new PdfDocument(); document.Info.Title = fileName; @@ -91,6 +85,6 @@ private static Task NewFileName(string fileName, string directory) { } var timeStamp = DateTime.Now.ToString("yyyyMMddHHmmss"); - return Task.FromResult($"{fileName}-({timeStamp})"); + return Task.FromResult($"{fileName}-{timeStamp}"); } } diff --git a/PdfTool/MainWindow.xaml.cs b/PdfTool/MainWindow.xaml.cs index 9ad31f2..63f4b59 100644 --- a/PdfTool/MainWindow.xaml.cs +++ b/PdfTool/MainWindow.xaml.cs @@ -82,8 +82,5 @@ private void OnDragLeave(object sender, DragEventArgs e) { border.Background = Brushes.Lavender; } - private void BtnMaintainAspectRatio_Click(object sender, RoutedEventArgs e) { - _settings.MaintainAspectRatio = BtnMaintainAspectRatio.IsChecked; - MessageBox.Show(_settings.MaintainAspectRatio.ToString()); - } + private void BtnMaintainAspectRatio_Click(object sender, RoutedEventArgs e) => _settings.MaintainAspectRatio = BtnMaintainAspectRatio.IsChecked; }