ASP.Net - View all PDF pages using C# and VB .Net

ASP.Net - View all PDF pages using C# and VB .Net.


Complete code.

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Collections;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Result.Text = "";
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (FileUpload1.PostedFile.FileName.Length == 0 || FileUpload1.FileBytes.Length == 0)
        {
            Result.Text = "Please select PDF file at first!";
            return;
        }
        
        SautinSoft.PdfFocus f = new SautinSoft.PdfFocus();
		//this property is necessary only for registered version
		//f.Serial = "XXXXXXXXXXX";
		
        f.OpenPdf(FileUpload1.FileBytes);

        if (f.PageCount > 0)
        {
            //set image properties
            f.ImageOptions.ImageFormat = System.Drawing.Imaging.ImageFormat.Png;
            f.ImageOptions.Dpi = 72;

            //Let's convert whole PDF document
            ArrayList pages = f.ToImage();

            //Show images
            if (pages.Count > 0)
            {
                int width = 3;
                int imgWidth = 300;

                HtmlTable table = new HtmlTable();
                table.Border = 1;
                table.CellPadding = 3;
                table.CellSpacing = 3;

                HtmlTableRow row;
                HtmlTableCell cell;
                HtmlImage img;
                
                string imagePath = Server.MapPath("~");
                string imageName = "Page";

                row = new HtmlTableRow();
                int count = 0;
                foreach (byte[] page in pages)
                {
                    count++;
                    string src = imageName + count.ToString() + ".png";
                    File.WriteAllBytes(Path.Combine(imagePath, src), page);

                    cell = new HtmlTableCell();
                    cell.Style.Add("vertical-align","top");
                    img = new HtmlImage();

                    img.Src = src;
                    img.Width = imgWidth;
                    cell.InnerHtml = "<div align=\"center\">Page" + count.ToString() + "</div>";
                    
                    cell.Controls.Add(img);
                    row.Cells.Add(cell);

                    if (count % width == 0)
                    {
                        table.Rows.Add(row);
                        row = new HtmlTableRow();
                    }
                }
                table.Rows.Add(row);
                this.Controls.Add(table);
            }
   
        }
        else
        {
            Result.Text = "Converting failed!";
        }
    }
}

Download.

        
            Imports System
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.IO

Partial Public Class _Default
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        Result.Text = ""
    End Sub
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
        If FileUpload1.PostedFile.FileName.Length = 0 OrElse FileUpload1.FileBytes.Length = 0 Then
            Result.Text = "Please select PDF file at first!"
            Return
        End If

        Dim f As New SautinSoft.PdfFocus()
        'this property is necessary only for registered version
        'f.Serial = "XXXXXXXXXXX"

        f.OpenPdf(FileUpload1.FileBytes)

        If f.PageCount > 0 Then
            'set image properties
            f.ImageOptions.ImageFormat = System.Drawing.Imaging.ImageFormat.Png
            f.ImageOptions.Dpi = 72

            'Let's convert whole PDF document
            Dim pages As ArrayList = f.ToImage()

            'Show images
            If pages.Count > 0 Then
                Dim width As Integer = 3
                Dim imgWidth As Integer = 300

                Dim table As New HtmlTable()
                table.Border = 1
                table.CellPadding = 3
                table.CellSpacing = 3

                Dim row As HtmlTableRow
                Dim cell As HtmlTableCell
                Dim img As HtmlImage

                Dim imagePath As String = Server.MapPath("~")
                Dim imageName As String = "Page"

                row = New HtmlTableRow()
                Dim count As Integer = 0
                For Each page As Byte() In pages
                    count += 1
                    Dim src As String = imageName & count.ToString() & ".png"
                    File.WriteAllBytes(Path.Combine(imagePath, src), page)

                    cell = New HtmlTableCell()
                    cell.Style.Add("vertical-align", "top")
                    img = New HtmlImage()

                    img.Src = src
                    img.Width = imgWidth
                    cell.InnerHtml = "<div align=""center"">Page" & count.ToString() & "</div>"

                    cell.Controls.Add(img)
                    row.Cells.Add(cell)

                    If count Mod width = 0 Then
                        table.Rows.Add(row)
                        row = New HtmlTableRow()
                    End If
                Next page
                table.Rows.Add(row)
                Me.Controls.Add(table)
            End If

        Else
            Result.Text = "Converting failed!"
        End If
    End Sub
End Class

Download.


If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below:



Questions and suggestions from you are always welcome!

We are developing .Net components since 2002. We know PDF, DOCX, RTF, HTML, XLSX and Images formats. If you need any assistance with creating, modifying or converting documents in various formats, we can help you. We will write any code example for you absolutely free.