|
| 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