Pages

Showing posts with label Techniques and Logics. Show all posts
Showing posts with label Techniques and Logics. Show all posts

Monday, July 20, 2009

How to create PDF document using PDFDoc Scout library in ASP.NET

This page contains step by step tutorial how to create PDF document in ASP.NET using PDFDoc Scout library.

IMPORTANT NOTE: To use PDFDoc Scout library on web-server you have to have additional "Web License"
PDFDoc Scout library is capable of generating of in-memory PDF files so file needn't to be stored as a file on hard drive and can be streamed right into the browser window.
There is a special "GenerateInMemoryFile" property for such purposes. Set this property to TRUE and the library will generate and keep your PDF as in-memory stream without using of any temporary files.
1) Install PDFDoc Scout library on your computer and run Visual Studio.NET
2) Go to File menu and select New Project:
New project menu
Select ASP.NET Web Application project type and click OK
ASP.NET new project wizard
3) Visual Studio.NET will create new empty ASP.NET project. Double-click on the empty space of the form:
New blank project generated by ASP.NET
This will open source code editor window on procedure handling Page_Load event. We will place our code for PDF PDF animation generation into this procedure:
Page load handler procedure generated by ASP.NET IDE
4) Use the following code for procedure (you can simply copy and paste this code from this page into ASP.NET source code editor window):

'Put user code to initialize the page here
Dim PDFDoc
Dim Size As Long
Dim MemoryImage As System.Array
' create new PDFDoc object
PDFDoc = CreateObject("PDFDocScout.PDFDocument")
' initalize library
PDFDoc.InitLibrary("demo", "demo")
' set in-memory mode
PDFDoc.GenerateInMemoryFile = true ' set to True to generate PDF document in memory without any files on disk to output it to end-user to browser
' starts PDF document generation
PDFDoc.BeginDocument ' start PDF document generation

' add text to current page
PDFDoc.Page.AddText "Hello, World!", 100, 100, 15

PDFDoc.EndDocument ' close PDF document generation

' get size of generated in-memory PDF document
Size = PDFDoc.BinaryImageSize
' create new buffer with size equal to generated pdf document file
Dim Buffer(CInt(Size)) As Byte
' get in-memory pdf file as byte stream
MemoryImage = PDFDoc.BinaryImage
' copy byte stream into buffer
Array.Copy(MemoryImage, Buffer, Size)
' clear http output
Response.Clear()
' set the content type to PDF
Response.ContentType = "application/pdf"
' add content type header
Response.AddHeader("Content-Type", "application/pdf")
' set the content disposition
Response.AddHeader("Content-Disposition", "inline;filename=helloworld.pdf")
' write the buffer with pdf file to the output
Response.BinaryWrite(Buffer)
Response.End()
' set library object instance to Nothing
PDFDoc = Nothing

5) Now run ASP.NET project using Debug | Start command:
Start project menu
Visual Studio.NET will run ASP.NET project on web-server and you will see Internet Explorer window with generated PDF document:
PDF document generated by ASP.NET application
Click here to download the source code of this example.

Saturday, June 27, 2009

Setting the Value of a TextBox with TextMode=Password

When the TextMode property of an ASP.NET TextBox is set to Password the value set in the Text property will not display at runtime. This can be a pain, however it is actually by design to prevent the unmasked password from being displayed in the HTML source of the page.

While the security reasons are good to not display the masked password value, leaving it unmasked in the source, it is also necessary at times to display the masked value in the TextBox. For example, a user profile page where the user has the ability to change their password. It makes sense to display it there. After all, the user has already authenticated to get to the page (although the value is sent with the data to the browser and could easily be sniffed).

Security reasons aside, you can work around this by adding the password value to the control as as Attribute. Since the TextBox renders as an HTML input control, you can set the value attribute easily, just as you would set the Text property.

PasswordText.Attributes.Add("value", "ThePassword");

