This code gets the .pdf file and converts it into an image for ImageUrl to display from the DropDownList.
It takes the .pdf bytes from DB and base64String converts the bytes to image.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Configuration;
using System.IO;
using System.Drawing.Imaging;
using PQScan.PDFToImage;
namespace PDFToImage
{
public partial class _Default : System.Web.UI.Page
{
private int increments(int count)
{
count = 0;
count += 1;
return count;
}
private void DeleteDirectory(string path)
{
if (Directory.Exists(path))
{
//Delete all files from the Directory
foreach (string file in Directory.GetFiles(path))
{
File.Delete("Order.pdf");
}
}
}
public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
return ms.ToArray();
}
protected void ddComapanyFilter_SelectedIndexChanged(object sender, EventArgs e)
{
string conString = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
SqlCommand cmd = new SqlCommand("SELECT Name, Id, Data FROM RetPDF where Id=@id", con);
con.Open();
string path = @"C:\Program Files\IIS Express\";
if (Directory.Exists(path))
{
//Delete all files from the Directory
foreach (string file in Directory.GetFiles(path))
{
File.Delete("Order.pdf");
}
cmd.Parameters.AddWithValue("@id", ddlCountries.SelectedItem.Value);
using (var reader = cmd.ExecuteReader())
{
int count = 1;
if (reader.Read())
{
byte[] imagedata = (byte[])reader["Data"];
if (Directory.Exists(path))
{
//Delete all files from the Directory
foreach (string file in Directory.GetFiles(path))
{
File.Delete("Order" + increments(count) + ".pdf");
}
count++;
FileStream fileStream = new FileStream("Order" + increments(count) + ".pdf", FileMode.CreateNew);
System.IO.File.WriteAllBytes(fileStream.ToString(), imagedata);
PDFDocument pdf = new PDFDocument();
// Create a file stream with PDF information
FileStream stream = new FileStream(fileStream.ToString(), FileMode.Open);
// Load PDF document from file stream.
pdf.LoadPDF(stream);
//if(pdf.PageCount !=-1)
pdf.DPI = 200;
// Set the width of output jpeg image.
int width = pdf.GetPageWidth(0) / 2;
// Set the height output jpeg image.
int height = pdf.GetPageHeight(0) / 2;
// Convert the first PDF page to image with the expected size.
Bitmap jpg = pdf.ToImage(0, width, height);
// Save image to jpg format.
jpg.Save("result.jpeg", ImageFormat.Jpeg);
Image1.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(imageToByteArray(jpg));
con.Close();
}
}
}
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string path = @"C:\Program Files\IIS Express\";
DeleteDirectory(path);
string conString = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
SqlCommand cmd = new SqlCommand("SELECT Name, Id, Data FROM RetPDF");
using (SqlConnection con = new SqlConnection(conString))
{
con.Open();
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataSet ds = new DataSet())
{
sda.Fill(ds);
ddlCountries.DataSource = ds;
ddlCountries.DataTextField = "Name";
ddlCountries.DataValueField = "id";
ddlCountries.DataBind();
}
}
}
}
}
}
}