@@ -32,45 +32,46 @@ protected void PDFToImage_Click(object sender, EventArgs e)
32
32
// Check if it's a PDF file
33
33
if ( Path . GetExtension ( uploadedFile . FileName ) . Equals ( ".pdf" , StringComparison . OrdinalIgnoreCase ) )
34
34
{
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 )
43
37
{
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 )
53
44
{
54
- Directory . CreateDirectory ( Server . MapPath ( "~/PdfToImage/" ) ) ;
45
+ outputStream = imageConverter . Convert ( 0 , imageConverter . PageCount - 1 , false , false ) ;
55
46
}
56
- foreach ( Stream stream in outputStream )
47
+ else if ( imageConverter . PageCount == 1 )
57
48
{
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 ) ;
63
50
}
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 )
66
52
{
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
+ }
71
73
}
72
- }
73
- // You can perform further processing with the PDF file here
74
+ }
74
75
}
75
76
}
76
77
}
0 commit comments