예제 #1
0
        public HighlightCanvasToolbarControl()
        {
            InitializeComponent();

            ButtonPointErase.Icon    = Icons.GetAppIcon(Icons.InkPointErase);
            ButtonPointErase.Click  += ButtonPointErase_Click;
            ButtonPointErase.ToolTip = "Erase your highlights.";

            // Populate our buttons array for easy operation
            buttons = new List <AugmentedButton>();
            buttons.Add(ButtonColor1);
            buttons.Add(ButtonColor2);
            buttons.Add(ButtonColor3);
            buttons.Add(ButtonColor4);
            buttons.Add(ButtonColor5);

            // Set up the buttons
            for (int i = 0; i < buttons.Count; ++i)
            {
                var button = buttons[i];
                button.Background = new SolidColorBrush(StandardHighlightColours.GetColor(i));

                button.Click  += ButtonColor_Click;
                button.ToolTip = "Select a new highlighting colour.";
            }
        }
예제 #2
0
        public static Bitmap RenderHighlights(int width, int height, PDFDocument pdf_document, int page)
        {
            WPFDoEvents.AssertThisCodeIs_NOT_RunningInTheUIThread();

            // Render onto a scratch image in solid
            Bitmap bitmap = new Bitmap(width, height);     // <--- must b Dispose()d by caller

            using (Graphics graphics = Graphics.FromImage(bitmap))
            {
                double   last_right        = Double.NegativeInfinity;
                double   last_top          = Double.NegativeInfinity;
                double   last_bottom       = Double.NegativeInfinity;
                PointF[] adjoinment_points = new PointF[4];

                // TODO: next call can be very costly; MUST run in background!
                var highlights = pdf_document.Highlights.GetHighlightsForPage(page);

                foreach (PDFHighlight highlight in highlights)
                {
                    using (Brush highlight_pen = new SolidBrush(StandardHighlightColours.GetColor_Drawing(highlight.Color)))
                    {
                        graphics.FillRectangle(highlight_pen, (float)(highlight.Left * width), (float)(highlight.Top * height), (float)(highlight.Width * width), (float)(highlight.Height * height));

                        // Do some adjoining
                        if (Math.Abs(last_right - highlight.Left) < highlight.Height * 0.75 && Math.Abs(last_top - highlight.Top) < highlight.Height * 0.75 && Math.Abs(last_bottom - highlight.Bottom) < highlight.Height * 0.75)
                        {
                            // 0 -- 1
                            // |    |
                            // 3 -- 2

                            adjoinment_points[0].X = (float)(last_right * width);
                            adjoinment_points[0].Y = (float)(last_top * height);

                            adjoinment_points[1].X = (float)(highlight.Left * width);
                            adjoinment_points[1].Y = (float)(highlight.Top * height);

                            adjoinment_points[2].X = (float)(highlight.Left * width);
                            adjoinment_points[2].Y = (float)(highlight.Bottom * height);

                            adjoinment_points[3].X = (float)(last_right * width);
                            adjoinment_points[3].Y = (float)(last_bottom * height);

                            graphics.FillPolygon(highlight_pen, adjoinment_points);
                        }

                        // Remember the last position for future potential adjoining
                        last_right  = highlight.Right;
                        last_top    = highlight.Top;
                        last_bottom = highlight.Bottom;
                    }
                }
            }

            return(bitmap);
        }
        public static Bitmap RenderHighlights(int width, int height, PDFDocument pdf_document, int page)
        {
            // Render onto a scratch image in solid
            Bitmap bitmap = new Bitmap(width, height);

            using (Graphics graphics = Graphics.FromImage(bitmap))
            {
                double   last_right        = Double.NegativeInfinity;
                double   last_top          = Double.NegativeInfinity;
                double   last_bottom       = Double.NegativeInfinity;
                PointF[] adjoinment_points = new PointF[4];

                foreach (PDFHighlight highlight in pdf_document.Highlights.GetHighlightsForPage(page))
                {
                    Brush highlight_pen = new SolidBrush(StandardHighlightColours.GetColor_Drawing(highlight.Color));
                    graphics.FillRectangle(highlight_pen, (float)(highlight.Left * width), (float)(highlight.Top * height), (float)(highlight.Width * width), (float)(highlight.Height * height));

                    // Do some adjoining
                    if (Math.Abs(last_right - highlight.Left) < highlight.Height * 0.75 && Math.Abs(last_top - highlight.Top) < highlight.Height * 0.75 && Math.Abs(last_bottom - highlight.Bottom) < highlight.Height * 0.75)
                    {
                        // 0 -- 1
                        // |    |
                        // 3 -- 2

                        adjoinment_points[0].X = (float)(last_right * width);
                        adjoinment_points[0].Y = (float)(last_top * height);

                        adjoinment_points[1].X = (float)(highlight.Left * width);
                        adjoinment_points[1].Y = (float)(highlight.Top * height);

                        adjoinment_points[2].X = (float)(highlight.Left * width);
                        adjoinment_points[2].Y = (float)(highlight.Bottom * height);

                        adjoinment_points[3].X = (float)(last_right * width);
                        adjoinment_points[3].Y = (float)(last_bottom * height);

                        graphics.FillPolygon(highlight_pen, adjoinment_points);
                    }

                    // Remember the last position for future potential adjoining
                    last_right  = highlight.Right;
                    last_top    = highlight.Top;
                    last_bottom = highlight.Bottom;
                }
            }

            return(bitmap);
        }
