Showing posts with label pdfs. Show all posts
Showing posts with label pdfs. Show all posts

Sunday, 5 August 2012

Get Recent records from the table

Here is a small code snippet for getting the recent records from the table. 

drop table VENKAT_TABLE
go
create table Venkat_Table(code varchar(100),rate decimal(10,8),date1 date)
go
insert into VENKAT_TABLE values ('sam', 1.240000, '01/08/2004') 
insert into VENKAT_TABLE values ('sam', 1.840000, '02/08/2004') 
insert into VENKAT_TABLE values ('pal', 1.240000, '01/08/2005') 
insert into VENKAT_TABLE values ('pal', 0.840000, '10/08/2003') 

select code,rate,date1 from 
(
select ROW_NUMBER() over (partition by code order by date1) as rank1,*
  from VENKAT_TABLE ) t
  where rank1=1

Session State Timeout in ASP.Net

Recently, I thought of including some job related information in my websiteWWW.KaaShivInfoTech.com

After deploying my code, I faced a typical issue of application pool fill for the objects.

Due to some poor coding, I allowed the user to logged in with a username and password. Unfortunately, I didn't give proper access for signing out.

Guessing, the session of the user took 30 minutes expiration time (Standard session expiration time). Since, most of the users are accessing this site and closing it with proper signing out. The objects started accumulating. At present, I dont have any idea to implement sign out option.

So I thought putting some timeout options in the connection string and web.config file. Thought of sharing the same to you all.


In the connection string, I've added timout option as 5 minutes. If the database is accessed for more than 5 minutes, the connection should be thrown out.

or else,

we can do some changes in web.config file under system.web section


cookieless="true"

regenerateExpiredSessionId="true"

Difference between Stored Procedure (SP) and User Defined Function

Ø   User Defined Function can be executed using the "SELECT" clause whereas Stored Procedure cannot be Executed..
Ø    User Defined Function does not return any output parameters while Stored Procedure will return output parameters.
Ø   User Defined Function cannot make permanent changes to server environments while SP's can change some of the server environment.
Ø   If there is an error in UDF its stops executing. But in SP's it just ignores the error and moves to the next statement. 

Methods to Zoom PDF File by PDF Viewer with C#

Zoom function in PDF file can help people change the text size of PDF document page according to their view preference or need. When you want to increase the percentage of the PDF file text, you can click the button of zoom out or choose the percentage number directly as well as scroll down your mouse. And if you want to zoom back to the original text size, you can click the Actual Size button. Actually, you can completely control the text percentage of the PDF document, which is much more convenient than the zoom of Excel and Word files. Now it is time to learn how to zoom PDF file by PDF Viewer with C#, VB.NET.

How to zoom PDF document by PDF Viewer with C#, VB.NET

In this article, I will introduce seven kinds of zoom functions to control PDF file text size by using a PDF Viewer component for .NET and WPF Spire.PDFViewer.  It does NOT require Adobe Reader or any other 3rd party software/library installed on system. The whole procedure can be finished by below steps.


Step1.  Create a new project.

1.     Create a new project in Windows Forms Application.

2.     Set the Target Framework to be .NET Framework 2 or above in Properties.

3.      Add a toolScript and pdfDocumentViewer in Form1 from Toolbox. Then, add seven buttons, two ComboBoxes and a label from the toolScript dropdown list in Form1. 

4.      Set the Properties of the tools respectively. Such as the "Name", "Display Style", "Text" and " ToolTipText" of buttons, ComboBoxes and label. You can set the Dock property of pdfDocumentViewer in its Properties in order to view the PDF in enough space.

Step2. Methods to zoom PDF file by Spire.PDFViewer

1.      Add Spire. PDFViewer.Forms dll as reference.

2.      Add below namespace at the top of the method.


C# Code:

using System.IO;using System.Windows.Forms;using Spire.PdfViewer.Forms;

3.      Zoom PDF file. In this step, I have seven kinds of zoom fuctions to control the PDF text size. They are: 


