Skip to content

Commit cacee21

Browse files
authored
Merge pull request #14 from SyncfusionExamples/WPF-264742
264742 - How to display/generate PDF pages as thumbnails?
2 parents 1051165 + 2c419a1 commit cacee21

17 files changed

Lines changed: 735 additions & 0 deletions
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
5+
</startup>
6+
</configuration>
Binary file not shown.
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{2D7B60B6-DB7C-4E04-AFDD-C71BFF712F35}</ProjectGuid>
8+
<OutputType>WinExe</OutputType>
9+
<RootNamespace>DisplayPDFpagesAsThumbnails</RootNamespace>
10+
<AssemblyName>DisplayPDFpagesAsThumbnails</AssemblyName>
11+
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
12+
<FileAlignment>512</FileAlignment>
13+
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
14+
<Deterministic>true</Deterministic>
15+
<TargetFrameworkProfile />
16+
</PropertyGroup>
17+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
18+
<PlatformTarget>AnyCPU</PlatformTarget>
19+
<DebugSymbols>true</DebugSymbols>
20+
<DebugType>full</DebugType>
21+
<Optimize>false</Optimize>
22+
<OutputPath>bin\Debug\</OutputPath>
23+
<DefineConstants>DEBUG;TRACE</DefineConstants>
24+
<ErrorReport>prompt</ErrorReport>
25+
<WarningLevel>4</WarningLevel>
26+
</PropertyGroup>
27+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
28+
<PlatformTarget>AnyCPU</PlatformTarget>
29+
<DebugType>pdbonly</DebugType>
30+
<Optimize>true</Optimize>
31+
<OutputPath>bin\Release\</OutputPath>
32+
<DefineConstants>TRACE</DefineConstants>
33+
<ErrorReport>prompt</ErrorReport>
34+
<WarningLevel>4</WarningLevel>
35+
</PropertyGroup>
36+
<ItemGroup>
37+
<packageReference Include ="Syncfusion.PdfViewer.Windows" version = "*" />
38+
<Reference Include="System" />
39+
<Reference Include="System.Core" />
40+
<Reference Include="System.Xml.Linq" />
41+
<Reference Include="System.Data.DataSetExtensions" />
42+
<Reference Include="Microsoft.CSharp" />
43+
<Reference Include="System.Data" />
44+
<Reference Include="System.Deployment" />
45+
<Reference Include="System.Drawing" />
46+
<Reference Include="System.Net.Http" />
47+
<Reference Include="System.Windows.Forms" />
48+
<Reference Include="System.Xml" />
49+
</ItemGroup>
50+
<ItemGroup>
51+
<Compile Include="Form1.cs">
52+
<SubType>Form</SubType>
53+
</Compile>
54+
<Compile Include="Form1.Designer.cs">
55+
<DependentUpon>Form1.cs</DependentUpon>
56+
</Compile>
57+
<Compile Include="Program.cs" />
58+
<Compile Include="Properties\AssemblyInfo.cs" />
59+
<EmbeddedResource Include="Form1.resx">
60+
<DependentUpon>Form1.cs</DependentUpon>
61+
</EmbeddedResource>
62+
<EmbeddedResource Include="Properties\Resources.resx">
63+
<Generator>ResXFileCodeGenerator</Generator>
64+
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
65+
<SubType>Designer</SubType>
66+
</EmbeddedResource>
67+
<Compile Include="Properties\Resources.Designer.cs">
68+
<AutoGen>True</AutoGen>
69+
<DependentUpon>Resources.resx</DependentUpon>
70+
</Compile>
71+
<None Include="packages.config" />
72+
<None Include="Properties\Settings.settings">
73+
<Generator>SettingsSingleFileGenerator</Generator>
74+
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
75+
</None>
76+
<Compile Include="Properties\Settings.Designer.cs">
77+
<AutoGen>True</AutoGen>
78+
<DependentUpon>Settings.settings</DependentUpon>
79+
<DesignTimeSharedInput>True</DesignTimeSharedInput>
80+
</Compile>
81+
</ItemGroup>
82+
<ItemGroup>
83+
<None Include="App.config" />
84+
</ItemGroup>
85+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
86+
</Project>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.12.35707.178 d17.12
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DisplayPDFpagesAsThumbnails", "DisplayPDFpagesAsThumbnails.csproj", "{2D7B60B6-DB7C-4E04-AFDD-C71BFF712F35}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{2D7B60B6-DB7C-4E04-AFDD-C71BFF712F35}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{2D7B60B6-DB7C-4E04-AFDD-C71BFF712F35}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{2D7B60B6-DB7C-4E04-AFDD-C71BFF712F35}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{2D7B60B6-DB7C-4E04-AFDD-C71BFF712F35}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>WinExe</OutputType>
5+
<TargetFramework>net8.0-windows</TargetFramework>
6+
<Nullable>enable</Nullable>
7+
<UseWindowsForms>true</UseWindowsForms>
8+
<ImplicitUsings>enable</ImplicitUsings>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.PdfViewer.Windows" Version="*" />
13+
</ItemGroup>
14+
15+
</Project>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup />
4+
<ItemGroup>
5+
<Compile Update="Form1.cs">
6+
<SubType>Form</SubType>
7+
</Compile>
8+
</ItemGroup>
9+
</Project>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.12.35707.178 d17.12
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DisplayPDFpagesAsThumbnails_NET", "DisplayPDFpagesAsThumbnails_NET.csproj", "{7714F1FB-8A22-4268-96D8-4548D7C17409}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{7714F1FB-8A22-4268-96D8-4548D7C17409}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{7714F1FB-8A22-4268-96D8-4548D7C17409}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{7714F1FB-8A22-4268-96D8-4548D7C17409}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{7714F1FB-8A22-4268-96D8-4548D7C17409}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal

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

