Pages

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.

No comments:

Post a Comment