private void OnDrop_PDFAnnotation(object drop_object, Point mouse_current_virtual) { PDFAnnotation pdf_annotation = (PDFAnnotation)drop_object; PDFAnnotationNodeContent panc = new PDFAnnotationNodeContent(null, pdf_annotation.DocumentFingerprint, pdf_annotation.Guid.Value); drag_drop_manager.SceneRenderingControl.AddNewNodeControl(panc, mouse_current_virtual.X, mouse_current_virtual.Y); }
public void ProcessKeyPress(KeyEventArgs e) { if (false) { } else if (Key.P == e.Key) { PDFAnnotation pdf_annotation = pdf_annotation_node_content.PDFAnnotation.Underlying; PDFDocument pdf_document = pdf_annotation_node_content.PDFDocument.Underlying; int target_resolution = 150; int actual_resolution = target_resolution; double resolution_rescale_factor = 1; Image annotation_image = PDFAnnotationToImageRenderer.RenderAnnotation(pdf_document, pdf_annotation, actual_resolution); BitmapSource cropped_image_page = BitmapImageTools.FromImage(annotation_image, (int)(annotation_image.Width * resolution_rescale_factor), (int)(annotation_image.Height * resolution_rescale_factor)); ImageIcon.Source = cropped_image_page; ImageIcon.Width = cropped_image_page.Width / 2; TextText.MaxWidth = ImageIcon.Width; e.Handled = true; } }
private void drag_area_tracker_OnDragComplete(bool button_left_pressed, bool button_right_pressed, Point mouse_down_point, Point mouse_up_point) { FeatureTrackingManager.Instance.UseFeature(Features.Document_AddAnnotation); int MINIMUM_DRAG_SIZE_TO_CREATE_ANNOTATION = 20; if (Math.Abs(mouse_up_point.X - mouse_down_point.X) < MINIMUM_DRAG_SIZE_TO_CREATE_ANNOTATION || Math.Abs(mouse_up_point.Y - mouse_down_point.Y) < MINIMUM_DRAG_SIZE_TO_CREATE_ANNOTATION) { Logging.Info("Drag area too small to create annotation"); return; } PDFAnnotation pdf_annotation = new PDFAnnotation(pdf_renderer_control_stats.pdf_document.PDFRenderer.DocumentFingerprint, page, PDFAnnotationEditorControl.LastAnnotationColor, ConfigurationManager.Instance.ConfigurationRecord.Account_Nickname); pdf_annotation.Left = Math.Min(mouse_up_point.X, mouse_down_point.X) / ActualWidth; pdf_annotation.Top = Math.Min(mouse_up_point.Y, mouse_down_point.Y) / ActualHeight; pdf_annotation.Width = Math.Abs(mouse_up_point.X - mouse_down_point.X) / ActualWidth; pdf_annotation.Height = Math.Abs(mouse_up_point.Y - mouse_down_point.Y) / ActualHeight; pdf_renderer_control_stats.pdf_document.GetAnnotations().AddUpdatedAnnotation(pdf_annotation); PDFAnnotationItem pdf_annotation_item = new PDFAnnotationItem(this, pdf_annotation, pdf_renderer_control_stats); pdf_annotation_item.ResizeToPage(ActualWidth, ActualHeight); Children.Add(pdf_annotation_item); }
protected virtual void Dispose(bool disposing) { Logging.Debug("LibraryIndexHoverPopup::Dispose({0}) @{1}", disposing, dispose_count); WPFDoEvents.InvokeInUIThread(() => { WPFDoEvents.SafeExec(() => { ImageThumbnail.Visibility = Visibility.Collapsed; ImageThumbnail.Source = null; }); WPFDoEvents.SafeExec(() => { pdf_document = null; specific_pdf_annotation = null; }); WPFDoEvents.SafeExec(() => { DataContext = null; }); ++dispose_count; }); }
internal void ProcessAnnotation(PDFAnnotation pdf_annotation) { if (pdf_annotation.Deleted) { return; } ProcessTags(TagTools.ConvertTagBundleToTags(pdf_annotation.Tags)); }
private void OnDrop_PDFAnnotation(object drop_object, Point mouse_current_virtual) { PDFAnnotation pdf_annotation = (PDFAnnotation)drop_object; ASSERT.Test(pdf_annotation != null); PDFAnnotationNodeContent panc = new PDFAnnotationNodeContent(null, pdf_annotation.DocumentFingerprint, pdf_annotation.Guid.Value); DragDropManager?.SceneRenderingControl?.AddNewNodeControl(panc, mouse_current_virtual.X, mouse_current_virtual.Y); }
protected virtual void Dispose(bool disposing) { Logging.Debug("PDFAnnotationItem::Dispose({0}) @{1}", disposing, dispose_count); WPFDoEvents.InvokeInUIThread(() => { WPFDoEvents.SafeExec(() => { if (dispose_count == 0) { // Get rid of managed resources / get rid of cyclic references: if (null != pdf_annotation) { pdf_annotation.Bindable.PropertyChanged -= pdf_annotation_PropertyChanged; } ButtonAnnotationDetails.MouseEnter -= ButtonAnnotationDetails_MouseEnter; ButtonAnnotationDetails.MouseLeave -= ButtonAnnotationDetails_MouseLeave; ButtonAnnotationDetails.MouseDown -= ButtonAnnotationDetails_MouseDown; TextAnnotationText.GotFocus -= TextAnnotationText_GotFocus; TextAnnotationText.LostFocus -= TextAnnotationText_LostFocus; TextAnnotationText.PreviewMouseDown -= TextAnnotationText_PreviewMouseDown; TextAnnotationText.PreviewMouseMove -= TextAnnotationText_PreviewMouseMove; TextAnnotationText.PreviewMouseUp -= TextAnnotationText_PreviewMouseUp; ObjTagEditorControl.GotFocus -= ObjTagEditorControl_GotFocus; ObjTagEditorControl.LostFocus -= ObjTagEditorControl_LostFocus; ObjTagEditorControl.TagFeature_Add = null; ObjTagEditorControl.TagFeature_Remove = null; Dispatcher.ShutdownStarted -= Dispatcher_ShutdownStarted; } }); WPFDoEvents.SafeExec(() => { // Clear the references for sanity's sake DataContext = null; }); WPFDoEvents.SafeExec(() => { pdf_annotation_layer = null; pdf_annotation = null; pdf_annotation_editor_control_popup = null; }); ++dispose_count; }); }
private static void run_AnnotationEdit_MouseDown(object sender, MouseButtonEventArgs e) { Run run = (Run)sender; PDFAnnotation pdf_annotation = (PDFAnnotation)run.Tag; PDFAnnotationEditorControl pdf_annotation_editor_control = new PDFAnnotationEditorControl(); pdf_annotation_editor_control.SetAnnotation(pdf_annotation); AugmentedToolWindow pdf_annotation_editor_control_popup = new AugmentedToolWindow(pdf_annotation_editor_control, "Edit Annotation"); pdf_annotation_editor_control_popup.IsOpen = true; e.Handled = true; }
public void SetAnnotation(PDFAnnotation value) { pdf_annotation = value; DataContext = pdf_annotation.Bindable; if (null != pdf_annotation) { ObjColorPicker.SelectedColor = pdf_annotation.Color; Visibility = Visibility.Visible; } else { Visibility = Visibility.Collapsed; } }
public void SetPopupContent(PDFDocument pdf_document, int page, PDFAnnotation specific_pdf_annotation = null) { DataContext = null; ObjThemeSwatch.Background = ThemeBrushes.GetBrushForDocument(pdf_document); ImageThumbnail.Source = null; // Set the new listener this.pdf_document = pdf_document; this.page = page; this.specific_pdf_annotation = specific_pdf_annotation; if (null != pdf_document) { DataContext = pdf_document.Bindable; DisplayThumbnail(); } }
protected virtual void Dispose(bool disposing) { Logging.Debug("PDFAnnotationItem::Dispose({0}) @{1}", disposing, dispose_count); try { if (dispose_count == 0) { // Get rid of managed resources / get rid of cyclic references: if (null != pdf_annotation) { pdf_annotation.Bindable.PropertyChanged -= pdf_annotation_PropertyChanged; } ButtonAnnotationDetails.MouseEnter -= ButtonAnnotationDetails_MouseEnter; ButtonAnnotationDetails.MouseLeave -= ButtonAnnotationDetails_MouseLeave; ButtonAnnotationDetails.MouseDown -= ButtonAnnotationDetails_MouseDown; TextAnnotationText.GotFocus -= TextAnnotationText_GotFocus; TextAnnotationText.LostFocus -= TextAnnotationText_LostFocus; TextAnnotationText.PreviewMouseDown -= TextAnnotationText_PreviewMouseDown; TextAnnotationText.PreviewMouseMove -= TextAnnotationText_PreviewMouseMove; TextAnnotationText.PreviewMouseUp -= TextAnnotationText_PreviewMouseUp; ObjTagEditorControl.GotFocus -= ObjTagEditorControl_GotFocus; ObjTagEditorControl.LostFocus -= ObjTagEditorControl_LostFocus; ObjTagEditorControl.TagFeature_Add = null; ObjTagEditorControl.TagFeature_Remove = null; } // Clear the references for sanity's sake DataContext = null; pdf_annotation_layer = null; pdf_annotation = null; pdf_renderer_control_stats = null; pdf_annotation_editor_control_popup = null; } catch (Exception ex) { Logging.Error(ex); } ++dispose_count; }
public static bool LocateFirstPDFDocumentWithAnnotation(string library_fingerprint, string document_fingerprint, Guid annotation_guid, out PDFDocument out_pdf_document, out PDFAnnotation out_pdf_annotation) { WPFDoEvents.AssertThisCodeIs_NOT_RunningInTheUIThread(); // First attempt to find it in the specified library if (null != library_fingerprint) { WebLibraryDetail web_library_detail = WebLibraryManager.Instance.GetLibrary(library_fingerprint); if (null != web_library_detail) { PDFDocument pdf_document = web_library_detail.Xlibrary.GetDocumentByFingerprint(document_fingerprint); if (null != pdf_document) { PDFAnnotation pdf_annotation = pdf_document.GetAnnotationByGuid(annotation_guid); if (null != pdf_annotation) { out_pdf_document = pdf_document; out_pdf_annotation = pdf_annotation; return(true); } } } } // If we couldn't find it, then try in all the other libraries... foreach (WebLibraryDetail web_library_detail in WebLibraryManager.Instance.WebLibraryDetails_WorkingWebLibraries) { PDFDocument pdf_document = web_library_detail.Xlibrary.GetDocumentByFingerprint(document_fingerprint); if (null != pdf_document) { PDFAnnotation pdf_annotation = pdf_document.GetAnnotationByGuid(annotation_guid); if (null != pdf_annotation) { out_pdf_document = pdf_document; out_pdf_annotation = pdf_annotation; return(true); } } } // Out of luck! out_pdf_document = null; out_pdf_annotation = null; return(false); }
public static List <PDFAnnotation> ConvertRegionsToPDFAnnotations(List <RegionOfInterest> regions, string tag, PDFDocument pdf_document, int page) { List <PDFAnnotation> annotations = new List <PDFAnnotation>(); foreach (var region in regions) { PDFAnnotation pdf_annotation = new PDFAnnotation(pdf_document.Fingerprint, page, Colors.Black, null); pdf_annotation.Left = 0; pdf_annotation.Top = Math.Max(0, region.top - PROXIMITY_MARGIN); pdf_annotation.Width = 1; pdf_annotation.Height = Math.Min(1, region.height + 2 * PROXIMITY_MARGIN); pdf_annotation.Tags = tag; annotations.Add(pdf_annotation); } return(annotations); }
internal static void ReadFromDisk(PDFDocument pdf_document) { WPFDoEvents.AssertThisCodeIs_NOT_RunningInTheUIThread(); byte[] annotations_data = null; // Try to load the annotations from file if they exist var items = pdf_document.LibraryRef.Xlibrary.LibraryDB.GetLibraryItems(PDFDocumentFileLocations.ANNOTATIONS, new List <string>() { pdf_document.Fingerprint }); ASSERT.Test(items.Count < 2); if (0 < items.Count) { annotations_data = items[0].data; } // If we actually have some annotations, load them if (null != annotations_data) { List <DictionaryBasedObject> annotation_dictionaries = null; try { annotation_dictionaries = ReadFromStream_JSON(annotations_data); } catch (Exception) { annotation_dictionaries = ReadFromStream_BINARY(annotations_data); } if (null != annotation_dictionaries) { foreach (DictionaryBasedObject annotation_dictionary in annotation_dictionaries) { PDFAnnotation pdf_annotation = new PDFAnnotation(annotation_dictionary, false); pdf_document.AddUpdatedAnnotation(pdf_annotation); } } } }
internal static void ReadFromDisk(PDFDocument pdf_document, PDFAnnotationList annotations, Dictionary <string, byte[]> library_items_annotations_cache) { byte[] annotations_data = null; // Try the cache if (null != library_items_annotations_cache) { library_items_annotations_cache.TryGetValue(pdf_document.Fingerprint, out annotations_data); } else // Try to load the annotations from file if they exist { var items = pdf_document.Library.LibraryDB.GetLibraryItems(pdf_document.Fingerprint, PDFDocumentFileLocations.ANNOTATIONS); if (0 < items.Count) { annotations_data = items[0].data; } } // If we actually have some annotations, load them if (null != annotations_data) { List <DictionaryBasedObject> annotation_dictionaries = null; try { annotation_dictionaries = ReadFromStream_JSON(annotations_data); } catch (Exception) { annotation_dictionaries = ReadFromStream_BINARY(annotations_data); } if (null != annotation_dictionaries) { foreach (DictionaryBasedObject annotation_dictionary in annotation_dictionaries) { PDFAnnotation pdf_annotation = new PDFAnnotation(annotation_dictionary, false); annotations.AddUpdatedAnnotation(pdf_annotation); } } } }
public PDFAnnotationItem(PDFAnnotationLayer pdf_annotation_layer, PDFAnnotation pdf_annotation) { this.pdf_annotation_layer = new WeakReference <PDFAnnotationLayer>(pdf_annotation_layer); this.pdf_annotation = pdf_annotation; DataContext = pdf_annotation.Bindable; InitializeComponent(); MouseEnter += TextAnnotationText_MouseEnter; MouseLeave += TextAnnotationText_MouseLeave; ButtonAnnotationDetails.MouseEnter += ButtonAnnotationDetails_MouseEnter; ButtonAnnotationDetails.MouseLeave += ButtonAnnotationDetails_MouseLeave; ButtonAnnotationDetails.Cursor = Cursors.Hand; ButtonAnnotationDetails.MouseDown += ButtonAnnotationDetails_MouseDown; ButtonAnnotationDetails.Source = Icons.GetAppIcon(Icons.Metadata); ButtonAnnotationDetails.Width = 32; ButtonAnnotationDetails.ToolTip = "Edit this annotation."; RenderOptions.SetBitmapScalingMode(ButtonAnnotationDetails, BitmapScalingMode.HighQuality); TextAnnotationText.Background = Brushes.Transparent; TextAnnotationText.GotFocus += TextAnnotationText_GotFocus; TextAnnotationText.LostFocus += TextAnnotationText_LostFocus; TextAnnotationText.PreviewMouseDown += TextAnnotationText_PreviewMouseDown; TextAnnotationText.PreviewMouseMove += TextAnnotationText_PreviewMouseMove; TextAnnotationText.PreviewMouseUp += TextAnnotationText_PreviewMouseUp; ObjTagEditorControl.GotFocus += ObjTagEditorControl_GotFocus; ObjTagEditorControl.LostFocus += ObjTagEditorControl_LostFocus; pdf_annotation.Bindable.PropertyChanged += pdf_annotation_PropertyChanged; ObjTagEditorControl.TagFeature_Add = Features.Document_AddAnnotationTag; ObjTagEditorControl.TagFeature_Remove = Features.Document_RemoveAnnotationTag; ReColor(); //Unloaded += PDFAnnotationItem_Unloaded; Dispatcher.ShutdownStarted += Dispatcher_ShutdownStarted; }
protected virtual void Dispose(bool disposing) { Logging.Debug("LibraryIndexHoverPopup::Dispose({0}) @{1}", disposing, dispose_count); try { ImageThumbnail.Visibility = Visibility.Collapsed; ImageThumbnail.Source = null; pdf_document = null; specific_pdf_annotation = null; DataContext = null; } catch (Exception ex) { Logging.Error(ex); } ++dispose_count; }
/// <summary> /// Sorts by page, then y-offset /// </summary> /// <param name="a1"></param> /// <param name="a2"></param> /// <returns></returns> private static int AnnotationSorter(PDFAnnotation a1, PDFAnnotation a2) { if (a1.Page < a2.Page) { return(-1); } if (a1.Page > a2.Page) { return(+1); } if (a1.Top < a2.Top) { return(-1); } if (a1.Top > a2.Top) { return(+1); } return(0); }
private static PDFAnnotation ConvertLegacyAnnotationToPDFAnnotation(PDFDocument pdf_document, int page, PdfLoadedPage raw_pdf_page, PdfAnnotation raw_pdf_annotation) { // Try a popup { PdfLoadedPopupAnnotation raw_popup_annotation = raw_pdf_annotation as PdfLoadedPopupAnnotation; if (null != raw_popup_annotation) { Logging.Debug(" - page = {0}", raw_pdf_page.Size); Logging.Debug(" - annotation = {0}", raw_popup_annotation.Bounds); Logging.Debug(" - text = {0}", raw_popup_annotation.Text); // Work out the relative coordinates double BUFFER_HORIZ = 0.05; double BUFFER_VERT = 0.1; double left = BUFFER_HORIZ; double width = 1 - 2 * BUFFER_HORIZ; double top = 1.0 - (raw_popup_annotation.Bounds.Top / raw_pdf_page.Size.Height) - BUFFER_VERT / 2 + (raw_popup_annotation.Bounds.Height / raw_pdf_page.Size.Height) / 2; double height = BUFFER_VERT; //Bound it top = Math.Max(0, top); string text = raw_popup_annotation.Icon + ": " + raw_popup_annotation.Text; // Create the annotation PDFAnnotation pdf_annotation = new PDFAnnotation(pdf_document.PDFRenderer.DocumentFingerprint, page + 1, PDFAnnotationEditorControl.LastAnnotationColor, null); pdf_annotation.Legacy = true; pdf_annotation.Left = left; pdf_annotation.Top = top; pdf_annotation.Width = width; pdf_annotation.Height = height; pdf_annotation.Text = text; return(pdf_annotation); } } // Try a highlight { PdfLoadedTextMarkupAnnotation raw_markup_annotation = raw_pdf_annotation as PdfLoadedTextMarkupAnnotation; if (null != raw_markup_annotation) { Logging.Debug(" - page = {0}", raw_pdf_page.Size); Logging.Debug(" - annotation = {0}", raw_markup_annotation.Bounds); // Work out the relative coordinates double left = raw_markup_annotation.Bounds.Left / raw_pdf_page.Size.Width; double width = raw_markup_annotation.Bounds.Width / raw_pdf_page.Size.Width; double top = 1 - (raw_markup_annotation.Bounds.Top / raw_pdf_page.Size.Height); double height = raw_markup_annotation.Bounds.Height / raw_pdf_page.Size.Height; string text = raw_markup_annotation.TextMarkupAnnotationType.ToString(); // Create the annotation PDFAnnotation pdf_annotation = new PDFAnnotation(pdf_document.PDFRenderer.DocumentFingerprint, page + 1, PDFAnnotationEditorControl.LastAnnotationColor, null); pdf_annotation.Legacy = true; pdf_annotation.Left = left; pdf_annotation.Top = top; pdf_annotation.Width = width; pdf_annotation.Height = height; pdf_annotation.Text = text; return(pdf_annotation); } } // We don't understand this annotation { return(null); } }
public static void RenderAnnotations(Image image, PDFDocument pdf_document, int page, PDFAnnotation specific_pdf_annotation) { int TRANSPARENCY = (int)Math.Round(ConfigurationManager.Instance.ConfigurationRecord.GUI_AnnotationPrintTransparency * 255); using (Graphics graphics = Graphics.FromImage(image)) { foreach (PDFAnnotation pdf_annotation in pdf_document.GetAnnotations()) { if (pdf_annotation.Deleted) { continue; } // Must be same page if (pdf_annotation.Page != page) { continue; } // If they want specifics, must be same annotation if (null != specific_pdf_annotation && pdf_annotation != specific_pdf_annotation) { continue; } // If we get here, do it! using (Brush highlight_pen = new SolidBrush(Color.FromArgb(TRANSPARENCY, ColorTools.ConvertWindowsToDrawingColor(pdf_annotation.Color)))) { graphics.FillRectangle(highlight_pen, (float)(pdf_annotation.Left * image.Width), (float)(pdf_annotation.Top * image.Height), (float)(pdf_annotation.Width * image.Width), (float)(pdf_annotation.Height * image.Height)); } } } }
public static int ImportLegacyAnnotations(PDFDocument pdf_document) { int imported_count = 0; if (!pdf_document.DocumentExists) { Logging.Info("Not importing legacy annotations for {0} because it has no PDF.", pdf_document.Fingerprint); return(imported_count); } Logging.Info("+Importing legacy annotations from {0}", pdf_document.Fingerprint); using (AugmentedPdfLoadedDocument raw_pdf_document = new AugmentedPdfLoadedDocument(pdf_document.PDFRenderer.PDFFilename)) { for (int page = 0; page < raw_pdf_document.Pages.Count; ++page) { PdfLoadedPage raw_pdf_page = (PdfLoadedPage)raw_pdf_document.Pages[page]; if (null != raw_pdf_page.Annotations) { foreach (PdfAnnotation raw_pdf_annotation in raw_pdf_page.Annotations) { PDFAnnotation pdf_annotation = ConvertLegacyAnnotationToPDFAnnotation(pdf_document, page, raw_pdf_page, raw_pdf_annotation); if (null != pdf_annotation) { // Check if we already have this annotation PDFAnnotation matching_existing_pdf_annotation = null; foreach (PDFAnnotation existing_pdf_annotation in pdf_document.Annotations) { if (true && existing_pdf_annotation.Page == pdf_annotation.Page && existing_pdf_annotation.Left == pdf_annotation.Left && existing_pdf_annotation.Top == pdf_annotation.Top && existing_pdf_annotation.Text == pdf_annotation.Text ) { matching_existing_pdf_annotation = existing_pdf_annotation; } } if (null == matching_existing_pdf_annotation) { // Add it to the PDFDocument pdf_document.Annotations.AddUpdatedAnnotation(pdf_annotation); imported_count++; } else { if (matching_existing_pdf_annotation.Deleted) { Logging.Info("Undeleting an identical legacy annotation."); matching_existing_pdf_annotation.Deleted = false; matching_existing_pdf_annotation.Bindable.NotifyPropertyChanged(); imported_count++; } else { Logging.Info("Not importing an identical legacy annotation."); } } } } } } } Logging.Info("-Importing legacy annotations from {0}", pdf_document.Fingerprint); return(imported_count); }
internal static Dictionary <string, PDFAnnotationList> BulkReadFromDisk(WebLibraryDetail web_library_detail) { WPFDoEvents.AssertThisCodeIs_NOT_RunningInTheUIThread(); // Every document which has any annotations, may have many. So its one fingerprint, many annotation records. // We do not (yet) bother about potential fringe cases where annotations end up in a corrupted database in duplicate: // we KNOW that PDFAnnotationList code can manage that as it does compare incoming annotations // to already existing ones (used by the GUI annotation editor, of course :-) ) // Hence we choose a PDFAnnotationList for the annotations. Dictionary <string, PDFAnnotationList> rv = new Dictionary <string, PDFAnnotationList>(); // Try to load the annotations from file if they exist Dictionary <string, byte[]> library_items_annotations_cache = web_library_detail.Xlibrary.LibraryDB.GetLibraryItemsAsCache(PDFDocumentFileLocations.ANNOTATIONS); foreach (var item in library_items_annotations_cache) { string fingerprint = item.Key; byte[] annotations_data = item.Value; List <DictionaryBasedObject> annotation_dictionaries; try { annotation_dictionaries = ReadFromStream_JSON(annotations_data); } catch (Exception) { annotation_dictionaries = ReadFromStream_BINARY(annotations_data); } if (null != annotation_dictionaries) { foreach (DictionaryBasedObject annotation_dictionary in annotation_dictionaries) { PDFAnnotation pdf_annotation = new PDFAnnotation(annotation_dictionary, false); if (pdf_annotation.DocumentFingerprint != fingerprint) { Logging.Error($"Corrupted ANNOTATIONS record in database? Fingerprints do not match: key: '{fingerprint}' record-ref: '{pdf_annotation.DocumentFingerprint}'"); // We DO NOT know which one of those fingerprints is TRUE. We DO NOT want to loose the data, so we assign to BOTH. // This happens when we first clone the annotation and modify that fingerprint to store the SECOND record, // after which we follow the regular code path further below to store the FIRST (= ORIGINAL) record. PDFAnnotationList lst = null; _ = rv.TryGetValue(fingerprint, out lst); if (lst == null) { lst = new PDFAnnotationList(); rv.Add(fingerprint, lst); } var dupdict = (DictionaryBasedObject)annotation_dictionary.Clone(); // HACK: override the fingerprint in the decoded record: this duplicates code from `PDFAnnotation.DocumentFingerprint` dupdict["DocumentFingerprint"] = fingerprint; PDFAnnotation dup = new PDFAnnotation(annotation_dictionary, true); lst.__AddUpdatedAnnotation(dup); } { PDFAnnotationList lst = null; _ = rv.TryGetValue(pdf_annotation.DocumentFingerprint, out lst); if (lst == null) { lst = new PDFAnnotationList(); rv.Add(pdf_annotation.DocumentFingerprint, lst); } lst.__AddUpdatedAnnotation(pdf_annotation); } } } } return(rv); }
private static void BackgroundRenderImages_BACKGROUND(FlowDocument flow_document, List <AnnotationWorkGenerator.AnnotationWork> annotation_works, AnnotationReportOptions annotation_report_options) { Thread.Sleep(annotation_report_options.InitialRenderDelayMilliseconds); PDFDocument last_pdf_document = null; StatusManager.Instance.ClearCancelled("AnnotationReportBackground"); for (int j = 0; j < annotation_works.Count; ++j) { StatusManager.Instance.UpdateStatus("AnnotationReportBackground", "Building annotation report image", j, annotation_works.Count, true); if (StatusManager.Instance.IsCancelled("AnnotationReportBackground")) { Logging.Warn("User cancelled annotation report generation"); break; } AnnotationWorkGenerator.AnnotationWork annotation_work = annotation_works[j]; PDFDocument pdf_document = annotation_work.pdf_document; // Clear down our previously caches pages if (null != last_pdf_document && last_pdf_document != pdf_document) { if (last_pdf_document.DocumentExists) { last_pdf_document.PDFRenderer.FlushCachedPageRenderings(); } } // Remember this PDF document so we can flush it if necessary last_pdf_document = pdf_document; // Now render each image PDFAnnotation pdf_annotation = annotation_work.pdf_annotation; if (null != pdf_annotation) { try { // Clear the waiting for processing text annotation_work.processing_error.Dispatcher.Invoke(new Action(() => { annotation_work.processing_error.Text = ""; } ), DispatcherPriority.Background); if (pdf_document.DocumentExists) { // Fill in the paragraph text if (null != annotation_work.annotation_paragraph) { annotation_work.processing_error.Dispatcher.Invoke( new Action(() => BuildAnnotationWork_FillAnnotationText(pdf_document, pdf_annotation, annotation_work)), DispatcherPriority.Background); } if (null != annotation_work.report_floater) { annotation_work.processing_error.Dispatcher.Invoke(new Action(() => { try { System.Drawing.Image annotation_image = PDFAnnotationToImageRenderer.RenderAnnotation(pdf_document, pdf_annotation, 80); BitmapSource cropped_image_page = BitmapImageTools.FromImage(annotation_image); annotation_work.report_image.Source = cropped_image_page; annotation_work.report_floater.Width = new FigureLength(cropped_image_page.PixelWidth / 1); } catch (Exception ex) { Logging.Warn(ex, "There was a problem while rendering an annotation."); annotation_work.report_image.Source = Icons.GetAppIcon(Icons.AnnotationReportImageError); annotation_work.processing_error.Text = "There was a problem while rendering this annotation."; } } ), DispatcherPriority.Background); } } else { annotation_work.processing_error.Dispatcher.Invoke(new Action(() => { if (null != annotation_work.report_image) { annotation_work.report_image.Source = Icons.GetAppIcon(Icons.AnnotationReportImageError); } annotation_work.processing_error.Text = "Can't show image: The PDF does not exist locally."; } ), DispatcherPriority.Background); } } catch (Exception ex) { Logging.Error(ex, "There was an error while rendering page {0} for document {1} for the annotation report", pdf_annotation.Page, pdf_annotation.DocumentFingerprint); annotation_work.processing_error.Dispatcher.Invoke(new Action(() => { if (null != annotation_work.report_image) { annotation_work.report_image.Source = Icons.GetAppIcon(Icons.AnnotationReportImageError); } annotation_work.processing_error.Text = "Can't show image: There was an error rendering the metadata image."; } ), DispatcherPriority.Background); } } } // And flush the rendering cache of the last document if (null != last_pdf_document) { if (last_pdf_document.DocumentExists) { last_pdf_document.PDFRenderer.FlushCachedPageRenderings(); } } StatusManager.Instance.ClearStatus("AnnotationReportBackground"); }
private static void BuildAnnotationWork_FillAnnotationText(PDFDocument pdf_document, PDFAnnotation pdf_annotation, AnnotationWorkGenerator.AnnotationWork annotation_work) { int current_color = -1; Run current_run = new Run(); annotation_work.annotation_paragraph.Inlines.Add(current_run); try { // Get the text for this annotation WordList word_list = pdf_document.PDFRenderer.GetOCRText(pdf_annotation.Page); if (null != word_list) { StringBuilder current_sb = new StringBuilder(); foreach (var word in word_list) { if (word.Contains(pdf_annotation.Left, pdf_annotation.Top, pdf_annotation.Width, pdf_annotation.Height)) { // Find out what colour this word is... int new_color = -1; foreach (PDFHighlight highlight in pdf_document.Highlights.GetHighlightsForPage(pdf_annotation.Page)) { if (highlight.Page == pdf_annotation.Page && word.ContainsMajority(highlight.Left, highlight.Top, highlight.Width, highlight.Height)) { new_color = highlight.Color; break; } } // If the colour has change if (new_color != current_color) { // Emit the existing span current_run.Text = current_sb.ToString(); // Create the new span current_color = new_color; current_run = new Run(); current_sb = new StringBuilder(); annotation_work.annotation_paragraph.Inlines.Add(current_run); if (-1 != new_color) { current_run.Background = new SolidColorBrush(StandardHighlightColours.GetColor(new_color)); } } // Tidy up dashes on line-ends string current_text = word.Text; string current_spacer = " "; if (current_text.EndsWith("-")) { current_text = current_text.TrimEnd('-'); current_spacer = ""; } // Append the new text current_sb.Append(current_text); current_sb.Append(current_spacer); } } // Emit the final span current_run.Text = current_sb.ToString(); } else { Run run = new Run(); run.Background = Brushes.Orange; run.Text = String.Format("OCR is not complete for page {0}", pdf_annotation.Page); annotation_work.annotation_paragraph.Inlines.Add(run); } } catch (Exception ex) { Logging.Error(ex, "There was a problem while trying to add annotation text for document {0}", pdf_document.Fingerprint); Run run = new Run(); run.Background = Brushes.Red; run.Text = String.Format("Processing error: {0}", ex.Message); annotation_work.annotation_paragraph.Inlines.Add(run); } }
internal static AnnotationReport BuildReport(Library library, List <PDFDocument> pdf_documents, AnnotationReportOptions annotation_report_options) { AnnotationReport annotation_report = new AnnotationReport(); StandardFlowDocument flow_document = annotation_report.flow_document; // Create a list of all the work we need to do List <AnnotationWorkGenerator.AnnotationWork> annotation_works = AnnotationWorkGenerator.GenerateAnnotationWorks(library, pdf_documents, annotation_report_options); // Now build the report PDFDocument last_pdf_document = null; for (int j = 0; j < annotation_works.Count; ++j) { StatusManager.Instance.UpdateStatus("AnnotationReport", "Building annotation report", j, annotation_works.Count); AnnotationWorkGenerator.AnnotationWork annotation_work = annotation_works[j]; PDFDocument pdf_document = annotation_work.pdf_document; PDFAnnotation pdf_annotation = annotation_work.pdf_annotation; // If this is a new PDFDocument, print out the header if (last_pdf_document != pdf_document) { last_pdf_document = pdf_document; Logging.Info("Processing {0}", pdf_document.Fingerprint); if (!annotation_report_options.SuppressPDFDocumentHeader) { Span bold_title = new Span(); bold_title.FontSize = 30; bold_title.FontFamily = ThemeTextStyles.FontFamily_Header; bold_title.Inlines.Add(new LineBreak()); if (!String.IsNullOrEmpty(pdf_document.TitleCombined)) { bold_title.Inlines.Add(pdf_document.TitleCombined); bold_title.Inlines.Add(new LineBreak()); } Span bold = new Span(); bold.FontSize = 16; if (!String.IsNullOrEmpty(pdf_document.YearCombined)) { bold.Inlines.Add(pdf_document.YearCombined); bold.Inlines.Add(" · "); } if (!String.IsNullOrEmpty(pdf_document.AuthorsCombined)) { bold.Inlines.Add(pdf_document.AuthorsCombined); } if (!String.IsNullOrEmpty(pdf_document.Publication)) { Italic italic = new Italic(); italic.Inlines.Add(pdf_document.Publication); bold.Inlines.Add(new LineBreak()); bold.Inlines.Add(italic); } if (!String.IsNullOrEmpty(pdf_document.Tags)) { bold.Inlines.Add(new LineBreak()); Run run = new Run(); run.Text = "[" + pdf_document.Tags + "]"; bold.Inlines.Add(run); } bold.Inlines.Add(new LineBreak()); { bold.Inlines.Add(new LineBreak()); Span click_options = new Span(); { { Run run = new Run(" Open "); run.Background = ThemeColours.Background_Brush_Blue_VeryVeryDark; run.Foreground = Brushes.White; run.Cursor = Cursors.Hand; run.Tag = pdf_document; run.MouseDown += run_Open_MouseDown; click_options.Inlines.Add(run); } click_options.Inlines.Add(new Run(" ")); { Run run = new Run(" Cite (Author, Date) "); run.Background = ThemeColours.Background_Brush_Blue_VeryVeryDark; run.Foreground = Brushes.White; run.Cursor = Cursors.Hand; run.Tag = pdf_document; run.MouseDown += run_Cite_MouseDown_Together; click_options.Inlines.Add(run); } click_options.Inlines.Add(new Run(" ")); { Run run = new Run(" [Cite Author (Date)] "); run.Background = ThemeColours.Background_Brush_Blue_VeryVeryDark; run.Foreground = Brushes.White; run.Cursor = Cursors.Hand; run.Tag = pdf_document; run.MouseDown += run_Cite_MouseDown_Separate; click_options.Inlines.Add(run); } click_options.Inlines.Add(new LineBreak()); bold.Inlines.Add(click_options); annotation_report.AddClickOption(click_options); } } Paragraph paragraph_header = new Paragraph(); paragraph_header.Inlines.Add(bold_title); paragraph_header.Inlines.Add(bold); Section section_header = new Section(); section_header.Background = ThemeColours.Background_Brush_Blue_VeryVeryDarkToWhite; if (Colors.Transparent != pdf_document.Color) { section_header.Background = new SolidColorBrush(pdf_document.Color); } section_header.Blocks.Add(paragraph_header); flow_document.Blocks.Add(new Paragraph(new LineBreak())); flow_document.Blocks.Add(section_header); //flow_document.Blocks.Add(new Paragraph(new LineBreak())); bool have_document_details = false; // Add the paper comment if we need to if (annotation_report_options.IncludeComments) { string comment_text = pdf_document.Comments; if (!String.IsNullOrEmpty(comment_text)) { have_document_details = true; flow_document.Blocks.Add(new Paragraph(new Run("●"))); { Paragraph paragraph = new Paragraph(); { Bold header = new Bold(); header.Inlines.Add("Comments: "); paragraph.Inlines.Add(header); } { Italic italic = new Italic(); italic.Inlines.Add(comment_text); paragraph.Inlines.Add(italic); } flow_document.Blocks.Add(paragraph); } } } if (annotation_report_options.IncludeAbstract) { string abstract_text = pdf_document.Abstract; if (PDFAbstractExtraction.CANT_LOCATE != abstract_text) { have_document_details = true; flow_document.Blocks.Add(new Paragraph(new Run("●"))); { Paragraph paragraph = new Paragraph(); { Bold header = new Bold(); header.Inlines.Add("Abstract: "); paragraph.Inlines.Add(header); } { Italic italic = new Italic(); italic.Inlines.Add(abstract_text); paragraph.Inlines.Add(italic); } flow_document.Blocks.Add(paragraph); } } } if (have_document_details) { flow_document.Blocks.Add(new Paragraph(new Run("●"))); } } } // Print out the annotation if (null != pdf_annotation) { // First the header { //flow_document.Blocks.Add(new Paragraph(new Run(" ● "))); Paragraph paragraph = new Paragraph(); paragraph.Inlines.Add(new LineBreak()); { Bold coloured_blob = new Bold(); coloured_blob.Foreground = new SolidColorBrush(pdf_annotation.Color); coloured_blob.Inlines.Add(" ■ "); paragraph.Inlines.Add(coloured_blob); } { Span italic = new Span(); italic.FontSize = 16; string annotation_header = String.Format("Page {0}", pdf_annotation.Page); italic.Inlines.Add(annotation_header); //Underline underline = new Underline(italic); paragraph.Inlines.Add(italic); } { Bold coloured_blob = new Bold(); coloured_blob.Foreground = new SolidColorBrush(pdf_annotation.Color); coloured_blob.Inlines.Add(" ■ "); paragraph.Inlines.Add(coloured_blob); } paragraph.Inlines.Add(new LineBreak()); // List the tags for this annotation if (!annotation_report_options.SuppressPDFAnnotationTags) { if (!String.IsNullOrEmpty(pdf_annotation.Tags)) { paragraph.Inlines.Add(" [" + pdf_annotation.Tags.Replace(";", "; ") + "] "); paragraph.Inlines.Add(new LineBreak()); } } bool is_not_synthetic_annotation = (null == pdf_annotation.Tags || (!pdf_annotation.Tags.Contains(HighlightToAnnotationGenerator.HIGHLIGHTS_TAG) && !pdf_annotation.Tags.Contains(InkToAnnotationGenerator.INKS_TAG))); { paragraph.Inlines.Add(new LineBreak()); Span click_options = new Span(); { Run run = new Run(" Open "); run.Background = ThemeColours.Background_Brush_Blue_VeryDark; run.Foreground = Brushes.White; run.Cursor = Cursors.Hand; run.Tag = annotation_work; run.MouseDown += run_AnnotationOpen_MouseDown; click_options.Inlines.Add(run); } if (is_not_synthetic_annotation) { click_options.Inlines.Add(new Run(" ")); Run run = new Run(" Edit "); run.Background = ThemeColours.Background_Brush_Blue_VeryDark; run.Foreground = Brushes.White; run.Cursor = Cursors.Hand; run.Tag = pdf_annotation; run.MouseDown += run_AnnotationEdit_MouseDown; click_options.Inlines.Add(run); } { click_options.Inlines.Add(new Run(" ")); Run run = new Run(" Cite (Author, Date) "); run.Background = ThemeColours.Background_Brush_Blue_VeryDark; run.Foreground = Brushes.White; run.Cursor = Cursors.Hand; run.Tag = pdf_document; run.MouseDown += run_Cite_MouseDown_Together; click_options.Inlines.Add(run); } { click_options.Inlines.Add(new Run(" ")); Run run = new Run(" Cite Author (Date) "); run.Background = ThemeColours.Background_Brush_Blue_VeryDark; run.Foreground = Brushes.White; run.Cursor = Cursors.Hand; run.Tag = pdf_document; run.MouseDown += run_Cite_MouseDown_Separate; click_options.Inlines.Add(run); } click_options.Inlines.Add(new LineBreak()); paragraph.Inlines.Add(click_options); annotation_report.AddClickOption(click_options); } { Run run = new Run("Waiting for processing..."); run.Foreground = Brushes.Red; paragraph.Inlines.Add(run); annotation_work.processing_error = run; } paragraph.Background = ThemeColours.Background_Brush_Blue_DarkToWhite; flow_document.Blocks.Add(paragraph); } if (!String.IsNullOrEmpty(pdf_annotation.Text)) { Paragraph paragraph = new Paragraph(); paragraph.Inlines.Add(pdf_annotation.Text); flow_document.Blocks.Add(paragraph); } { // Prepare for some annotation image if ((!annotation_report_options.ObeySuppressedImages || !pdf_annotation.AnnotationReportSuppressImage) && !annotation_report_options.SuppressAllImages) { Image image = new Image(); MouseWheelDisabler.DisableMouseWheelForControl(image); image.Source = Icons.GetAppIcon(Icons.AnnotationReportImageWaiting); BlockUIContainer image_container = new BlockUIContainer(image); Figure floater = new Figure(image_container); floater.HorizontalAnchor = FigureHorizontalAnchor.PageCenter; floater.WrapDirection = WrapDirection.None; floater.Width = new FigureLength(64); floater.Cursor = Cursors.Hand; floater.Tag = annotation_work; floater.MouseDown += Floater_MouseDown; Paragraph paragraph = new Paragraph(); paragraph.Inlines.Add(floater); annotation_work.report_image = image; annotation_work.report_floater = floater; flow_document.Blocks.Add(paragraph); } // Prepare for some annotation text if ((!annotation_report_options.ObeySuppressedText || !pdf_annotation.AnnotationReportSuppressText) && !annotation_report_options.SuppressAllText) { Paragraph paragraph = new Paragraph(); annotation_work.annotation_paragraph = paragraph; flow_document.Blocks.Add(paragraph); } } } // Add another paragraph to separate nicely { Paragraph paragraph = new Paragraph(); flow_document.Blocks.Add(paragraph); } } // Render the images in the background BackgroundRenderImages(flow_document, annotation_works, annotation_report_options); // Finito! StatusManager.Instance.ClearStatus("AnnotationReport"); return(annotation_report); }