-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
83 lines (70 loc) · 3.34 KB
/
Copy pathProgram.cs
File metadata and controls
83 lines (70 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using System;
using System.IO;
using GroupDocs.Parser;
using GroupDocs.Samples.ExtractAnnotationsFromPdf.Methods;
namespace GroupDocs.Samples.ExtractAnnotationsFromPdf
{
public static class Program
{
private const string LicensePath = @"YOUR-LICENSE-PATH-HERE";
private static readonly string ProjectRoot = Directory.GetCurrentDirectory();
private static readonly string InputDir = Path.Combine(ProjectRoot, "resources");
private static readonly string OutputDir = Path.Combine(ProjectRoot, "output");
public static int Main()
{
try
{
SetLicense();
Directory.CreateDirectory(OutputDir);
var pdfPath = Path.Combine(InputDir, "document-with-annotations.pdf");
if (!File.Exists(pdfPath))
{
Console.WriteLine($"FAIL missing input file at {InputDir}");
return 2;
}
var supported = CheckAnnotationSupport.Run(pdfPath);
Assert(supported, $"CheckAnnotationSupport reported annotations supported = {supported}");
var allAnnotations = ExtractAllAnnotations.Run(pdfPath);
Assert(allAnnotations.Count > 0, $"ExtractAllAnnotations returned {allAnnotations.Count} annotations");
var byPage = ExtractAnnotationsByPage.Run(pdfPath);
Assert(byPage.Count > 0, $"ExtractAnnotationsByPage reported {byPage.Count} page-tagged annotations");
var textWithAnnotations = ExtractTextWithAnnotations.Run(pdfPath);
Assert(!string.IsNullOrEmpty(textWithAnnotations), $"ExtractTextWithAnnotations returned {textWithAnnotations.Length} characters");
var csvPath = Path.Combine(OutputDir, "annotations.csv");
ExportAnnotationsToCsv.Run(byPage, csvPath);
Assert(File.Exists(csvPath) && new FileInfo(csvPath).Length > 0, $"ExportAnnotationsToCsv wrote {new FileInfo(csvPath).Length} bytes to annotations.csv");
var jsonPath = Path.Combine(OutputDir, "annotations.json");
ExportAnnotationsToJson.Run(byPage, jsonPath);
Assert(File.Exists(jsonPath) && new FileInfo(jsonPath).Length > 0, $"ExportAnnotationsToJson wrote {new FileInfo(jsonPath).Length} bytes to annotations.json");
Console.WriteLine();
Console.WriteLine("ALL PASS");
return 0;
}
catch (Exception ex)
{
Console.WriteLine($"FAIL {ex.GetType().Name}: {ex.Message}");
Console.WriteLine(ex.StackTrace);
return 1;
}
}
private static void SetLicense()
{
if (!File.Exists(LicensePath))
{
Console.WriteLine($"WARN license file not found at {LicensePath}; running in evaluation mode");
return;
}
var license = new License();
license.SetLicense(LicensePath);
Console.WriteLine("License applied");
}
private static void Assert(bool condition, string message)
{
if (!condition)
{
throw new Exception($"assert failed: {message}");
}
Console.WriteLine($"PASS {message}");
}
}
}