首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iTextSharp .pdf工具

iTextSharp .pdf工具
EN

Stack Overflow用户
提问于 2012-09-20 19:45:01
回答 1查看 761关注 0票数 1

下午所有人,

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

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

我使用的是VB示例。我已经将iTextSharp.dll添加到我的项目中,并根据请求添加了名称空间。我只是创建了一个空白页面,并在页面上添加了一个按钮,使用下面的代码,我似乎不能获得这个来生成文件?

这是我的代码。

代码语言:javascript
复制
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

结束类

这是按钮的代码...

代码语言:javascript
复制
<%@ 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>

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

问候贝蒂

EN

回答 1

Stack Overflow用户

发布于 2012-09-20 19:50:49

我不确定这是否能解决所有问题,但至少你会错过按钮上的Click_Event。

代码语言:javascript
复制
<asp:Button ID="btnGeneratePDF" runat="server" Text="Generate .PDF" OnClick="btnGeneratePDF_Click" />

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12511912

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档