Our first application will create a one-page PDF document, set
its Title and Creator properties, and draw the phrase
"Hello, World!" in large Helvetica letters across the page.
C# |
<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Import Namespace="Persits.PDF" %>
<script runat="server" language="c#">
void Page_Load(Object Source, EventArgs E)
{
// Create instance of the PDF manager.
PdfManager objPDF = new PdfManager();
// Create new document.
PdfDocument objDoc = objPDF.CreateDocument();
// Specify title and creator for the document.
objDoc.Title = "AspPDF Chapter 3 Hello World Sample";
objDoc.Creator = "John Smith";
// Add a page to document.
PdfPage objPage = objDoc.Pages.Add();
// Obtain font.
PdfFont objFont = objDoc.Fonts["Helvetica"];
// Draw text using this parameter string:
string strParams = "x=0; y=650; width=612; alignment=center; size=50";
objPage.Canvas.DrawText("Hello World!", strParams, objFont);
// Save, generate unique file name to avoid overwriting existing file.
string strFilename = objDoc.Save(Server.MapPath("hello.pdf"), false);
txtResult.Text = "Success! Download your PDF file <A TARGET=_new HREF=" + strFilename + ">here</A>";
}
</script>
<html>
<body>
<form id="form1" runat="server">
<asp:Label ID="txtResult" runat="server"/>
</form>
</body>
</html>
|
VB.NET |
<%@ Page Language="vb" AutoEventWireup="true" %>
<%@ Import Namespace="Persits.PDF" %>
<script runat="server" language="vb">
Sub Page_Load(Source As Object, E As EventArgs)
' Create instance of the PDF manager.
Dim objPDF As PdfManager = New PdfManager()
' Create new document.
Dim objDoc As PdfDocument = objPDF.CreateDocument()
' Specify title and creator for the document.
objDoc.Title = "AspPDF Chapter 3 Hello World Sample"
objDoc.Creator = "John Smith"
' Add a page to document.
Dim objPage As PdfPage = objDoc.Pages.Add()
' Obtain font.
Dim objFont As PdfFont = objDoc.Fonts("Helvetica")
' Draw text using this parameter string:
Dim strParams As string = "x=0; y=650; width=612; alignment=center; size=50"
objPage.Canvas.DrawText("Hello World!", strParams, objFont)
' Save, generate unique file name to avoid overwriting existing file.
Dim strFilename As string = objDoc.Save(Server.MapPath("hello.pdf"), False)
txtResult.Text = "Success! Download your PDF file <A TARGET=_new HREF=" + strFilename + ">here</A>"
End Sub
</script>
<html>
<body>
<form id="form1" runat="server">
<asp:Label ID="txtResult" runat="server"/>
</form>
</body>
</html>
|
The first line creates an instance of the PdfManager object,
AspPDF.NET's top-level object. The following line creates an empty PdfDocument
object:
PdfDocument objDoc = objPDF.CreateDocument();
The following two lines set the document's Title and Creator:
objDoc.Title = "AspPDF Chapter 3 Hello World Sample";
objDoc.Creator = "John Smith";
These values can be viewed
in Acrobat Reader under File/Document Properties/Summary. Other document properties
that can be set via the PdfDocument object include Subject, Author, Keywords,
Producer, CreationDate and ModDate.
PdfPage objPage = objDoc.Pages.Add();
The line above adds a new page to the document. There are 4 overloaded versions of the PdfPages.Add method.
The version with no arguments (used in this sample) adds a page
with the Width and Height set to 612 and 792 user units, respectively,
which corresponds to the standard 8.5" x 11" size. By default, the page is added to the end of the document.
The other overloaded versions of Add allow you to specify the custom page Width/Height and/or position
of the new page within the document.
PdfFont objFont = objDoc.Fonts["Helvetica"];
The line above queries the objDoc.Fonts collection for a desired font. The
indexer method of the PdfFonts collection expects a font name
as the argument.
Font management is described in detail in Chapter 6.
string strParams = "x=0; y=650; width=612; alignment=center; size=50";
objPage.Canvas.DrawText( "Hello World!", strParams, objFont );
The two lines above display the text "Hello World!" in size 50 Helvetica on the page.
(The auxiliary string variable Params is used for readability purposes only.
The second argument to Canvas.DrawText could also be an
initialized PdfParam object.)
Beside the text and font arguments, five numeric arguments are being passed
to the DrawText method. X and Y are always required, the others are
optional in most cases. In this case, text alignment is set to center
which makes the Width parameter required as well.
string strFilename = objDoc.Save( Server.MapPath("hello.pdf"), false );
The line above saves the document to disk. There are two overloaded versions of the Save method,
both requiring a full file path to be passed as the 1st argument.
The second Boolean argument is expected by one of the overloaded versions but not the other.
If set to True (or not specified at all) it instructs the Save method to overwrite an existing file.
If set to False, it forces unique file name generation. For example, if the file
hello.pdf already exists in the specified directory and another document
is being saved under the same name, the Save method tries the filenames
hello(1).pdf, hello(2).pdf, etc., until a non-existent name is found.
The Save method returns the filename (without the path) under which the file was
saved.
Click the links below to run this code sample:
http://localhost/asppdf.net/manual_03/03_hello.cs.aspx
http://localhost/asppdf.net/manual_03/03_hello.vb.aspx