2012-09-20 85 views
1

下午所有,iTextSharp .pdf工具

我被告知我可以使用iTextSharp帮助我将我的网页转换为.PDF文件。我正在使用以下链接作为示例教程,但无法生成.pdf?

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

我现在用的是VB样品访问。我已经将iTextSharp.dll添加到我的项目中并按要求添加了命名空间。我简单地创建了一个空白页面,并在页面上添加了一个按钮,并使用下面的代码,我似乎无法得到这个来生成文件?

这里是我的代码...

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 

末级

下面是按钮的代码...

<%@ 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> 

可能有人请看看本作我让我知道我要去哪里错了?

问候 贝蒂

回答

1

不知道这是否会解决这一切,但你至少漏掉您的按钮移到了Click_Event。

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

(我看到你昨天问这个问题:Button ClickEvent is not triggered同样的问题,要尽量记住下一次;-))

+0

您的解决方案非常感谢。我已经添加了OnClick =“btnGeneratePDF_Click”,并且还添加了CausesValidation =“False”按钮,但是看起来事件是在我点击这个时触发的,但是我没有为.PDF创建任何内容? –