diff --git a/PDF-to-image/Convert-PDF-Pages-to-Single-Image-.NET/Convert-PDF-Pages-to-Single-Image.csproj b/PDF-to-image/Convert-PDF-Pages-to-Single-Image-.NET/Convert-PDF-Pages-to-Single-Image.csproj new file mode 100644 index 0000000..6f914bb --- /dev/null +++ b/PDF-to-image/Convert-PDF-Pages-to-Single-Image-.NET/Convert-PDF-Pages-to-Single-Image.csproj @@ -0,0 +1,13 @@ + + + + Exe + net8.0 + Convert_PDF_to_Image + + + + + + + diff --git a/PDF-to-image/Convert-PDF-Pages-to-Single-Image-.NET/Convert-PDF-Pages-to-Single-Image.sln b/PDF-to-image/Convert-PDF-Pages-to-Single-Image-.NET/Convert-PDF-Pages-to-Single-Image.sln new file mode 100644 index 0000000..8b67d4d --- /dev/null +++ b/PDF-to-image/Convert-PDF-Pages-to-Single-Image-.NET/Convert-PDF-Pages-to-Single-Image.sln @@ -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 diff --git a/PDF-to-image/Convert-PDF-Pages-to-Single-Image-.NET/Input.pdf b/PDF-to-image/Convert-PDF-Pages-to-Single-Image-.NET/Input.pdf new file mode 100644 index 0000000..8773442 Binary files /dev/null and b/PDF-to-image/Convert-PDF-Pages-to-Single-Image-.NET/Input.pdf differ diff --git a/PDF-to-image/Convert-PDF-Pages-to-Single-Image-.NET/Program.cs b/PDF-to-image/Convert-PDF-Pages-to-Single-Image-.NET/Program.cs new file mode 100644 index 0000000..f1a2dc3 --- /dev/null +++ b/PDF-to-image/Convert-PDF-Pages-to-Single-Image-.NET/Program.cs @@ -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(); + } + /// + /// Combines multiple images from streams into a single image. + /// + /// Streams containing the images to be combined. + /// Output path where the combined image will be saved. + 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); + } + } + } + } + } + } +}