public AnnotationReport()
            {
                flow_document = new StandardFlowDocument();
                flow_document.TextAlignment = TextAlignment.Center;
                flow_document.Background    = Brushes.White;

                click_options = new List <ClickOption>();
            }
Ejemplo n.º 2
0
        public static void ExportToTextAndLaunch(PDFDocument pdf_document)
        {
            FeatureTrackingManager.Instance.UseFeature(Features.Document_ExportToText);

            // We have to hack this into an annotation report until we have decided if we want to use the collapsible stuff of annotation report
            StandardFlowDocument flow_document = DoExport(pdf_document);

            AsyncAnnotationReportBuilder.AnnotationReport annotation_report = new AsyncAnnotationReportBuilder.AnnotationReport(flow_document);
            ReportViewerControl report_view_control = new ReportViewerControl(annotation_report);
            string title = String.Format("Text export of PDF titled '{0}'", pdf_document.TitleCombined);

            MainWindowServiceDispatcher.Instance.OpenNewWindow(title, Icons.GetAppIcon(Icons.ExportToText), true, true, report_view_control);
        }
Ejemplo n.º 3
0
        public static StandardFlowDocument DoExport(PDFDocument pdf_document)
        {
            StandardFlowDocument flow_document = new StandardFlowDocument();

            flow_document.Background = Brushes.White;

            using (AugmentedPdfLoadedDocument doc = new AugmentedPdfLoadedDocument(pdf_document.DocumentPath))
            {
                for (int page = 1; page <= pdf_document.PageCount; ++page)
                {
                    string msg = $"Exporting page {page}/{pdf_document.PageCountAsString}";

                    StatusManager.Instance.UpdateStatus("ExportToWord", msg, page, Math.Max(1, pdf_document.PageCount));

                    try
                    {
                        Logging.Info(msg);

                        // Add the header for the page
                        {
                            Bold bold = new Bold();
                            bold.Inlines.Add(String.Format("--- Page {0} ---", page));
                            flow_document.Blocks.Add(new Paragraph(bold));
                        }

                        StringBuilder sb    = new StringBuilder();
                        WordList      words = pdf_document.GetOCRText(page);
                        if (null != words)
                        {
                            Word last_word = null;

                            foreach (Word word in words)
                            {
                                // Add a newline if we seem to have a linebreak
                                if (
                                    (
                                        (null != last_word && last_word.Left > word.Left) &&
                                        (word.Top > last_word.Top + 2 * last_word.Height) &&
                                        (word.Text.Length > 0 && !Char.IsLower(word.Text[0]))
                                    )
                                    ||
                                    (
                                        (null != last_word && last_word.Left > word.Left) &&
                                        (word.Top > last_word.Top + 2.5 * last_word.Height)
                                    )
                                    )
                                {
                                    sb.Append("\n\n");
                                }

                                sb.Append(word.Text);
                                sb.Append(' ');

                                last_word = word;
                            }
                        }
                        else
                        {
                            sb.AppendLine(String.Format("OCR text is not yet ready for page {0}.  Please try again in a few minutes.", page));
                        }

                        // Add the text from the page
                        {
                            Paragraph paragraph = new Paragraph();
                            paragraph.Inlines.Add(sb.ToString());
                            flow_document.Blocks.Add(paragraph);
                        }

                        // Add the images from the page
                        PdfPageBase pdf_page = doc.Pages[page - 1];
                        Image[]     images   = pdf_page.ExtractImages();

                        if (null != images)
                        {
                            foreach (Image image in images)
                            {
                                BitmapSource bitmap_source = BitmapImageTools.FromImage(image);

                                System.Windows.Controls.Image image_to_render = new System.Windows.Controls.Image();
                                image_to_render.Source = bitmap_source;
                                BlockUIContainer image_container = new BlockUIContainer(image_to_render);
                                Figure           floater         = new Figure(image_container);
                                floater.HorizontalAnchor = FigureHorizontalAnchor.PageLeft;
                                floater.WrapDirection    = WrapDirection.None;
                                floater.Width            = new FigureLength(image.Width);

                                {
                                    Paragraph paragraph = new Paragraph();
                                    paragraph.Inlines.Add(floater);
                                    flow_document.Blocks.Add(paragraph);
                                }
                            }
                        }

                        // Add the annotations from the page
#if false
                        {
                            Logging.Debug("This PDF has {0} annotations", pdf_page.Annotations.Count);
                            Logging.Debug("The size of the page is {0}", pdf_page.Size);
                            foreach (var annotation in pdf_page.Annotations)
                            {
                                PdfLoadedUriAnnotation uri_annotation = annotation as PdfLoadedUriAnnotation;
                                if (null != uri_annotation)
                                {
                                    Logging.Debug("There is a URL to '{0}' with text '{1}' with bounds '{2}'",
                                                  uri_annotation.Uri,
                                                  uri_annotation.Bounds,
                                                  uri_annotation.Text,
                                                  null);
                                }
                            }
                        }
#endif
                    }
                    catch (Exception ex)
                    {
                        Logging.Error(ex, "There was a problem exporting page {0}", page);
                    }
                }
            }

            StatusManager.Instance.UpdateStatus("ExportToWord", "Finished exporting");

            return(flow_document);
        }
        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);
        }
 public AnnotationReport(StandardFlowDocument flow_document)
 {
     this.flow_document = flow_document;
     click_options      = new List <ClickOption>();
 }