|
| 1 | +namespace PdfSharpCore.Pdf.Advanced |
| 2 | +{ |
| 3 | + /// <summary> |
| 4 | + /// Represent a file stream embedded in the PDF document |
| 5 | + /// </summary> |
| 6 | + public class PdfEmbeddedFile : PdfDictionary |
| 7 | + { |
| 8 | + private readonly PdfDictionary paramsDictionary; |
| 9 | + |
| 10 | + public PdfEmbeddedFile(PdfDocument document) |
| 11 | + : base(document) |
| 12 | + { |
| 13 | + this.paramsDictionary = new PdfDictionary(); |
| 14 | + |
| 15 | + Elements.SetName(Keys.Type, "/EmbeddedFile"); |
| 16 | + Elements.SetObject(Keys.Params, paramsDictionary); |
| 17 | + } |
| 18 | + |
| 19 | + public PdfEmbeddedFile(PdfDocument document, byte[] bytes, string checksum = null) |
| 20 | + : this(document) |
| 21 | + { |
| 22 | + this.CreateStreamAndSetProperties(bytes, checksum); |
| 23 | + } |
| 24 | + |
| 25 | + public void CreateStreamAndSetProperties(byte[] bytes, string checksum = null) |
| 26 | + { |
| 27 | + this.CreateStream(bytes); |
| 28 | + |
| 29 | + this.paramsDictionary.Elements.SetInteger(Keys.Size, bytes.Length); |
| 30 | + |
| 31 | + if (string.IsNullOrEmpty(checksum)) |
| 32 | + this.paramsDictionary.Elements.Remove(Keys.CheckSum); |
| 33 | + else |
| 34 | + this.paramsDictionary.Elements.SetString(Keys.CheckSum, checksum); |
| 35 | + } |
| 36 | + |
| 37 | + public string MimeType |
| 38 | + { |
| 39 | + get { return Elements.GetName(Keys.Subtype); } |
| 40 | + set { Elements.SetName(Keys.Subtype, value); } |
| 41 | + } |
| 42 | + |
| 43 | + // TODO : Add properties for the subdictionnary Params and the subsubdictionnary Mac |
| 44 | + |
| 45 | + /// <summary> |
| 46 | + /// Predefined keys of this embedded file. |
| 47 | + /// </summary> |
| 48 | + public class Keys : PdfDictionary.PdfStream.Keys |
| 49 | + { |
| 50 | + /// <summary> |
| 51 | + /// (Optional) The type of PDF object that this dictionary describes; if present, |
| 52 | + /// must be EmbeddedFile for an embedded file stream. |
| 53 | + /// </summary> |
| 54 | + [KeyInfo(KeyType.Name | KeyType.Optional, FixedValue = "EmbeddedFile")] |
| 55 | + public const string Type = "/Type"; |
| 56 | + |
| 57 | + /// <summary> |
| 58 | + /// (Optional) The subtype of the embedded file. The value of this entry must be a |
| 59 | + /// first-class name, as defined in Appendix E. Names without a registered prefix |
| 60 | + /// must conform to the MIME media type names defined in Internet RFC 2046, |
| 61 | + /// Multipurpose Internet Mail Extensions (MIME), Part Two: Media Types(see the |
| 62 | + /// Bibliography), with the provision that characters not allowed in names must |
| 63 | + /// use the 2-character hexadecimal code format described in Section 3.2.4, |
| 64 | + /// “Name Objects.” |
| 65 | + /// </summary> |
| 66 | + [KeyInfo(KeyType.Name | KeyType.Optional)] |
| 67 | + public const string Subtype = "/Subtype"; |
| 68 | + |
| 69 | + /// <summary> |
| 70 | + /// (Optional) An embedded file parameter dictionary containing additional, |
| 71 | + /// file-specific information (see Table 3.43). |
| 72 | + /// </summary> |
| 73 | + [KeyInfo(KeyType.Dictionary | KeyType.Optional)] |
| 74 | + public const string Params = "/Params"; |
| 75 | + |
| 76 | + /// <summary> |
| 77 | + /// (Optional) The size of the embedded file, in bytes. |
| 78 | + /// </summary> |
| 79 | + [KeyInfo(KeyType.Integer | KeyType.Optional)] |
| 80 | + public const string Size = "/Size"; |
| 81 | + |
| 82 | + /// <summary> |
| 83 | + /// (Optional) The date and time when the embedded file was created. |
| 84 | + /// </summary> |
| 85 | + [KeyInfo(KeyType.Date | KeyType.Optional)] |
| 86 | + public const string CreationDate = "/CreationDate"; |
| 87 | + |
| 88 | + /// <summary> |
| 89 | + /// (Optional) The date and time when the embedded file was last modified. |
| 90 | + /// </summary> |
| 91 | + [KeyInfo(KeyType.Date | KeyType.Optional)] |
| 92 | + public const string ModDate = "/ModDate"; |
| 93 | + |
| 94 | + /// <summary> |
| 95 | + /// (Optional) A subdictionary containing additional information specific to Mac OS files (see Table 3.44). |
| 96 | + /// </summary> |
| 97 | + [KeyInfo(KeyType.Dictionary | KeyType.Optional)] |
| 98 | + public const string Mac = "/Mac"; |
| 99 | + |
| 100 | + /// <summary> |
| 101 | + /// (Optional) A 16-byte string that is the checksum of the bytes of the uncompressed |
| 102 | + /// embedded file. The checksum is calculated by applying the standard MD5 message-digest |
| 103 | + /// algorithm (described in Internet RFC 1321, The MD5 Message-Digest Algorithm; see the |
| 104 | + /// Bibliography) to the bytes of the embedded file stream. |
| 105 | + /// </summary> |
| 106 | + [KeyInfo(KeyType.Dictionary | KeyType.Optional)] |
| 107 | + public const string CheckSum = "/CheckSum"; |
| 108 | + |
| 109 | + /// <summary> |
| 110 | + /// Gets the KeysMeta for these keys. |
| 111 | + /// </summary> |
| 112 | + internal static DictionaryMeta Meta |
| 113 | + { |
| 114 | + get |
| 115 | + { |
| 116 | + if (Keys.meta == null) |
| 117 | + Keys.meta = CreateMeta(typeof(Keys)); |
| 118 | + return Keys.meta; |
| 119 | + } |
| 120 | + } |
| 121 | + static DictionaryMeta meta; |
| 122 | + } |
| 123 | + |
| 124 | + /// <summary> |
| 125 | + /// Gets the KeysMeta of this dictionary type. |
| 126 | + /// </summary> |
| 127 | + internal override DictionaryMeta Meta |
| 128 | + { |
| 129 | + get { return Keys.Meta; } |
| 130 | + } |
| 131 | + } |
| 132 | +} |
0 commit comments