Change PDF Selection Options and Use CustomPaint | C#ByteScout PDF Viewer SDK

Change PDF Selection Options and Use CustomPaint | C#

Program.cs:

C#
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace Example
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

Form1.cs:

C#
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using Bytescout.PDFViewer;

namespace Example
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            pdfViewerControl1.InputFile = "sample.pdf";

            base.OnLoad(e);
        }

        private void tsbOpen_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog openFileDialog = new OpenFileDialog())
            {
                openFileDialog.Title = @"Open PDF Document";
                openFileDialog.Filter = @"PDF Files (*.pdf)|*.pdf|All Files|*.*";

                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    this.Text = openFileDialog.FileName;

                    Cursor = Cursors.WaitCursor;

                    try
                    {
                        pdfViewerControl1.InputFile = openFileDialog.FileName;
                    }
                    catch (Exception exception)
                    {
                        MessageBox.Show(exception.Message);
                    }
                    finally
                    {
                        Cursor = Cursors.Default;
                    }
                }
            }
        }

        private void cbMultiSelectMode_CheckedChanged(object sender, EventArgs e)
        {
            pdfViewerControl1.MultiSelectMode = cbMultiSelectMode.Checked;
        }

        private void cbAllowResizeSelection_CheckedChanged(object sender, EventArgs e)
        {
            pdfViewerControl1.AllowResizeSelectionRectangles = cbAllowResizeSelection.Checked;
        }

        private void cbLockSelection_CheckedChanged(object sender, EventArgs e)
        {
            pdfViewerControl1.LockSelection = cbLockSelection.Checked;
        }

        List<String> _labels = new List<string>();

        private void pdfViewerControl1_SelectionChanged(object sender, SelectionChange selectionChange, int selectionIndex)
        {
            if (pdfViewerControl1.Selection.Length > _labels.Count)
            {
                for (int i = _labels.Count; i < pdfViewerControl1.Selection.Length; i++)
                {
                    _labels.Add(new Random().Next().ToString());
                }
            }
        }

        private void pdfViewerControl1_CustomPaint(object sender, PaintEventArgs e)
        {
            // Paint labels
            for (int i = 0; i < pdfViewerControl1.Selection.Length; i++)
            {
                RectangleF pdfRect = pdfViewerControl1.SelectionInPoints[i];
                Rectangle pixelRect = pdfViewerControl1.TranslateRectangleFromPointsToPixels(pdfRect);

                Size textSize = TextRenderer.MeasureText(_labels[i], Font);
                Rectangle textRectangle = new Rectangle(pixelRect.Left, pixelRect.Top - textSize.Height - 6, textSize.Width + 2, textSize.Height + 2);

                e.Graphics.FillRectangle(Brushes.DarkBlue, textRectangle);
                TextRenderer.DrawText(e.Graphics, _labels[i], Font, textRectangle, Color.White, TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter);
            }
        }
    }
}