1

Afternoon All,

I have been advised that i can use iTextSharp to help me convert my web pages into .PDF files. I am using the following link as a sample tutorial but cannot generate the .pdf?

Visit http://www.dotnetspark.com/kb/654-simple-way-to-create-pdf-document-using.aspx

I am using the VB sample. I have added the iTextSharp.dll to my project and added the namespaces as requested. I have simply created a blank page and added a button to the page and using the following code i cant seem to get this to generate the file?

Here is my code...

Imports iTextSharp.text
Imports iTextSharp.text.pdf

Partial Class pdf
Inherits System.Web.UI.Page

Protected Sub btnGeneratePDF_Click(ByVal sender As Object, ByVal e As EventArgs)
    'Create Document class obejct and set its size to letter and give space left, right, Top, Bottom Margin
    Dim doc As New Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35)
    Try
        Dim wri As PdfWriter = PdfWriter.GetInstance(doc, New FileStream("d:\myfolder\test.pdf", FileMode.Create))
        'Open Document to write
        doc.Open()

        'Write some content
        Dim paragraph As New Paragraph("This is my first line using Paragraph.")
        Dim pharse As New Phrase("This is my second line using Pharse.")
        Dim chunk As New Chunk(" This is my third line using Chunk.")
        ' Now add the above created text using different class object to our pdf document
        doc.Add(paragraph)
        doc.Add(pharse)
        doc.Add(chunk)
    Catch dex As DocumentException


        'Handle document exception
    Catch ioex As IOException
        'Handle IO exception
    Catch ex As Exception
        'Handle Other Exception
    Finally
        'Close document
        doc.Close()
    End Try
End Sub

End Class

Here is the code for the button...

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="pdf.aspx.vb" Inherits="pdf" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>

    <asp:Button ID="btnGeneratePDF" runat="server" Text="Generate .PDF" />

</div>
</form>
</body>
</html>

Could someone please take a look at this for me and let me know where i am going wrong?

Regards Betty

Betty B
  • 185
  • 1
  • 4
  • 20

1 Answers1

1

Not sure if this will fix it all, but you're at least missing the Click_Event from your buttton.

<asp:Button ID="btnGeneratePDF" runat="server" Text="Generate .PDF" OnClick="btnGeneratePDF_Click" />

(And I see that you asked this question yesterday: Button ClickEvent is not triggered with the same problem, try to remember next time ;-))

Community
  • 1
  • 1
Thomas
  • 1,563
  • 3
  • 17
  • 37
  • Many thanks for your solution. I have added the OnClick="btnGeneratePDF_Click" and also added CausesValidation="False" to the button but it looks like the event is triggered when i click this but i i have nothing created for the .PDF? – Betty B Sep 20 '12 at 12:28