Use this to set the value, instead of setting the Text property. You can still read the value from the control via the Text property.

Saturday, June 20, 2009

Accessing the Html Header in ASP.NET 2.0

Taking a look at the Page class there is a Header property that looks tempting to be able to do something like dynamically add a stylesheet. The problem is that when you type in Page.Header it doesn't appear that you have full control over the header, even though there is the ever so tempting System.Web.UI.HtmlControls.HtmlHead class. What I had been doing is throwing an id and a runat="server" onto the head tag in my HTML, which I really didn't like because an id tag on the head tag isn't valid XHTML 1.1.

Today, a break through. This has always bothered me, so I took a deeper look and it turns out that Page.Header is defined as IPageHeader. Perhaps, just maybe I could cast Page.Header into System.Web.UI.HtmlControls.HtmlHead. And sure enough, it worked great. Anyways, I feel a little silly about not figuring that out sooner but now that I've gotten this working I feel much better. Anyways, here's some example code to add a stylesheet to a page:

Dim header As Web.UI.HtmlControls.HtmlHead
header = TryCast(Me.Page.Header, Web.UI.HtmlControls.HtmlHead)
If header IsNot Nothing Then
Dim link As New HtmlLink
link.Attributes.Add("href", "~/whatever.css")
link.Attributes.Add("media", "screen")
link.Attributes.Add("rel", "stylesheet")
link.Attributes.Add("type", "text/css")
header.Controls.Add(link)
End If

Wednesday, June 10, 2009

GridView Paging using ASP.NET AJAX Slider Extender

As given in the ASP.NET AJAX Toolkit documentation “The Slider extender allows to upgrade an asp:TextBox to a graphical slider that allows the user to choose a numeric value from a finite range.” In this article, we will explore how to implement paging in an ASP.NET GridView using an ASP.NET AJAX SliderExtender.
Note: Visual Studio 2008 is using and thereby utilizing the ASP.NET AJAX plumbing that comes along with it.

Click here for details .......

Saturday, June 6, 2009

Checking Link Validity with ASP.NET

If you have an index of links, it can be important to check that a link really exists before showing it off on your site. Here's a way to do it.

This is really not a difficult proposition. Think about it - all you have to do is try to reach the page, and if you can't reach it, chances are it's not there. ASP.NET provides plenty of network functionality as part of the framework - this is one of the things the framework was designed for, after all - network computing - so why not?

In this case, the class we need to look at is the WebRequest class, a rather aptly named class which allows you to request documents over HTTP or HTTPS. The ASP.NET documentation (which you shoudl have handy, if you don't want to recieve everyone's wrath online) explains it quite well, but here's my example.

First, we set up a little form into which one can enter a URL. Let's pretend this is part of my 'links' section on this site. Into this textbox goes the URL, and the form gets submitted. Then, on submission, we simply request the page. Now, if it's successful, there's all manner of things you can do. You could use Regular expression to strip out the title tag and meta tags (this is explained in the ASP section), you could cache the page for later. You could pull all the text and perform analysis on it, like a mini-google to rank the page based on its content. Of course, if the requestisn't successful, then you can reject the link outright, and save yourself from the old dead-link syndrome.

Here's the simple checkURL function which is called on submission.

void checkURL(Object o, EventArgs e)
{

pnlDone.Visible = true;
pnlStart.Visible = false;
WebRequest objRequest = WebRequest.Create(strURL.Value);
lblExists.Text = "Link unchecked";
try
{
WebResponse objResponse = objRequest.GetResponse();
lblExists.Text = "Link exists";
objResponse.Close();
}
catch(WebException ex)
{
lblExists.Text = "Link doesn't exist ";
}
}

Things to note. The first thing to note is that my form is in one asp:panel, and the results are displayed in another, hence I do the show/hide shuffle at the start. Then I request the URL in a try/catch block. If the page is not found, the code will throw a WebException, which will usually in this context be a ProtocolError exception, though we're not concerned with that in this simple code. IF the exception is thrown, we tell the user. If it's not, well, we can go on to add the link to our database, carrying out whichever operations we want to on the way. Very simple, eh?

