C# and VB.NET languages are both supported to use in ASP.NET application. Here, we provide Visual C# and Visual Basic examples to describe how to convert PDF document to JPG image in a sample web page for viewing.
C# Example for ASP.NET Web Project
using System; using System.Web.UI; using System.IO; using PQScan.PDFToImage; public partial class _Default: System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { // Prepare a test document. string pdfFileName = Server.MapPath("sample.pdf"); // Create an instance of PQScan.PDFToImage.PDFDocument object. PDFDocument pdfDoc = new PDFDocument(); // Load your PDF document. pdfDoc.LoadPDF(pdfFileName); // Prepare response. Response.Clear(); Response.ContentType = "image/jpeg"; // Change the first page of PDF document to the output image. System.Drawing.Image jpgImage = pdfDoc.ToImage(0); using (MemoryStream ms = new MemoryStream()) { // Save image to jpg format. jpgImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); // Show jpg image to your aspx web page. Response.OutputStream.Write(ms.GetBuffer(), 0, (int)ms.Length); } jpgImage.Dispose(); Response.End(); } }
VB.NET Example for ASP.NET Web Project
Imports System Imports System.Web.UI Imports System.IO Imports PQScan.PDFToImage Public partial Class _Default Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) ' Prepare a test document. Dim pdfFileName As String = Server.MapPath("sample.pdf") ' Create an instance of PQScan.PDFToImage.PDFDocument object. Dim pdfDoc As PDFDocument = New PDFDocument() ' Load your PDF document. pdfDoc.LoadPDF(pdfFileName) ' Prepare response. Response.Clear() Response.ContentType = "image/jpeg" ' Turn the first page of PDF document to the output image. Dim jpgImage As System.Drawing.Image = pdfDoc.ToImage(0) Imports (MemoryStream ms = New MemoryStream()) { ' Save image to jpeg format. jpgImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg) ' Show jpeg image to your aspx web page. Response.OutputStream.Write(ms.GetBuffer(), 0, CType(ms.Length, Integer)) } jpgImage.Dispose() Response.End() End Sub End Class