Externe Markierungen umwandeln und verbinden, Text hinter Annotation

Jan J. hinzugefügt 5 Monaten her
unbeantwortet

Liebes Citavi-Team, lieber Sebastian,


Ihre Antwort auf meine andere Frage hat ein wenig Pandoras Box geöffnet. Nachdem ich jetzt verstanden habe, wie das mit den EntityLinks geht, habe ich mich wieder dran gesetzt, die große Baustelle "Externe Markierungen in einem Abwasch umwandeln" und "Markierungen nachträglich verbinden" anzugehen. Letzeres klappt in einem AddOn problemlos, für den interessierten, das geht so, bzw. ich mach das so:


using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using SwissAcademic;
using SwissAcademic.Citavi;
using SwissAcademic.Citavi.Shell;
using SwissAcademic.Pdf;
using SwissAcademic.Pdf.Analysis;

namespace BornheimsToolbox
{
    class QuotationMerger
    {
        public void MergeQuotations(List<KnowledgeItem> quotations)
        {
            if (quotations.Count <= 1) return;

            Reference reference = quotations.FirstOrDefault().Reference;
            if (reference == null) return;

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

            Location location = reference.Locations.ToList().FirstOrDefault();

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

            string text = string.Empty;

            var pdfLocations = reference.GetPDFLocations();

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

            Document document = null;

            var address = location.Address.Resolve().LocalPath;
            document = new Document(address);

            quotations.Sort(new KnowledgeItemComparer(store));

            foreach (KnowledgeItem quotation in quotations)
            {
                if (quotation.EntityLinks.Any() && quotation.EntityLinks.FirstOrDefault(link => link.Target is Annotation) != null)
                {
                    Annotation annotation = quotation.EntityLinks.FirstOrDefault(link => link.Target is Annotation).Target as Annotation;
                    {
                        if (annotation.Location != null && ((location != null && annotation.Location == location) || location == null))
                        {
                            location = annotation.Location;
                            citaviAnnotations.Add(annotation);
                            if (!string.IsNullOrEmpty(quotation.PageRange.OriginalString)) pageRangesList.Add(quotation.PageRange);
                            quads.AddRange(annotation.Quads);
                        }
                        
                        
                    }
                }
                text = MergeRTF(text, quotation.TextRtf);
            }

            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));
                    }
                }
            }

            pageRangesList = MergeAdjacent(pageRangesList);

            int j = 0;
            foreach (PageRange pageRange in pageRangesList)
            {
                if (j > 0) pageRangeText += ", ";
                pageRangeText += pageRange.StartPage;
                if (pageRange.EndPage != null && !string.IsNullOrEmpty(pageRange.EndPage.OriginalString) && pageRange.EndPage != pageRange.StartPage) pageRangeText += "-" + pageRange.EndPage;
                j = j + 1;
            }


            Annotation newAnnotation = new Annotation(location);
            newAnnotation.Quads = quads;
            location.Annotations.Add(newAnnotation);

            KnowledgeItem newQuotation = new KnowledgeItem(reference, QuotationType.DirectQuotation);
            newQuotation.TextRtf = text;
            newQuotation.PageRange = pageRangeText;
            project.AllKnowledgeItems.Add(newQuotation);
            reference.Quotations.Add(newQuotation);

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

            foreach (KnowledgeItem quotation in quotations)
            {
                reference.Quotations.Remove(quotation);
            }
        }
        private static string MergeRTF(string s1, string s2)
        {
            RichTextBox rtbTemp = new RichTextBox();
            RichTextBox rtbMerged = new RichTextBox();

            if (!string.IsNullOrEmpty(s1))
            {
                rtbTemp.Rtf = s1;
                rtbTemp.SelectAll();
                rtbTemp.Cut();
                rtbMerged.Paste();
                rtbMerged.Select(1, 1);
                Font font = rtbMerged.SelectionFont;
                rtbMerged.AppendText(" […] ");
                rtbMerged.Select(rtbMerged.Text.Length - 5, 5);
                rtbMerged.SelectionFont = new Font(font, System.Drawing.FontStyle.Bold);
                rtbMerged.Select(rtbMerged.Text.Length, 0);
            }

            rtbTemp.Rtf = s2;
            rtbTemp.SelectAll();
            rtbTemp.Cut();
            rtbMerged.Paste();

            return rtbMerged.Rtf;
        }
        public static List<PageRange> MergeAdjacent(List<PageRange> pageRangesList)
        {
            int i = pageRangesList.Count;

            if (pageRangesList.Count < 1) return pageRangesList;

            List<PageRange> newList = new List<PageRange>();

            PageNumber minPage = new PageNumber();
            PageNumber maxPage = new PageNumber();

            int minPageAsInteger = 0;
            int maxPageAsInteger = 0;

            PageRange first = pageRangesList.FirstOrDefault();
            PageRange last = pageRangesList.Last();

            NumberingType previousPageRangeNumberingType = new NumberingType();
            bool PreviousPageRangeWasNumber = false;

            foreach (PageRange pageRange in pageRangesList)
            {
                PageNumber currentStartPage = pageRange.StartPage;
                PageNumber currentEndPage = pageRange.EndPage;
                if (currentEndPage == null) currentEndPage = currentStartPage;
                if (string.IsNullOrEmpty(currentEndPage.OriginalString)) currentEndPage = currentStartPage;

                int currentStartPageAsInteger = 0;
                int currentEndPageAsInteger = 0;

                bool startPageIsNumber = Int32.TryParse(currentStartPage.OriginalString.Replace(".", ""), out currentStartPageAsInteger);
                bool endPageIsNumber = Int32.TryParse(currentEndPage.OriginalString.Replace(".", ""), out currentEndPageAsInteger);

                bool IsDiscreteRange = false;

                bool SameNumberingType = (previousPageRangeNumberingType == pageRange.NumberingType);

                if (pageRangesList.Count == 1)
                {

                    PageRange dummyPageRange = new PageRange();
                    dummyPageRange = dummyPageRange.Update(pageRange.NumberingType);

                    Double result;
                    if (Double.TryParse(pageRange.StartPage.ToString(), out result) == false)
                    {
                        newList.Add(dummyPageRange.Update(pageRange.OriginalString));
                    }
                    else
                    {
                        newList.Add(dummyPageRange.Update(string.Format("{0}-{1}", currentStartPage.OriginalString, currentEndPage.OriginalString)));
                    }
                    continue;
                }
                else if (pageRange == first)
                {
                    minPage = currentStartPage;
                    maxPage = currentEndPage;
                    minPageAsInteger = currentStartPageAsInteger;
                    maxPageAsInteger = currentEndPageAsInteger;
                }
                else if (!PreviousPageRangeWasNumber || !SameNumberingType || !startPageIsNumber || !endPageIsNumber || pageRange.StartPage.Number == null)
                {
                    IsDiscreteRange = true;
                }
                else if (currentStartPageAsInteger.CompareTo(minPageAsInteger) >= 0 && currentEndPageAsInteger.CompareTo(maxPageAsInteger) <= 0 && SameNumberingType)
                {
                    // In this case, we don't have to do anything because the current page range is within the range defined by minPage & maxPage
                }
                else if (currentStartPageAsInteger.CompareTo(maxPageAsInteger + 1) < 1 && SameNumberingType)
                {
                    maxPage = currentEndPage;
                    maxPageAsInteger = currentEndPageAsInteger;
                }
                else
                {
                    IsDiscreteRange = true;
                }

                if (IsDiscreteRange && pageRange == last)
                {
                    PageRange dummyPageRange = new PageRange();
                    dummyPageRange = dummyPageRange.Update(previousPageRangeNumberingType);
                    newList.Add(dummyPageRange.Update(string.Format("{0}-{1}", minPage.OriginalString, maxPage.OriginalString)));
                    minPage = currentStartPage;
                    maxPage = currentEndPage;
                    dummyPageRange = dummyPageRange.Update(pageRange.NumberingType);
                    newList.Add(dummyPageRange.Update(string.Format("{0}-{1}", minPage.OriginalString, maxPage.OriginalString)));
                }
                else if (IsDiscreteRange)
                {
                    PageRange dummyPageRange = new PageRange();
                    dummyPageRange = dummyPageRange.Update(previousPageRangeNumberingType);
                    newList.Add(dummyPageRange.Update(string.Format("{0}-{1}", minPage.OriginalString, maxPage.OriginalString)));
                    minPage = currentStartPage;
                    maxPage = currentEndPage;
                    minPageAsInteger = currentStartPageAsInteger;
                    maxPageAsInteger = currentEndPageAsInteger;
                }
                else if ((newList.Count == 0 || !PreviousPageRangeWasNumber || !SameNumberingType) && pageRange == last)
                {
                    PageRange dummyPageRange = new PageRange();
                    dummyPageRange = dummyPageRange.Update(pageRange.NumberingType);
                    newList.Add(dummyPageRange.Update(string.Format("{0}-{1}", minPage.OriginalString, maxPage.OriginalString)));
                }
                previousPageRangeNumberingType = pageRange.NumberingType;
                PreviousPageRangeWasNumber = true;
            }
            return newList;
        } // end MergeAdjacent
    }
    class PageRangeComparer : IComparer<PageRange>
    {
        public int Compare(PageRange x, PageRange y)
        {
            if (x == null)
            {
                if (y == null)
                {
                    return 0;
                }
                else
                {
                    return -1;
                }
            }
            else
            {
                if (y == null)
                {
                    return 1;
                }
                else
                {
                    if (x.NumberingType.CompareTo(y.NumberingType) != 0)
                    {
                        return x.NumberingType.CompareTo(y.NumberingType);
                    }
                    else if (x.NumeralSystem.CompareTo(y.NumeralSystem) != 0)
                    {
                        return x.NumeralSystem.CompareTo(y.NumeralSystem);
                    }
                    else
                    {
                        return x.StartPage.CompareTo(y.StartPage);
                    }
                }
            }
        }
    } // end PageRangeComparer
}
Der erste Schritt, umwandeln der externen Markierungen, geht auch, so halb...


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using SwissAcademic.Citavi;
using SwissAcademic.Citavi.Shell;
using SwissAcademic.Citavi.Shell.Controls.Preview;
using SwissAcademic.Controls.WindowsForms;
using SwissAcademic.Citavi.Controls.Wpf;
using SwissAcademic.Pdf;
using SwissAcademic.Pdf.Analysis;

