mirror of
https://github.com/UglyToad/PdfPig.git
synced 2025-11-28 17:47:12 +08:00
move console runner to named file and clean output then run in action
This commit is contained in:
108
tools/UglyToad.PdfPig.ConsoleRunner/Program.cs
Normal file
108
tools/UglyToad.PdfPig.ConsoleRunner/Program.cs
Normal file
@@ -0,0 +1,108 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using Console = System.Console;
|
||||
|
||||
namespace UglyToad.PdfPig.ConsoleRunner
|
||||
{
|
||||
public static class Program
|
||||
{
|
||||
public static int Main(string[] args)
|
||||
{
|
||||
if (args.Length == 0)
|
||||
{
|
||||
Console.WriteLine("At least 1 argument, path to test file directory, must be provided.");
|
||||
return 7;
|
||||
}
|
||||
|
||||
var path = args[0];
|
||||
|
||||
if (!Directory.Exists(path))
|
||||
{
|
||||
Console.WriteLine($"The provided path is not a valid directory: {path}.");
|
||||
return 7;
|
||||
}
|
||||
|
||||
var maxCount = default(int?);
|
||||
|
||||
if (args.Length > 1 && int.TryParse(args[1], out var countIn))
|
||||
{
|
||||
maxCount = countIn;
|
||||
}
|
||||
|
||||
var hasError = false;
|
||||
var errorBuilder = new StringBuilder();
|
||||
var fileList = Directory.GetFiles(path, "*.pdf", SearchOption.AllDirectories);
|
||||
var runningCount = 0;
|
||||
|
||||
Console.WriteLine($"Found {fileList.Length} files.");
|
||||
|
||||
Console.WriteLine($"{GetCleanFilename("File")}| Size\t| Words\t| Pages");
|
||||
|
||||
foreach (var file in fileList)
|
||||
{
|
||||
if (maxCount.HasValue && runningCount >= maxCount)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var numWords = 0;
|
||||
var numPages = 0;
|
||||
using (var pdfDocument = PdfDocument.Open(file))
|
||||
{
|
||||
foreach (var page in pdfDocument.GetPages())
|
||||
{
|
||||
numPages++;
|
||||
foreach (var word in page.GetWords())
|
||||
{
|
||||
if (word != null)
|
||||
{
|
||||
numWords++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var filename = Path.GetFileName(file);
|
||||
|
||||
var size = new FileInfo(file);
|
||||
|
||||
Console.WriteLine($"{GetCleanFilename(filename)}| {size.Length}\t| {numWords}\t| {numPages}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
hasError = true;
|
||||
errorBuilder.AppendLine($"Parsing document {file} failed due to an error.")
|
||||
.Append(ex)
|
||||
.AppendLine();
|
||||
}
|
||||
|
||||
runningCount++;
|
||||
}
|
||||
|
||||
if (hasError)
|
||||
{
|
||||
Console.WriteLine(errorBuilder.ToString());
|
||||
return 5;
|
||||
}
|
||||
|
||||
Console.WriteLine("Complete! :)");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
private static string GetCleanFilename(string name, int maxLength = 30)
|
||||
{
|
||||
if (name.Length <= maxLength)
|
||||
{
|
||||
var fillLength = maxLength - name.Length;
|
||||
|
||||
return name + new string(' ', fillLength);
|
||||
}
|
||||
|
||||
return name.Substring(0, maxLength);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"profiles": {
|
||||
"UglyToad.PdfPig.ConsoleRunner": {
|
||||
"commandName": "Project",
|
||||
"commandLineArgs": "\"C:\\temp\\pdfs\\archive\""
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>netcoreapp2.1</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\UglyToad.PdfPig\UglyToad.PdfPig.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,31 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 16
|
||||
VisualStudioVersion = 16.0.30907.101
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UglyToad.PdfPig.ConsoleRunner", "UglyToad.PdfPig.ConsoleRunner.csproj", "{0C3AEF4B-BEEA-44E5-89F6-12108AC3FE9F}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UglyToad.PdfPig", "..\..\src\UglyToad.PdfPig\UglyToad.PdfPig.csproj", "{4E37F4FA-4127-48C8-9151-235307C820E1}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{0C3AEF4B-BEEA-44E5-89F6-12108AC3FE9F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{0C3AEF4B-BEEA-44E5-89F6-12108AC3FE9F}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{0C3AEF4B-BEEA-44E5-89F6-12108AC3FE9F}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{0C3AEF4B-BEEA-44E5-89F6-12108AC3FE9F}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{4E37F4FA-4127-48C8-9151-235307C820E1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{4E37F4FA-4127-48C8-9151-235307C820E1}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{4E37F4FA-4127-48C8-9151-235307C820E1}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{4E37F4FA-4127-48C8-9151-235307C820E1}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {500F0DF0-D180-446D-9981-BC320C17DC7A}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
Reference in New Issue
Block a user