Tuesday, September 28, 2010

Merging Pdf files in Asp.Net

Solution

1. First u need to download 'itextsharp.dll' from the internet.
2. Then add this dll using add reference.
3. Import the below name space.

Imports iTextSharp.text
Imports iTextSharp.text.pdf


4. Use the below function to merge the pdf.

Public Sub Merge()

Private ReadOnly m_documents As List(Of PdfReader)
Dim newDocument As Document = Nothing

//Merge pdf path. All the pdf files will merged here and become one pdf.
ByVal outputStream As Stream
outputStream = New FileStream(pdfMergePath, FileMode.Create)

Try
newDocument = New Document()
Dim pdfWriter__1 As PdfWriter = PdfWriter.GetInstance(newDocument, outputStream)

newDocument.Open()
newDocument.SetPageSize(PageSize.A3)

Dim pdfContentByte__2 As PdfContentByte = pdfWriter__1.DirectContentUnder

// Add all the pdf to merge
m_documents.Add(New PdfReader(pdfPath1))
m_documents.Add(New PdfReader(pdfPath2))
m_documents.Add(New PdfReader(pdfPath2))

For Each doc In m_documents
totalPages += doc.NumberOfPages
Next

Dim currentPage As Integer = 1
For Each pdfReader As PdfReader In m_documents
For page As Integer = 1 To pdfReader.NumberOfPages
newDocument.NewPage()

Dim importedPage As PdfImportedPage = pdfWriter__1.GetImportedPage(pdfReader, page)
pdfContentByte__2.AddTemplate(importedPage, 0, 0)

pdfContentByte__2.BeginText()
m_baseFont = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED)
pdfContentByte__2.SetFontAndSize(m_baseFont, 9)
pdfContentByte__2.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, String.Format("{0} of {1}", currentPage, totalPages), 520, 5, 0)
pdfContentByte__2.EndText()

currentPage = currentPage + 1
Next
Next

Finally
outputStream.Flush()
If newDocument IsNot Nothing Then
newDocument.Close()
End If
outputStream.Close()
End Try

End Sub

Browser display area or view port width & height using Javascript

Solution

The below script will help u to get the width and height of the browser view port area.

function fnScreenWidth() {
//for IE
var viewportwidth;
var viewportheight;

// the more standards compliant browsers (mozilla/netscape/opera/IE7) //use window.innerWidth and window.innerHeight

if (typeof window.innerWidth != 'undefined') {
viewportwidth = window.innerWidth;
viewportheight = window.innerHeight;
}

// IE6 in standards compliant mode (i.e. with a valid doctype as the //first line in the document)

else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0) {
viewportwidth = document.documentElement.clientWidth;
viewportheight = document.documentElement.clientHeight;
}

// older versions of IE
else {
viewportwidth = document.getElementsByTagName('body')[0].clientWidth;
viewportheight = document.getElementsByTagName('body')[0].clientHeight;
}


alert('Your viewport width is ' + viewportwidth + 'x' + viewportheight);

}


You can call this java script from body tag,

body onload="fnScreenWidth()"

Get Physical Path of Application in ASP.Net

Solution

HttpRuntime.AppDomainAppPath

This wil gets the physical drive path of the application directory for the application hosted in the current application domain.

Monday, September 27, 2010

Accessing session in a class file in Asp.Net

Solution

The Session property provides programmatic access to the properties and methods of the HttpSessionState class. Because, ASP.NET pages contain a default reference to the System.Web namespace (which contains the HttpContext class), you can reference the members of HttpContext on an .aspx page without the fully qualified class reference to HttpContext. For example, you can use just Session("SessionVariable1") to get or set the value of the session state variable SessionVariable1. However, class file will not inherit System.web namespace. So we need access like below,

HttpContext.Current.Session("Session_Name")

Password Encoding and Decoding in ASP.Net

Encoding

Public Function fnBase64Encode(ByVal strPwd As String) As String
Dim byteEncode(strPwd.Length) As Byte
Dim strEncoded As String

byteEncode = System.Text.Encoding.UTF8.GetBytes(strPwd)
strEncoded = Convert.ToBase64String(byteEncode)

Return strEncoded
End Function


Decoding

