internal static void ReadFromStream(PDFDocument pdf_document, PDFHightlightList highlights, Dictionary <string, byte[]> /* can be null */ library_items_highlights_cache)
        {
            WPFDoEvents.AssertThisCodeIs_NOT_RunningInTheUIThread();

            byte[] highlights_data = null;

            if (null != library_items_highlights_cache)
            {
                library_items_highlights_cache.TryGetValue(pdf_document.Fingerprint, out highlights_data);
            }
            else
            {
                List <LibraryDB.LibraryItem> library_items = pdf_document.LibraryRef.Xlibrary.LibraryDB.GetLibraryItems(PDFDocumentFileLocations.HIGHLIGHTS, new List <string>()
                {
                    pdf_document.Fingerprint
                });
                ASSERT.Test(library_items.Count < 2);
                if (0 < library_items.Count)
                {
                    highlights_data = library_items[0].data;
                }
            }

            if (null != highlights_data)
            {
                try
                {
                    List <PDFHighlight> highlights_list = null;

                    // First try normal
                    try
                    {
                        highlights_list = ReadFromStream_JSON(highlights_data);
                    }
                    catch (Exception)
                    {
                        highlights_list = ReadFromStream_PROTOBUF(highlights_data);
                        FeatureTrackingManager.Instance.UseFeature(Features.Legacy_Highlights_ProtoBuf);
                    }

                    if (null != highlights_list)
                    {
                        foreach (PDFHighlight highlight in highlights_list)
                        {
                            pdf_document.AddUpdatedHighlight(highlight);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Logging.Error(ex, "There was a problem loading the Highlights for document {0}", pdf_document.Fingerprint);
                }
            }
        }
        internal static List <PDFAnnotation> GenerateAnnotations(PDFDocument pdf_document, Dictionary <string, byte[]> library_items_highlights_cache)
        {
            List <PDFAnnotation> annotations = new List <PDFAnnotation>();

            if (pdf_document.DocumentExists)
            {
                PDFHightlightList highlight_list = pdf_document.GetHighlights(library_items_highlights_cache);
                foreach (int page in highlight_list.GetAffectedPages())
                {
                    annotations.AddRange(GenerateAnnotations(pdf_document, page, highlight_list));
                }
            }

            return(annotations);
        }
        internal static List <PDFAnnotation> GenerateAnnotations(PDFDocument pdf_document, int page, PDFHightlightList highlight_list)
        {
            List <RegionOfInterest> regions = new List <RegionOfInterest>();

            // Collect all the highlights on this page
            foreach (var highlight in highlight_list.GetHighlightsForPage(page))
            {
                if (page == highlight.Page && 0 != highlight.Width)
                {
                    regions.Add(new RegionOfInterest(highlight.Left, highlight.Top, highlight.Width, highlight.Height));
                }
            }

            RegionOfInterest.AggregateRegions(regions);

            // Build a list of annotations
            List <PDFAnnotation> annotations = RegionOfInterest.ConvertRegionsToPDFAnnotations(regions, HIGHLIGHTS_TAG, pdf_document, page);

            return(annotations);
        }