Zoom: Manually choose the percentage.
Zoom Out: Increase the PDF text size. 
Zoom In: Decrease the PDF text size.
Zoom Dynamic: Scroll down/up the mouse directly to change the text size. A second click can cancel zoom dynamic.
Actual Size: When you click it, the document changes to the original size.
Fit Page: Fit page control the PDF page not going across the PDF page height and margin.
Fit Width: When you click the Fit Width button, you cannot see the margin of PDF document and only see the PDF text content.

Please look at the main code:

C# Code:

namespace pdfzoom{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        //private bool _isZoomout = true;        private int _zoom = 100;        private bool _isZoomDynamic = false;        private void Form1_Load(object sender, EventArgs e)        {            string pdfDoc = @"D:\michelle\PDFViewer.pdf";            if (File.Exists(pdfDoc))            {                this.pdfDocumentViewer1.LoadFromFile(pdfDoc);            }            //add zoom values to comboBox            int[] intZooms = new Int32[] { 25, 50, 75, 100, 125, 150, 200, 300, 400, 500, 600, 700, 800, 900, 1000 };            foreach (int zoom in intZooms)            {                this.comBoxZoom.Items.Add(zoom.ToString());            }            this.comBoxZoom.SelectedIndex = 3;            //pdfDocumentViewer mouseWheel event            this.pdfDocumentViewer1.MouseWheel += new MouseEventHandler(this.pdfDocumentViewer1_MouseWheel);            this.pdfDocumentViewer1.LostFocus += new EventHandler(this.pdfDocumentViewer_LostFocus);        }        private void pdfDocumentViewer_LostFocus(Object sender, EventArgs args)        {            this._isZoomDynamic = false;            this._zoom = 100;        }        private void pdfDocumentViewer1_MouseWheel(Object sender, MouseEventArgs args)        {            if (this._isZoomDynamic)            {                int wheelValue = (Int32)args.Delta / 24;                this._zoom += wheelValue;                if (this._zoom < 0)                    this._zoom = 0;                this.pdfDocumentViewer1.ZoomTo(this._zoom);            }        }        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)            {                try                {                    string pdfFile = dialog.FileName;                    this.pdfDocumentViewer1.LoadFromFile(pdfFile);                }                catch (Exception exe)                {                    MessageBox.Show(exe.Message, "Spire.PdfViewer Demo", MessageBoxButtons.OK, MessageBoxIcon.Error);                }            }        }        private void comBoxZoom_SelectedIndexChanged(object sender, EventArgs e)        {            if (this.pdfDocumentViewer1.PageCount > 0)            {                int zoomValue = Int32.Parse(this.comBoxZoom.SelectedItem.ToString());                this.pdfDocumentViewer1.ZoomTo(zoomValue);            }        }        private void btnZoomOut_Click(object sender, EventArgs e)        {            if (this.pdfDocumentViewer1.PageCount > 0)            {                int delta = 10;                this._zoom += delta;                this.pdfDocumentViewer1.ZoomTo(this._zoom);            }        }        private void btnZoonIn_Click(object sender, EventArgs e)        {            if (this.pdfDocumentViewer1.PageCount > 0)            {                int delta = 5;                this._zoom -= delta;                if (this._zoom < 0)                    this._zoom = 0;                this.pdfDocumentViewer1.ZoomTo(this._zoom);            }        }        private void btnActural_Click(object sender, EventArgs e)        {            this._zoom = 100;            this._isZoomDynamic = false;            this.btnDynamic.Text = "Zoom Dynamic";            if (this.pdfDocumentViewer1.PageCount > 0)            {                this.pdfDocumentViewer1.ZoomTo(100);                this.comBoxZoom.SelectedIndex = 3;            }        }        private void btnFitPage_Click(object sender, EventArgs e)        {            if (this.pdfDocumentViewer1.PageCount > 0)            {                this.pdfDocumentViewer1.ZoomTo(ZoomMode.FitPage);            }        }        private void btnFitWidth_Click(object sender, EventArgs e)        {            if (this.pdfDocumentViewer1.PageCount > 0)            {                this.pdfDocumentViewer1.ZoomTo(ZoomMode.FitWidth);            }        }        private void btnDynamic_Click(object sender, EventArgs e)        {            this._isZoomDynamic = !this._isZoomDynamic;            if (this._isZoomDynamic)            {                this.btnDynamic.Text = "Cancel dynamic zoom";                this.btnDynamic.ToolTipText = "Cancel dynamic zoom";            }            else            {                this.btnDynamic.Text = "Zoom dynamic";                this.btnDynamic.ToolTipText = "Zoom dynamic";            }        }        private void pdfDocumentViewer1_PdfLoaded(object sender, EventArgs args)        {            this.btnDynamic.Enabled = true;            this.comBoxPages.Items.Clear();            int totalPage = this.pdfDocumentViewer1.PageCount;            for (int i = 1; i <= totalPage; i++)            {                this.comBoxPages.Items.Add(i.ToString());            }            this.comBoxPages.SelectedIndex = 0;        }        private void pdfDocumentViewer1_PageNumberChanged(object sender, EventArgs args)        {            if (this.comBoxPages.Items.Count <= 0)                return;            if (this.pdfDocumentViewer1.CurrentPageNumber != this.comBoxPages.SelectedIndex + 1)            {                this.comBoxPages.SelectedIndex = this.pdfDocumentViewer1.CurrentPageNumber - 1;            }        }        private void comBoxPages_SelectedIndexChanged(object sender, EventArgs e)        {            int soucePage = this.pdfDocumentViewer1.CurrentPageNumber;            int targetPage = this.comBoxPages.SelectedIndex + 1;            if (soucePage != targetPage)            {                this.pdfDocumentViewer1.GoToPage(targetPage);            }        }        private void comBoxPages_Click(object sender, EventArgs e)        {        }    }}

Step3. Debug the project 

Press F5 to launch the project

Note: 1. If you click the buttons and they cannot operate the zoom functions, please check the click event of each button.

