【问题标题】:iTextSharp .pdf tooliTextSharp .pdf 工具
【发布时间】:2012-09-12 18:39:19
【问题描述】:

下午,

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

谁能帮我看看这个,让我知道我哪里出错了?

问候 贝蒂

【问题讨论】:

    标签: asp.net visual-studio-2010 itextsharp


    【解决方案1】:

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

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

    (我看到你昨天问了这个问题:Button ClickEvent is not triggered 有同样的问题,下次尝试记住;-))

    【讨论】:

    • 非常感谢您的解决方案。我已经添加了 OnClick="btnGeneratePDF_Click" 并且还添加了 CausesValidation="False" 到按钮,但是当我单击它时似乎触发了该事件,但我没有为 .PDF 创建任何内容?
    猜你喜欢
    • 2013-05-20
    • 2014-11-30
    • 2018-05-28
    • 2011-07-27
    • 2011-02-18
    • 1970-01-01
    • 1970-01-01
    • 2017-02-07
    相关资源
    最近更新 更多