예제 #1
0
        void QuotationSmartRepeater_ActiveListItemChanged(object o, EventArgs a)
        {
            if (Program.ActiveProjectShell.PrimaryMainForm.GetSelectedQuotations().Count == 0)
            {
                return;
            }

            KnowledgeItem activeQuotation = Program.ActiveProjectShell.PrimaryMainForm.GetSelectedQuotations().FirstOrDefault();

            if (activeQuotation.EntityLinks == null)
            {
                return;
            }
            if (activeQuotation.EntityLinks.Where(e => e.Indication == EntityLink.PdfKnowledgeItemIndication).Count() == 0)
            {
                return;
            }

            Annotation annotation = activeQuotation.EntityLinks.Where(e => e.Indication == EntityLink.PdfKnowledgeItemIndication).FirstOrDefault().Target as Annotation;

            PreviewControl previewControl = PreviewMethods.GetPreviewControl();

            if (previewControl == null)
            {
                return;
            }

            SwissAcademic.Citavi.Controls.Wpf.PdfViewControl pdfViewControl = previewControl.GetPdfViewControl();
            if (pdfViewControl == null)
            {
                return;
            }

            pdfViewControl.GoToAnnotation(annotation);
        }
        public static void SimplifyAnnotations()
        {
            PreviewControl previewControl = PreviewMethods.GetPreviewControl();

            if (previewControl == null)
            {
                return;
            }

            PdfViewControl pdfViewControl = previewControl.GetPdfViewControl();

            if (pdfViewControl == null)
            {
                return;
            }

            Document document = pdfViewControl.Document;

            if (document == null)
            {
                return;
            }

            Project project = Program.ActiveProjectShell.Project;

            if (project == null)
            {
                return;
            }

            Location location = previewControl.ActiveLocation;

            if (location == null)
            {
                return;
            }

            Reference reference = location.Reference;

            if (reference == null)
            {
                return;
            }

            List <Annotation> annotations = previewControl.GetPdfViewControl().GetSelectedAnnotations().ToList();

            foreach (Annotation annotation in annotations)
            {
                List <Quad> newQuads = new List <Quad>();
                foreach (int i in annotation.Quads.Select(q => q.PageIndex).Distinct())
                {
                    List <Quad> quadsOnPage = annotation.Quads.Where(q => q.PageIndex == i).ToList();

                    newQuads.AddRange(quadsOnPage.SimpleQuads());
                }
                annotation.Quads = newQuads;
            }
        }
        public static List <Annotation> EquivalentAnnotations(this Highlight highlight)
        {
            List <Annotation> equivalentAnnotations = new List <Annotation>();

            PreviewControl previewControl = PreviewMethods.GetPreviewControl();

            if (previewControl == null)
            {
                return(null);
            }

            PdfViewControl pdfViewControl = previewControl.GetPdfViewControl();

            if (pdfViewControl == null)
            {
                return(null);
            }

            Document document = pdfViewControl.Document;

            if (document == null)
            {
                return(null);
            }

            Project project = Program.ActiveProjectShell.Project;

            if (project == null)
            {
                return(null);
            }

            Location location = previewControl.ActiveLocation;

            if (location == null)
            {
                return(null);
            }

            List <Annotation> annotationsAtThisLocation = location.Annotations.ToList();

            if (annotationsAtThisLocation == null)
            {
                return(null);
            }

            equivalentAnnotations.AddRange(annotationsAtThisLocation.Where(a => !highlight.AsAnnotationQuads().RoundedQuads().Except(a.Quads.ToList().RoundedQuads()).Any()));
            equivalentAnnotations.AddRange(annotationsAtThisLocation.Where(a => !highlight.AsAnnotationQuads().SimpleQuads().RoundedQuads().Except(a.Quads.ToList().RoundedQuads()).Any()));

            return(equivalentAnnotations);
        }
예제 #4
0
        void KnowledgeItemPreviewSmartRepeater_ActiveListItemChanged(object o, EventArgs a)
        {
            if (Program.ActiveProjectShell.PrimaryMainForm.GetSelectedKnowledgeItems().Count == 0)
            {
                return;
            }

            KnowledgeItem activeQuotation = Program.ActiveProjectShell.PrimaryMainForm.GetSelectedKnowledgeItems().FirstOrDefault();

            if (activeQuotation.EntityLinks == null)
            {
                return;
            }
            if (activeQuotation.EntityLinks.Where(e => e.Indication == EntityLink.PdfKnowledgeItemIndication).Count() == 0)
            {
                return;
            }

            Annotation annotation = activeQuotation.EntityLinks.Where(e => e.Indication == EntityLink.PdfKnowledgeItemIndication).FirstOrDefault().Target as Annotation;

            PreviewControl previewControl = PreviewMethods.GetPreviewControl();

            if (previewControl == null)
            {
                return;
            }

            PdfViewControl pdfViewControl = previewControl.GetPdfViewControl();

            if (pdfViewControl == null)
            {
                return;
            }

            Document document = pdfViewControl.Document;

            if (document == null)
            {
                return;
            }

            if (previewControl.ActiveLocation != annotation.Location)
            {
                Program.ActiveProjectShell.ShowPreviewFullScreenForm(annotation.Location, previewControl, null);
            }
            pdfViewControl.GoToAnnotation(annotation);

            Program.ActiveProjectShell.PrimaryMainForm.Activate();
        }
        public static Annotation TemporaryAnnotation(this Highlight highlight)
        {
            PreviewControl previewControl = PreviewMethods.GetPreviewControl();

            if (previewControl == null)
            {
                return(null);
            }

            PdfViewControl pdfViewControl = previewControl.GetPdfViewControl();

            if (pdfViewControl == null)
            {
                return(null);
            }

            Document document = pdfViewControl.Document;

            if (document == null)
            {
                return(null);
            }

            Location location = previewControl.ActiveLocation;

            if (location == null)
            {
                return(null);
            }

            Annotation temporaryAnnotation = new Annotation(location);

            temporaryAnnotation.Quads         = highlight.AsAnnotationQuads().TemporaryQuads();
            temporaryAnnotation.OriginalColor = System.Drawing.Color.FromArgb(255, 255, 255, 0);
            temporaryAnnotation.Visible       = true;

            return(temporaryAnnotation);
        }
