Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
BinaryPdfDocumentCodec.cs
1using System;
3using System.Threading.Tasks;
4using Waher.Content;
6
8{
13 {
17 public const string DefaultContentType = "application/pdf";
18
22 public const string DefaultFileExtension = "pdf";
23
27 public static readonly string[] PdfContentTypes = new string[]
28 {
30 };
31
35 public static readonly string[] PdfFileExtensions = new string[]
36 {
38 };
39
43 public string[] ContentTypes => PdfContentTypes;
44
49
56 public bool TryGetContentType(string FileExtension, out string ContentType)
57 {
58 if (string.Compare(FileExtension, DefaultFileExtension, true) == 0)
59 {
60 ContentType = DefaultContentType;
61 return true;
62 }
63 else
64 {
65 ContentType = string.Empty;
66 return false;
67 }
68 }
69
76 public bool TryGetFileExtension(string ContentType, out string FileExtension)
77 {
78 switch (ContentType.ToLower())
79 {
81 FileExtension = DefaultFileExtension;
82 return true;
83
84 default:
85 FileExtension = string.Empty;
86 return false;
87 }
88 }
89
96 public bool Decodes(string ContentType, out Grade Grade)
97 {
98 if (Array.IndexOf(PdfContentTypes, ContentType) >= 0)
99 {
100 Grade = Grade.Barely;
101 return true;
102 }
103 else
104 {
105 Grade = Grade.NotAtAll;
106 return false;
107 }
108 }
109
120 public Task<ContentResponse> DecodeAsync(string ContentType, byte[] Data, System.Text.Encoding Encoding,
121 KeyValuePair<string, string>[] Fields, Uri BaseUri, ICodecProgress Progress)
122 {
123 if (IsPdf(Data))
124 return Task.FromResult(new ContentResponse(ContentType, new BinaryPdfDocument(Data), Data));
125 else
126 return Task.FromResult(new ContentResponse(new Exception("Not a valid PDF document.")));
127 }
128
134 public static bool IsPdf(byte[] Data)
135 {
136 if (Data is null || Data.Length < 12)
137 return false;
138
139 string Header = System.Text.Encoding.ASCII.GetString(Data, 0, 10);
140 if (!Header.TrimStart().StartsWith("%PDF-"))
141 return false;
142
143 string Footer = System.Text.Encoding.ASCII.GetString(
144 Data, Data.Length - 10, 10);
145
146 if (!Footer.TrimEnd().EndsWith("%%EOF"))
147 return false;
148
149 return true;
150 }
151
159 public bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
160 {
161 if (InternetContent.IsAccepted(PdfContentTypes, AcceptedContentTypes) &&
162 (Object is BinaryPdfDocument))
163 {
164 Grade = Grade.Ok;
165 return true;
166 }
167 else
168 {
169 Grade = Grade.NotAtAll;
170 return false;
171 }
172 }
173
182 public Task<ContentResponse> EncodeAsync(object Object, System.Text.Encoding Encoding, ICodecProgress Progress,
183 params string[] AcceptedContentTypes)
184 {
185 if (Object is BinaryPdfDocument Doc)
186 return Task.FromResult(new ContentResponse(DefaultContentType, Doc, Doc.Document));
187 else
188 return Task.FromResult(new ContentResponse(new ArgumentException("Unable to encode as PDF document.", nameof(Object))));
189 }
190 }
191}
Contains information about a response to a content request.
Static class managing encoding and decoding of internet content.
static bool IsAccepted(string ContentType, params string[] AcceptedContentTypes)
Checks if a given content type is acceptable.
Encoder and Decoder of Binary PDF Documents.
bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
If the encoder encodes a given object.
static bool IsPdf(byte[] Data)
Checks if a Binary BLOB is a PDF document.
string[] FileExtensions
Supported file extensions.
bool TryGetFileExtension(string ContentType, out string FileExtension)
Tries to get the file extension of an item, given its Content-Type.
bool Decodes(string ContentType, out Grade Grade)
If the decoder decodes an object with a given content type.
static readonly string[] PdfContentTypes
Binary content types.
Task< ContentResponse > EncodeAsync(object Object, System.Text.Encoding Encoding, ICodecProgress Progress, params string[] AcceptedContentTypes)
Encodes an object.
bool TryGetContentType(string FileExtension, out string ContentType)
Tries to get the content type of an item, given its file extension.
Task< ContentResponse > DecodeAsync(string ContentType, byte[] Data, System.Text.Encoding Encoding, KeyValuePair< string, string >[] Fields, Uri BaseUri, ICodecProgress Progress)
Decodes an object.
static readonly string[] PdfFileExtensions
Binary content types.
Interface for reporting progress about an encoding or decoding.
Basic interface for Internet Content decoders. A class implementing this interface and having a defau...
Basic interface for Internet Content encoders. A class implementing this interface and having a defau...
Grade
Grade enumeration
Definition: Grade.cs:7