-
-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathPdfReadDocument.XmpMetadata.cs
More file actions
167 lines (141 loc) · 7.44 KB
/
Copy pathPdfReadDocument.XmpMetadata.cs
File metadata and controls
167 lines (141 loc) · 7.44 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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
using System.Xml;
using System.Xml.Linq;
using OfficeIMO.Pdf.Filters;
namespace OfficeIMO.Pdf;
public sealed partial class PdfReadDocument {
private const string DublinCoreNamespaceUri = "http://purl.org/dc/elements/1.1/";
private const string PdfAIdentificationNamespaceUri = "http://www.aiim.org/pdfa/ns/id/";
/// <summary>Maximum decoded XMP metadata size parsed as XML.</summary>
public const int MaxXmpMetadataBytes = 4_000_000;
/// <summary>Catalog XMP metadata stream discovered from /Metadata.</summary>
public PdfXmpMetadataInfo? XmpMetadata { get; }
private PdfXmpMetadataInfo? ExtractXmpMetadata() {
PdfDictionary? catalog = FindCatalog();
if (catalog is null ||
!catalog.Items.TryGetValue("Metadata", out PdfObject? metadataObject)) {
return null;
}
int? objectNumber = metadataObject is PdfReference reference ? reference.ObjectNumber : null;
if (ResolveObject(metadataObject) is not PdfStream stream) {
return null;
}
bool decodedWithinLimit = StreamDecoder.TryDecode(stream.Dictionary, stream.Data, MaxXmpMetadataBytes, out byte[] decoded, _objects);
string? rawXml = decodedWithinLimit ? DecodeMetadataText(decoded) : null;
int decodedSizeBytes = decodedWithinLimit ? decoded.Length : MaxXmpMetadataBytes + 1;
XDocument? document = rawXml is null ? null : TryParseXml(rawXml);
return new PdfXmpMetadataInfo(
objectNumber,
TryReadName(stream.Dictionary, "Subtype"),
TryReadStreamFilter(stream),
stream.Data.Length,
decodedSizeBytes,
StreamDecoder.GetUnsupportedFilters(stream.Dictionary, _objects).AsReadOnly(),
rawXml,
document is not null,
document is null ? null : ReadAltText(document, "title"),
document is null ? null : ReadFirstCollectionText(document, "creator"),
document is null ? null : ReadAltText(document, "description"),
document is null ? Array.Empty<string>() : ReadCollectionText(document, "subject"),
document is null ? null : ReadElementText(document, "Producer"),
document is null ? null : ReadElementText(document, "Keywords"),
document is null ? null : ReadIntegerElementByNamespace(document, "part", PdfAIdentificationNamespaceUri),
document is null ? null : ReadElementTextByNamespace(document, "conformance", PdfAIdentificationNamespaceUri),
document is null ? null : ReadIntegerElementByNamespace(document, "part", PdfUaIdentification.NamespaceUri),
document is null ? null : ReadElementTextByNamespace(document, "DocumentType", PdfElectronicInvoiceMetadata.FacturXNamespaceUri),
document is null ? null : ReadElementTextByNamespace(document, "DocumentFileName", PdfElectronicInvoiceMetadata.FacturXNamespaceUri),
document is null ? null : ReadElementTextByNamespace(document, "Version", PdfElectronicInvoiceMetadata.FacturXNamespaceUri),
document is null ? null : ReadElementTextByNamespace(document, "ConformanceLevel", PdfElectronicInvoiceMetadata.FacturXNamespaceUri));
}
private static string? DecodeMetadataText(byte[] data) {
if (data.Length == 0) {
return string.Empty;
}
if (data.Length >= 3 &&
data[0] == 0xEF &&
data[1] == 0xBB &&
data[2] == 0xBF) {
return Encoding.UTF8.GetString(data, 3, data.Length - 3);
}
if (data.Length >= 2 &&
data[0] == 0xFE &&
data[1] == 0xFF) {
return Encoding.BigEndianUnicode.GetString(data, 2, data.Length - 2);
}
if (data.Length >= 2 &&
data[0] == 0xFF &&
data[1] == 0xFE) {
return Encoding.Unicode.GetString(data, 2, data.Length - 2);
}
return Encoding.UTF8.GetString(data);
}
private static XDocument? TryParseXml(string? rawXml) {
if (string.IsNullOrWhiteSpace(rawXml)) {
return null;
}
try {
var settings = new XmlReaderSettings {
DtdProcessing = DtdProcessing.Prohibit,
MaxCharactersInDocument = MaxXmpMetadataBytes,
XmlResolver = null
};
using var stringReader = new StringReader(rawXml!);
using XmlReader reader = XmlReader.Create(stringReader, settings);
return XDocument.Load(reader, LoadOptions.None);
} catch (Exception ex) when (ex is System.Xml.XmlException || ex is InvalidOperationException) {
return null;
}
}
private static string? ReadAltText(XDocument document, string localName) {
XElement? element = FindElementByNamespace(document, localName, DublinCoreNamespaceUri);
if (element is null) {
return null;
}
XElement? defaultItem = element
.Descendants()
.FirstOrDefault(e => e.Name.LocalName == "li" &&
string.Equals((string?)e.Attribute(XNamespace.Xml + "lang"), "x-default", StringComparison.OrdinalIgnoreCase));
return NormalizeXmlText(defaultItem?.Value) ?? NormalizeXmlText(element.Descendants().FirstOrDefault(e => e.Name.LocalName == "li")?.Value);
}
private static string? ReadFirstCollectionText(XDocument document, string localName) {
IReadOnlyList<string> values = ReadCollectionText(document, localName);
return values.Count == 0 ? null : values[0];
}
private static IReadOnlyList<string> ReadCollectionText(XDocument document, string localName) {
XElement? element = FindElementByNamespace(document, localName, DublinCoreNamespaceUri);
if (element is null) {
return Array.Empty<string>();
}
var values = new List<string>();
foreach (XElement item in element.Descendants().Where(e => e.Name.LocalName == "li")) {
string? text = NormalizeXmlText(item.Value);
if (text is not null) {
values.Add(text);
}
}
return values.Count == 0 ? Array.Empty<string>() : values.AsReadOnly();
}
private static string? ReadElementText(XDocument document, string localName) {
return NormalizeXmlText(document.Descendants().FirstOrDefault(e => e.Name.LocalName == localName)?.Value);
}
private static string? ReadElementTextByNamespace(XDocument document, string localName, string namespaceUri) {
XElement? element = FindElementByNamespace(document, localName, namespaceUri);
return NormalizeXmlText(element?.Value);
}
private static int? ReadIntegerElementByNamespace(XDocument document, string localName, string namespaceUri) {
string? value = ReadElementTextByNamespace(document, localName, namespaceUri);
return int.TryParse(value, System.Globalization.NumberStyles.Integer, System.Globalization.CultureInfo.InvariantCulture, out int result)
? result
: null;
}
private static XElement? FindElementByNamespace(XDocument document, string localName, string namespaceUri) {
return document.Descendants().FirstOrDefault(e =>
e.Name.LocalName == localName &&
string.Equals(e.Name.NamespaceName, namespaceUri, StringComparison.Ordinal));
}
private static string? NormalizeXmlText(string? value) {
if (string.IsNullOrWhiteSpace(value)) {
return null;
}
return value!.Trim();
}
}