You can use the Ghostscript.NET library to convert PDF files to images in C# MVC.
xxxxxxxxxx
using Ghostscript.NET.Rasterizer;
// Set the path to the PDF file
string pdfFilePath = "path/to/pdf/file.pdf";
// Set the path to the output directory
string outputDir = "path/to/output/dir";
// Create an instance of the GhostscriptRasterizer class
var rasterizer = new GhostscriptRasterizer();
// Set the resolution (in DPI) for the output image(s)
rasterizer.RenderingResolution = 300;
// Open the PDF file
rasterizer.Open(pdfFilePath);
// Loop through the pages in the PDF file and save each one as an image file
for (int pageNumber = 1; pageNumber <= rasterizer.PageCount; pageNumber++)
{
// Set the output image file name
string outputFileName = $"page{pageNumber}.png";
// Set the output image file path
string outputFilePath = Path.Combine(outputDir, outputFileName);
// Render the page to a PNG image
var image = rasterizer.GetPage(300, 300, pageNumber);
// Save the image file
image.Save(outputFilePath, System.Drawing.Imaging.ImageFormat.Png);
}
// Close the PDF file and dispose of the rasterizer object
rasterizer.Close();
rasterizer.Dispose();
This code snippet opens a PDF file, sets the output resolution to 300 DPI, and then loops through each page in the PDF file, rendering it to a PNG image and saving it to the specified output directory. You can modify the code to save the images in a different format (such as JPG or BMP) if needed.
xxxxxxxxxx
static void Main(string[] args)
{
PdfDocument doc = new PdfDocument();
doc.LoadFromFile(@"D:\test.pdf");
List<string> list = new List<string>();
for (int i = 0; i < doc.Pages.Count; i++)
{
System.Drawing.Image bmp = doc.SaveAsImage(i);
string fileName =string.Format("Page-{0}.png", i + 1);
bmp.Save(fileName, System.Drawing.Imaging.ImageFormat.Png);
list.Add(fileName);
}
Bitmap b = CombineBitmap(list.ToArray());
b.Save("D:\\total.png");
foreach (var item in list)
{
File.Delete(item);
}
}
public static System.Drawing.Bitmap CombineBitmap(string[] files)
{
//read all images into memory
List<System.Drawing.Bitmap> images = new List<System.Drawing.Bitmap>();
System.Drawing.Bitmap finalImage = null;
try
{
int width = 0;
int height = 0;
foreach (string image in files)
{
//create a Bitmap from the file and add it to the list
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(image);
//update the size of the final bitmap
width += bitmap.Width;
height = bitmap.Height > height ? bitmap.Height : height;
images.Add(bitmap);
}
//create a bitmap to hold the combined image
finalImage = new System.Drawing.Bitmap(width, height);
//get a graphics object from the image so we can draw on it
using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(finalImage))
{
//set background color
g.Clear(System.Drawing.Color.Black);
//go through each image and draw it on the final image
int offset = 0;
foreach (System.Drawing.Bitmap image in images)
{
g.DrawImage(image,
new System.Drawing.Rectangle(offset, 0, image.Width, image.Height));
offset += image.Width;
}
}
return finalImage;
}
catch (Exception)
{
if (finalImage != null)
finalImage.Dispose();
//throw ex;
throw;
}
finally
{
//clean up memory
foreach (System.Drawing.Bitmap image in images)
{
image.Dispose();
}
}
}