Turning to the previous page, the next page, the first page, the last page, or even switch to any existing target page is the frequently used manipulation, which helps us to save a lot of time on viewing a PDF document with multiple pages on website.

How can we achieve this function programmatically in ASP.NET? Spire.PDFViewer for ASP.NET provides developers the feature of paging which allows users to turn page according to their requirements. This article will demonstrate how to realize paging with Spire.PDFViewer for ASP.NET.

Note: Please download Spire.PDFViewer for ASP.NET and install it correctly.

Then follow the detail steps below:

Step 1: create a new project and add the .dll files from bin folder of Spire.PDFViewer as references.

How to realize paging with Spire.PDFViewer for ASP.NET

Step 2: Add the PDFDocumentViewer control of Spire.PDFViewer into toolbox and drag it into Deafault.aspx.

How to realize paging with Spire.PDFViewer for ASP.NET

Step 3: Paging.

Main code:

Section 1: Call the LoadFromFile() method of PdfDocumentViewer to load a sample PDF file in Default.aspx.cs. Note that you have to add the following if statement and !IsPostBack property before loading the pdf file.

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //load the sample PDF file
                this.PdfDocumentViewer1.CacheInterval = 1000;
                this.PdfDocumentViewer1.CacheTime = 1200;
                this.PdfDocumentViewer1.CacheNumberImage = 1000;
                this.PdfDocumentViewer1.ScrollInterval = 300;
                this.PdfDocumentViewer1.ZoomFactor = 1f;
                this.PdfDocumentViewer1.CustomErrorMessages = "";
                this.PdfDocumentViewer1.LoadFromFile("files/PDFViewer.pdf");
            }
        }

Section 2: Drag four buttons and a textbox from toolbox into Deafault.aspx, set their properties as below.

How to realize paging with Spire.PDFViewer for ASP.NET

The generated source code in Deafault.aspx:

<div>
        <input type="button" id="btnFirst" value="first" onclick="pdfdocumentviewer1.FirstPage()" />
        <input type="button" id="btnPrevious" value="Previous" onclick="pdfdocumentviewer1.PreviousPage()" />
        <input type="text" id="PdfDocumentViewer1_PdfViewerCurrentPage" value="1" style="width: 50px;" />
        <span id="PdfDocumentViewer1_PdfViwerCountPage"></span>
        <input type="button" id="btnNext" value="Next" onclick="pdfdocumentviewer1.NextPage()" />
        <input type="button" id="btnLast" value="Last" onclick="pdfdocumentviewer1.LastPage()" />

        <cc1:PdfDocumentViewer id="PdfDocumentViewer1" runat="server">
        </cc1:PdfDocumentViewer>
</div>

Run the project and you'll get the following output:

How to realize paging with Spire.PDFViewer for ASP.NET

Spire.PDFViewer for ASP.NET contains two controls: PDFViewer and PDFDocumentViewer. Generally, PDFDocumentViewer is used for loading and viewing PDF files on website. But actually, it can also achieve other features such as zoom, fit and page after a simple design.

We've introduced the usage of PDFViewer in the previous article, so this article will illustrate how to zoom PDF File via PDFDocumentViewer in ASP.NET.

Before start, download Spire.PDFViewer for ASP.NET and install it on your system.

Step 1: Create a new ASP.NET Empty Web Application in Visual Studio. Add a new web Form to the project.

Step 2: Add the .dll files from the bin folder as the references of this project.

How to Zoom PDF File via PDFDocumentViewer in ASP.NET

Step 3: Add the PDFDocumentViewer control into toolbox and drag it into Deafault.aspx.

How to Zoom PDF File via PDFDocumentViewer in ASP.NET

(Detail manipulations of step 1, 2, 3 refer to this article: How to use Spire.PDFViewer for ASP.NET)

Step 4: Zoom PDF file via Spire. PDFDocumentViewer. Zoom feature is divided into three types in this article:

  • Zoom: choose the zoom percentage manually.
  • Zoom in: Increase the display page page zoom factor by ten percent.
  • Zoom out: decrease the display page page zoom factor by ten percent.

Main codes

Section 1: Call the LoadFromFile() method of PdfDocumentViewer to load a sample PDF file in Default.aspx.cs. Note that you have to add the following if statement and !IsPostBack property before loading the pdf file.

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //load the sample PDF file
                this.PdfDocumentViewer1.CacheInterval = 1000;
                this.PdfDocumentViewer1.CacheTime = 1200;
                this.PdfDocumentViewer1.CacheNumberImage = 1000;
                this.PdfDocumentViewer1.ScrollInterval = 300;
                this.PdfDocumentViewer1.ZoomFactor = 1f;
                this.PdfDocumentViewer1.CustomErrorMessages = "";
                this.PdfDocumentViewer1.LoadFromFile("files/PDFViewer.pdf");
            }
        }

Section 2: Design, Drag a DropDownList and two buttons from toolbox into Deafault.aspx, set the properties like "ID", "text" etc. as below.

