我有以下代码,我想使用iTextSharp创建PDF 如何在不创建数据库的情况下这样做(因为datagridview中每行的代码都不同)?
Option Strict On
Option Explicit On
Public Class Form1
Dim counter As Integer
Dim Total As String
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim List = New List(Of Test)
Try
'
' Fill in the data grid with a List
'
'
' code for First Row
'
Dim Bonus = (CDbl(ComboBox2.SelectedItem) * CDbl(TextBox2.Text) * 0.001)
Dim SumAssured = TextBox2.Text
Dim NormalCover = CDbl(TextBox2.Text) + Bonus
Dim DAB = (CDbl(CStr(TextBox2.Text)) * 2)
Dim Premium = TextBox4.Text
Dim Tax = (CDbl(TextBox4.Text) * CDec(ComboBox1.SelectedItem) * 0.01)
Dim Net_outgo = CDbl(CDbl(TextBox4.Text) - Tax)
Dim AccBenefit = DAB + Bonus
Dim Return_From_LIC = CStr(0)
List.Add(New Test(TextBox1.Text, CStr(NormalCover), CStr(AccBenefit), Premium, CStr(Tax), CStr(Net_outgo), CStr(Return_From_LIC)))
'
' code for Second to second Last Row
'
For Me.counter = 0 To CInt((CDbl(TextBox3.Text) - 2))
TextBox1.Text = CStr(CDbl(TextBox1.Text) + 1)
NormalCover = CDbl(CStr(NormalCover + Bonus))
AccBenefit = CDbl(CStr(AccBenefit + Bonus))
Premium = TextBox4.Text
Tax = CDbl(TextBox4.Text) * CDec(ComboBox1.SelectedItem) * 0.01
Net_outgo = CDbl(CDbl(TextBox4.Text) - Tax)
Return_From_LIC = CStr(0)
List.Add(New Test(TextBox1.Text, CStr(NormalCover), CStr(AccBenefit), Premium, CStr(Tax), CStr(Net_outgo), CStr(Return_From_LIC)))
Next
'
' Code of Last column
'
TextBox1.Text = CStr(CDbl(TextBox1.Text) + 1)
NormalCover = CDbl(CStr(NormalCover + Bonus))
AccBenefit = CDbl(CStr(AccBenefit + Bonus))
Premium = CStr(0)
Tax = CDbl(CStr(0))
Net_outgo = CDbl(CStr(0))
Return_From_LIC = CStr(CDbl(TextBox2.Text) + Bonus * CDbl(TextBox3.Text))
List.Add(New Test(TextBox1.Text, CStr(NormalCover), CStr(AccBenefit), Premium, CStr(Tax), CStr(Net_outgo), CStr(Return_From_LIC)))
'
' Code of Summary column
'
TextBox1.Text = "Total"
NormalCover = Nothing
AccBenefit = Nothing
Premium = CStr(CDbl(TextBox4.Text) * CDbl(TextBox3.Text))
Tax = CDbl(TextBox4.Text) * CDec(ComboBox1.SelectedItem) * 0.01 * CDbl(TextBox3.Text)
Net_outgo = CDbl(CDbl(CDbl(TextBox4.Text) - (CDbl(TextBox4.Text) * CDec(ComboBox1.SelectedItem) * 0.01)) * CDbl(TextBox3.Text))
Return_From_LIC = CStr("-")
List.Add(New Test(TextBox1.Text, CStr(NormalCover), CStr(AccBenefit), Premium, CStr(Tax), CStr(Net_outgo), CStr(Return_From_LIC)))
DataGridView1.DataSource = List
TextBox1.Text = Nothing
TextBox2.Text = Nothing
TextBox3.Text = Nothing
TextBox4.Text = Nothing
ComboBox1.SelectedItem = Nothing
ComboBox2.SelectedItem = Nothing
Catch ex As Exception
MsgBox("Check Your Input Values")
End Try
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
'
' Fill in the data grid on form load.
'
Me.DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
End Sub
End Class
''' <summary>
''' This class contains Seven properties.
''' </summary>
Public Class Test
Public Sub New(ByVal Age As String, ByVal Normal_Risk_Cover As String, _
ByVal Accidental_Risk_Cover As String, ByVal Annual_Premium As String, _
ByVal Tax_Rebate As String, ByVal Net_Outgo As String, ByVal Return_From_LIC As String)
_Age = Age
_Normal_Risk_Cover = Normal_Risk_Cover
_Accidental_Risk_Cover = Accidental_Risk_Cover
_Annual_Premium = Annual_Premium
_Tax_Rebate = Tax_Rebate
_Net_Outgo = Net_Outgo
_Return_From_LIC = Return_From_LIC
End Sub
Private _Age As String
Public Property Age() As String
Get
Return _Age
End Get
Set(ByVal value As String)
_Age = value
End Set
End Property
Private _Normal_Risk_Cover As String
Public Property Normal_Risk_Cover() As String
Get
Return _Normal_Risk_Cover
End Get
Set(ByVal value As String)
_Normal_Risk_Cover = value
End Set
End Property
Private _Accidental_Risk_Cover As String
Public Property Accidental_Risk_Cover() As String
Get
Return _Accidental_Risk_Cover
End Get
Set(ByVal value As String)
_Accidental_Risk_Cover = value
End Set
End Property
Private _Annual_Premium As String
Public Property Annual_Premium() As String
Get
Return _Annual_Premium
End Get
Set(ByVal value As String)
_Annual_Premium = value
End Set
End Property
Private _Tax_Rebate As String
Public Property Tax_Rebate() As String
Get
Return _Tax_Rebate
End Get
Set(ByVal value As String)
_Tax_Rebate = value
End Set
End Property
Private _Net_Outgo As String
Public Property Net_Outgo() As String
Get
Return _Net_Outgo
End Get
Set(ByVal value As String)
_Net_Outgo = value
End Set
End Property
Private _Return_From_LIC As String
Public Property Return_From_LIC() As String
Get
Return _Return_From_LIC
End Get
Set(ByVal value As String)
_Return_From_LIC = value
End Set
End Property
End Class
答案 0 :(得分:0)
我能够解决我的问题我在我的代码中做了一点改动,现在没有创建数据库我可以为其他人创建pdf文件,知道我提交下面的代码供其他人学习,如果需要可以进行更多改进
Option Strict On
Option Explicit On
Imports System
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports System.IO
Public Class Form1
Dim dsPeople As New DataSet("People")
Dim dt As New DataTable
Dim counter As Integer
Dim Total As String
Dim TrueTypeFont As Object
Private Sub Button1_Click(sender As System.Object, e As
System.EventArgs) Handles Button1.Click
Try
'
' code for First Row
'
Dim Bonus = (CDbl(ComboBox2.SelectedItem) * CDbl(TextBox2.Text) *
0.001)
Dim SumAssured = TextBox2.Text
Dim NormalCover = CDbl(TextBox2.Text) + Bonus
Dim DAB = (CDbl(CStr(TextBox2.Text)) * 2)
Dim Premium = TextBox4.Text
Dim Tax = (CDbl(TextBox4.Text) * CDec(ComboBox1.SelectedItem) *
0.01)
Dim Net_outgo = CDbl(CDbl(TextBox4.Text) - Tax)
Dim AccBenefit = DAB + Bonus
Dim Return_From_LIC = CStr(0)
dt.Rows.Add(New Object() {TextBox1.Text, CStr(NormalCover), CStr
(AccBenefit), Premium, CStr(Tax), CStr(Net_outgo), CStr(Return_From_LIC)})
'
' code for Second to second Last Row
'
For Me.counter = 0 To CInt((CDbl(TextBox3.Text) - 2))
TextBox1.Text = CStr(CDbl(TextBox1.Text) + 1)
NormalCover = CDbl(CStr(NormalCover + Bonus))
AccBenefit = CDbl(CStr(AccBenefit + Bonus))
Premium = TextBox4.Text
Tax = CDbl(TextBox4.Text) * CDec(ComboBox1.SelectedItem) * 0.01
Net_outgo = CDbl(CDbl(TextBox4.Text) - Tax)
Return_From_LIC = CStr(0)
dt.Rows.Add(New Object() {TextBox1.Text, CStr(NormalCover), CStr
(AccBenefit), Premium, CStr(Tax), CStr(Net_outgo), CStr(Return_From_LIC)})
Next
'
' Code of Last column
'
TextBox1.Text = CStr(CDbl(TextBox1.Text) + 1)
NormalCover = CDbl(CStr(NormalCover + Bonus))
AccBenefit = CDbl(CStr(AccBenefit + Bonus))
Premium = CStr(0)
Tax = CDbl(CStr(0))
Net_outgo = CDbl(CStr(0))
Return_From_LIC = CStr(CDbl(TextBox2.Text) + Bonus * CDbl
(TextBox3.Text))
dt.Rows.Add(New Object() {TextBox1.Text, CStr(NormalCover), CStr
(AccBenefit), Premium, CStr(Tax), CStr(Net_outgo), CStr(Return_From_LIC)})
'
' Code of Summary column
'
TextBox1.Text = "Total"
NormalCover = Nothing
AccBenefit = Nothing
Premium = CStr(CDbl(TextBox4.Text) * CDbl(TextBox3.Text))
Tax = CDbl(TextBox4.Text) * CDec(ComboBox1.SelectedItem) * 0.01 *
CDbl(TextBox3.Text)
Net_outgo = CDbl(CDbl(CDbl(TextBox4.Text) - (CDbl(TextBox4.Text)*
CDec(ComboBox1.SelectedItem) * 0.01)) * CDbl(TextBox3.Text))
Return_From_LIC = CStr("-")
dt.Rows.Add(New Object() {TextBox1.Text, CStr(NormalCover), CStr
(AccBenefit), Premium, CStr(Tax), CStr(Net_outgo), CStr(Return_From_LIC)})
DataGridView1.DataSource = dt
TextBox1.Text = Nothing
TextBox2.Text = Nothing
TextBox3.Text = Nothing
TextBox4.Text = Nothing
ComboBox1.SelectedItem = Nothing
ComboBox2.SelectedItem = Nothing
Catch ex As Exception
MsgBox("Check Your Input Values")
End Try
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs)
Handles MyBase.Load
'
' Fill in the data grid on form load.
'
'GetCustomers()
dt = dsPeople.Tables.Add("dtPeople")
'create table columns
dt.Columns.Add("Age", GetType(String))
dt.Columns.Add("Natural Cover", GetType(String))
dt.Columns.Add("Accidental Cover", GetType(String))
dt.Columns.Add("Premium", GetType(String))
dt.Columns.Add("Tax Rebate", GetType(String))
dt.Columns.Add("Net Outgo", GetType(String))
dt.Columns.Add("Return From LIC", GetType(String))
Me.DataGridView1.AutoSizeColumnsMode =
DataGridViewAutoSizeColumnsMode.AllCells
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs)
Handles Button2.Click
Dim bHasErrors As Boolean = False
Dim ErrorMessage As String = ""
Dim FileName As String = System.IO.Path.Combine
(AppDomain.CurrentDomain.BaseDirectory, "Customers.pdf")
Dim Document As Document = New
Document '(iTextSharp.text.PageSize.LETTER, 50, 10, 10, 10)
Try
PdfWriter.GetInstance(Document, New System.IO.FileStream(FileName,
System.IO.FileMode.Create))
Document.Open()
Document.NewPage()
Document.Add(New Paragraph("Hello World", FontFactory.GetFont
("Arial", 20, BaseColor.BLACK)))
Dim ch As New Chunk("Your Policy Presentation ", FontFactory.GetFont
("Arial", 15, BaseColor.BLACK))
Document.Add(ch)
Dim aTable As PdfPTable
aTable = New PdfPTable(dt.Columns.Count)
For Each col As DataColumn In dt.Columns
aTable.AddCell(col.ColumnName)
Next
Document.Add(aTable)
For Each row As DataRow In dt.Rows
aTable = New PdfPTable(dt.Columns.Count)
aTable.AddCell(row.Field(Of String)("Age"))
aTable.AddCell(row.Field(Of String)("Natural Cover"))
aTable.AddCell(row.Field(Of String)("Accidental Cover"))
aTable.AddCell(row.Field(Of String)("Premium"))
aTable.AddCell(row.Field(Of String)("Tax Rebate"))
aTable.AddCell(row.Field(Of String)("Net Outgo"))
aTable.AddCell(row.Field(Of String)("Return From LIC"))
'aTable.AddCell(row.Field(Of String)("Identifier"))
Document.Add(aTable)
Next
Catch de As DocumentException
bHasErrors = True
ErrorMessage = de.Message
Catch ioe As System.IO.IOException
bHasErrors = True
ErrorMessage = ioe.Message
End Try
Document.Close()
If bHasErrors Then
MessageBox.Show("Failed to create document" & Environment.NewLine &
ErrorMessage)
Else
MessageBox.Show("The document" & Environment.NewLine & FileName &
Environment.NewLine & "has been created")
End If
Process.Start(System.IO.Path.Combine
(AppDomain.CurrentDomain.BaseDirectory, "Customers.pdf"))
End Sub
End Class