Skip to content

Commit 682ab26

Browse files
Added sample for Convert PDF pages to single Image in NET core Application
1 parent 800a459 commit 682ab26

File tree

4 files changed

+122
-0
lines changed

4 files changed

+122
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Convert_PDF_to_Image</RootNamespace>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Syncfusion.PdfToImageConverter.Net" Version="*" />
11+
</ItemGroup>
12+
13+
</Project>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.9.34622.214
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Convert-PDF-Pages-to-Single-Image", "Convert-PDF-Pages-to-Single-Image.csproj", "{DD88C92F-A41B-4A24-8106-54E6B93E70B0}"
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+
{DD88C92F-A41B-4A24-8106-54E6B93E70B0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{DD88C92F-A41B-4A24-8106-54E6B93E70B0}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{DD88C92F-A41B-4A24-8106-54E6B93E70B0}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{DD88C92F-A41B-4A24-8106-54E6B93E70B0}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {27AA5CE1-6AA8-4360-B3FB-540F5965D098}
24+
EndGlobalSection
25+
EndGlobal
205 KB
Binary file not shown.
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
using SkiaSharp;
2+
using Syncfusion.PdfToImageConverter;
3+
using System;
4+
using System.IO;
5+
6+
namespace Convert_PDF_to_Image
7+
{
8+
internal class Program
9+
{
10+
static void Main(string[] args)
11+
{
12+
//Initialize PDF to Image converter.
13+
PdfToImageConverter imageConverter = new PdfToImageConverter();
14+
//Load the PDF document as a stream
15+
FileStream inputStream = new FileStream("../../../Input.pdf", FileMode.Open, FileAccess.ReadWrite);
16+
imageConverter.Load(inputStream);
17+
//Convert PDF to Image.
18+
Stream[] imageStreams = imageConverter.Convert(0, imageConverter.PageCount - 1, false, false);
19+
CombineImages(imageStreams, "Output.png");
20+
21+
//Dispose the image streams.
22+
foreach (Stream imageStream in imageStreams)
23+
imageStream.Dispose();
24+
}
25+
/// <summary>
26+
/// Combines multiple images from streams into a single image.
27+
/// </summary>
28+
/// <param name="imageStreams">Streams containing the images to be combined.</param>
29+
/// <param name="outputPath">Output path where the combined image will be saved.</param>
30+
public static void CombineImages(Stream[] imageStreams, string outputPath)
31+
{
32+
if (imageStreams == null || imageStreams.Length == 0)
33+
throw new ArgumentException("No images to combine.");
34+
35+
// Load all images and get their dimensions
36+
SKBitmap[] bitmaps = new SKBitmap[imageStreams.Length];
37+
int maxWidth = 0;
38+
int totalHeight = 0;
39+
int margin = 20;
40+
41+
for (int i = 0; i < imageStreams.Length; i++)
42+
{
43+
imageStreams[i].Position = 0;
44+
bitmaps[i] = SKBitmap.Decode(imageStreams[i]);
45+
maxWidth = Math.Max(maxWidth, bitmaps[i].Width);
46+
totalHeight += bitmaps[i].Height + margin;
47+
}
48+
49+
// Add margins to the total width and height
50+
int combinedWidth = maxWidth + 2 * margin;
51+
// Add margin at the bottom
52+
totalHeight += margin;
53+
54+
// Create a new bitmap with the combined dimensions
55+
using (SKBitmap combinedBitmap = new SKBitmap(combinedWidth, totalHeight))
56+
{
57+
using (SKCanvas canvas = new SKCanvas(combinedBitmap))
58+
{
59+
// Set background color to the specified color
60+
canvas.Clear(new SKColor(240, 240, 240));
61+
62+
// Draw each bitmap onto the canvas
63+
int yOffset = margin;
64+
for (int i = 0; i < bitmaps.Length; i++)
65+
{
66+
int xOffset = (combinedWidth - bitmaps[i].Width) / 2; // Center the image horizontally
67+
canvas.DrawBitmap(bitmaps[i], new SKPoint(xOffset, yOffset));
68+
yOffset += bitmaps[i].Height + margin; // Add margin between rows
69+
}
70+
71+
// Save the combined bitmap to the output stream
72+
using (SKImage image = SKImage.FromBitmap(combinedBitmap))
73+
{
74+
using (SKData data = image.Encode(SKEncodedImageFormat.Png, 100))
75+
{
76+
using (FileStream stream = File.OpenWrite(outputPath))
77+
data.SaveTo(stream);
78+
}
79+
}
80+
}
81+
}
82+
}
83+
}
84+
}

0 commit comments

Comments
 (0)