How to Zoom PDF File via PDFDocumentViewer in ASP.NET

Generated source code is shown here:

<select id="PdfDocumentViewer1_SelectCurrentZoomLevel" name="PdfDocumentViewer1_SelectCurrentZoomLevel" onchange="pdfdocumentviewer1.SelectDropdownBox(this.value)">
            <option value="0.5">50%</option>
            <option value="0.75">75%</option>
            <option value="1" selected="selected">100%</option>
            <option value="1.5">150%</option>
            <option value="2">200%</option>
            <option value="4">400%</option>
        </select>
        <input type="button" id="btnZoomIn" value="Zoom In" onclick="pdfdocumentviewer1.ZoomPage()" />
        <input type="button" id="btnZoomOut" value="Zoom Out" onclick="pdfdocumentviewer1.NarrowPage()" />

Effect screenshot after designing:

How to Zoom PDF File via PDFDocumentViewer in ASP.NET

Thursday, 12 November 2015 08:23

How to use Spire.PDFViewer for ASP.NET

Written by support iceblue

Spire.PDFViewer is a powerful and independent ASP.NET library, by which users can easily achieve functions such as load and view pdf files on website, switch to target page, fit page, page down/up, zoom in/out pdf files etc.

Then how to use Spire.PDFViewer for ASP.NET? This article will introduce the usage of Spire.PDFViewer for ASP.NET to you.

Before start, please download Spire.PDFViewer for ASP.NET and install it on your system.

Detail steps overview:

Step 1: Create a new ASP.NET Empty Web Application in Visual Studio.

How to use Spire.PDFViewer for ASP.NET

Add a new web Form to Test1.

How to use Spire.PDFViewer for ASP.NET

How to use Spire.PDFViewer for ASP.NET

Then add the .dll files from the bin folder as the references of Test1.

How to use Spire.PDFViewer for ASP.NET

Now the three .dll files have been added into the References.

How to use Spire.PDFViewer for ASP.NET

Step 2: Add the PDFViewer control and the PDFDocumentViewer control into toolbox.

First, right-click toolbox, select add tab to add a new tab named SpirePDFViewer.

How to use Spire.PDFViewer for ASP.NET

Second, add the PDFViewer control and the PDFDocumentViewer control into SpirePDFViewer.

How to use Spire.PDFViewer for ASP.NET

How to use Spire.PDFViewer for ASP.NET

Now all of the controls have been added into SpirePDFViewer.

How to use Spire.PDFViewer for ASP.NET

Step 3: Right-click Default.aspx, select view designer, and then drag the PDFViewer control from toolbox into Deafault.aspx.

How to use Spire.PDFViewer for ASP.NET

Step 4: Double-click Default.aspx.cs, add the code below to load a PDF file, Note that you have to add the following if statement and !IsPostBack property before loading the pdf file.

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //load the sample PDF file
                this.pdfViewer1.CacheInterval = 1000;
                this.pdfViewer1.CacheTime = 1200;
                this.pdfViewer1.CacheNumberImage = 1000;
                this.pdfViewer1.ScrollInterval = 300;
                this.pdfViewer1.ZoomFactor = 1f;
                this.pdfViewer1.CustomErrorMessages = "";
                this.pdfViewer1.LoadFromFile("PDF file/Test.pdf");
            }
        }

Effect screenshot:

How to use Spire.PDFViewer for ASP.NET

Sometimes we need to make use of the attachments added to the PDF files. There are 2 kinds of attachment in pdf, one is common attachment that added to the file directly, the other is attachment annotations which is add to the particular place as an annotation like the following picture, when you click correlative icon, that attachment will be opened.

In this article, we will learn how to extract these two kinds of attachment in C# via Spire.PdfViewer.

Extract the attachments from PDF document via PDFViewer

Here are the steps:

Step 1: Download PdfViewer, add PdfViewer Control to VS Toolbox (How to add control).

Extract the attachments from PDF document via PDFViewer

Step 2: Create a Windows Forms application, design Form as below.

  • Two buttons respectively for common attachment and attachment annotation, and set name for them.
  • One open file button and one close attachment button.
  • A PdfDocmentViewer in the middle and one ViewList on the bottom.

Step 3: Get attachments

1) Get common attachment via methord GetAttachments(), then traverse attachment array and get common attachment property.

PdfDocumentAttachment[] attchments = this.pdfDocumentViewer1.GetAttachments();
if (attchments != null && attchments.Length > 0)
         {
             for (int i = 0; i < attchments.Length; i++)
             {
                 PdfDocumentAttachment attachment = attchments[i];
                 string fileName = attachment.FileName;
                 string mimeType = attachment.MimeType;
                 string desc = attachment.Description;
                 DateTime createDate = attachment.CreationTime;
                 DateTime modifyDate = attachment.ModifyTime;
                 Object data = attachment.Data;
                 ListViewItem item = new ListViewItem();
                 item.Text = Path.GetFileName(fileName);
                 item.SubItems.Add(mimeType);
                 item.SubItems.Add(desc);
                 item.SubItems.Add(createDate.ToShortDateString());
                 item.SubItems.Add(modifyDate.ToShortDateString());
                 item.Tag = attachment;
                 this.listView1.Items.Add(item);
             }
         }

