mirror of
https://github.com/UglyToad/PdfPig.git
synced 2026-03-10 00:23:29 +08:00
Compare commits
2 Commits
nullabilit
...
support-co
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3131dae49e | ||
|
|
52c0635273 |
@@ -1,4 +1,5 @@
|
||||
using System.Buffers;
|
||||
using System;
|
||||
using System.Buffers;
|
||||
|
||||
namespace UglyToad.PdfPig.Core;
|
||||
|
||||
|
||||
@@ -71,7 +71,7 @@
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override bool Equals(object? obj)
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
return obj is IndirectReference other && Equals(other);
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
/// <summary>
|
||||
/// Convert the string to bytes using the ISO 8859-1 encoding.
|
||||
/// </summary>
|
||||
public static byte[]? StringAsLatin1Bytes(string? s)
|
||||
public static byte[] StringAsLatin1Bytes(string s)
|
||||
{
|
||||
if (s == null)
|
||||
{
|
||||
|
||||
@@ -264,7 +264,7 @@
|
||||
/// Try to convert raw bytes to a PdfDocEncoding encoded string. If unsupported characters are encountered
|
||||
/// meaning we cannot safely round-trip the value to bytes this will instead return false.
|
||||
/// </summary>
|
||||
public static bool TryConvertBytesToString(ReadOnlySpan<byte> bytes, out string? result)
|
||||
public static bool TryConvertBytesToString(ReadOnlySpan<byte> bytes, out string result)
|
||||
{
|
||||
result = null;
|
||||
if (bytes.Length == 0)
|
||||
|
||||
@@ -70,7 +70,7 @@
|
||||
/// <summary>
|
||||
/// Returns a value indicating whether this <see cref="PdfLine"/> is equal to a specified <see cref="PdfLine"/> .
|
||||
/// </summary>
|
||||
public override bool Equals(object? obj)
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
return obj is PdfLine other && Equals(other);
|
||||
}
|
||||
|
||||
@@ -83,7 +83,7 @@
|
||||
/// <summary>
|
||||
/// Returns a value indicating whether this <see cref="PdfPoint"/> is equal to a specified <see cref="PdfPoint"/> .
|
||||
/// </summary>
|
||||
public override bool Equals(object? obj)
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
return obj is PdfPoint other && Equals(other);
|
||||
}
|
||||
|
||||
@@ -177,7 +177,7 @@
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override bool Equals(object? obj)
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
return obj is PdfRectangle other && Equals(other);
|
||||
}
|
||||
|
||||
@@ -241,8 +241,8 @@
|
||||
public bool IsClosed()
|
||||
{
|
||||
var filteredCount = 0;
|
||||
IPathCommand? last = null;
|
||||
IPathCommand? first = null;
|
||||
IPathCommand last = null;
|
||||
IPathCommand first = null;
|
||||
for (int i = Commands.Count - 1; i >= 0; i--)
|
||||
{
|
||||
var cmd = Commands[i];
|
||||
@@ -376,14 +376,14 @@
|
||||
/// Gets a <see cref="PdfRectangle"/> which entirely contains the geometry of the defined path.
|
||||
/// </summary>
|
||||
/// <returns>For paths which don't define any geometry this returns <see langword="null"/>.</returns>
|
||||
public static PdfRectangle? GetBoundingRectangle(IReadOnlyList<PdfSubpath>? path)
|
||||
public static PdfRectangle? GetBoundingRectangle(IReadOnlyList<PdfSubpath> path)
|
||||
{
|
||||
if (path == null || path.Count == 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var bboxes = path.Select(x => x.GetBoundingRectangle()).Where(x => x.HasValue).Select(x => x!.Value).ToList();
|
||||
var bboxes = path.Select(x => x.GetBoundingRectangle()).Where(x => x.HasValue).Select(x => x.Value).ToList();
|
||||
if (bboxes.Count == 0)
|
||||
{
|
||||
return null;
|
||||
@@ -433,7 +433,7 @@
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override bool Equals(object? obj)
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
return (obj is Close);
|
||||
}
|
||||
@@ -479,7 +479,7 @@
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override bool Equals(object? obj)
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (obj is Move move)
|
||||
{
|
||||
@@ -545,7 +545,7 @@
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override bool Equals(object? obj)
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (obj is Line line)
|
||||
{
|
||||
@@ -651,7 +651,7 @@
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override bool Equals(object? obj)
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (obj is QuadraticBezierCurve curve)
|
||||
{
|
||||
@@ -809,7 +809,7 @@
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override bool Equals(object? obj)
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (obj is CubicBezierCurve curve)
|
||||
{
|
||||
@@ -944,7 +944,7 @@
|
||||
/// <summary>
|
||||
/// Compares two <see cref="PdfSubpath"/>s for equality. Paths will only be considered equal if the commands which construct the paths are in the same order.
|
||||
/// </summary>
|
||||
public override bool Equals(object? obj)
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (!(obj is PdfSubpath path) || Commands.Count != path.Commands.Count)
|
||||
{
|
||||
|
||||
@@ -463,7 +463,7 @@
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override bool Equals(object? obj)
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
return obj is TransformationMatrix other && Equals(other);
|
||||
}
|
||||
|
||||
@@ -7,8 +7,6 @@
|
||||
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||
<SignAssembly>true</SignAssembly>
|
||||
<AssemblyOriginatorKeyFile>..\pdfpig.snk</AssemblyOriginatorKeyFile>
|
||||
<Nullable>enable</Nullable>
|
||||
<WarningsAsErrors>nullable</WarningsAsErrors>
|
||||
</PropertyGroup>
|
||||
<ItemGroup Condition="'$(TargetFramework)'=='net462'">
|
||||
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
|
||||
|
||||
@@ -89,7 +89,7 @@ namespace UglyToad.PdfPig.Core
|
||||
/// <inheritdoc />
|
||||
public override bool TryGetSecond(out B b)
|
||||
{
|
||||
b = default!;
|
||||
b = default(B);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -135,7 +135,7 @@ namespace UglyToad.PdfPig.Core
|
||||
/// <inheritdoc />
|
||||
public override bool TryGetFirst(out A a)
|
||||
{
|
||||
a = default!;
|
||||
a = default(A);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Console = System.Console;
|
||||
|
||||
@@ -7,6 +12,128 @@ namespace UglyToad.PdfPig.ConsoleRunner
|
||||
{
|
||||
public static class Program
|
||||
{
|
||||
private class OptionalArg
|
||||
{
|
||||
public required string ShortSymbol { get; init; }
|
||||
|
||||
public required string Symbol { get; init; }
|
||||
|
||||
public required bool SupportsValue { get; init; }
|
||||
|
||||
public string? Value { get; set; }
|
||||
}
|
||||
|
||||
private class ParsedArgs
|
||||
{
|
||||
public required IReadOnlyList<OptionalArg> SuppliedArgs { get; init; }
|
||||
|
||||
public required string SuppliedDirectoryPath { get; init; }
|
||||
}
|
||||
|
||||
private const string FileSymbol = "f";
|
||||
private const string RepeatSymbol = "r";
|
||||
|
||||
private static IReadOnlyList<OptionalArg> GetSupportedArgs() =>
|
||||
[
|
||||
new OptionalArg
|
||||
{
|
||||
SupportsValue = false,
|
||||
ShortSymbol = "nr",
|
||||
Symbol = "no-recursion"
|
||||
},
|
||||
new OptionalArg
|
||||
{
|
||||
SupportsValue = true,
|
||||
ShortSymbol = "o",
|
||||
Symbol = "output"
|
||||
},
|
||||
new OptionalArg
|
||||
{
|
||||
SupportsValue = true,
|
||||
ShortSymbol = "l",
|
||||
Symbol = "limit"
|
||||
},
|
||||
new OptionalArg
|
||||
{
|
||||
SupportsValue = true,
|
||||
ShortSymbol = "f",
|
||||
Symbol = "file"
|
||||
},
|
||||
new OptionalArg
|
||||
{
|
||||
SupportsValue = true,
|
||||
ShortSymbol = "r",
|
||||
Symbol = "repeats"
|
||||
}
|
||||
];
|
||||
|
||||
private static bool TryParseArgs(
|
||||
string[] args,
|
||||
[NotNullWhen(true)] out ParsedArgs? parsed)
|
||||
{
|
||||
parsed = null;
|
||||
string? path = null;
|
||||
var suppliedOpts = new List<OptionalArg>();
|
||||
|
||||
var opts = GetSupportedArgs();
|
||||
|
||||
for (var i = 0; i < args.Length; i++)
|
||||
{
|
||||
var str = args[i];
|
||||
|
||||
var isOptFlag = str.StartsWith('-');
|
||||
|
||||
if (!isOptFlag)
|
||||
{
|
||||
if (path == null)
|
||||
{
|
||||
path = str;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var item = opts.SingleOrDefault(x =>
|
||||
string.Equals("-" + x.ShortSymbol, str, StringComparison.OrdinalIgnoreCase)
|
||||
|| string.Equals("--" + x.Symbol, str, StringComparison.OrdinalIgnoreCase));
|
||||
|
||||
if (item == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (item.SupportsValue)
|
||||
{
|
||||
if (i == args.Length - 1)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
i++;
|
||||
item.Value = args[i];
|
||||
}
|
||||
|
||||
suppliedOpts.Add(item);
|
||||
}
|
||||
}
|
||||
|
||||
if (path == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
parsed = new ParsedArgs
|
||||
{
|
||||
SuppliedArgs = suppliedOpts,
|
||||
SuppliedDirectoryPath = path
|
||||
};
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static int Main(string[] args)
|
||||
{
|
||||
if (args.Length == 0)
|
||||
@@ -15,71 +142,140 @@ namespace UglyToad.PdfPig.ConsoleRunner
|
||||
return 7;
|
||||
}
|
||||
|
||||
var path = args[0];
|
||||
|
||||
if (!Directory.Exists(path))
|
||||
if (!TryParseArgs(args, out var parsed))
|
||||
{
|
||||
Console.WriteLine($"The provided path is not a valid directory: {path}.");
|
||||
var strJoined = string.Join(" ", args);
|
||||
Console.WriteLine($"Unrecognized arguments passed: {strJoined}");
|
||||
return 7;
|
||||
}
|
||||
|
||||
var maxCount = default(int?);
|
||||
|
||||
if (args.Length > 1 && int.TryParse(args[1], out var countIn))
|
||||
if (!Directory.Exists(parsed.SuppliedDirectoryPath))
|
||||
{
|
||||
maxCount = countIn;
|
||||
Console.WriteLine($"The provided path is not a valid directory: {parsed.SuppliedDirectoryPath}.");
|
||||
return 7;
|
||||
}
|
||||
|
||||
int? maxCount = null;
|
||||
var limit = parsed.SuppliedArgs.SingleOrDefault(x => x.ShortSymbol == "l");
|
||||
if (limit?.Value != null && int.TryParse(limit.Value, CultureInfo.InvariantCulture, out var maxCountArg))
|
||||
{
|
||||
Console.WriteLine($"Limiting input files to first: {maxCountArg}");
|
||||
maxCount = maxCountArg;
|
||||
}
|
||||
|
||||
var noRecursionMode = parsed.SuppliedArgs.Any(x => x.ShortSymbol == "nr");
|
||||
var outputOpt = parsed.SuppliedArgs.SingleOrDefault(x => x.ShortSymbol == "o" && x.Value != null);
|
||||
|
||||
var fileOpt = parsed.SuppliedArgs.SingleOrDefault(x => x.ShortSymbol == FileSymbol && x.Value != null);
|
||||
|
||||
var hasError = false;
|
||||
var errorBuilder = new StringBuilder();
|
||||
var fileList = Directory.GetFiles(path, "*.pdf", SearchOption.AllDirectories);
|
||||
var fileList = Directory.GetFiles(
|
||||
parsed.SuppliedDirectoryPath,
|
||||
"*.pdf",
|
||||
noRecursionMode ? SearchOption.TopDirectoryOnly : SearchOption.AllDirectories)
|
||||
.OrderBy(x => x).ToList();
|
||||
var runningCount = 0;
|
||||
|
||||
Console.WriteLine($"Found {fileList.Length} files.");
|
||||
|
||||
Console.WriteLine($"{GetCleanFilename("File")}| Size\t| Words\t| Pages");
|
||||
|
||||
foreach (var file in fileList)
|
||||
if (fileOpt?.Value != null)
|
||||
{
|
||||
if (maxCount.HasValue && runningCount >= maxCount)
|
||||
{
|
||||
break;
|
||||
}
|
||||
fileList = fileList.Where(x => x.EndsWith(fileOpt.Value, StringComparison.OrdinalIgnoreCase)).ToList();
|
||||
}
|
||||
|
||||
try
|
||||
var repeatOpt = parsed.SuppliedArgs.SingleOrDefault(x => x.ShortSymbol == RepeatSymbol);
|
||||
|
||||
var repeats = 1;
|
||||
if (repeatOpt?.Value != null && int.TryParse(repeatOpt.Value, CultureInfo.InvariantCulture, out repeats))
|
||||
{
|
||||
}
|
||||
|
||||
Console.WriteLine($"Found {fileList.Count} files.");
|
||||
Console.WriteLine();
|
||||
|
||||
PrintTableColumns("File", "Size", "Words", "Pages", "Open cost (μs)", "Total cost (μs)", "Page cost (μs)");
|
||||
|
||||
var dataList = new List<DataRecord>();
|
||||
|
||||
var sw = new Stopwatch();
|
||||
for (int i = 0; i < repeats; i++)
|
||||
{
|
||||
foreach (var file in fileList)
|
||||
{
|
||||
var numWords = 0;
|
||||
var numPages = 0;
|
||||
using (var pdfDocument = PdfDocument.Open(file))
|
||||
if (maxCount.HasValue && runningCount >= maxCount)
|
||||
{
|
||||
foreach (var page in pdfDocument.GetPages())
|
||||
{
|
||||
numPages++;
|
||||
foreach (var word in page.GetWords())
|
||||
{
|
||||
if (word != null)
|
||||
{
|
||||
numWords++;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
var filename = Path.GetFileName(file);
|
||||
try
|
||||
{
|
||||
var numWords = 0;
|
||||
var numPages = 0;
|
||||
long openMicros;
|
||||
long totalPageMicros;
|
||||
|
||||
var size = new FileInfo(file);
|
||||
sw.Reset();
|
||||
sw.Start();
|
||||
|
||||
Console.WriteLine($"{GetCleanFilename(filename)}| {size.Length}\t| {numWords}\t| {numPages}");
|
||||
using (var pdfDocument = PdfDocument.Open(file))
|
||||
{
|
||||
sw.Stop();
|
||||
|
||||
openMicros = sw.Elapsed.Microseconds;
|
||||
|
||||
sw.Start();
|
||||
|
||||
foreach (var page in pdfDocument.GetPages())
|
||||
{
|
||||
numPages++;
|
||||
foreach (var word in page.GetWords())
|
||||
{
|
||||
if (word != null)
|
||||
{
|
||||
numWords++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sw.Stop();
|
||||
totalPageMicros = sw.Elapsed.Microseconds;
|
||||
}
|
||||
|
||||
var filename = Path.GetFileName(file);
|
||||
|
||||
var size = new FileInfo(file);
|
||||
|
||||
var item = new DataRecord
|
||||
{
|
||||
FileName = filename,
|
||||
OpenCostMicros = openMicros,
|
||||
Pages = numPages,
|
||||
Size = size.Length,
|
||||
Words = numWords,
|
||||
TotalCostMicros = totalPageMicros + openMicros,
|
||||
PerPageMicros = Math.Round(totalPageMicros / (double)Math.Max(numPages, 1), 2)
|
||||
};
|
||||
|
||||
dataList.Add(item);
|
||||
|
||||
PrintTableColumns(
|
||||
item.FileName,
|
||||
item.Size,
|
||||
item.Words,
|
||||
item.Pages,
|
||||
item.OpenCostMicros,
|
||||
item.TotalCostMicros,
|
||||
item.PerPageMicros);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
hasError = true;
|
||||
errorBuilder.AppendLine($"Parsing document {file} failed due to an error.")
|
||||
.Append(ex)
|
||||
.AppendLine();
|
||||
}
|
||||
|
||||
runningCount++;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
hasError = true;
|
||||
errorBuilder.AppendLine($"Parsing document {file} failed due to an error.")
|
||||
.Append(ex)
|
||||
.AppendLine();
|
||||
}
|
||||
|
||||
runningCount++;
|
||||
}
|
||||
|
||||
if (hasError)
|
||||
@@ -88,12 +284,71 @@ namespace UglyToad.PdfPig.ConsoleRunner
|
||||
return 5;
|
||||
}
|
||||
|
||||
if (outputOpt != null && outputOpt.Value != null)
|
||||
{
|
||||
WriteOutput(outputOpt.Value, dataList);
|
||||
}
|
||||
|
||||
Console.WriteLine("Complete! :)");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
private static string GetCleanFilename(string name, int maxLength = 30)
|
||||
private static void WriteOutput(string outPath, IReadOnlyList<DataRecord> records)
|
||||
{
|
||||
using var fs = File.OpenWrite(outPath);
|
||||
using var sw = new StreamWriter(fs);
|
||||
|
||||
sw.WriteLine("File,Size,Words,Pages,Open Cost,Total Cost,Per Page");
|
||||
foreach (var record in records)
|
||||
{
|
||||
var sizeStr = record.Size.ToString("D", CultureInfo.InvariantCulture);
|
||||
var wordsStr = record.Words.ToString("D", CultureInfo.InvariantCulture);
|
||||
var pagesStr = record.Pages.ToString("D", CultureInfo.InvariantCulture);
|
||||
var openCostStr = record.OpenCostMicros.ToString("D", CultureInfo.InvariantCulture);
|
||||
var totalCostStr = record.TotalCostMicros.ToString("D", CultureInfo.InvariantCulture);
|
||||
var ppcStr = record.PerPageMicros.ToString("F2", CultureInfo.InvariantCulture);
|
||||
|
||||
var numericPartsStr = string.Join(",",
|
||||
[
|
||||
sizeStr,
|
||||
wordsStr,
|
||||
pagesStr,
|
||||
openCostStr,
|
||||
totalCostStr,
|
||||
ppcStr
|
||||
]);
|
||||
|
||||
sw.WriteLine($"\"{record.FileName}\",{numericPartsStr}");
|
||||
}
|
||||
|
||||
sw.Flush();
|
||||
}
|
||||
|
||||
private static void PrintTableColumns(params object[] values)
|
||||
{
|
||||
for (var i = 0; i < values.Length; i++)
|
||||
{
|
||||
var value = values[i];
|
||||
var valueStr = value.ToString();
|
||||
|
||||
var cleaned = GetCleanStr(valueStr ?? string.Empty);
|
||||
|
||||
var padChars = 16 - cleaned.Length;
|
||||
|
||||
var padding = padChars > 0 ? new string(' ', padChars) : string.Empty;
|
||||
|
||||
var padded = cleaned + padding;
|
||||
|
||||
Console.Write("| ");
|
||||
|
||||
Console.Write(padded);
|
||||
}
|
||||
|
||||
Console.WriteLine();
|
||||
}
|
||||
|
||||
private static string GetCleanStr(string name, int maxLength = 16)
|
||||
{
|
||||
if (name.Length <= maxLength)
|
||||
{
|
||||
@@ -105,4 +360,21 @@ namespace UglyToad.PdfPig.ConsoleRunner
|
||||
return name.Substring(0, maxLength);
|
||||
}
|
||||
}
|
||||
|
||||
internal class DataRecord
|
||||
{
|
||||
public required string FileName { get; init; }
|
||||
|
||||
public required long Size { get; init; }
|
||||
|
||||
public required int Words { get; init; }
|
||||
|
||||
public required int Pages { get; init; }
|
||||
|
||||
public required long OpenCostMicros { get; init; }
|
||||
|
||||
public required long TotalCostMicros { get; init; }
|
||||
|
||||
public required double PerPageMicros { get; init; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
{
|
||||
"profiles": {
|
||||
"UglyToad.PdfPig.ConsoleRunner": {
|
||||
"commandName": "Project",
|
||||
"commandLineArgs": "\"C:\\temp\\pdfs\\archive\""
|
||||
"commandName": "Project",
|
||||
"commandLineArgs": "\"C:\\temp\\pdfs\\archive\""
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@
|
||||
<LangVersion>latest</LangVersion>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
Reference in New Issue
Block a user