Skip to content

Commit 33cf392

Browse files
authored
Merge pull request #26 from SyncfusionExamples/pdftoimage
PdfToImageConverter
2 parents 651086a + 3f0979b commit 33cf392

File tree

2 files changed

+40
-37
lines changed

2 files changed

+40
-37
lines changed

PDF-to-image/PDF-to-image-in-.NET/PDF_to_Image_Core/Controllers/HomeController.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using PDF_to_Image_Core.Models;
33
using System.Diagnostics;
44
using Syncfusion.PdfToImageConverter;
5-
using Microsoft.AspNetCore.Hosting.Server;
65

76
namespace PDF_to_Image_Core.Controllers
87
{
@@ -27,11 +26,14 @@ public async Task<IActionResult> ConvertToImage(MyPdfUploadViewModel model)
2726
{
2827
if (model.PdfFile != null && model.PdfFile.Length > 0)
2928
{
30-
string filePath = Path.Combine(_webEnvironment.WebRootPath, "Sample.pdf");
31-
32-
using (var stream = new FileStream(filePath, FileMode.OpenOrCreate))
29+
// Create a memory stream to hold the file data
30+
using (MemoryStream stream = new MemoryStream())
3331
{
34-
await model.PdfFile.CopyToAsync(stream);
32+
// Copy the file data to the memory stream
33+
model.PdfFile.CopyTo(stream);
34+
35+
// Reset the position of the memory stream to the beginning
36+
stream.Seek(0, SeekOrigin.Begin);
3537
PdfToImageConverter pdfToImageConverter = new PdfToImageConverter();
3638
pdfToImageConverter.Load(stream);
3739
Stream imageStream = pdfToImageConverter.Convert(0, false, false);

PDF-to-image/PDF_to_Image_MVC/PDF_to_Image_MVC/PDFToImage.aspx.cs

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -32,45 +32,46 @@ protected void PDFToImage_Click(object sender, EventArgs e)
3232
// Check if it's a PDF file
3333
if (Path.GetExtension(uploadedFile.FileName).Equals(".pdf", StringComparison.OrdinalIgnoreCase))
3434
{
35-
// Save or process the PDF file
36-
string filePath = Server.MapPath("~/" + "Sample.pdf");
37-
uploadedFile.SaveAs(filePath);
38-
FileStream inputPDFStream = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite);
39-
PdfToImageConverter imageConverter = new PdfToImageConverter(inputPDFStream);
40-
Stream[] outputStream = new Stream[imageConverter.PageCount];
41-
// Convert all the PDF pages to images
42-
if (imageConverter.PageCount > 1)
35+
// Create a stream to read the file data
36+
using (Stream inputPDFStream = uploadedFile.InputStream)
4337
{
44-
outputStream = imageConverter.Convert(0, imageConverter.PageCount - 1, false, false);
45-
}
46-
else if (imageConverter.PageCount == 1)
47-
{
48-
outputStream[0] = imageConverter.Convert(0, false, false);
49-
}
50-
if (outputStream != null)
51-
{
52-
if (!Directory.Exists(Server.MapPath("~/PdfToImage/")))
38+
// Reset the position of the stream to the beginning
39+
inputPDFStream.Seek(0, SeekOrigin.Begin);
40+
PdfToImageConverter imageConverter = new PdfToImageConverter(inputPDFStream);
41+
Stream[] outputStream = new Stream[imageConverter.PageCount];
42+
// Convert all the PDF pages to images
43+
if (imageConverter.PageCount > 1)
5344
{
54-
Directory.CreateDirectory(Server.MapPath("~/PdfToImage/"));
45+
outputStream = imageConverter.Convert(0, imageConverter.PageCount - 1, false, false);
5546
}
56-
foreach (Stream stream in outputStream)
47+
else if (imageConverter.PageCount == 1)
5748
{
58-
if (stream != null)
59-
{
60-
Bitmap bitmap = new Bitmap(stream);
61-
bitmap.Save(Server.MapPath("~/PdfToImage/Image"+ Guid.NewGuid().ToString() + ".png"), ImageFormat.Png);
62-
}
49+
outputStream[0] = imageConverter.Convert(0, false, false);
6350
}
64-
imageConverter.Dispose();
65-
if (MessageBox.Show("Do you want to view the converted image?", "Confirmation", MessageBoxButton.YesNo, MessageBoxImage.Information) == MessageBoxResult.Yes)
51+
if (outputStream != null)
6652
{
67-
//Launching the PDF file using the default Application.[Acrobat Reader]
68-
System.Diagnostics.Process process = new System.Diagnostics.Process();
69-
process.StartInfo = new System.Diagnostics.ProcessStartInfo(Server.MapPath("~/PdfToImage/"));
70-
process.Start();
53+
if (!Directory.Exists(Server.MapPath("~/PdfToImage/")))
54+
{
55+
Directory.CreateDirectory(Server.MapPath("~/PdfToImage/"));
56+
}
57+
foreach (Stream stream in outputStream)
58+
{
59+
if (stream != null)
60+
{
61+
Bitmap bitmap = new Bitmap(stream);
62+
bitmap.Save(Server.MapPath("~/PdfToImage/Image" + Guid.NewGuid().ToString() + ".png"), ImageFormat.Png);
63+
}
64+
}
65+
imageConverter.Dispose();
66+
if (MessageBox.Show("Do you want to view the converted image?", "Confirmation", MessageBoxButton.YesNo, MessageBoxImage.Information) == MessageBoxResult.Yes)
67+
{
68+
//Launching the PDF file using the default Application.[Acrobat Reader]
69+
System.Diagnostics.Process process = new System.Diagnostics.Process();
70+
process.StartInfo = new System.Diagnostics.ProcessStartInfo(Server.MapPath("~/PdfToImage/"));
71+
process.Start();
72+
}
7173
}
72-
}
73-
// You can perform further processing with the PDF file here
74+
}
7475
}
7576
}
7677
}

0 commit comments

Comments
 (0)