none
HOW TO GENERATE A PRINTABLE FORM RRS feed

  • Question

  • Hi Guys, I'm a newbie about vb.net programming. I wonder if you could help me bout this program:

    I have a listview1 in my form, If I click an item in the listview1 and click Generate Form button, A window will pop and fill up all the fields with data selected in the listview1 into a designed form slip. Then user can either print that slip or save it as pdf or image file.


    • Edited by zirj0hn Tuesday, January 16, 2018 9:08 AM
    Tuesday, January 16, 2018 9:07 AM

All replies

  • Hi,

    For your purpose, please refer to the following demo:

    Add iTextSharp reference to your project first:

    Then the code:

    Form1:

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            listView1.View = View.List
            For i As Integer = 0 To 5 - 1
                Dim lvitem As ListViewItem = New ListViewItem()
                lvitem.SubItems(0).Text = "Item" & i.ToString()
                listView1.Items.Add(lvitem)
            Next
        End Sub
    
        Private selectItem As String = ""
    
        Private Sub button1_Click(sender As Object, e As EventArgs) Handles button1.Click
            For Each itm As ListViewItem In listView1.SelectedItems
                If itm.Selected Then
                    selectItem = itm.Text
                End If
            Next
    
            Dim f2 As Form2 = New Form2(selectItem)
            f2.Show()
        End Sub
    End Class
    

    Form2:

    Imports System.IO
    Imports iTextSharp.text
    Imports iTextSharp.text.pdf
    
    Public Class Form2
        Public Sub New(ByVal text As String)
            InitializeComponent()
            item = text
        End Sub
    
        Private item As String = ""
    
        Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            dataGridView1.Columns.Add("Name", "Header")
            dataGridView1.Rows.Add()
            dataGridView1.Rows(0).Cells(0).Value = item
        End Sub
    
        Private Sub button1_Click(sender As Object, e As EventArgs) Handles button1.Click
            Dim pdfTable As PdfPTable = New PdfPTable(dataGridView1.ColumnCount)
            pdfTable.DefaultCell.Padding = 3
            pdfTable.WidthPercentage = 30
            pdfTable.HorizontalAlignment = Element.ALIGN_LEFT
            pdfTable.DefaultCell.BorderWidth = 1
            For Each column As DataGridViewColumn In dataGridView1.Columns
                Dim cell As PdfPCell = New PdfPCell(New Phrase(column.HeaderText))
                pdfTable.AddCell(cell)
            Next
    
            Dim row As Integer = dataGridView1.Rows.Count
            Dim cell2 As Integer = dataGridView1.Rows(1).Cells.Count
            For i As Integer = 0 To row - 1 - 1
                For j As Integer = 0 To cell2 - 1
                    If dataGridView1.Rows(i).Cells(j).Value Is Nothing Then
                        dataGridView1.Rows(i).Cells(j).Value = "null"
                    End If
    
                    pdfTable.AddCell(dataGridView1.Rows(i).Cells(j).Value.ToString())
                Next
            Next
    
            Dim folderPath As String = "D:\Log\"
            If Not Directory.Exists(folderPath) Then
                Directory.CreateDirectory(folderPath)
            End If
    
            Using stream As FileStream = New FileStream(folderPath & "DataGridViewExport.pdf", FileMode.Create)
                Dim pdfDoc As Document = New Document(PageSize.A2, 10.0F, 10.0F, 10.0F, 0F)
                PdfWriter.GetInstance(pdfDoc, stream)
                pdfDoc.Open()
                pdfDoc.Add(pdfTable)
                pdfDoc.Close()
                stream.Close()
            End Using
    
            MessageBox.Show("Done")
        End Sub
    End Class

    Result:

    Hope this helps!

    Regards,

    Stanly


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, January 17, 2018 2:03 AM
    Moderator
  • Hi thanks stanly. I appreciate your effort and I will take it as my reference for my future projects.

    can't post here an image to show and explain well about what im trying to say. Maybe sending you an email? Is that ok

    Thursday, January 18, 2018 4:54 AM
  • Hi,

    The above program does not meet your requirements?

    If you want to post a picture, please verify your account first:

    Verify Account 41

    Regards,

    Stanly


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, January 18, 2018 7:51 AM
    Moderator
  • hi Stan,

    When I click the Generate MRB Form button...I wanna transfer the data from the listview to a printable form like below:

    Monday, January 22, 2018 5:40 AM
  • Hi,

    I'm afraid this is similar to the demo I provided above, please refer to it to achieve your purpose by yourself.

    Regards,

    Stanly


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, January 23, 2018 3:10 AM
    Moderator
  • But you just move the data from listview to a gridview... The form I showed you is not a gridview, it's an designed rdlc. And the problem is to transafer that data from listview to rdlc field. But thank you anyways, I would use that as a reference for my future projects. Still, I really wanna know how to do that. maybe someone here would understand and solve this question. I would appreciate it and give him a hundred and one vote.. :p
    Tuesday, January 23, 2018 5:23 AM
  • Hi,

    One way would be to setup a parameter for each of the fields you want to bring in and set the value of the parameter to whatever you want in your app. In the report you would then set the value of each text box to be the parameter and it should work just fine.

    Or if you are using RDLC files (which you are) you could put your data into a dataset and pass that to the report and then have each field in the report a column in the dataset

    Refer: How to pass textbox/Combobox value to rdlc report text field?

    How to pass textbox/Combobox value to rdlc report text field?

    Regards,

    Stanly


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, January 23, 2018 6:06 AM
    Moderator
  • Hi,

    For your purpose, please refer to the following demo:

    Add iTextSharp reference to your project first:

    Then the code:

    Form1:

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            listView1.View = View.List
            For i As Integer = 0 To 5 - 1
                Dim lvitem As ListViewItem = New ListViewItem()
                lvitem.SubItems(0).Text = "Item" & i.ToString()
                listView1.Items.Add(lvitem)
            Next
        End Sub
    
        Private selectItem As String = ""
    
        Private Sub button1_Click(sender As Object, e As EventArgs) Handles button1.Click
            For Each itm As ListViewItem In listView1.SelectedItems
                If itm.Selected Then
                    selectItem = itm.Text
                End If
            Next
    
            Dim f2 As Form2 = New Form2(selectItem)
            f2.Show()
        End Sub
    End Class

    Form2:

    Imports System.IO
    Imports iTextSharp.text
    Imports iTextSharp.text.pdf
    
    Public Class Form2
        Public Sub New(ByVal text As String)
            InitializeComponent()
            item = text
        End Sub
    
        Private item As String = ""
    
        Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            dataGridView1.Columns.Add("Name", "Header")
            dataGridView1.Rows.Add()
            dataGridView1.Rows(0).Cells(0).Value = item
        End Sub
    
        Private Sub button1_Click(sender As Object, e As EventArgs) Handles button1.Click
            Dim pdfTable As PdfPTable = New PdfPTable(dataGridView1.ColumnCount)
            pdfTable.DefaultCell.Padding = 3
            pdfTable.WidthPercentage = 30
            pdfTable.HorizontalAlignment = Element.ALIGN_LEFT
            pdfTable.DefaultCell.BorderWidth = 1
            For Each column As DataGridViewColumn In dataGridView1.Columns
                Dim cell As PdfPCell = New PdfPCell(New Phrase(column.HeaderText))
                pdfTable.AddCell(cell)
            Next
    
            Dim row As Integer = dataGridView1.Rows.Count
            Dim cell2 As Integer = dataGridView1.Rows(1).Cells.Count
            For i As Integer = 0 To row - 1 - 1
                For j As Integer = 0 To cell2 - 1
                    If dataGridView1.Rows(i).Cells(j).Value Is Nothing Then
                        dataGridView1.Rows(i).Cells(j).Value = "null"
                    End If
    
                    pdfTable.AddCell(dataGridView1.Rows(i).Cells(j).Value.ToString())
                Next
            Next
    
            Dim folderPath As String = "D:\Log\"
            If Not Directory.Exists(folderPath) Then
                Directory.CreateDirectory(folderPath)
            End If
    
            Using stream As FileStream = New FileStream(folderPath & "DataGridViewExport.pdf", FileMode.Create)
                Dim pdfDoc As Document = New Document(PageSize.A2, 10.0F, 10.0F, 10.0F, 0F)
                PdfWriter.GetInstance(pdfDoc, stream)
                pdfDoc.Open()
                pdfDoc.Add(pdfTable)
                pdfDoc.Close()
                stream.Close()
            End Using
    
            MessageBox.Show("Done")
        End Sub
    End Class

    Result:

    Hope this helps!

    Regards,

    Stanly


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    off topic..how did you make that picture like animated gif with high quality. what app did you use. is it video converted to gif or something? Just curious..thanks
    Friday, May 18, 2018 1:38 AM