@@ -15,7 +15,9 @@ public partial class Form1 : Form
15
15
{
16
16
string filePath ;
17
17
int thumbnailZoomfactor = 4 ;
18
+ TableLayoutPanel tableLayoutPanel ;
18
19
FlowLayoutPanel thumbnailLayoutPanel ;
20
+ PdfViewerControl pdfViewerControl ;
19
21
public Form1 ( )
20
22
{
21
23
InitializeComponent ( ) ;
@@ -24,18 +26,19 @@ public Form1()
24
26
25
27
private void Form1_Load ( object sender , EventArgs e )
26
28
{
27
- //Configure the existing tableLayoutPanel to two columns and one row.
28
- tableLayoutPanel1 . Dock = DockStyle . Fill ;
29
- tableLayoutPanel1 . ColumnStyles . Clear ( ) ;
30
- tableLayoutPanel1 . ColumnCount = 2 ;
31
- tableLayoutPanel1 . ColumnStyles . Add ( new ColumnStyle ( SizeType . Percent , 16F ) ) ;
32
- tableLayoutPanel1 . ColumnStyles . Add ( new ColumnStyle ( SizeType . Percent , 84F ) ) ;
29
+ //Initialize a new TableLayoutPanel and configure the tableLayoutPanel to hold two column and one row
30
+ tableLayoutPanel = new TableLayoutPanel ( ) ;
31
+ tableLayoutPanel . Dock = DockStyle . Fill ;
32
+ tableLayoutPanel . ColumnStyles . Clear ( ) ;
33
+ tableLayoutPanel . ColumnCount = 2 ;
34
+ tableLayoutPanel . ColumnStyles . Add ( new ColumnStyle ( SizeType . Percent , 16F ) ) ;
35
+ tableLayoutPanel . ColumnStyles . Add ( new ColumnStyle ( SizeType . Percent , 84F ) ) ;
33
36
34
- tableLayoutPanel1 . RowStyles . Clear ( ) ;
35
- tableLayoutPanel1 . RowCount = 1 ;
36
- tableLayoutPanel1 . RowStyles . Add ( new RowStyle ( SizeType . Percent , 100F ) ) ;
37
+ tableLayoutPanel . RowStyles . Clear ( ) ;
38
+ tableLayoutPanel . RowCount = 1 ;
39
+ tableLayoutPanel . RowStyles . Add ( new RowStyle ( SizeType . Percent , 100F ) ) ;
37
40
38
- //Create a scrollable FlowlayoutPanel for the thumbnails
41
+ //Create a Scrolllable layout panel for the thumbnail images
39
42
thumbnailLayoutPanel = new FlowLayoutPanel
40
43
{
41
44
Dock = DockStyle . Fill ,
@@ -45,28 +48,28 @@ private void Form1_Load(object sender, EventArgs e)
45
48
} ;
46
49
47
50
//Add the thumbnailLayout to the first column of the tableLayoutPanel
48
- tableLayoutPanel1 . Controls . Add ( thumbnailLayoutPanel , 0 , 0 ) ;
51
+ tableLayoutPanel . Controls . Add ( thumbnailLayoutPanel , 0 , 0 ) ;
49
52
50
- //Remove the existing pdfViewercontrol from the form, so that we can newly insert everytime into the tableLayoutPanel.
51
- this . Controls . Remove ( pdfViewerControl1 ) ;
53
+ pdfViewerControl = new PdfViewerControl ( ) ;
54
+ pdfViewerControl . Dock = DockStyle . Fill ;
52
55
//Add the pdfViewer to the second column of the tableLayoutPanel
53
- pdfViewerControl1 . Dock = DockStyle . Fill ;
54
- tableLayoutPanel1 . Controls . Add ( pdfViewerControl1 , 1 , 0 ) ;
55
- // Load the PDF file.
56
+ tableLayoutPanel . Controls . Add ( pdfViewerControl , 1 , 0 ) ;
56
57
#if NETCOREAPP
57
58
filePath = @"../../../Data/PDF_Succinctly.pdf" ;
58
59
#else
59
60
filePath = @"../../Data/PDF_Succinctly.pdf" ;
60
61
#endif
61
- pdfViewerControl1 . Load ( filePath ) ;
62
- pdfViewerControl1 . DocumentLoaded += PdfViewerControl1_DocumentLoaded ;
62
+ // Load the PDF file.
63
+ pdfViewerControl . Load ( filePath ) ;
64
+ pdfViewerControl . DocumentLoaded += PdfViewerControl_DocumentLoaded ;
65
+ this . Controls . Add ( tableLayoutPanel ) ;
63
66
this . WindowState = FormWindowState . Maximized ;
64
67
}
65
68
66
69
/// <summary>
67
- /// Event triggers once the document has been loaded
70
+ /// Triggers once the document is loaded it invoke the conversion of pdf page to image
68
71
/// </summary>
69
- private void PdfViewerControl1_DocumentLoaded ( object sender , EventArgs args )
72
+ private void PdfViewerControl_DocumentLoaded ( object sender , EventArgs args )
70
73
{
71
74
thumbnailLayoutPanel . Controls . Clear ( ) ;
72
75
ExportAsImage ( ) ;
@@ -77,23 +80,23 @@ private void PdfViewerControl1_DocumentLoaded(object sender, EventArgs args)
77
80
/// </summary>
78
81
private async void ExportAsImage ( )
79
82
{
80
- //Calculate height and width for the thumbnail panel
81
- float height = pdfViewerControl1 . LoadedDocument . Pages [ 0 ] . Size . Height / thumbnailZoomfactor ;
82
- float width = pdfViewerControl1 . LoadedDocument . Pages [ 0 ] . Size . Width / thumbnailZoomfactor ;
83
- this . tableLayoutPanel1 . ColumnStyles [ 0 ] . SizeType = SizeType . Absolute ;
84
- if ( pdfViewerControl1 . LoadedDocument . Pages . Count > 4 )
83
+ //Calculate height and width of the panel
84
+ float height = pdfViewerControl . LoadedDocument . Pages [ 0 ] . Size . Height / thumbnailZoomfactor ;
85
+ float width = pdfViewerControl . LoadedDocument . Pages [ 0 ] . Size . Width / thumbnailZoomfactor ;
86
+ this . tableLayoutPanel . ColumnStyles [ 0 ] . SizeType = SizeType . Absolute ;
87
+ if ( pdfViewerControl . LoadedDocument . Pages . Count > 4 )
85
88
{
86
- this . tableLayoutPanel1 . ColumnStyles [ 0 ] . Width = width + 30 ;
89
+ this . tableLayoutPanel . ColumnStyles [ 0 ] . Width = width + 30 ;
87
90
}
88
91
else
89
92
{
90
- this . tableLayoutPanel1 . ColumnStyles [ 0 ] . Width = width + 5 ;
93
+ this . tableLayoutPanel . ColumnStyles [ 0 ] . Width = width + 5 ;
91
94
}
92
- for ( int i = 0 ; i < pdfViewerControl1 . LoadedDocument . Pages . Count ; i ++ )
95
+ for ( int i = 0 ; i < pdfViewerControl . LoadedDocument . Pages . Count ; i ++ )
93
96
{
94
97
PictureBox picture = new PictureBox ( ) ;
95
98
//Convert the PDF page as image
96
- Bitmap image = new Bitmap ( await Task . Run ( ( ) => pdfViewerControl1 . LoadedDocument . ExportAsImage ( i ) ) , new Size ( ( int ) width , ( int ) height ) ) ;
99
+ Bitmap image = new Bitmap ( await Task . Run ( ( ) => pdfViewerControl . LoadedDocument . ExportAsImage ( i ) ) , new Size ( ( int ) width , ( int ) height ) ) ;
97
100
//Set the exported image to the picture control
98
101
picture . Image = image ;
99
102
picture . Update ( ) ;
@@ -108,18 +111,15 @@ private async void ExportAsImage()
108
111
}
109
112
110
113
/// <summary>
111
- /// Event triggered once clicked the thumbnail images
114
+ /// Triggered whenever the thumbnail images is clicked
112
115
/// </summary>
113
- /// <param name="sender"></param>
114
- /// <param name="e"></param>
115
116
private void Picture_MouseUp ( object sender , MouseEventArgs e )
116
117
{
117
118
PictureBox pictureBox = ( PictureBox ) sender ;
118
119
//Get the index of the page
119
120
int index = thumbnailLayoutPanel . Controls . IndexOf ( pictureBox ) ;
120
121
//Navigate to the specified page
121
- pdfViewerControl1 . GoToPageAtIndex ( index + 1 ) ;
122
-
122
+ pdfViewerControl . GoToPageAtIndex ( index + 1 ) ;
123
123
}
124
124
}
125
125
}
0 commit comments