Ejemplo n.º 1
0
        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 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");
        }