Lines changed: 40 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
using Syncfusion.Windows.Forms.PdfViewer;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.ComponentModel;
5+
using System.Data;
6+
using System.Drawing;
7+
using System.Linq;
8+
using System.Text;
9+
using System.Threading.Tasks;
10+
using System.Windows.Forms;
11+
12+
namespace DisplayPDFpagesAsThumbnails
13+
{
14+
public partial class Form1 : Form
15+
{
16+
string filePath;
17+
int thumbnailZoomfactor = 4;
18+
TableLayoutPanel tableLayoutPanel;
19+
FlowLayoutPanel thumbnailLayoutPanel;
20+
PdfViewerControl pdfViewerControl;
21+
public Form1()
22+
{
23+
InitializeComponent();
24+
this.Load += Form1_Load;
25+
}
26+
27+
private void Form1_Load(object sender, EventArgs e)
28+
{
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));
36+
37+
tableLayoutPanel.RowStyles.Clear();
38+
tableLayoutPanel.RowCount = 1;
39+
tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Percent, 100F));
40+
41+
//Create a Scrolllable layout panel for the thumbnail images
42+
thumbnailLayoutPanel = new FlowLayoutPanel
43+
{
44+
Dock = DockStyle.Fill,
45+
AutoScroll = true,
46+
FlowDirection = FlowDirection.TopDown,
47+
WrapContents = false,
48+
};
49+
50+
//Add the thumbnailLayout to the first column of the tableLayoutPanel
51+
tableLayoutPanel.Controls.Add(thumbnailLayoutPanel,0,0);
52+
53+
pdfViewerControl = new PdfViewerControl();
54+
pdfViewerControl.Dock = DockStyle.Fill;
55+
//Add the pdfViewer to the second column of the tableLayoutPanel
56+
tableLayoutPanel.Controls.Add(pdfViewerControl,1,0);
57+
#if NETCOREAPP
58+
filePath = @"../../../Data/PDF_Succinctly.pdf";
59+
#else
60+
filePath = @"../../Data/PDF_Succinctly.pdf";
61+
#endif
62+
// Load the PDF file.
63+
pdfViewerControl.Load(filePath);
64+
pdfViewerControl.DocumentLoaded += PdfViewerControl_DocumentLoaded;
65+
this.Controls.Add(tableLayoutPanel);
66+
this.WindowState = FormWindowState.Maximized;
67+
}
68+
69+
/// <summary>
70+
/// Triggers once the document is loaded it invoke the conversion of pdf page to image
71+
/// </summary>
72+
private void PdfViewerControl_DocumentLoaded(object sender, EventArgs args)
73+
{
74+
thumbnailLayoutPanel.Controls.Clear();
75+
ExportAsImage();
76+
}
77+
78+
/// <summary>
79+
/// Converts the PDF pages into images
80+
/// </summary>
81+
private async void ExportAsImage()
82+
{
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)
88+
{
89+
this.tableLayoutPanel.ColumnStyles[0].Width = width + 30;
90+
}
91+
else
92+
{
93+
this.tableLayoutPanel.ColumnStyles[0].Width = width + 5;
94+
}
95+
for (int i = 0; i < pdfViewerControl.LoadedDocument.Pages.Count; i++)
96+
{
97+
PictureBox picture = new PictureBox();
98+
//Convert the PDF page as image
99+
Bitmap image = new Bitmap(await Task.Run(() => pdfViewerControl.LoadedDocument.ExportAsImage(i)), new Size((int)width, (int)height));
100+
//Set the exported image to the picture control
101+
picture.Image = image;
102+
picture.Update();
103+
picture.Height = (int)height;
104+
picture.Width = (int)width;
105+
picture.Visible = true;
106+
picture.Refresh();
107+
picture.MouseUp += Picture_MouseUp;
108+
//Add the picture control to the thumbnailPanel
109+
thumbnailLayoutPanel.Controls.Add(picture);
110+
}
111+
}
112+
113+
/// <summary>
114+
/// Triggered whenever the thumbnail images is clicked
115+
/// </summary>
116+
private void Picture_MouseUp(object sender, MouseEventArgs e)
117+
{
118+
PictureBox pictureBox = (PictureBox)sender;
119+
//Get the index of the page
120+
int index = thumbnailLayoutPanel.Controls.IndexOf(pictureBox);
121+
//Navigate to the specified page
122+
pdfViewerControl.GoToPageAtIndex(index + 1);
123+
}
124+
}
125+
}

0 commit comments

Comments
 (0)