1

下午所有,

有人建议我可以使用 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>

有人可以帮我看看这个,让我知道我哪里出错了吗?

问候贝蒂

4

1 回答 1

1

不确定这是否能解决所有问题,但您至少错过了按钮中的 Click_Event。

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

(我看到你昨天问了这个问题:Button ClickEvent is not triggered with the same problem, try to remember next time;-))

于 2012-09-20T11:50:49.173 回答