This demo is in C# programming language which demonstrates how to convert PDF file to GIF image in ASP.NET program and view on a web page. Just copy the sample code below and paste to your web application page load event. And you will see the GIF image converted from PDF document on the html page after running your ASP.NET web form 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) { // Get your PDF document ready. 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/gif"; // Turn the first page of your PDF document to the output image. System.Drawing.Image gifImage = pdfDoc.ToImage(0); using (MemoryStream ms = new MemoryStream()) { // Save image as gif format. gifImage.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); // Show gif image to the aspx web page. Response.OutputStream.Write(ms.GetBuffer(), 0, (int)ms.Length); } gifImage.Dispose(); Response.End(); } }
Here, VB.NET demo code for changing and streaming PDF document to GIF raster image is offered as well.
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 sample PDF file. Dim pdfFileName As String = Server.MapPath("sample.pdf") ' Create an instance of PQScan.PDFToImage.PDFDocument object. Dim pdfDoc As PDFDocument = New PDFDocument() ' Load the PDF file. pdfDoc.LoadPDF(pdfFileName) ' Prepare response. Response.Clear() Response.ContentType = "image/gif" ' Transform the first page of PDF file to the output image. Dim gifImage As System.Drawing.Image = pdfDoc.ToImage(0) Imports (MemoryStream ms = New MemoryStream()) { ' Save image as gif format. gifImage.Save(ms, System.Drawing.Imaging.ImageFormat.Gif) ' Display gif image to the aspx web page. Response.OutputStream.Write(ms.GetBuffer(), 0, CType(ms.Length, Integer)) } gifImage.Dispose() Response.End() End Sub End Class