|
| 1 | +# Create or Generate images from PDF in ASP.NET Core |
| 2 | + |
| 3 | +## Steps to create images from PDF in ASP.NET Core |
| 4 | + |
| 5 | +Step 1: Create a new C# ASP.NET Core Web Application project. |
| 6 | + |
| 7 | +Step 2: Select Web Application pattern (Model-View-Controller) for the project. |
| 8 | + |
| 9 | +Step 3: Install the [Syncfusion.Pdf.Net.Core](https://www.nuget.org/packages/Syncfusion.PdfToImageConverter.Net/) [NuGet package](https://help.syncfusion.com/document-processing/nuget-packages) as reference to your ASP.NET Core applications from [NuGet.org](https://www.nuget.org/). |
| 10 | + |
| 11 | +Step 4: A default controller with name HomeController.cs gets added on creation of ASP.NET Core project. Include the following namespaces in that HomeController.cs file. |
| 12 | + |
| 13 | + |
| 14 | +``` |
| 15 | +using Syncfusion.PdfToImageConverter; |
| 16 | +using System.IO; |
| 17 | +``` |
| 18 | + |
| 19 | +Step 5: A default action method named Index will be present in HomeController.cs. Right click on Index method and select Go To View where you will be directed to its associated view page Index.cshtml. Add a new button in the Index.cshtml as shown below. |
| 20 | + |
| 21 | +``` |
| 22 | +@{Html.BeginForm("CreateImage", "Home", FormMethod.Get); |
| 23 | + { |
| 24 | + <div> |
| 25 | + <input type="submit" value="Create PDF to Image" style="width:200px;height:27px" /> |
| 26 | + </div> |
| 27 | + } |
| 28 | + Html.EndForm(); |
| 29 | +} |
| 30 | +``` |
| 31 | + |
| 32 | +Step 6: Add a new action method named ``CreateImage`` in HomeController.cs file and include the below code example to generate a image from PDF document using the PdfToImageConverter. |
| 33 | +``` |
| 34 | +//Initialize PDF to Image converter. |
| 35 | +PdfToImageConverter imageConverter = new PdfToImageConverter(); |
| 36 | +
|
| 37 | +//Load the PDF document as a stream |
| 38 | +FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/Input.pdf"), FileMode.Open, FileAccess.ReadWrite); |
| 39 | +
|
| 40 | +imageConverter.Load(inputStream); |
| 41 | +
|
| 42 | +//Convert PDF to Image. |
| 43 | +Stream outputStream = imageConverter.Convert(0, false, false); |
| 44 | +
|
| 45 | +//Rewind the stream position to the beginning before copying. |
| 46 | +outputStream.Position = 0; |
| 47 | +
|
| 48 | +//Create file stream. |
| 49 | +using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.jpeg"), FileMode.Create, FileAccess.ReadWrite)) |
| 50 | +{ |
| 51 | + //Save the image to file stream. |
| 52 | + outputStream.CopyTo(outputFileStream); |
| 53 | +} |
| 54 | +//Dispose the imageConverter |
| 55 | +imageConverter.Dispose(); |
| 56 | +``` |
0 commit comments