          2. When the ComboBox cannot show page number in the dropdown list, pleae check the SelectedIndexChanged event of it.


Preview

                                                                        Form1

       
                                                               Zoom Percentage
        
                                                               Zoom Dynamic

As above picture, you can easily control the whole PDF document pages by the methods that I introduced by using Spire.PDF Viewer. Spire.PDFViewer not only enables you to open a PDF document that you load from system in the code, but also allows you to open any other PDF file by clicking the Open button in Form1, and then, choose it from a dialog box. You also can choose which page you want to view or read in the dropdown list. Why not give it a try?

Page PDF Document by PDF Viewer with C#,VB.NET

Things become difficult when a PDF document has tens or hundreds of pages. It wastes much time for people to reach the page that they want to go. Especially for those products manuals or statute books, because of its unchangeable character, PDF format becomes their best choice. But at the same time, it is neither possible to finish reading in a short period nor reach the right page in seconds. While things turn very easy when you use the PDF Viewer paging function, you can get the page by choose the page number or other buttons such as First, Previous, Next, Last and so on.

In this post, I will share a method to page PDF Document by PDF Viewer with C#. VB.NET.It is very necessary for me to choose PDF Viewer compoenent Spire.PDFViewer to be my tool. So please make sure that you freely install Spire.PDFViewer on system.

Freely Download Spire.PDFViewer

How to page PDF Document by PDF Viewer with C#, VB.NET
Using Spire.PDFViewer, I only use the below steps to finish this task. Because the code is a little long, so I only use C# to page the PDF Document. Please look at the following procedure.
Step1. Create a new project.

1.     Create a new project in Windows Forms Application in Visual Studio.

2.     Choose .NET Framework 2 or above as the Target framework in Properties.

Step2.Add reference and design the Form.
1.     Add Spire.PDF DLL and Spire.PDFViewer Forms DLL as reference from your downladed Spire.PDFViewer.

2.     Add a toolScript in Form 1. And then, add five buttons and a ComboBox at the same time in toolScript dropdown list.

3.     Set the Properties of the five buttons and the ComboBox. I set the buttons' Name to be btn_Open, btn_First, btn_Previous, btn_Next, btn_Last.  The ComboBox to be comBoxPages . Also the default button DisplayStyle is "image", I change it to be "Text" and set the "Text" to be "Open, First, Previous, Next, Last"

4.     Add pdfDocumentViewer in Form1, and set its Dock in Properties to set the Form size.

Step3. Page PDF Document by PDF Viewer 

1.     Add the following using at the top of the method.

C# 
using System.IO;
using Spire.PdfViewer;
using Spire.PdfViewer.Forms;
using Spire.Pdf;

2.     Load a PDF file from system.

