Skip to content
Merged
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,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Convert_PDF_to_Image</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Syncfusion.PdfToImageConverter.Net" Version="*" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.9.34622.214
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Convert-PDF-Pages-to-Single-Image", "Convert-PDF-Pages-to-Single-Image.csproj", "{DD88C92F-A41B-4A24-8106-54E6B93E70B0}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{DD88C92F-A41B-4A24-8106-54E6B93E70B0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DD88C92F-A41B-4A24-8106-54E6B93E70B0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DD88C92F-A41B-4A24-8106-54E6B93E70B0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DD88C92F-A41B-4A24-8106-54E6B93E70B0}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {27AA5CE1-6AA8-4360-B3FB-540F5965D098}
EndGlobalSection
EndGlobal
Binary file not shown.
84 changes: 84 additions & 0 deletions PDF-to-image/Convert-PDF-Pages-to-Single-Image-.NET/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using SkiaSharp;
using Syncfusion.PdfToImageConverter;
using System;
using System.IO;

namespace Convert_PDF_to_Image
{
internal class Program
{
static void Main(string[] args)
{
//Initialize PDF to Image converter.
PdfToImageConverter imageConverter = new PdfToImageConverter();
//Load the PDF document as a stream
FileStream inputStream = new FileStream("../../../Input.pdf", FileMode.Open, FileAccess.ReadWrite);
imageConverter.Load(inputStream);
//Convert PDF to Image.
Stream[] imageStreams = imageConverter.Convert(0, imageConverter.PageCount - 1, false, false);
CombineImages(imageStreams, "Output.png");

//Dispose the image streams.
foreach (Stream imageStream in imageStreams)
imageStream.Dispose();
}
/// <summary>
/// Combines multiple images from streams into a single image.
/// </summary>
/// <param name="imageStreams">Streams containing the images to be combined.</param>
/// <param name="outputPath">Output path where the combined image will be saved.</param>
public static void CombineImages(Stream[] imageStreams, string outputPath)
{
if (imageStreams == null || imageStreams.Length == 0)
throw new ArgumentException("No images to combine.");

// Load all images and get their dimensions
SKBitmap[] bitmaps = new SKBitmap[imageStreams.Length];
int maxWidth = 0;
int totalHeight = 0;
int margin = 20;

for (int i = 0; i < imageStreams.Length; i++)
{
imageStreams[i].Position = 0;
bitmaps[i] = SKBitmap.Decode(imageStreams[i]);
maxWidth = Math.Max(maxWidth, bitmaps[i].Width);
totalHeight += bitmaps[i].Height + margin;
}

// Add margins to the total width and height
int combinedWidth = maxWidth + 2 * margin;
// Add margin at the bottom
totalHeight += margin;

// Create a new bitmap with the combined dimensions
using (SKBitmap combinedBitmap = new SKBitmap(combinedWidth, totalHeight))
{
using (SKCanvas canvas = new SKCanvas(combinedBitmap))
{
// Set background color to the specified color
canvas.Clear(new SKColor(240, 240, 240));

// Draw each bitmap onto the canvas
int yOffset = margin;
for (int i = 0; i < bitmaps.Length; i++)
{
int xOffset = (combinedWidth - bitmaps[i].Width) / 2; // Center the image horizontally
canvas.DrawBitmap(bitmaps[i], new SKPoint(xOffset, yOffset));
yOffset += bitmaps[i].Height + margin; // Add margin between rows
}

// Save the combined bitmap to the output stream
using (SKImage image = SKImage.FromBitmap(combinedBitmap))
{
using (SKData data = image.Encode(SKEncodedImageFormat.Png, 100))
{
using (FileStream stream = File.OpenWrite(outputPath))
data.SaveTo(stream);
}
}
}
}
}
}
}