예제 #4
0
        internal static void Export(Library library, string base_path, Dictionary <string, PDFDocumentExportItem> pdf_document_export_items)
        {
            List <PDFDocumentExportItem> pdf_document_export_items_values = new List <PDFDocumentExportItem>(pdf_document_export_items.Values);

            for (int i = 0; i < pdf_document_export_items_values.Count; ++i)
            {
                var item = pdf_document_export_items_values[i];
                try
                {
                    StatusManager.Instance.UpdateStatus("ContentExport", String.Format("Exporting entry {0} of {1}", i, pdf_document_export_items_values.Count), i, pdf_document_export_items_values.Count);

                    using (PdfDocument document = PdfReader.Open(item.filename, PdfDocumentOpenMode.Modify))
                    {
                        // First the properties
                        document.Info.Title            = item.pdf_document.TitleCombined;
                        document.Info.Author           = item.pdf_document.AuthorsCombined;
                        document.Info.ModificationDate = DateTime.Now;
                        document.Info.Creator          = "Qiqqa.com library export v1";
                        {
                            StringBuilder sb = new StringBuilder();
                            foreach (string tag in TagTools.ConvertTagBundleToTags(item.pdf_document.Tags))
                            {
                                sb.Append(tag);
                                sb.Append(";");
                            }
                            document.Info.Keywords = sb.ToString();
                        }

                        // Then the main comments (if any)
                        {
                            string comments = item.pdf_document.Comments;
                            if (!String.IsNullOrEmpty(comments))
                            {
                                PdfPage page = document.Pages[0];

                                using (XGraphics gfx = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Prepend))
                                {
                                    XRect             rect           = new XRect(5, 5, 100, 100);
                                    PdfTextAnnotation new_annotation = new PdfTextAnnotation();
                                    new_annotation.Contents  = comments;
                                    new_annotation.Icon      = PdfTextAnnotationIcon.Note;
                                    new_annotation.Rectangle = new PdfRectangle(gfx.Transformer.WorldToDefaultPage(rect));
                                    page.Annotations.Add(new_annotation);
                                }
                            }
                        }

                        // Then the highlights
                        foreach (PDFHighlight highlight in item.pdf_document.Highlights.GetAllHighlights())
                        {
                            PdfPage page = document.Pages[highlight.Page - 1];
                            using (XGraphics gfx = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Prepend))
                            {
                                XColor color = XColor.FromArgb(StandardHighlightColours.GetColor(highlight.Color));
                                XPen   pen   = new XPen(color);
                                XBrush brush = new XSolidBrush(color);
                                XRect  rect  = new XRect(highlight.Left * gfx.PageSize.Width, highlight.Top * gfx.PageSize.Height, highlight.Width * gfx.PageSize.Width, highlight.Height * gfx.PageSize.Height);
                                gfx.DrawRectangle(brush, rect);
                            }
                        }

                        // Then the annotations
                        foreach (PDFAnnotation annotation in item.pdf_document.GetAnnotations())
                        {
                            if (annotation.Deleted)
                            {
                                continue;
                            }

                            PdfPage page = document.Pages[annotation.Page - 1];
                            using (XGraphics gfx = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Prepend))
                            {
                                XColor color = XColor.FromArgb(annotation.Color);
                                XPen   pen   = new XPen(color);
                                XBrush brush = new XSolidBrush(color);
                                XRect  rect  = new XRect(annotation.Left * gfx.PageSize.Width, annotation.Top * gfx.PageSize.Height, annotation.Width * gfx.PageSize.Width, annotation.Height * gfx.PageSize.Height);
                                gfx.DrawRectangle(brush, rect);

                                PdfTextAnnotation new_annotation = new PdfTextAnnotation();
                                new_annotation.Contents  = String.Format("Tags:{0}\n{1}", annotation.Tags, annotation.Text);
                                new_annotation.Icon      = PdfTextAnnotationIcon.Note;
                                new_annotation.Color     = color;
                                new_annotation.Opacity   = 0.5;
                                new_annotation.Open      = false;
                                new_annotation.Rectangle = new PdfRectangle(gfx.Transformer.WorldToDefaultPage(rect));
                                page.Annotations.Add(new_annotation);

                                //foreach (var pair in new_annotation.Elements)
                                //{
                                //    Logging.Debug("   {0}={1}", pair.Key, pair.Value);
                                //}
                            }
                        }

                        // Save it
                        Logging.Info("Saving {0}", item.filename);
                        document.Save(item.filename);
                    }
                }
                catch (Exception ex)
                {
                    Logging.Error(ex, "Error updating PDF for " + item.filename);
                }

                StatusManager.Instance.UpdateStatus("ContentExport", String.Format("Exported your PDF content"));
            }
        }
        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);
            }
        }