Skip to content

Commit

Permalink
Performed testing and fixed all risen issues
Browse files Browse the repository at this point in the history
  • Loading branch information
dusrdev committed Sep 14, 2022
1 parent 6b18263 commit be437cf
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 29 deletions.
2 changes: 1 addition & 1 deletion PdfTool/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="HorizontalContentAlignment" Value="Center" />
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="Height" Value="25"/>
<Setter Property="Height" Value="20"/>
<Setter Property="Padding" Value="0" />
<Setter Property="Template">
<Setter.Value>
Expand Down
29 changes: 12 additions & 17 deletions PdfTool/Controller/ImageToPdfConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,14 @@ public async Task<Result> 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");

Expand All @@ -65,35 +63,32 @@ public async Task<Result> ConvertImagesAsync(string[] filePaths) {
/// <param name="y"></param>
/// <param name="width"></param>
/// <param name="height"></param>
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;
}
Expand Down
8 changes: 1 addition & 7 deletions PdfTool/Controller/PdfMerger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,6 @@ public async Task<Result> 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;

Expand Down Expand Up @@ -91,6 +85,6 @@ private static Task<string> NewFileName(string fileName, string directory) {
}

var timeStamp = DateTime.Now.ToString("yyyyMMddHHmmss");
return Task.FromResult($"{fileName}-({timeStamp})");
return Task.FromResult($"{fileName}-{timeStamp}");
}
}
5 changes: 1 addition & 4 deletions PdfTool/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit be437cf

Please sign in to comment.