Results:

Extract the attachments from PDF document via PDFViewer

2) Extract attachment annotation:

Get attachment annotations via methord GetAttachmentAnnotaions(). Then traverse annotation array and save each annotation property as an individual item in listview.

PdfDocumentAttachmentAnnotation[] annotations = this.pdfDocumentViewer1.GetAttachmentAnnotaions();  
if (annotations != null && annotations.Length > 0)
         {
             for (int i = 0; i < annotations.Length; i++)
             {
                 PdfDocumentAttachmentAnnotation annotation = annotations[i];
                 ListViewItem item = new ListViewItem(annotation.FileName);
                 item.SubItems.Add(annotation.Text);
                 item.SubItems.Add(annotation.PageIndex.ToString());
                 item.SubItems.Add(annotation.Location.ToString());
                 item.Tag = annotation;
                 this.listView1.Items.Add(item);
             }
         }

Results:

Extract the attachments from PDF document via PDFViewer

3) ListView

Here if we click the file name information of annotation attachment in the listView, PdfDocumentViewer will go to specified attachment annotation.

if (this.m_isAttachmentAnnotation)
     {
         PdfDocumentAttachmentAnnotation annotation = (PdfDocumentAttachmentAnnotation)this.listView1.SelectedItems[0].Tag;
         this.pdfDocumentViewer1.GotoAttachmentAnnotation(annotation);
     }

Double click it, the attachment can be saved to local. Get data of annotation, and write into file.

PdfDocumentAttachmentAnnotation annotation = (PdfDocumentAttachmentAnnotation)item.Tag;
byte[] data = annotation.Data;
writer.Write(data);

About saving common attachment:

PdfDocumentAttachment annotation = (PdfDocumentAttachment)item.Tag;
byte[] data = annotation.Data;
writer.Write(data);

Spire.PDFViewer is a powerful PDF Viewer component performed on .NET and WPF. It enables developers to load PDF document from stream, file and byte array. Also it supports rotation, page layout setup and thumbnail control. Worth a shot. Click to know more

Wednesday, 31 December 2014 07:04

Create PDF Document Thumbnail via PDFViewer

Written by support iceblue

PDF document thumbnails, appearing in the left-hand corner of a PDF reader, provide miniature previews of document pages. The 'Thumbnail View' pane allows you to view page thumbnails of all the pages included in the PDF document and you can simply click on the thumbnail to quickly travel to that page.

This article presents how to create a PDF document thumbnail view in your own .NET PDF viewer via Spire.PDFViewer. Detailed steps are as below:

Step 1: Create a new project in Windows Forms Application.

Step 2: Add Spire.PDFViewer control to VS Toolbox.

Create PDF Document Thumbnail via PDFViewer

Step 3: Design Form1.

  • Add a ToolStrip to Form1. Insert two Buttons, one TextBox and one Label to ToolStrip.
  • Drag PdfDocumentThumbnail and PdfDocumentViewer control from Toolbox to Form1.
  • Set properties of the Buttons, Labels, TextBox, PdfDocumentThumbnail and PdfDocumentViewer as below.

Tools

Properties

As

ToolStripButton1 Name btnOpen
DisplayStyle Image
ToolStripButton2 Name btnThumbnailRatio
DisplayStyle Image
ToolStripLabel1 Name toolStripLabel1
DisplayStyle Text
Text Ratio
ToolStripTextBox Name txtThumbnailRatio
PdfDocumentThumbnail Name pdfDocumentThumbnail1
Dock Fill
PdfDocumentViewer Name pdfDocumentViewer1
Dock Full

Step 4: Set code for btnOpen and btnThumbnailRatio.

1) Create a OpenFileDailog in btnOpen click event which enables you to open a file with correct type.

    private void btnOpen_Click(object sender, EventArgs e)
 {
     OpenFileDialog dialog = new OpenFileDialog();
     dialog.Filter = "PDF document(*.pdf)|*.pdf";
     DialogResult result = dialog.ShowDialog();
     if (result == DialogResult.OK)
     {
         string pdfDocument = dialog.FileName;
         this.pdfDocumentViewer1.LoadFromFile(pdfDocument);
     }

 }

2) Set zoom ratio for thumbnail view mode.

  private void btnThumbnailRatio_Click(object sender, EventArgs e)
  {
      if (this.pdfDocumentViewer1.IsDocumentLoaded)
     {
          int percent = 0;
          bool isNumeric = int.TryParse(this.txtThumbnailRatio.Text, out percent);
          if (isNumeric)
          {
              this.pdfDocumentThumbnail1.ZoomPercent =Math.Abs(percent);
          }
      }
  }

Run this program and open an existing PDF document, you'll get following output:

Create PDF Document Thumbnail via PDFViewer

Page 1 of 2