I have basically ended up with 2 versions of a PDF-Selector. Both versions do the same thing. I'm not sure which solution is better. Maybe someone could give me a code review.
I use: PDFSharp-Nuget-Package, Visual Studio community 2017, Resharper
My program takes 4 inputs:
- source path
- destination path
- starting page number
- ending page number
The original PDF is loaded from the source path and split into a new PDF file to the destination path. Only page numbers between the starting page number and ending page number will be saved.
1st Version:
using System;
using System.Diagnostics;
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
namespace PdfSplitter_V1
{
class ProgramV1
{
static void Main(string[] args)
{
Console.WriteLine("this application stores selected pages of a pdf-file");
PdfDocument inputDocument = new PdfDocument();
PdfDocument outputDocument = new PdfDocument();
string inputPath = "";
string outputPath = "";
inputPath = GetValues.GetPath("Enter a valid input path", inputPath, outputPath);
outputPath = GetValues.GetPath("where do you want to save the file? enter a unique filename!", inputPath, outputPath);
inputDocument = PdfReader.Open(inputPath, PdfDocumentOpenMode.Import);
int countPages = inputDocument.PageCount;
int min = GetValues.GetMin("Enter the starting page", countPages);
int max = GetValues.GetMax("Enter the ending page", countPages, min);
for (int i = min-1; i < max; i++)
{
outputDocument.AddPage(inputDocument.Pages[i]);
}
outputDocument.Save(outputPath);
Process.Start(outputPath);
}
}
public class GetValues
{
public static string GetPath(string prompt, string inputPath, string outputPath)
{
string path;
do
{
path = GetInputs.GetString(prompt);
} while (path.Equals(inputPath) || path.Equals(outputPath));
return path;
}
public static int GetMin(string prompt, int countPages)
{
int min;
do
{
min = GetInputs.GetInt(prompt);
} while (min > countPages);
return min;
}
public static int GetMax(string prompt, int countPages, int min)
{
int max;
do
{
max = GetInputs.GetInt(prompt);
} while ((max < min) && (max > countPages));
return max;
}
}
public static class GetInputs
{
public static string GetString(string prompt)
{
string input;
do
{
Console.WriteLine(prompt);
input = Console.ReadLine();
} while (String.IsNullOrEmpty(input));
return input;
}
public static int GetInt(string prompt)
{
string input;
int output;
do
{
Console.WriteLine(prompt);
input = Console.ReadLine();
} while (!int.TryParse(input, out output) || (output <= 0));
return output;
}
}
}
2nd Version:
using System;
using System.Diagnostics;
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
namespace PdfSplitter
{
class ProgramV2
{
static void Main(string[] args)
{
Console.WriteLine("this application stores selected pages of a pdf-file");
PdfDocument inputDocument;
PdfDocument outputDocument = new PdfDocument();
GetValue newGetValue = new GetValue();
Tuple<string, string> paths = newGetValue.GetPaths("Enter a valid input path", "Enter a valid output path");
string inputPath = paths.Item1;
string outputPath = paths.Item2;
inputDocument = PdfReader.Open(inputPath, PdfDocumentOpenMode.Import);
int countPages = inputDocument.PageCount;
const string promptMin = "Enter a starting page";
const string promptMax = "Enter a ending page";
Tuple<int, int>minMax = newGetValue.GetMinMax(promptMin, promptMax, countPages);
int min = minMax.Item1;
int max = minMax.Item2;
for (int i = min - 1; i < max; i++)
{
outputDocument.AddPage(inputDocument.Pages[i]);
}
outputDocument.Save(outputPath);
Process.Start(outputPath);
Console.WriteLine("Console.Readline");
Console.ReadLine();
}
}
public class GetValue
{
private string m_inputPath { get; set; }
private string m_outputPath { get; set; }
private int m_min { get; set; }
private int m_max { get; set; }
public Tuple<string, string> GetPaths(string promptInputPath, string promptOutputPath)
{
while (String.IsNullOrEmpty(m_inputPath))
{
m_inputPath = GetInputs.GetString(promptInputPath);
}
while (String.IsNullOrEmpty(m_outputPath) || m_outputPath.Equals(m_inputPath))
{
m_outputPath = GetInputs.GetString(promptOutputPath);
}
return Tuple.Create(m_inputPath, m_outputPath);
}
public Tuple<int, int> GetMinMax(string promptMin, string propmtMax, int countPages)
{
do
{
m_min = GetInputs.GetInt(promptMin);
} while (m_min > countPages);
do
{
m_max = GetInputs.GetInt(propmtMax);
} while (m_max < m_min || m_max > countPages);
return Tuple.Create(m_min, m_max);
}
}
public static class GetInputs
{
public static string GetString(string prompt)
{
string input;
do
{
Console.WriteLine(prompt);
input = Console.ReadLine();
} while (String.IsNullOrEmpty(input));
return input;
}
public static int GetInt(string prompt)
{
string input;
int output;
do
{
Console.WriteLine(prompt);
input = Console.ReadLine();
} while (!int.TryParse(input, out output) || (output <= 0));
return output;
}
}
}