        private void Form1_Load(object sender, EventArgs e)
        {
            string pdfDoc = @"D:\michelle\Spire.Office.pdf";
            if (File.Exists(pdfDoc))
            {
                this.pdfDocumentViewer1.LoadFromFile(pdfDoc);
            }
        }

3.     Open a PDF Document .

       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 pdfFile = dialog.FileName;
                    this.pdfDocumentViewer1.LoadFromFile(pdfFile);
                }
            }
        }
4.   Page the PDF file by PDF Viewer.

private void btnFirst_Click(object sender, EventArgs e)
        {
            if (this.pdfDocumentViewer1.PageCount > 0)
            {
                int currentPage = this.pdfDocumentViewer1.CurrentPageNumber;
                if (currentPage != 1)
                {
                    this.pdfDocumentViewer1.GoToFirstPage();
                }
                else
                {
                    MessageBox.Show("Current page is already the first!", "Spire.PdfViewer Demo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }
        private void btnPrevious_Click(object sender, EventArgs e)
        {
            if (this.pdfDocumentViewer1.PageCount > 0)
            {
                int currentPage = this.pdfDocumentViewer1.CurrentPageNumber;
                if (currentPage > 1)
                {
                    this.pdfDocumentViewer1.GoToPreviousPage();
                }
                else
                {
                    MessageBox.Show("Current page is already the first!", "Spire.PdfViewer Demo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }
        private void btnNext_Click(object sender, EventArgs e)
        {
            if (this.pdfDocumentViewer1.PageCount > 0)
            {
                int currentPage = this.pdfDocumentViewer1.CurrentPageNumber;
                int totalPage = this.pdfDocumentViewer1.PageCount;
                if (currentPage < totalPage)
                {
                    this.pdfDocumentViewer1.GoToNextPage();
                }
                else
                {
                    MessageBox.Show("Current page is already the last!", "Spire.PdfViewer Demo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }
        private void pdfDocumentViewer1_PdfLoaded(object sender, EventArgs args)
        {
            this.comBoxPages.Items.Clear();
            int totalPage = this.pdfDocumentViewer1.PageCount;
            for (int i = 1; i <= totalPage; i++)
            {
                this.comBoxPages.Items.Add(i.ToString());
            }
            this.comBoxPages.SelectedIndex = 0;
        }
        private void comBoxPages_SelectedIndexChanged(object sender, EventArgs e)
        {
            int soucePage = this.pdfDocumentViewer1.CurrentPageNumber;
            int targetPage = this.comBoxPages.SelectedIndex + 1;
            if (soucePage != targetPage)
            {
                this.pdfDocumentViewer1.GoToPage(targetPage);
            }
        }
        private void btnLast_Click(object sender, EventArgs e)
        {
            if (this.pdfDocumentViewer1.PageCount > 0)
            {
                int currentPage = this.pdfDocumentViewer1.CurrentPageNumber;
                int totalPage = this.pdfDocumentViewer1.PageCount;
                if (currentPage != totalPage)
                {
                    this.pdfDocumentViewer1.GoToLastPage();
                }
                else
                {
                    MessageBox.Show("Current page is already the last!", "Spire.PdfViewer Demo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }
        private void pdfDocumentViewer1_PageNumberChanged(object sender, EventArgs args)
        {
            if (this.comBoxPages.Items.Count <= 0)
                return;
            if (this.pdfDocumentViewer1.CurrentPageNumber != this.comBoxPages.SelectedIndex + 1)
            {
                this.comBoxPages.SelectedIndex = this.pdfDocumentViewer1.CurrentPageNumber - 1;
            }
        }


Preview

                              
You can either click the First, Previous, Next, Last button to view the PDF file or choose the page number to directly reach the page. The Open button allows you to open another PDF Document.

Popular posts