Skip to content

Commit 37c4a73

Browse files
committed
246742 - Optimized the sample
1 parent 04754d2 commit 37c4a73

File tree

1 file changed

+27
-44
lines changed
  • How-to/How-to-display-PDF-pages-as-thumbnails/DisplayPDFpagesAsThumbnails

1 file changed

+27
-44
lines changed

How-to/How-to-display-PDF-pages-as-thumbnails/DisplayPDFpagesAsThumbnails/Form1.cs

Lines changed: 27 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public partial class Form1 : Form
1515
{
1616
string filePath;
1717
int thumbnailZoomfactor = 4;
18+
FlowLayoutPanel thumbnailLayoutPanel;
1819
public Form1()
1920
{
2021
InitializeComponent();
@@ -23,39 +24,34 @@ public Form1()
2324

2425
private void Form1_Load(object sender, EventArgs e)
2526
{
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));
3933

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));
4837

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
5140
{
5241
Dock = DockStyle.Fill,
53-
AutoScroll = true
42+
AutoScroll = true,
43+
FlowDirection = FlowDirection.TopDown,
44+
WrapContents = false,
5445
};
55-
thumbnailScrollHost.Controls.Add(tableLayoutPanel1);
5646

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
5853
pdfViewerControl1.Dock = DockStyle.Fill;
54+
tableLayoutPanel1.Controls.Add(pdfViewerControl1, 1, 0);
5955
// Load the PDF file.
6056
#if NETCOREAPP
6157
filePath = @"../../../Data/PDF_Succinctly.pdf";
@@ -64,13 +60,6 @@ private void Form1_Load(object sender, EventArgs e)
6460
#endif
6561
pdfViewerControl1.Load(filePath);
6662
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);
7463
this.WindowState = FormWindowState.Maximized;
7564
}
7665

@@ -79,9 +68,7 @@ private void Form1_Load(object sender, EventArgs e)
7968
/// </summary>
8069
private void PdfViewerControl1_DocumentLoaded(object sender, EventArgs args)
8170
{
82-
tableLayoutPanel1.Controls.Clear();
83-
tableLayoutPanel1.RowStyles.Clear();
84-
tableLayoutPanel1.RowCount = 0;
71+
thumbnailLayoutPanel.Controls.Clear();
8572
ExportAsImage();
8673
}
8774

@@ -90,10 +77,7 @@ private void PdfViewerControl1_DocumentLoaded(object sender, EventArgs args)
9077
/// </summary>
9178
private async void ExportAsImage()
9279
{
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
9781
float height = pdfViewerControl1.LoadedDocument.Pages[0].Size.Height / thumbnailZoomfactor;
9882
float width = pdfViewerControl1.LoadedDocument.Pages[0].Size.Width / thumbnailZoomfactor;
9983
this.tableLayoutPanel1.ColumnStyles[0].SizeType = SizeType.Absolute;
@@ -116,11 +100,10 @@ private async void ExportAsImage()
116100
picture.Height = (int)height;
117101
picture.Width = (int)width;
118102
picture.Visible = true;
119-
picture.Margin = new Padding(25, 5, 10, 0);
120103
picture.Refresh();
121104
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);
124107
}
125108
}
126109

@@ -133,7 +116,7 @@ private void Picture_MouseUp(object sender, MouseEventArgs e)
133116
{
134117
PictureBox pictureBox = (PictureBox)sender;
135118
//Get the index of the page
136-
int index = tableLayoutPanel1.GetRow(pictureBox);
119+
int index = thumbnailLayoutPanel.Controls.IndexOf(pictureBox);
137120
//Navigate to the specified page
138121
pdfViewerControl1.GoToPageAtIndex(index + 1);
139122

0 commit comments

Comments
 (0)