예제 #6
0
        public static void CreateCommentAnnotation(List <KnowledgeItem> quotations)
        {
            PreviewControl previewControl = PreviewMethods.GetPreviewControl();

            if (previewControl == null)
            {
                return;
            }

            PdfViewControl pdfViewControl = previewControl.GetPdfViewControl();

            if (pdfViewControl == null)
            {
                return;
            }

            Annotation lastAnnotation = null;

            foreach (KnowledgeItem quotation in quotations)
            {
                if (quotation.EntityLinks.Any() && quotation.EntityLinks.Where(link => link.Target is KnowledgeItem).FirstOrDefault() != null)
                {
                    Reference reference = quotation.Reference;
                    if (reference == null)
                    {
                        return;
                    }

                    Project project = reference.Project;
                    if (project == null)
                    {
                        return;
                    }

                    KnowledgeItem mainQuotation = quotation.EntityLinks.ToList().Where(n => n != null && n.Indication == EntityLink.CommentOnQuotationIndication && n.Target as KnowledgeItem != null).ToList().FirstOrDefault().Target as KnowledgeItem;

                    Annotation mainQuotationAnnotation = mainQuotation.EntityLinks.Where(link => link.Target is Annotation && link.Indication == EntityLink.PdfKnowledgeItemIndication).FirstOrDefault().Target as Annotation;
                    if (mainQuotationAnnotation == null)
                    {
                        return;
                    }

                    Location location = mainQuotationAnnotation.Location;
                    if (location == null)
                    {
                        return;
                    }

                    List <Annotation> oldAnnotations = quotation.EntityLinks.Where(e => e.Indication == EntityLink.PdfKnowledgeItemIndication).Select(e => (Annotation)e.Target).ToList();

                    foreach (Annotation oldAnnotation in oldAnnotations)
                    {
                        location.Annotations.Remove(oldAnnotation);
                    }

                    Annotation newAnnotation = new Annotation(location);

                    newAnnotation.OriginalColor = System.Drawing.Color.FromArgb(255, 255, 255, 0);
                    newAnnotation.Quads         = mainQuotationAnnotation.Quads;
                    newAnnotation.Visible       = false;
                    location.Annotations.Add(newAnnotation);


                    EntityLink newEntityLink = new EntityLink(project);
                    newEntityLink.Source     = quotation;
                    newEntityLink.Target     = newAnnotation;
                    newEntityLink.Indication = EntityLink.PdfKnowledgeItemIndication;
                    project.EntityLinks.Add(newEntityLink);

                    lastAnnotation = newAnnotation;
                }
                pdfViewControl.GoToAnnotation(lastAnnotation);
            }
        }
        public static void ExportAnnotations(List <Reference> references)
        {
            List <Reference> exportFailed = new List <Reference>();
            int exportCounter             = 0;

            PreviewControl previewControl = PreviewMethods.GetPreviewControl();

            if (previewControl != null)
            {
                previewControl.ShowNoPreview();
            }

            foreach (Reference reference in references)
            {
                List <KnowledgeItem> quotations = reference.Quotations.Where(q => q.EntityLinks.Where(e => e.Indication == EntityLink.PdfKnowledgeItemIndication).Count() > 0).ToList();

                List <LinkedResource> linkedResources = reference.Locations.Where(l => l.LocationType == LocationType.ElectronicAddress && l.Address.LinkedResourceType == LinkedResourceType.AttachmentFile).Select(l => (LinkedResource)l.Address).ToList();

                List <KnowledgeItem> comments = quotations.Where(q => q.QuotationType == QuotationType.Comment && q.EntityLinks.Where(e => e.Indication == EntityLink.PdfKnowledgeItemIndication).Count() > 0).ToList();

                List <KnowledgeItem> directQuotations = quotations.Where(q => q.QuotationType == QuotationType.DirectQuotation && q.EntityLinks.Where(e => e.Indication == EntityLink.PdfKnowledgeItemIndication).Count() > 0).ToList();

                List <KnowledgeItem> indirectQuotations = quotations.Where(q => q.QuotationType == QuotationType.IndirectQuotation && q.EntityLinks.Where(e => e.Indication == EntityLink.PdfKnowledgeItemIndication).Count() > 0).ToList();

                List <KnowledgeItem> quickReferences = quotations.Where(q => q.QuotationType == QuotationType.QuickReference && q.EntityLinks.Where(e => e.Indication == EntityLink.PdfKnowledgeItemIndication).Count() > 0).ToList();

                List <KnowledgeItem> summaries = quotations.Where(q => q.QuotationType == QuotationType.Summary && q.EntityLinks.Where(e => e.Indication == EntityLink.PdfKnowledgeItemIndication).Count() > 0).ToList();

                foreach (Location location in reference.Locations)
                {
                    foreach (Annotation annotation in location.Annotations.Where(a => a.Visible == true && a.EntityLinks.Where(e => e.Indication == EntityLink.PdfKnowledgeItemIndication).Count() == 0).ToList())
                    {
                        location.Annotations.Remove(annotation);
                    }
                    foreach (Annotation annotation in location.Annotations.Where(a => a.Visible == false && a.EntityLinks.Where(e => e.Indication == EntityLink.SourceAnnotLinkIndication).Count() == 0 && a.EntityLinks.Where(e => e.Indication == EntityLink.PdfKnowledgeItemIndication).Count() == 0).ToList())
                    {
                        location.Annotations.Remove(annotation);
                    }
                }

                foreach (LinkedResource linkedResource in linkedResources)
                {
                    string pathToFile = linkedResource.Resolve().LocalPath;
                    if (!System.IO.File.Exists(pathToFile))
                    {
                        continue;
                    }

                    List <Rect> coveredRects = new List <Rect>();

                    Document document = new Document(pathToFile);
                    if (document == null)
                    {
                        continue;
                    }

                    previewControl.ShowNoPreview();


                    List <KnowledgeItem> commentsAtThisLocation = comments.Where(d => ((Annotation)d.EntityLinks.Where(e => e.Indication == EntityLink.PdfKnowledgeItemIndication).ToList().FirstOrDefault().Target).Location.Address == linkedResource).ToList();

                    List <KnowledgeItem> directQuotationsAtThisLocation = directQuotations.Where(d => d.EntityLinks != null && d.EntityLinks.Where(e => e.Indication == EntityLink.PdfKnowledgeItemIndication) != null && ((Annotation)d.EntityLinks.Where(e => e.Indication == EntityLink.PdfKnowledgeItemIndication).ToList().FirstOrDefault().Target).Location.Address == linkedResource).ToList();

                    List <KnowledgeItem> indirectQuotationsAtThisLocation = indirectQuotations.Where(d => d.EntityLinks != null && d.EntityLinks.Where(e => e.Indication == EntityLink.PdfKnowledgeItemIndication) != null && ((Annotation)d.EntityLinks.Where(e => e.Indication == EntityLink.PdfKnowledgeItemIndication).ToList().FirstOrDefault().Target).Location.Address == linkedResource).ToList();

                    List <KnowledgeItem> quickReferencesAtThisLocation = quickReferences.Where(d => ((Annotation)d.EntityLinks.Where(e => e.Indication == EntityLink.PdfKnowledgeItemIndication).ToList().FirstOrDefault().Target).Location.Address == linkedResource).ToList();

                    List <KnowledgeItem> summariesAtThisLocation = summaries.Where(d => ((Annotation)d.EntityLinks.Where(e => e.Indication == EntityLink.PdfKnowledgeItemIndication).ToList().FirstOrDefault().Target).Location.Address == linkedResource).ToList();

                    List <Annotation> commentAnnotationsAtThisLocation = commentsAtThisLocation.Select(d => (Annotation)d.EntityLinks.Where(e => e.Indication == EntityLink.PdfKnowledgeItemIndication).ToList().FirstOrDefault().Target).ToList();

                    List <Annotation> directQuotationAnnotationsAtThisLocation = directQuotationsAtThisLocation.Select(d => (Annotation)d.EntityLinks.Where(e => e.Indication == EntityLink.PdfKnowledgeItemIndication).ToList().FirstOrDefault().Target).ToList();

                    List <Annotation> indirectQuotationAnnotationsAtThisLocation = indirectQuotationsAtThisLocation.Select(d => (Annotation)d.EntityLinks.Where(e => e.Indication == EntityLink.PdfKnowledgeItemIndication).ToList().FirstOrDefault().Target).ToList();

                    List <Annotation> quickReferenceAnnotationsAtThisLocation = quickReferencesAtThisLocation.Select(d => (Annotation)d.EntityLinks.Where(e => e.Indication == EntityLink.PdfKnowledgeItemIndication).ToList().FirstOrDefault().Target).ToList();

                    List <Annotation> summaryAnnotationsAtThisLocation = summaries.Select(d => (Annotation)d.EntityLinks.Where(e => e.Indication == EntityLink.PdfKnowledgeItemIndication).ToList().FirstOrDefault().Target).ToList();


                    DeleteExistingHighlights(document);

                    foreach (Annotation annotation in commentAnnotationsAtThisLocation)
                    {
                        System.Drawing.Color highlightColor = KnownColors.AnnotationComment100;
                        CreateHighlightAnnots(annotation, highlightColor, document, coveredRects, out coveredRects);
                    }

                    foreach (Annotation annotation in directQuotationAnnotationsAtThisLocation)
                    {
                        System.Drawing.Color highlightColor = KnownColors.AnnotationDirectQuotation100;
                        CreateHighlightAnnots(annotation, highlightColor, document, coveredRects, out coveredRects);
                    }

                    foreach (Annotation annotation in indirectQuotationAnnotationsAtThisLocation)
                    {
                        System.Drawing.Color highlightColor = KnownColors.AnnotationIndirectQuotation100;
                        CreateHighlightAnnots(annotation, highlightColor, document, coveredRects, out coveredRects);
                    }

                    foreach (Annotation annotation in quickReferenceAnnotationsAtThisLocation)
                    {
                        System.Drawing.Color highlightColor = KnownColors.AnnotationQuickReference100;
                        CreateHighlightAnnots(annotation, highlightColor, document, coveredRects, out coveredRects);
                    }

                    foreach (Annotation annotation in summaryAnnotationsAtThisLocation)
                    {
                        System.Drawing.Color highlightColor = KnownColors.AnnotationSummary100;
                        CreateHighlightAnnots(annotation, highlightColor, document, coveredRects, out coveredRects);
                    }

                    try
                    {
                        document.Save(pathToFile, SDFDoc.SaveOptions.e_remove_unused);
                        exportCounter++;
                    }
                    catch
                    {
                        exportFailed.Add(reference);
                    }
                    document.Close();
                }
            }
            string message = "Annotations exported to {0} locations.";

            message = string.Format(message, exportCounter);
            if (exportFailed.Count == 0)
            {
                MessageBox.Show(message, "Citavi", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else
            {
                DialogResult showFailed = MessageBox.Show(message + "\n Would you like to show a selection of references where export has failed?", "Citavi Macro", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                if (showFailed == DialogResult.Yes)
                {
                    var filter = new ReferenceFilter(exportFailed, "Export failed", false);
                    Program.ActiveProjectShell.PrimaryMainForm.ReferenceEditorFilterSet.Filters.ReplaceBy(new List <ReferenceFilter> {
                        filter
                    });
                }
            }
        }
예제 #8
0
        public static KnowledgeItem CombineQuotations(List <KnowledgeItem> quotations)
        {
            if (quotations.Count() == 1)
            {
                return(quotations.FirstOrDefault());
            }
            if (quotations.Count == 0)
            {
                return(null);
            }

            // Static Variables

            PreviewControl previewControl = PreviewMethods.GetPreviewControl();

            if (previewControl == null)
            {
                return(null);
            }

            PdfViewControl pdfViewControl = previewControl.GetPdfViewControl();

            if (pdfViewControl == null)
            {
                return(null);
            }

            Document document = pdfViewControl.Document;

            if (document == null)
            {
                return(null);
            }

            Reference reference = quotations.FirstOrDefault().Reference;

            if (reference == null)
            {
                return(null);
            }

            Project project = Program.ActiveProjectShell.Project;

            if (project == null)
            {
                return(null);
            }

            List <Location> locations = quotations.GetPDFLocations().Distinct().ToList();

            if (locations.Count != 1)
            {
                return(null);
            }
            Location location = locations.FirstOrDefault();

            List <QuotationType> quotationTypes = quotations.Select(q => q.QuotationType).Distinct().ToList();
            var itemToRemove = quotationTypes.SingleOrDefault(q => q == QuotationType.Highlight);

            quotationTypes.Remove(itemToRemove);

            QuotationType quotationType = quotationTypes.FirstOrDefault();

            if (quotationTypes.Count == 0)
            {
                quotationType = QuotationType.Highlight;
            }

            // Dynamic Variables

            KnowledgeItem newQuotation = new KnowledgeItem(reference, quotationType);

            string text = string.Empty;

            List <Quad>      quads          = new List <Quad>();
            List <PageRange> pageRangesList = new List <PageRange>();
            string           pageRangeText  = string.Empty;

            List <PageWidth> store = new List <PageWidth>();

            // The Magic

            if (document != null)
            {
                for (int i = 1; i <= document.GetPageCount(); i++)
                {
                    pdftron.PDF.Page page = document.GetPage(i);
                    if (page.IsValid())
                    {
                        var re = page.GetCropBox();
                        store.Add(new PageWidth(location, i, re.Width()));
                    }
                    else
                    {
                        store.Add(new PageWidth(location, i, 0.00));
                    }
                }
            }

            quotations.Sort(new KnowledgeItemComparer(store));

            foreach (KnowledgeItem quotation in quotations)
            {
                if (!string.IsNullOrEmpty(quotation.Text))
                {
                    text = MergeRTF(text, quotation.TextRtf);
                }
                if (!string.IsNullOrEmpty(quotation.PageRange.OriginalString))
                {
                    pageRangesList.Add(quotation.PageRange);
                }
            }

            pageRangesList = PageRangeMerger.MergeAdjacent(pageRangesList);
            pageRangeText  = PageRangeMerger.PageRangeListToString(pageRangesList);

            newQuotation.TextRtf   = text;
            newQuotation.PageRange = pageRangeText;
            newQuotation.PageRange = newQuotation.PageRange.Update(quotations[0].PageRange.NumberingType);
            newQuotation.PageRange = newQuotation.PageRange.Update(quotations[0].PageRange.NumeralSystem);

            reference.Quotations.Add(newQuotation);
            project.AllKnowledgeItems.Add(newQuotation);

            return(newQuotation);
        }
예제 #9
0
        public static void MergeAnnotations()
        {
            PreviewControl previewControl = PreviewMethods.GetPreviewControl();

            if (previewControl == null)
            {
                return;
            }

            PdfViewControl pdfViewControl = previewControl.GetPdfViewControl();

            if (pdfViewControl == null)
            {
                return;
            }

            Document document = pdfViewControl.Document;

            if (document == null)
            {
                return;
            }

            Project project = Program.ActiveProjectShell.Project;

            if (project == null)
            {
                return;
            }

            Location location = previewControl.ActiveLocation;

            if (location == null)
            {
                return;
            }

            Reference reference = location.Reference;

            if (reference == null)
            {
                return;
            }

            List <Annotation> annotations = pdfViewControl.GetSelectedAnnotations().ToList();

            List <KnowledgeItem> quotations = annotations
                                              .Where
                                              (
                a =>
                a.EntityLinks.Where(e => e.Indication == EntityLink.PdfKnowledgeItemIndication).Count() > 0
                                              )
                                              .Select
                                              (
                a =>
                (KnowledgeItem)a.EntityLinks.Where(e => e.Indication == EntityLink.PdfKnowledgeItemIndication).FirstOrDefault().Source
                                              )
                                              .ToList();

            List <QuotationType> quotationTypes = quotations.Select(q => q.QuotationType).Distinct().ToList();
            var itemToRemove = quotationTypes.SingleOrDefault(q => q == QuotationType.Highlight);

            quotationTypes.Remove(itemToRemove);

            if (quotations.Count() > 0)
            {
                if (quotationTypes.Count > 1)
                {
                    MessageBox.Show("Can't merge quotations, more than one type of quotation is selected.");
                    return;
                }
            }

            // Dynamic Variables

            // The Magic

            KnowledgeItem newQuotation  = CombineQuotations(quotations);
            Annotation    newAnnotation = CombineAnnotations(annotations);

            if (quotations.Count() > 1)
            {
                EntityLink newEntityLink = new EntityLink(project);
                newEntityLink.Source     = newQuotation;
                newEntityLink.Target     = newAnnotation;
                newEntityLink.Indication = EntityLink.PdfKnowledgeItemIndication;
                project.EntityLinks.Add(newEntityLink);
                newAnnotation.Visible = false;
                reference.Quotations.RemoveRange(quotations);
            }
            else if (quotations.Count == 1)
            {
                EntityLink newEntityLink = new EntityLink(project);
                newEntityLink.Source     = quotations.FirstOrDefault();
                newEntityLink.Target     = newAnnotation;
                newEntityLink.Indication = EntityLink.PdfKnowledgeItemIndication;
                project.EntityLinks.Add(newEntityLink);
                newAnnotation.Visible = false;
            }

            if (quotations.Count > 1 && quotationTypes.Count > 0)
            {
                Program.ActiveProjectShell.ShowKnowledgeItemFormForExistingItem(Program.ActiveProjectShell.PrimaryMainForm, newQuotation);
            }
        }
        public static Annotation CreateKnowledgeItemIndicationAnnotation(this Highlight highlight, bool RedrawAnnotations)
        {
            Project project = Program.ActiveProjectShell.Project;

            if (project == null)
            {
                return(null);
            }

            PreviewControl previewControl = PreviewMethods.GetPreviewControl();

            if (previewControl == null)
            {
                return(null);
            }

            PdfViewControl pdfViewControl = previewControl.GetPdfViewControl();

            if (pdfViewControl == null)
            {
                return(null);
            }

            Document document = pdfViewControl.Document;

            if (document == null)
            {
                return(null);
            }

            Location location = previewControl.ActiveLocation;

            if (location == null)
            {
                return(null);
            }

            Reference reference = location.Reference;

            if (reference == null)
            {
                return(null);
            }

            Annotation newAnnotation = new Annotation(location);

            List <Quad> quads = new List <Quad>();

            if (RedrawAnnotations)
            {
                quads = highlight.AsAnnotationQuads().SimpleQuads();
            }
            else
            {
                quads = highlight.AsAnnotationQuads();
            }


            newAnnotation.OriginalColor = System.Drawing.Color.FromArgb(255, 255, 255, 0);
            newAnnotation.Quads         = quads;
            newAnnotation.Visible       = false;

            location.Annotations.Add(newAnnotation);

            return(newAnnotation);
        }
        public static bool CreateNewQuotationAndAnnotationFromHighlight(this Highlight highlight, QuotationType quotationType, bool ImportEmptyAnnotations, bool RedrawAnnotations, List <Annotation> temporaryAnnotations)
        {
            string highlightContents = highlight.GetContents();

            if (string.IsNullOrEmpty(highlightContents) && !ImportEmptyAnnotations)
            {
                return(false);
            }

            Project project = Program.ActiveProjectShell.Project;

            if (project == null)
            {
                return(false);
            }

            PreviewControl previewControl = PreviewMethods.GetPreviewControl();

            if (previewControl == null)
            {
                return(false);
            }

            PdfViewControl pdfViewControl = previewControl.GetPdfViewControl();

            if (pdfViewControl == null)
            {
                return(false);
            }

            Document document = pdfViewControl.Document;

            if (document == null)
            {
                return(false);
            }

            Location location = previewControl.ActiveLocation;

            if (location == null)
            {
                return(false);
            }

            Reference reference = location.Reference;

            if (reference == null)
            {
                return(false);
            }


            // Dynamic variables

            KnowledgeItem newQuotation       = null;
            KnowledgeItem newDirectQuotation = null;

            TextContent textContent = null;

            // Does any other annotation with the same quads already exist?

            Annotation existingAnnotation = highlight.EquivalentAnnotations().FirstOrDefault();

            if ((string.IsNullOrEmpty(highlightContents) && ImportEmptyAnnotations) || quotationType == QuotationType.Comment)

            {
                Annotation temporaryAnnotation = temporaryAnnotations.Where(a => !highlight.AsAnnotationQuads().TemporaryQuads().Except(a.Quads.ToList()).Any()).FirstOrDefault();
                if (temporaryAnnotation != null)
                {
                    pdfViewControl.GoToAnnotation(temporaryAnnotation);
                    textContent = (TextContent)pdfViewControl.GetSelectedContentFromType(pdfViewControl.GetSelectedContentType(), -1, false, true);
                    location.Annotations.Remove(temporaryAnnotation);
                }
                else
                {
                    return(false);
                }
            }

            int startPage = 1;

            if (reference.PageRange.StartPage.Number != null)
            {
                startPage = reference.PageRange.StartPage.Number.Value;
            }

            string pageRangeString = (startPage + highlight.GetPage().GetIndex() - 1).ToString();

            Annotation knowledgeItemIndicationAnnotation = highlight.CreateKnowledgeItemIndicationAnnotation(RedrawAnnotations);

            switch (quotationType)
            {
            case QuotationType.Comment:
                if (!string.IsNullOrEmpty(highlightContents))
                {
                    newQuotation = highlightContents.CreateNewQuotationFromHighlightContents(pageRangeString, reference, quotationType);
                    reference.Quotations.Add(newQuotation);
                    project.AllKnowledgeItems.Add(newQuotation);
                    newQuotation.LinkWithKnowledgeItemIndicationAnnotation(knowledgeItemIndicationAnnotation);

                    if (AnnotationsImporterColorPicker.ImportDirectQuotationLinkedWithCommentSelected)
                    {
                        Annotation newDirectQuotationIndicationAnnotation = highlight.CreateKnowledgeItemIndicationAnnotation(RedrawAnnotations);
                        newDirectQuotation = textContent.CreateNewQuotationFromAnnotationContent(pageRangeString, reference, QuotationType.DirectQuotation);
                        reference.Quotations.Add(newDirectQuotation);
                        project.AllKnowledgeItems.Add(newDirectQuotation);
                        newDirectQuotation.LinkWithKnowledgeItemIndicationAnnotation(newDirectQuotationIndicationAnnotation);

                        EntityLink commentDirectQuotationLink = new EntityLink(project);
                        commentDirectQuotationLink.Source     = newQuotation;
                        commentDirectQuotationLink.Target     = newDirectQuotation;
                        commentDirectQuotationLink.Indication = EntityLink.CommentOnQuotationIndication;
                        project.EntityLinks.Add(commentDirectQuotationLink);

                        newQuotation.CoreStatement           = newDirectQuotation.CoreStatement + " (Comment)";
                        newQuotation.CoreStatementUpdateType = UpdateType.Manual;
                    }
                }
                else if (string.IsNullOrEmpty(highlightContents) && ImportEmptyAnnotations)
                {
                    newQuotation = textContent.CreateNewQuotationFromAnnotationContent(pageRangeString, reference, quotationType);
                    reference.Quotations.Add(newQuotation);
                    project.AllKnowledgeItems.Add(newQuotation);
                    newQuotation.LinkWithKnowledgeItemIndicationAnnotation(knowledgeItemIndicationAnnotation);

                    if (AnnotationsImporterColorPicker.ImportDirectQuotationLinkedWithCommentSelected)
                    {
                        Annotation newDirectQuotationIndicationAnnotation = highlight.CreateKnowledgeItemIndicationAnnotation(RedrawAnnotations);
                        newDirectQuotation = textContent.CreateNewQuotationFromAnnotationContent(pageRangeString, reference, QuotationType.DirectQuotation);
                        reference.Quotations.Add(newDirectQuotation);
                        project.AllKnowledgeItems.Add(newDirectQuotation);
                        newDirectQuotation.LinkWithKnowledgeItemIndicationAnnotation(newDirectQuotationIndicationAnnotation);

                        EntityLink commentDirectQuotationLink = new EntityLink(project);
                        commentDirectQuotationLink.Source     = newQuotation;
                        commentDirectQuotationLink.Target     = newDirectQuotation;
                        commentDirectQuotationLink.Indication = EntityLink.CommentOnQuotationIndication;
                        project.EntityLinks.Add(commentDirectQuotationLink);

                        newQuotation.CoreStatement           = newDirectQuotation.CoreStatement + " (Comment)";
                        newQuotation.CoreStatementUpdateType = UpdateType.Manual;
                    }
                }
                break;

            case QuotationType.QuickReference:
                if (!string.IsNullOrEmpty(highlightContents))
                {
                    newQuotation = highlightContents.CreateNewQuickReferenceFromHighlightContents(pageRangeString, reference, quotationType);
                }
                else if (string.IsNullOrEmpty(highlightContents) && ImportEmptyAnnotations)
                {
                    newQuotation = textContent.CreateNewQuickReferenceFromAnnotationContent(pageRangeString, reference, quotationType);
                }
                reference.Quotations.Add(newQuotation);
                project.AllKnowledgeItems.Add(newQuotation);
                newQuotation.LinkWithKnowledgeItemIndicationAnnotation(knowledgeItemIndicationAnnotation);
                break;

            default:
                if (!string.IsNullOrEmpty(highlightContents))
                {
                    newQuotation = highlightContents.CreateNewQuotationFromHighlightContents(pageRangeString, reference, quotationType);
                    reference.Quotations.Add(newQuotation);
                    project.AllKnowledgeItems.Add(newQuotation);
                    newQuotation.LinkWithKnowledgeItemIndicationAnnotation(knowledgeItemIndicationAnnotation);
                }
                else if (string.IsNullOrEmpty(highlightContents) && ImportEmptyAnnotations)
                {
                    newQuotation = textContent.CreateNewQuotationFromAnnotationContent(pageRangeString, reference, quotationType);
                    reference.Quotations.Add(newQuotation);
                    project.AllKnowledgeItems.Add(newQuotation);
                    newQuotation.LinkWithKnowledgeItemIndicationAnnotation(knowledgeItemIndicationAnnotation);
                }
                break;
            }

            if (existingAnnotation != null)
            {
                existingAnnotation.LinkWithKnowledgeItemIndicationAnnotation(knowledgeItemIndicationAnnotation);
                existingAnnotation.Visible = false;
            }

            return(true);
        }
        public static void AnnotationsImport(QuotationType quotationType)
        {
            // Static Variables

            PreviewControl previewControl = PreviewMethods.GetPreviewControl();

            if (previewControl == null)
            {
                return;
            }

            PdfViewControl pdfViewControl = previewControl.GetPdfViewControl();

            if (pdfViewControl == null)
            {
                return;
            }

            Document document = pdfViewControl.Document;

            if (document == null)
            {
                return;
            }

            Location location = previewControl.ActiveLocation;

            if (location == null)
            {
                return;
            }

            List <Reference> references = Program.ActiveProjectShell.Project.References.Where(r => r.Locations.Contains(location)).ToList();

            if (references == null)
            {
                return;
            }
            if (references.Count != 1)
            {
                MessageBox.Show("The document is not linked with exactly one reference. Import aborted.");
            }

            Reference reference = references.FirstOrDefault();

            if (references == null)
            {
                return;
            }

            LinkedResource linkedResource = location.Address;

            string pathToFile = linkedResource.Resolve().LocalPath;

            PreviewBehaviour previewBehaviour = location.PreviewBehaviour;

            // Dynamic Variables

            string colorPickerCaption = string.Empty;

            List <ColorPt> selectedColorPts = new List <ColorPt>();

            int annotationsImportedCount = 0;

            bool ImportEmptyAnnotations = false;
            bool RedrawAnnotations      = true;

            // The Magic

            Form commentAnnotationsColorPicker = new AnnotationsImporterColorPicker(quotationType, document.ExistingColors(), out selectedColorPts);

            DialogResult dialogResult = commentAnnotationsColorPicker.ShowDialog();

            RedrawAnnotations      = AnnotationsImporterColorPicker.RedrawAnnotationsSelected;
            ImportEmptyAnnotations = AnnotationsImporterColorPicker.ImportEmptyAnnotationsSelected;

            if (dialogResult == DialogResult.Cancel)
            {
                MessageBox.Show("Import of external highlights cancelled.");
                return;
            }

            List <Annotation> temporaryAnnotations = new List <Annotation>();

            if (ImportEmptyAnnotations || quotationType == QuotationType.Comment)
            {
                for (int pageIndex = 1; pageIndex <= document.GetPageCount(); pageIndex++)
                {
                    pdftron.PDF.Page page = document.GetPage(pageIndex);
                    if (page.IsValid())
                    {
                        List <Annot> annotsToDelete = new List <Annot>();
                        for (int j = 0; j < page.GetNumAnnots(); j++)
                        {
                            Annot annot = page.GetAnnot(j);

                            if (annot.GetSDFObj() != null && (annot.GetType() == Annot.Type.e_Highlight || annot.GetType() == Annot.Type.e_Unknown))
                            {
                                Highlight highlight = new Highlight(annot);
                                if (highlight == null)
                                {
                                    continue;
                                }

                                ColorPt annotColorPt = annot.GetColorAsRGB();

                                if (selectedColorPts.Where(e =>
                                                           e.Get(0) == annotColorPt.Get(0) &&
                                                           e.Get(1) == annotColorPt.Get(1) &&
                                                           e.Get(2) == annotColorPt.Get(2) &&
                                                           e.Get(3) == annotColorPt.Get(3)
                                                           ).Count() == 0)
                                {
                                    continue;
                                }
                                Annotation temporaryAnnotation = highlight.TemporaryAnnotation();
                                location.Annotations.Add(temporaryAnnotation);
                                temporaryAnnotations.Add(temporaryAnnotation);
                            }
                        } // end for (int j = 1; j <= page.GetNumAnnots(); j++)
                    }     // end if (page.IsValid())
                }         // end for (int i = 1; i <= document.GetPageCount(); i++)

                previewControl.ShowNoPreview();
                location.PreviewBehaviour = PreviewBehaviour.SkipEntryPage;
                previewControl.ShowLocationPreview(location);
                document = new Document(pathToFile);
            }

            //Uncomment here to get an overview of all annotations
            //int x = 0;
            //foreach (Annotation a in location.Annotations)
            //{
            //    System.Diagnostics.Debug.WriteLine("Annotation " + x.ToString());
            //    int y = 0;
            //    foreach (Quad q in a.Quads)
            //    {
            //        System.Diagnostics.Debug.WriteLine("Quad " + y.ToString());
            //        System.Diagnostics.Debug.WriteLine("IsContainer: " + q.IsContainer.ToString());
            //        System.Diagnostics.Debug.WriteLine("MinX: " + q.MinX.ToString());
            //        System.Diagnostics.Debug.WriteLine("MinY: " + q.MinY.ToString());
            //        System.Diagnostics.Debug.WriteLine("MaxX: " + q.MaxX.ToString());
            //        System.Diagnostics.Debug.WriteLine("MaxY: " + q.MaxY.ToString());
            //        y = y + 1;
            //    }
            //    x = x + 1;
            //}

            int v = 0;

            for (int pageIndex = 1; pageIndex <= document.GetPageCount(); pageIndex++)
            {
                pdftron.PDF.Page page = document.GetPage(pageIndex);
                if (page.IsValid())
                {
                    List <Annot> annotsToDelete = new List <Annot>();
                    for (int j = 0; j < page.GetNumAnnots(); j++)
                    {
                        Annot annot = page.GetAnnot(j);

                        if (annot.GetSDFObj() != null && (annot.GetType() == Annot.Type.e_Highlight || annot.GetType() == Annot.Type.e_Unknown))
                        {
                            Highlight highlight = new Highlight(annot);
                            if (highlight == null)
                            {
                                continue;
                            }

                            ColorPt annotColorPt = annot.GetColorAsRGB();

                            if (selectedColorPts.Where(e =>
                                                       e.Get(0) == annotColorPt.Get(0) &&
                                                       e.Get(1) == annotColorPt.Get(1) &&
                                                       e.Get(2) == annotColorPt.Get(2) &&
                                                       e.Get(3) == annotColorPt.Get(3)
                                                       ).Count() == 0)
                            {
                                continue;
                            }

                            // Uncomment here to get an overview of all highlights
                            //System.Diagnostics.Debug.WriteLine("Highlight " + v.ToString());
                            //int w = 0;
                            //foreach (Quad q in highlight.AsAnnotationQuads())
                            //{
                            //    System.Diagnostics.Debug.WriteLine("Quad " + w.ToString());
                            //    System.Diagnostics.Debug.WriteLine("IsContainer: " + q.IsContainer.ToString());
                            //    System.Diagnostics.Debug.WriteLine("MinX: " + q.MinX.ToString());
                            //    System.Diagnostics.Debug.WriteLine("MinY: " + q.MinY.ToString());
                            //    System.Diagnostics.Debug.WriteLine("MaxX: " + q.MaxX.ToString());
                            //    System.Diagnostics.Debug.WriteLine("MaxY: " + q.MaxY.ToString());
                            //    w = w + 1;
                            //}
                            //v = v + 1;

                            if (highlight.UpdateExistingQuotation(quotationType, location))
                            {
                                annotationsImportedCount++;
                                continue;
                            }
                            if (highlight.CreateNewQuotationAndAnnotationFromHighlight(quotationType, ImportEmptyAnnotations, RedrawAnnotations, temporaryAnnotations))
                            {
                                annotationsImportedCount++;
                                continue;
                            }
                        }
                    } // end for (int j = 1; j <= page.GetNumAnnots(); j++)
                    foreach (Annot annot in annotsToDelete)
                    {
                        page.AnnotRemove(annot);
                    }
                } // end if (page.IsValid())
            }     // end for (int i = 1; i <= document.GetPageCount(); i++)

            foreach (Annotation annotation in temporaryAnnotations)
            {
                location.Annotations.Remove(annotation);
            }

            location.PreviewBehaviour = previewBehaviour;

            MessageBox.Show(annotationsImportedCount.ToString() + " highlights have been imported.");
        }