Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
261 views
in Technique[技术] by (71.8m points)

vb.net - Auto populate sticker and print multiple

I have a table of information that corresponds to the details I would like to print out on a generic sticker. So lets say the sticker has Name, Last Name, Employee Number and my table has 10 rows with indicating there are 10 employees. I will open the dialog page for print and i wasnt to select the printer, after the printer is selected, i want it to take the saved values from each row print a label with each employees information.

Currently I have programmed just regular printing and creating a table of information. I am just wondering how the print loop would go, i think it should be something like this:

   Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click 
    PrintDialog1.Document = PrintDocument1

    PrintDialog1.PrinterSettings = PrintDocument1.PrinterSettings



    If PrintDialog1.ShowDialog = DialogResult.OK Then

        PrintDocument1.DefaultPageSettings.Landscape = True
        For Each row In Table1
            Employeename.Text = NameRow
            Employeelastname.text = LastNameRow
            EnployeeNumber.text = EmployeeNumberCell
            PrintDocument1.Print()
        Next

    End If
End Sub
question from:https://stackoverflow.com/questions/65650155/auto-populate-sticker-and-print-multiple

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As the name suggests, the PrintPage event of the PrintDocument prints a page. If you want to print multiple pages, you need to raise that event multiple times. You do that by setting the e.HasMorePages property to True in the event handler when there are more pages to print. Here's a simple example of printing one record per page from a DataTable:

'The data to be printed.
Private table As DataTable

'The index of the row to print.
Private rowIndex As Integer

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    'Start printing at the first row.
    rowIndex = 0

    'Print the data.
    PrintDocument1.Print()
End Sub

Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    'Get the current row.
    Dim row = table.Rows(rowIndex)

    'Print the current row.
    e.Graphics.DrawString(row.Field(Of String)("SomeColumn"), Me.Font, Brushes.Black, 50, 50)

    rowIndex += 1

    'Print another page if and only if there is another row to be printed.
    e.HasMorePages = rowIndex < table.Rows.Count
End Sub

You can modify the actual printing code in the PrintPage event handler to print whatever data you want in whatever layout you want. If you have multiple values to print then you make multiple calls to e.Graphics.DrawX methods. If it's all text you want to print then it will be all DrawString calls, but you have DrawImage for printing images and DrawRectangle, etc, for printing shapes.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...