forked from VahidN/iTextSharp.LGPLv2.Core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestUtils.cs
121 lines (103 loc) · 3.73 KB
/
TestUtils.cs
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
using System;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using iTextSharp.text;
using iTextSharp.text.exceptions;
using iTextSharp.text.pdf;
namespace iTextSharp.LGPLv2.Core.FunctionalTests
{
public static class TestUtils
{
const string ITextExamplesFolder = "iTextExamples";
const string ResourcesFolder = "resources";
public static string Author => "VahidN";
public static string GetBaseDir()
{
var currentAssembly = typeof(TestUtils).GetTypeInfo().Assembly;
var root = Path.GetDirectoryName(currentAssembly.Location);
var idx = root.IndexOf($"{Path.DirectorySeparatorChar}bin", StringComparison.OrdinalIgnoreCase);
return root.Substring(0, idx);
}
public static string GetImagePath(string fileName)
{
return Path.Combine(GetBaseDir(), ITextExamplesFolder, ResourcesFolder, "img", fileName);
}
[MethodImpl(MethodImplOptions.NoInlining)]
public static string GetOutputFileName([CallerMemberName] string methodName = null)
{
return Path.Combine(GetOutputFolder(), $"{methodName}.pdf");
}
public static string GetOutputFolder()
{
var dir = Path.Combine(GetBaseDir(), "bin", "out");
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
return dir;
}
public static string GetPosterPath(string fileName)
{
return Path.Combine(GetBaseDir(), ITextExamplesFolder, ResourcesFolder, "posters", fileName);
}
public static string GetFontPath(string fileName)
{
return Path.Combine(GetBaseDir(), ITextExamplesFolder, ResourcesFolder, "fonts", fileName);
}
public static string GetTahomaFontPath()
{
return GetFontPath("tahoma.ttf");
}
public static string GetArialUnicodeMSFontPath()
{
return GetFontPath("arialuni.ttf");
}
public static string GetSimSunFontPath()
{
return GetFontPath("simsun.ttf");
}
public static string GetThaiFontPath()
{
return GetFontPath("thsarabunnew.ttf");
}
public static string GetTxtPath(string fileName)
{
return Path.Combine(GetBaseDir(), ITextExamplesFolder, ResourcesFolder, "txt", fileName);
}
public static string GetPdfsPath(string fileName)
{
return Path.Combine(GetBaseDir(), ITextExamplesFolder, ResourcesFolder, "pdfs", fileName);
}
public static Font GetUnicodeFont(
string fontName, string fontFilePath, float size, int style, BaseColor color)
{
if (!FontFactory.IsRegistered(fontName))
{
FontFactory.Register(fontFilePath);
}
return FontFactory.GetFont(fontName, BaseFont.IDENTITY_H, BaseFont.EMBEDDED, size, style, color);
}
public static void VerifyPdfFileIsReadable(byte[] file)
{
PdfReader reader = null;
try
{
reader = new PdfReader(file);
var author = reader.Info["Author"] as string;
if (string.IsNullOrWhiteSpace(author) || !author.Equals(Author))
{
throw new InvalidPdfException("This is not a valid PDF file.");
}
}
finally
{
reader?.Close();
}
}
public static void VerifyPdfFileIsReadable(string filePath)
{
VerifyPdfFileIsReadable(File.ReadAllBytes(filePath));
}
}
}