Okay. This is what I have. I have a pdf file that is converted into a byte[] and saved to a Db. What I want to do is to convert the pdf file into an image and display it in a partial view as a popup. I was able to achieve this, but there was a couple of problems. 1- The converted image had low quality 2- the process was slow
This is my code (I used TallComponents.PDF.Rasterizer library to do the conversion, don't mind if you guys recommend a better library)
var doc = service.GetAttachmentById(documentId);
ViewAttachmentViewModel model = new ViewAttachmentViewModel();
if (doc.Extension.ToLower().Trim() == ".pdf")
{
// convert pdf to image
try
{
Bitmap bitmap;
using (Stream stream = new MemoryStream(doc.Content))
{
Document m = new Document(stream);
Page p = m.Pages[0];
bitmap = new Bitmap((int)p.Width, (int)p.Height);
using (Graphics g = Graphics.FromImage(bitmap))
{
p.Draw(g);
}
}
ImageConverter converter = new ImageConverter();
byte[] cont = (byte[])converter.ConvertTo(bitmap, typeof(byte[]));
model = new ViewAttachmentViewModel
{
Id = documentId,
Content = cont,
FileName = doc.Title
};
}
catch (Exception ex)
{
// handle exception here;
}
}
else
{
model = new ViewAttachmentViewModel
{
Id = documentId,
Content = doc.Content,
FileName = doc.Title
};
}
return PartialView("_AttachmentPopupViewer", model);
partial view:
@{
var base64 = Convert.ToBase64String(Model.Content);
var imgSrc = String.Format("data:image/gif;base64,{0}", base64);
}
<img src="@imgSrc" />
I'm using PDFJS in my project. it works pretty well. give it a try
It's not converting pdf to image but it's a tool for parsing and rendering PDFs in your website
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.