Public Function fnBase64Decode(ByVal strPwd As String) As String
Dim encoder As New System.Text.UTF8Encoding()
Dim utf8Decode As System.Text.Decoder
Dim byteDecode() As Byte
Dim intCharCnt As Integer
Dim strDecoded As String

utf8Decode = encoder.GetDecoder()
byteDecode = Convert.FromBase64String(strPwd)
intCharCnt = utf8Decode.GetCharCount(byteDecode, 0, byteDecode.Length)

Dim charDecoded(intCharCnt) As Char

utf8Decode.GetChars(byteDecode, 0, byteDecode.Length, charDecoded, 0)
strDecoded = New String(charDecoded)

Return strDecoded
End Function

The maximum report processing jobs limit configured by your system administrator has been reached.

Possible Causes

The error message appears because the web application has exceeded the Crystal Reports reporting engine default print job limit of 75. Or, in other words, a high reporting load has been placed on the reporting engine. There are a number of reasons why the report engine would suffer from high loads and each reason may have a distinct solution as documented below.

Coding Issues

Proper coding requires that any objects be managed and thus destroyed when they are no longer needed. This is true for any object that supports IDisposable interface.

Too Many Print Jobs Submitted to the Report Engine

Starting in version 10 of Crystal Reports, the reporting engine was optimized for greatest report throughput. There are specific registry keys that control this optimization. By default, the print job limit is set to 75 print jobs. When a load is placed on the application it can hit the 75 print job limit and cause the error. Note that a print job includes main reports, subreports, and in-session report objects (e.g.; drill down, paging, searching, and more.). Therefore a single report could exceed the 75 print job limit. Consider a report that returns 75 records, with a subreport placed in the detail section and thus running for each record. This report will need to run 75 subreports plus the main report, reaching 76 print jobs and the error will be thrown.

Solutions

First Delete report files in temp folder(C:\Documents and Settings\[UserName]\Local Settings\Temp) with the extension rpt.

1. Dispose un-needed objects on Page_Unload

Ensure that you call .Dispose and .Close method when the report object is no longer need. Also, ensure that this is done not only with Crystal Reports, but FileStream, Database Connections, datasets or any other object that supports IDisposable interface.

Dim cryDoc As ReportDocument

On page Unload dispose the Report Document Object.

rDoc.Close()
rDoc.Dispose()

This is the Correct Solution.


2. Increase the 75 Print Job Limit

This may stop the error from occurring temporarily but as load increases, you will find the error will start to occur again or worse yet, the system may stop responding and a system reboot will be the only way to recover. For an application using the Crystal reports SDK, the registry key “InprocServer” sets the limit for the number of print jobs allowed to be processed by the report engine.

Note: The default value of 75 is the predetermined value for best performance in tested environments. Changing this value may affect performance of the system.

To change the PrintJobLimit value, follow these steps:

1)Click Start > Run. The Run dialog box appears.
2)Type “regedit” in the Open field. Click OK. The Registry Editor appears.
Navigate to the appropriate registry key value as documented below.

Crystal Reports 10.0.x
HKEY_LOCAL_MACHINE\SOFTWARE\Crystal Decisions\10.0\Report Application Server\ InprocServer\PrintJobLimit

Crystal Reports 10.2 (Visual Studio .NET 2005 bundle)

HKEY_LOCAL_MACHINE\SOFTWARE\Crystal Decisions\10.2\Report Application Server\ InprocServer\PrintJobLimit

Crystal Reports Basic for Visual Studio 2008 (Visual Studio .NET 2008 bundle)
HKEY_LOCAL_MACHINE\SOFTWARE\Business Objects\10.5\report Application server\InProcServer\ PrintJobLimit\PrintJobLimit

Crystal Reports XI Release 1 (11.0.x)
HKEY_LOCAL_MACHINE\SOFTWARE\Business Objects\Suite 11.0\Report Application Server\InprocServer\PrintJobLimit

Crystal Reports XI Release 2 (11.5.x)
HKEY_LOCAL_MACHINE\SOFTWARE\Business Objects\Suite 11.5\Report Application Server\InprocServer\PrintJobLimit


Crystal Reports 2008 (12.x.x)

HKEY_LOCAL_MACHINE\SOFTWARE\Business Objects\Suite 12.0\Report Application Server\InprocServer\PrintJobLimit