2012-09-20 117 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創建任何內容? –