public string MetadataAsString() { // keep the unrecognized data around so we may fix it later... string str = null; try { try { DictionaryBasedObject dictionary = PDFMetadataSerializer.ReadFromStream(data); str = JsonConvert.SerializeObject(dictionary.Attributes, Formatting.Indented); } catch (Exception ex) { Logging.Error(ex); str = StringTools.HumanReadableASCIIAndHexStr(data); } } catch (Exception ex) { Logging.Error(ex); } return(str); }
public static PDFDocument LoadFromMetaData(Library library, byte[] data, Dictionary <string, byte[]> /* can be null */ library_items_annotations_cache) { DictionaryBasedObject dictionary = PDFMetadataSerializer.ReadFromStream(data); PDFDocument pdf_document = new PDFDocument(library, dictionary); pdf_document.GetAnnotations(library_items_annotations_cache); return(pdf_document); }
/// <summary> /// Throws exception when metadata could not be converted to a valid PDFDocument instance. /// </summary> /// <param name="library"></param> /// <param name="data"></param> /// <param name="library_items_annotations_cache"></param> /// <returns></returns> public static PDFDocument LoadFromMetaData(Library library, byte[] data, Dictionary <string, byte[]> /* can be null */ library_items_annotations_cache) { DictionaryBasedObject dictionary = PDFMetadataSerializer.ReadFromStream(data); LockObject _lock = new LockObject(); PDFDocument pdf_document = new PDFDocument(_lock, library, dictionary); // thread-UNSAFE access is permitted as the PDF has just been created so there's no thread-safety risk yet. pdf_document.doc.GetAnnotations(library_items_annotations_cache); return(pdf_document); }
/// <summary> /// Throws exception when metadata could not be converted to a valid PDFDocument instance. /// </summary> /// <param name="library"></param> /// <param name="data"></param> /// <param name="library_items_annotations_cache"></param> /// <returns></returns> public static PDFDocument LoadFromMetaData(WebLibraryDetail web_library_detail, string fingerprint, byte[] data, PDFAnnotationList prefetched_annotations_for_document = null) { WPFDoEvents.AssertThisCodeIs_NOT_RunningInTheUIThread(); ASSERT.Test(!String.IsNullOrEmpty(fingerprint)); DictionaryBasedObject dictionary = PDFMetadataSerializer.ReadFromStream(data); // Recover partially damaged record: if there's no fingerprint in the dictionary, add the given one: string id = dictionary["Fingerprint"] as string; if (String.IsNullOrEmpty(id)) { Logging.Error("Library '{1}' corrupted? Sanity check: the document record for document fingerprint {0} lacked a fingerprint as part of the mandatory metadata. This has been fixed now.", fingerprint, web_library_detail.DescriptiveTitle); dictionary["Fingerprint"] = fingerprint; id = fingerprint; } // extra verification / sanity check: this will catch some very obscure DB corruption, or rather **manual editing**! ;-) if (id != fingerprint || true) { // see which of them makes the most sense... And DO remember that Qiqqa fingeerprint hashes are variable-length, due to an old systemic bug! Regex re = new Regex(@"^[a-zA-Z0-9]{20,}(?:_REF)?$"); bool id_is_possibly_legal = re.IsMatch(id); bool key_is_possibly_legal = re.IsMatch(fingerprint); // if the entry in the record itself is legal, run with that one. Otherwise, fall back to using the DB record KEY. if (id_is_possibly_legal) { Logging.Error("Library '{2}' corrupted? Sanity check: given fingerprint '{0}' does not match the fingerprint '{1}' obtained from the DB metadata record. Running with the fingerprint specified in the metadata record.", fingerprint, id, web_library_detail.DescriptiveTitle); } else { Logging.Error("Library '{2}' corrupted? Sanity check: given fingerprint '{0}' does not match the fingerprint '{1}' obtained from the DB metadata record. Running with the fingerprint specified as database record KEY, since the other fingerprint does not look like a LEGAL fingerprint.", fingerprint, id, web_library_detail.DescriptiveTitle); dictionary["Fingerprint"] = fingerprint; id = fingerprint; } } PDFDocument pdf_document = new PDFDocument(web_library_detail, dictionary, prefetched_annotations_for_document); // thread-UNSAFE access is permitted as the PDF has just been created and // is NOT known to the outside world (i.e. beyond the scope of this function) // so there's no thread-safety risk yet. _ = pdf_document.GetAnnotations(); return(pdf_document); }
public void SaveToMetaData() { // Save the metadata PDFMetadataSerializer.WriteToDisk(this); // Save the annotations if (null != annotations && annotations.Count > 0) { PDFAnnotationSerializer.WriteToDisk(this); } // Save the highlights if (null != highlights && highlights.Count > 0) { PDFHighlightSerializer.WriteToDisk(this); } // Save the inks if (null != inks) { PDFInkSerializer.WriteToDisk(this); } }