This article was inspired by a recent thread on ASP.NET, as a number of coming articles will be. These articles won't be long on content, but you can be sure the answers will be relevant to real-world problems, so keep checking back for some solutions inspired by real people.

How To Generate PDF Files Dynamically Using ASP.NET

There are currently many ways to generate PDF files dynamically. The most widely known way is to use ASP together with Acrobat Full Version (4.0 or 5.0) and Acrobat FDF Toolkit.

With Microsoft .NET many developers are wondering about how to use ASP.NET instead of ASP to tackle this same situation. To my surprise I could not find any related documentation from Adobe.

I asked this question in many forums and no one had an answer for me. I had to roll up my sleeves and to my surprise--it's not so difficult. Now, I will make it even easier for all of you.

Click here for details ......

Wednesday, May 27, 2009

Simple ASP.NET 2.0 Tips and Tricks that You May (or may not) have Heard About

ASP.NET 2.0 is an awesome framework for developing Web applications. If you've worked with it for awhile then that's no secret. It offers some great new features that you can implement with a minimal amount of code. I wanted to start a list of some of the most simple (yet cool) things you could do with it that required little or no C#/VB.NET code. If you have other suggestions add a comment and I'll update the list if the suggestion is a simple task that can be applied easily.

Click here for details ......

Tip/Trick: Url Rewriting with ASP.NET

People often ask for guidance on how they can dynamically "re-write" URLs and/or have the ability to publish cleaner URL end-points within their ASP.NET web applications.

This post post summarizes a few approaches you can take to cleanly map or rewrite URLs with ASP.NET, and have the option to structure the URLs of your application however you want.

Click here for details ......

Sunday, May 10, 2009

Convert an Excel XLS file to PDF

There are many times when developer needs to convert their files into the another format like excel, pdf, xml, word, etc. The following tip would let you find out that how to convert an excel file into the pdf format. See the below code:

C#

// Saving an XLS file in Aspose.Pdf xml format
Workbook wb = new Workbook();
wb.Open("C:\\book1.xls);
wb.Save("C:\\xls2pdf.xml", FileFormatType.AsposePdf);

// Converting XLS file to PDF through Aspose.Pdf using Aspose.Pdf xml file as a medium
Aspose.Pdf.Pdf pdf = new Aspose.Pdf.Pdf();
pdf.BindXML("c:\\xls2pdf.xml", null);
pdf.Save("C:\\xls2pdf.pdf");

VB.NET

' Saving an XLS file in Aspose.Pdf xml format
Dim wb as Workbook = new Workbook()
wb.Open("C:\book1.xls)
wb.Save("C:\xls2pdf.xml", FileFormatType.AsposePdf)

' Converting XLS file to PDF through Aspose.Pdf using Aspose.Pdf xml file as a medium
Dim pdf as Aspose.Pdf.Pdf = new Aspose.Pdf.Pdf()
pdf.BindXML("c:\\xls2pdf.xml", null)
pdf.Save("C:\xls2pdf.pdf")

Saturday, April 18, 2009

Executing ClientScript Before and After an Asynchronous PostBack using ASP.NET AJAX

A user recently mailed me to find out if there is a way to determine, when an asynchronous postback begins and ends in an ASP.NET AJAX page. He wanted to fire some JavaScript code during these events. Here’s how to determine the events.

Click here for details .....

Friday, April 17, 2009

Refactor Your Code

Refactoring is a technique to change the existing ugly code and make it beautiful without changing the workings of the code.

In short, making the code suck less! This website RefactorCode is developed to help developers refactor the code, therefore improving programming skills and overcome many challenges one faces when coding. You can submit your ugly code and other developers can find ways to improve it. Presenting to you, your very own refractor code station!

so HURRY UP!!! and Click Here