forked from wmjordan/PDFPatcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPdfModelHelper.cs
133 lines (128 loc) · 3.95 KB
/
PdfModelHelper.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
122
123
124
125
126
127
128
129
130
131
132
133
using System;
using System.IO;
using System.Text;
using iTextSharp.text.pdf;
namespace PDFPatcher.Model
{
static class PdfModelHelper
{
internal static T Locate<T>(this PdfDictionary source, params object[] path) where T : PdfObject {
PdfObject s = source;
if (s == null) {
return null;
}
PdfName n;
foreach (var item in path) {
n = item as PdfName;
if (s == null) {
return null;
}
if (n != null) {
if (s.Type != PdfObject.DICTIONARY && s.Type != PdfObject.STREAM) {
return null;
}
s = (s as PdfDictionary).GetDirectObject(n);
continue;
}
if (item is Int32 == false) {
throw new ArgumentException("参数类型必须为 Int32 或 PdfName");
}
int i = (int)item;
if (s.Type != PdfObject.ARRAY) {
return null;
}
s = (s as PdfArray).GetDirectObject(i);
}
return s as T;
}
internal static T Locate<T>(this PdfDictionary source, params PdfName[] path) where T : PdfObject {
return source.Locate<T>(true, path);
}
internal static T Locate<T>(this PdfDictionary source, bool resolveRef, params PdfName[] path) where T : PdfObject {
PdfObject o = null;
foreach (var item in path) {
if (source == null) {
return null;
}
o = resolveRef ? source.GetDirectObject(item) : source.Get(item);
source = o as PdfDictionary;
}
return o as T;
}
internal static T Locate<T>(this PdfArray source, int index) where T : PdfObject {
return source.Locate<T>(true, index);
}
internal static T Locate<T>(this PdfArray source, bool resolveRef, int index) where T : PdfObject {
if (source == null) {
return null;
}
return (resolveRef ? source.GetDirectObject(index) : source[index]) as T;
}
internal static T CastAs<T>(this PdfIndirectReference pdfRef) where T : PdfObject {
return PdfReader.GetPdfObject(pdfRef) as T;
}
internal static bool ValueIs(this PdfNumber obj, double value) {
return obj != null && obj.DoubleValue == value;
}
internal static bool ValueIs(this PdfBoolean obj, bool value) {
return obj != null && obj.BooleanValue == value;
}
internal static bool ValueIs(this PdfObject obj, PdfName value) {
return value.Equals(obj);
}
internal static int TryGetInt32(this PdfDictionary source, PdfName key, int defaultValue) {
var w = source.GetAsNumber(key);
return w?.IntValue ?? defaultValue;
}
internal static bool TryGetBoolean(this PdfDictionary source, PdfName key, bool defaultValue) {
var b = source.GetAsBoolean(key);
return b?.BooleanValue ?? defaultValue;
}
internal static string Decode(this PdfString text, Encoding encoding) {
if (text == null) {
return String.Empty;
}
var bytes = text.GetBytes();
using (MemoryStream ms = new MemoryStream(bytes)) {
if (encoding == null) {
if (bytes.Length >= 2 && (bytes[0] == 0xFF && bytes[1] == 0xFE || bytes[0] == 0xFE && bytes[1] == 0xFF)) {
using (TextReader r = new StreamReader(ms, true)) {
return r.ReadToEnd();
}
}
else {
return PdfEncodings.ConvertToString(bytes, PdfObject.TEXT_PDFDOCENCODING);
}
}
else {
// 忽略字节顺序标记
if (bytes.Length >= 2 && (bytes[0] == 0xFF && bytes[1] == 0xFE || bytes[0] == 0xFE && bytes[1] == 0xFF)) {
ms.Position += 2;
}
else if (bytes.Length >= 3 && bytes[0] == 0xef && bytes[1] == 0xbb && bytes[2] == 0xbf) {
ms.Position += 3;
}
else if (bytes.Length >= 4 && bytes[0] == 0 && bytes[1] == 0 && bytes[2] == 0xfe && bytes[3] == 0xff) {
ms.Position += 4;
}
using (TextReader r = new StreamReader(ms, encoding)) {
return r.ReadToEnd();
}
}
}
}
internal static PdfString ToPdfString(this string text) {
if (String.IsNullOrEmpty(text)) {
return new PdfString();
}
bool u = false;
foreach (var c in text) {
if (c > 127) {
u = true;
break;
}
}
return u ? new PdfString(text, PdfObject.TEXT_UNICODE) : new PdfString(text);
}
}
}