@@ -15,6 +15,7 @@ public partial class Form1 : Form
15
15
{
16
16
string filePath ;
17
17
int thumbnailZoomfactor = 4 ;
18
+ FlowLayoutPanel thumbnailLayoutPanel ;
18
19
public Form1 ( )
19
20
{
20
21
InitializeComponent ( ) ;
@@ -23,39 +24,34 @@ public Form1()
23
24
24
25
private void Form1_Load ( object sender , EventArgs e )
25
26
{
26
- // Create a parent TableLayoutPanel with two columns and one row.
27
- var parentLayout = new TableLayoutPanel
28
- {
29
- Dock = DockStyle . Fill ,
30
- ColumnCount = 2 ,
31
- RowCount = 1
32
- } ;
33
- parentLayout . ColumnStyles . Clear ( ) ;
34
- //Create rows and columns with specific size to view the thumbnail images and pdfviewer effectively
35
- parentLayout . ColumnStyles . Add ( new ColumnStyle ( SizeType . Percent , 16F ) ) ;
36
- parentLayout . ColumnStyles . Add ( new ColumnStyle ( SizeType . Percent , 84F ) ) ;
37
- parentLayout . RowStyles . Clear ( ) ;
38
- parentLayout . RowStyles . Add ( new RowStyle ( SizeType . Percent , 100F ) ) ;
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 ) ) ;
39
33
40
- //Remove the existing controls from the Form, so that we can re‐insert them into our new parentLayout.
41
- this . Controls . Remove ( tableLayoutPanel1 ) ;
42
- this . Controls . Remove ( pdfViewerControl1 ) ;
43
-
44
- //Prepare tableLayoutPanel1 to Auto‐size vertically.
45
- tableLayoutPanel1 . AutoSize = true ;
46
- //Set Dock style of tableLayoutPane to Top to grow downward as thumbnail images are added.
47
- tableLayoutPanel1 . Dock = DockStyle . Top ;
34
+ tableLayoutPanel1 . RowStyles . Clear ( ) ;
35
+ tableLayoutPanel1 . RowCount = 1 ;
36
+ tableLayoutPanel1 . RowStyles . Add ( new RowStyle ( SizeType . Percent , 100F ) ) ;
48
37
49
- //Create a scrollable Panel, and put tableLayoutPanel1 inside it.
50
- var thumbnailScrollHost = new Panel
38
+ //Create a scrollable FlowlayoutPanel for the thumbnails
39
+ thumbnailLayoutPanel = new FlowLayoutPanel
51
40
{
52
41
Dock = DockStyle . Fill ,
53
- AutoScroll = true
42
+ AutoScroll = true ,
43
+ FlowDirection = FlowDirection . TopDown ,
44
+ WrapContents = false ,
54
45
} ;
55
- thumbnailScrollHost . Controls . Add ( tableLayoutPanel1 ) ;
56
46
57
- // Configure pdfViewerControl to dock Fill.
47
+ //Add the thumbnailLayout to the first column of the tableLayoutPanel
48
+ tableLayoutPanel1 . Controls . Add ( thumbnailLayoutPanel , 0 , 0 ) ;
49
+
50
+ //Remove the existing pdfViewercontrol from the form, so that we can newly insert everytime into the tableLayoutPanel.
51
+ this . Controls . Remove ( pdfViewerControl1 ) ;
52
+ //Add the pdfViewer to the second column of the tableLayoutPanel
58
53
pdfViewerControl1 . Dock = DockStyle . Fill ;
54
+ tableLayoutPanel1 . Controls . Add ( pdfViewerControl1 , 1 , 0 ) ;
59
55
// Load the PDF file.
60
56
#if NETCOREAPP
61
57
filePath = @"../../../Data/PDF_Succinctly.pdf" ;
@@ -64,13 +60,6 @@ private void Form1_Load(object sender, EventArgs e)
64
60
#endif
65
61
pdfViewerControl1 . Load ( filePath ) ;
66
62
pdfViewerControl1 . DocumentLoaded += PdfViewerControl1_DocumentLoaded ;
67
-
68
- // Add both the scroll host and the PDF viewer into parentLayout
69
- parentLayout . Controls . Add ( thumbnailScrollHost , 0 , 0 ) ;
70
- parentLayout . Controls . Add ( pdfViewerControl1 , 1 , 0 ) ;
71
-
72
- // Finally, add parentLayout to the form.
73
- this . Controls . Add ( parentLayout ) ;
74
63
this . WindowState = FormWindowState . Maximized ;
75
64
}
76
65
@@ -79,9 +68,7 @@ private void Form1_Load(object sender, EventArgs e)
79
68
/// </summary>
80
69
private void PdfViewerControl1_DocumentLoaded ( object sender , EventArgs args )
81
70
{
82
- tableLayoutPanel1 . Controls . Clear ( ) ;
83
- tableLayoutPanel1 . RowStyles . Clear ( ) ;
84
- tableLayoutPanel1 . RowCount = 0 ;
71
+ thumbnailLayoutPanel . Controls . Clear ( ) ;
85
72
ExportAsImage ( ) ;
86
73
}
87
74
@@ -90,10 +77,7 @@ private void PdfViewerControl1_DocumentLoaded(object sender, EventArgs args)
90
77
/// </summary>
91
78
private async void ExportAsImage ( )
92
79
{
93
- int count = pdfViewerControl1 . LoadedDocument . Pages . Count ;
94
- tableLayoutPanel1 . ColumnCount = 1 ;
95
- tableLayoutPanel1 . RowCount = count ;
96
- //Calculate height and width for teh thumbnail panel
80
+ //Calculate height and width for the thumbnail panel
97
81
float height = pdfViewerControl1 . LoadedDocument . Pages [ 0 ] . Size . Height / thumbnailZoomfactor ;
98
82
float width = pdfViewerControl1 . LoadedDocument . Pages [ 0 ] . Size . Width / thumbnailZoomfactor ;
99
83
this . tableLayoutPanel1 . ColumnStyles [ 0 ] . SizeType = SizeType . Absolute ;
@@ -116,11 +100,10 @@ private async void ExportAsImage()
116
100
picture . Height = ( int ) height ;
117
101
picture . Width = ( int ) width ;
118
102
picture . Visible = true ;
119
- picture . Margin = new Padding ( 25 , 5 , 10 , 0 ) ;
120
103
picture . Refresh ( ) ;
121
104
picture . MouseUp += Picture_MouseUp ;
122
- //Add the picture control to the tablelayoutpanel
123
- tableLayoutPanel1 . Controls . Add ( picture , 0 , i ) ;
105
+ //Add the picture control to the thumbnailPanel
106
+ thumbnailLayoutPanel . Controls . Add ( picture ) ;
124
107
}
125
108
}
126
109
@@ -133,7 +116,7 @@ private void Picture_MouseUp(object sender, MouseEventArgs e)
133
116
{
134
117
PictureBox pictureBox = ( PictureBox ) sender ;
135
118
//Get the index of the page
136
- int index = tableLayoutPanel1 . GetRow ( pictureBox ) ;
119
+ int index = thumbnailLayoutPanel . Controls . IndexOf ( pictureBox ) ;
137
120
//Navigate to the specified page
138
121
pdfViewerControl1 . GoToPageAtIndex ( index + 1 ) ;
139
122
0 commit comments