เครื่องมือ 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>

ใครช่วยกรุณาดูสิ่งนี้ให้ฉันและแจ้งให้เราทราบว่าฉันไปไหนผิด?

ขอแสดงความนับถือเบ็ตตี้


person Betty B    schedule 20.09.2012    source แหล่งที่มา


คำตอบ (1)


ไม่แน่ใจว่าจะแก้ไขได้ทั้งหมดหรือไม่ แต่อย่างน้อยคุณก็ขาด Click_Event ออกจากปุ่มของคุณ

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

(และฉันเห็นว่าคุณถามคำถามนี้เมื่อวานนี้: ปุ่ม ClickEvent ไม่ถูกทริกเกอร์ ด้วย ปัญหาเดียวกัน พยายามจำครั้งต่อไป ;-))

person Thomas    schedule 20.09.2012
comment
ขอบคุณมากสำหรับการแก้ปัญหาของคุณ ฉันได้เพิ่ม OnClick=btnGeneratePDF_Click และยังเพิ่ม CausesValidation=False ให้กับปุ่มด้วย แต่ดูเหมือนว่าเหตุการณ์จะเกิดขึ้นเมื่อฉันคลิกที่นี่ แต่ฉันไม่มีอะไรสร้างขึ้นสำหรับ .PDF? - person Betty B; 20.09.2012