Skip to content

Commit 2c419a1

Browse files
committed
Optimized the sample
1 parent 37c4a73 commit 2c419a1

File tree

2 files changed

+35
-106
lines changed

2 files changed

+35
-106
lines changed

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

Lines changed: 1 addition & 72 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ public partial class Form1 : Form
1515
{
1616
string filePath;
1717
int thumbnailZoomfactor = 4;
18+
TableLayoutPanel tableLayoutPanel;
1819
FlowLayoutPanel thumbnailLayoutPanel;
20+
PdfViewerControl pdfViewerControl;
1921
public Form1()
2022
{
2123
InitializeComponent();
@@ -24,18 +26,19 @@ public Form1()
2426

2527
private void Form1_Load(object sender, EventArgs e)
2628
{
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));
3336

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

38-
//Create a scrollable FlowlayoutPanel for the thumbnails
41+
//Create a Scrolllable layout panel for the thumbnail images
3942
thumbnailLayoutPanel = new FlowLayoutPanel
4043
{
4144
Dock = DockStyle.Fill,
@@ -45,28 +48,28 @@ private void Form1_Load(object sender, EventArgs e)
4548
};
4649

4750
//Add the thumbnailLayout to the first column of the tableLayoutPanel
48-
tableLayoutPanel1.Controls.Add(thumbnailLayoutPanel, 0, 0);
51+
tableLayoutPanel.Controls.Add(thumbnailLayoutPanel,0,0);
4952

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;
5255
//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);
5657
#if NETCOREAPP
5758
filePath = @"../../../Data/PDF_Succinctly.pdf";
5859
#else
5960
filePath = @"../../Data/PDF_Succinctly.pdf";
6061
#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);
6366
this.WindowState = FormWindowState.Maximized;
6467
}
6568

6669
/// <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
6871
/// </summary>
69-
private void PdfViewerControl1_DocumentLoaded(object sender, EventArgs args)
72+
private void PdfViewerControl_DocumentLoaded(object sender, EventArgs args)
7073
{
7174
thumbnailLayoutPanel.Controls.Clear();
7275
ExportAsImage();
@@ -77,23 +80,23 @@ private void PdfViewerControl1_DocumentLoaded(object sender, EventArgs args)
7780
/// </summary>
7881
private async void ExportAsImage()
7982
{
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)
8588
{
86-
this.tableLayoutPanel1.ColumnStyles[0].Width = width + 30;
89+
this.tableLayoutPanel.ColumnStyles[0].Width = width + 30;
8790
}
8891
else
8992
{
90-
this.tableLayoutPanel1.ColumnStyles[0].Width = width + 5;
93+
this.tableLayoutPanel.ColumnStyles[0].Width = width + 5;
9194
}
92-
for (int i = 0; i < pdfViewerControl1.LoadedDocument.Pages.Count; i++)
95+
for (int i = 0; i < pdfViewerControl.LoadedDocument.Pages.Count; i++)
9396
{
9497
PictureBox picture = new PictureBox();
9598
//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));
97100
//Set the exported image to the picture control
98101
picture.Image = image;
99102
picture.Update();
@@ -108,18 +111,15 @@ private async void ExportAsImage()
108111
}
109112

110113
/// <summary>
111-
/// Event triggered once clicked the thumbnail images
114+
/// Triggered whenever the thumbnail images is clicked
112115
/// </summary>
113-
/// <param name="sender"></param>
114-
/// <param name="e"></param>
115116
private void Picture_MouseUp(object sender, MouseEventArgs e)
116117
{
117118
PictureBox pictureBox = (PictureBox)sender;
118119
//Get the index of the page
119120
int index = thumbnailLayoutPanel.Controls.IndexOf(pictureBox);
120121
//Navigate to the specified page
121-
pdfViewerControl1.GoToPageAtIndex(index + 1);
122-
122+
pdfViewerControl.GoToPageAtIndex(index + 1);
123123
}
124124
}
125125
}

0 commit comments

Comments
 (0)