using pdftron;
using pdftron.Filters;
using pdftron.FDF;
using pdftron.Common;
using pdftron.SDF;

using pdftron.PDF;
using pdftron.PDF.Annots;

namespace BornheimsToolbox
{
    class AnnotationConverter
    {
        public void ConvertAnnotations(Reference reference)
        {
            var pdfLocations = reference.GetPDFLocations();
            var knowledgeItems = reference.Quotations.ToList();

            var project = reference.Project;
                
            Document document = null;

            document = PDFUtilities.GetDocument(Program.ActiveProjectShell.PrimaryMainForm.PreviewControl);
            
            if (document != null)
            {  
                Location location = reference.Locations.FirstOrDefault();

                foreach (Annotation annotation in location.Annotations)
                {
                    if (annotation.EntityLinks.Count() >= 1) continue;

                    KnowledgeItem newQuotation = new KnowledgeItem(reference, QuotationType.DirectQuotation);
                    reference.Quotations.Add(newQuotation);
                    project.AllKnowledgeItems.Add(newQuotation);

                    EntityLink newEntityLink = new EntityLink(project);
                    newEntityLink.Source = newQuotation;
                    newEntityLink.Target = annotation;
                    newEntityLink.Indication = EntityLink.PdfKnowledgeItemIndication;
                    project.EntityLinks.Add(newEntityLink);
                }
            }
        }
    }
}
Lassen wir mal das Problem außer acht, dass das im Moment sich einfach den ersten Anhang des aktiven Titels schnappt – das kriege ich noch hin, dass stattdessen das aktive Dokument aus dem Vorschaufenster genommen wird. Das große Problem ist, dass das Wissenselement leer ist. Meine Frage ist vielmehr:

Ich frage mich, ob es eine einfache Methode gibt, den Text zu kriegen, welcher hinter der "annotation" liegt, denn sonst bleiben die Wissenselemente leer.

Beste Grüße

Jan Jakob

Beste Antwort
Foto

Hat sich erledigt. Wenn eine Annotation ausgewählt ist, kann ich per


PreviewControl previewControl = Program.ActiveProjectShell.PrimaryMainForm.PreviewControl;

            var type = previewControl.GetType();
            var propertyInfos = type.GetProperties(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
            var propertyInfo = propertyInfos.FirstOrDefault(prop => prop.Name.Equals("PdfViewControl", StringComparison.OrdinalIgnoreCase));

PdfViewControl pdfViewControl = propertyInfo.GetValue(Program.ActiveProjectShell.PrimaryMainForm.PreviewControl) as PdfViewControl;

                    Content content = pdfViewControl.GetSelectedContentFromType(pdfViewControl.GetSelectedContentType(), -1, false, true);
                    SwissAcademic.Citavi.Controls.Wpf.TextContent textContent = content as TextContent;


den Inhalt abfragen.

Kommentare (1)

Foto
1

Hat sich erledigt. Wenn eine Annotation ausgewählt ist, kann ich per


PreviewControl previewControl = Program.ActiveProjectShell.PrimaryMainForm.PreviewControl;

            var type = previewControl.GetType();
            var propertyInfos = type.GetProperties(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
            var propertyInfo = propertyInfos.FirstOrDefault(prop => prop.Name.Equals("PdfViewControl", StringComparison.OrdinalIgnoreCase));

PdfViewControl pdfViewControl = propertyInfo.GetValue(Program.ActiveProjectShell.PrimaryMainForm.PreviewControl) as PdfViewControl;

                    Content content = pdfViewControl.GetSelectedContentFromType(pdfViewControl.GetSelectedContentType(), -1, false, true);
                    SwissAcademic.Citavi.Controls.Wpf.TextContent textContent = content as TextContent;


den Inhalt abfragen.