Skip to content

264742 - How to display/generate PDF pages as thumbnails? #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
</startup>
</configuration>
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{2D7B60B6-DB7C-4E04-AFDD-C71BFF712F35}</ProjectGuid>
<OutputType>WinExe</OutputType>
<RootNamespace>DisplayPDFpagesAsThumbnails</RootNamespace>
<AssemblyName>DisplayPDFpagesAsThumbnails</AssemblyName>
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<packageReference Include ="Syncfusion.PdfViewer.Windows" version = "*" />
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Deployment" />
<Reference Include="System.Drawing" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Form1.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="Form1.Designer.cs">
<DependentUpon>Form1.cs</DependentUpon>
</Compile>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<EmbeddedResource Include="Form1.resx">
<DependentUpon>Form1.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
<SubType>Designer</SubType>
</EmbeddedResource>
<Compile Include="Properties\Resources.Designer.cs">
<AutoGen>True</AutoGen>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<None Include="packages.config" />
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
</None>
<Compile Include="Properties\Settings.Designer.cs">
<AutoGen>True</AutoGen>
<DependentUpon>Settings.settings</DependentUpon>
<DesignTimeSharedInput>True</DesignTimeSharedInput>
</Compile>
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.12.35707.178 d17.12
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DisplayPDFpagesAsThumbnails", "DisplayPDFpagesAsThumbnails.csproj", "{2D7B60B6-DB7C-4E04-AFDD-C71BFF712F35}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{2D7B60B6-DB7C-4E04-AFDD-C71BFF712F35}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2D7B60B6-DB7C-4E04-AFDD-C71BFF712F35}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2D7B60B6-DB7C-4E04-AFDD-C71BFF712F35}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2D7B60B6-DB7C-4E04-AFDD-C71BFF712F35}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net8.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWindowsForms>true</UseWindowsForms>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Syncfusion.PdfViewer.Windows" Version="*" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup />
<ItemGroup>
<Compile Update="Form1.cs">
<SubType>Form</SubType>
</Compile>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.12.35707.178 d17.12
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DisplayPDFpagesAsThumbnails_NET", "DisplayPDFpagesAsThumbnails_NET.csproj", "{7714F1FB-8A22-4268-96D8-4548D7C17409}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{7714F1FB-8A22-4268-96D8-4548D7C17409}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7714F1FB-8A22-4268-96D8-4548D7C17409}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7714F1FB-8A22-4268-96D8-4548D7C17409}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7714F1FB-8A22-4268-96D8-4548D7C17409}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
using Syncfusion.Windows.Forms.PdfViewer;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DisplayPDFpagesAsThumbnails
{
public partial class Form1 : Form
{
string filePath;
int thumbnailZoomfactor = 4;
TableLayoutPanel tableLayoutPanel;
FlowLayoutPanel thumbnailLayoutPanel;
PdfViewerControl pdfViewerControl;
public Form1()
{
InitializeComponent();
this.Load += Form1_Load;
}

private void Form1_Load(object sender, EventArgs e)
{
//Initialize a new TableLayoutPanel and configure the tableLayoutPanel to hold two column and one row
tableLayoutPanel = new TableLayoutPanel();
tableLayoutPanel.Dock = DockStyle.Fill;
tableLayoutPanel.ColumnStyles.Clear();
tableLayoutPanel.ColumnCount = 2;
tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 16F));
tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 84F));

tableLayoutPanel.RowStyles.Clear();
tableLayoutPanel.RowCount = 1;
tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Percent, 100F));

//Create a Scrolllable layout panel for the thumbnail images
thumbnailLayoutPanel = new FlowLayoutPanel
{
Dock = DockStyle.Fill,
AutoScroll = true,
FlowDirection = FlowDirection.TopDown,
WrapContents = false,
};

//Add the thumbnailLayout to the first column of the tableLayoutPanel
tableLayoutPanel.Controls.Add(thumbnailLayoutPanel,0,0);

pdfViewerControl = new PdfViewerControl();
pdfViewerControl.Dock = DockStyle.Fill;
//Add the pdfViewer to the second column of the tableLayoutPanel
tableLayoutPanel.Controls.Add(pdfViewerControl,1,0);
#if NETCOREAPP
filePath = @"../../../Data/PDF_Succinctly.pdf";
#else
filePath = @"../../Data/PDF_Succinctly.pdf";
#endif
// Load the PDF file.
pdfViewerControl.Load(filePath);
pdfViewerControl.DocumentLoaded += PdfViewerControl_DocumentLoaded;
this.Controls.Add(tableLayoutPanel);
this.WindowState = FormWindowState.Maximized;
}

/// <summary>
/// Triggers once the document is loaded it invoke the conversion of pdf page to image
/// </summary>
private void PdfViewerControl_DocumentLoaded(object sender, EventArgs args)
{
thumbnailLayoutPanel.Controls.Clear();
ExportAsImage();
}

/// <summary>
/// Converts the PDF pages into images
/// </summary>
private async void ExportAsImage()
{
//Calculate height and width of the panel
float height = pdfViewerControl.LoadedDocument.Pages[0].Size.Height / thumbnailZoomfactor;
float width = pdfViewerControl.LoadedDocument.Pages[0].Size.Width / thumbnailZoomfactor;
this.tableLayoutPanel.ColumnStyles[0].SizeType = SizeType.Absolute;
if (pdfViewerControl.LoadedDocument.Pages.Count > 4)
{
this.tableLayoutPanel.ColumnStyles[0].Width = width + 30;
}
else
{
this.tableLayoutPanel.ColumnStyles[0].Width = width + 5;
}
for (int i = 0; i < pdfViewerControl.LoadedDocument.Pages.Count; i++)
{
PictureBox picture = new PictureBox();
//Convert the PDF page as image
Bitmap image = new Bitmap(await Task.Run(() => pdfViewerControl.LoadedDocument.ExportAsImage(i)), new Size((int)width, (int)height));
//Set the exported image to the picture control
picture.Image = image;
picture.Update();
picture.Height = (int)height;
picture.Width = (int)width;
picture.Visible = true;
picture.Refresh();
picture.MouseUp += Picture_MouseUp;
//Add the picture control to the thumbnailPanel
thumbnailLayoutPanel.Controls.Add(picture);
}
}

/// <summary>
/// Triggered whenever the thumbnail images is clicked
/// </summary>
private void Picture_MouseUp(object sender, MouseEventArgs e)
{
PictureBox pictureBox = (PictureBox)sender;
//Get the index of the page
int index = thumbnailLayoutPanel.Controls.IndexOf(pictureBox);
//Navigate to the specified page
pdfViewerControl.GoToPageAtIndex(index + 1);
}
}
}
Loading