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 }); } } }
public static void ExportBookmarks(List <Reference> references) { List <Reference> exportFailed = new List <Reference>(); int exportCounter = 0; PreviewControl previewControl = Program.ActiveProjectShell.PrimaryMainForm.PreviewControl; 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).Select(l => (LinkedResource)l.Address).ToList(); List <KnowledgeItem> quickReferences = quotations.Where(q => q.QuotationType == QuotationType.QuickReference && q.EntityLinks.Where(e => e.Indication == EntityLink.PdfKnowledgeItemIndication).Count() > 0).ToList(); 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; } List <KnowledgeItem> quickReferencesAtThisLocation = quickReferences.Where(d => ((Annotation)d.EntityLinks.Where(e => e.Indication == EntityLink.PdfKnowledgeItemIndication).ToList().FirstOrDefault().Target).Location.Address == linkedResource).ToList(); List <Annotation> quickReferenceAnnotationsAtThisLocation = quickReferencesAtThisLocation.Select(d => (Annotation)d.EntityLinks.Where(e => e.Indication == EntityLink.PdfKnowledgeItemIndication).ToList().FirstOrDefault().Target).ToList(); DeleteExtraBookmarks(quickReferencesAtThisLocation, document); foreach (Annotation annotation in quickReferenceAnnotationsAtThisLocation) { ExportQuickReferenceAsBookmark(annotation, document); } try { document.Save(pathToFile, SDFDoc.SaveOptions.e_remove_unused); exportCounter++; } catch { exportFailed.Add(reference); } document.Close(); } } string message = "Bookmarks 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 }); } } }
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."); }