I would like to ask on how to put a border in print preview and it will also put the label value inside the border please see attached print preview screenshot thank you. Private Sub PrintDocument1_PrintPage(sender As Object, e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
print preview screenshot
here is my code:
Private Sub PrintDocument1_PrintPage(sender As Object, e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
e.Graphics.DrawString("CONOLOSCOPY", New Font("Arial", 20, FontStyle.Bold), Brushes.Red, New Point(300, 20))
e.Graphics.DrawString("ENDOSCOPY NO.: " + txt_ID.Text, New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(10, 60))
e.Graphics.DrawString("_________", New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(180, 60))
e.Graphics.DrawString("REG NO.: " + txt_REGNO.Text, New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(300, 60))
e.Graphics.DrawString("_________", New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(390, 60))
e.Graphics.DrawString("HEIGHT: " + txt_HEIGHT.Text, New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(500, 60))
e.Graphics.DrawString("_________", New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(580, 60))
e.Graphics.DrawString("DATE: " + txt_DATE.Text, New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(10, 80))
e.Graphics.DrawString("______________", New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(70, 80))
e.Graphics.DrawString("ROOM NO.: " + txt_ROOMNO.Text, New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(280, 80))
e.Graphics.DrawString("_________", New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(380, 80))
e.Graphics.DrawString("WEIGHT NO.: " + txt_WEIGHT.Text, New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(500, 80))
e.Graphics.DrawString("_________", New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(620, 80))
e.Graphics.DrawString("LAST NAME: " + txt_LNAME.Text, New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(10, 130))
e.Graphics.DrawString("__________________", New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(120, 130))
e.Graphics.DrawString("FIRST NAME: " + txt_FNAME.Text, New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(350, 130))
e.Graphics.DrawString("__________________________", New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(470, 130))
e.Graphics.DrawString("AGE: " + txt_AGE.Text, New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(10, 160))
e.Graphics.DrawString("_____", New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(55, 160))
e.Graphics.DrawString("SEX: " + txt_SEX.Text, New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(120, 160))
e.Graphics.DrawString("_____", New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(170, 160))
e.Graphics.DrawString("CELL NO.: " + txt_CELLNO.Text, New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(250, 160))
e.Graphics.DrawString("_________________", New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(340, 160))
e.Graphics.DrawString("ADDRESS: " + txt_ADDRESS.Text, New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(10, 190))
e.Graphics.DrawString("__________________________________________________", New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(100, 190))
e.Graphics.DrawString("EMAIL: " + txt_EMAIL.Text, New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(10, 240))
e.Graphics.DrawString("_______________________", New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(60, 240))
e.Graphics.DrawString("REFERRING MD: " + txt_REFERMD.Text, New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(10, 270))
e.Graphics.DrawString("________________", New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(160, 270))
e.Graphics.DrawString("ATTENDING MD: " + txt_MD.Text, New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(350, 270))
e.Graphics.DrawString("___________________", New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(495, 270))
e.Graphics.DrawString("ANESTHESIOLOGIST: " + txt_ANES.Text, New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(10, 300))
e.Graphics.DrawString("_______________________________", New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(210, 300))
e.Graphics.DrawString("PROCEDURE: " + txt_PROC.Text, New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(10, 330))
e.Graphics.DrawString("______________", New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(135, 330))
e.Graphics.DrawString("INDICATION: " + txt_INDICATION.Text, New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(350, 330))
e.Graphics.DrawString("_________________", New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(460, 330))
e.Graphics.DrawString("PREMEDICATION: " + txt_PREMED.Text, New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(10, 360))
e.Graphics.DrawString("_______________________________", New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(170, 360))
e.Graphics.DrawString("SCOPE USED: " + txt_SCOPE.Text, New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(10, 390))
e.Graphics.DrawString("___________________", New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(140, 390))
e.Graphics.DrawString("PROCTIME: " + txt_PROCTIME.Text, New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(400, 390))
e.Graphics.DrawString("___________________", New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(510, 390))
e.Graphics.DrawString("BIOPSY: " + txt_BIOPSY.Text, New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(370, 420))
e.Graphics.DrawString("___________________", New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(450, 420))
e.Graphics.DrawString("ESOPHAGUS: " + txt_ESOPHAGUS.Text, New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(10, 450))
e.Graphics.DrawString("CARDIA: " + txt_CARDIA.Text, New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(10, 480))
e.Graphics.DrawString("BODY: " + txt_BODY.Text, New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(10, 510))
e.Graphics.DrawString("ANTRUM: " + txt_ANTRUM.Text, New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(10, 540))
e.Graphics.DrawString("DUODENAL: " + txt_DUODENAL.Text, New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(10, 580))
e.Graphics.DrawString("ENDOSCOPIC DESCRIPTION: " + txt_ENDODESC.Text, New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(10, 610))
e.Graphics.DrawString("_________________________________________________", New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(270, 610))
e.Graphics.DrawString("THERAPEUTIC INTERVENTION " + txt_THERAPEUTIC.Text, New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(10, 660))
e.Graphics.DrawString("______________________________________________", New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(290, 660))
e.Graphics.DrawString("DIAGNOSIS: " + txt_DIAGNOSIS.Text, New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(10, 810))
e.Graphics.DrawString("___________________________________________________", New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(120, 810))
e.Graphics.DrawString("HISTOPATH: " + txt_HISTOPATH.Text, New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(10, 960))
e.Graphics.DrawString("_______________________________________________________", New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(120, 960))
e.Graphics.DrawString("ENDOSCOPIST: " + txt_ENDOSPCOPIST.Text, New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(10, 1010))
e.Graphics.DrawString("_______________________________________________________", New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(150, 1010))
End Sub
my problem here is how to print it on multiple pages, page counts are depends on the user input on textbox9. please help me. thanks
Private Sub PrintDocument1_PrintPage(sender As Object, e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
PrintDocument1.DefaultPageSettings.PaperSize = New System.Drawing.Printing.PaperSize("Paper Size Name", 300, 300)
Dim CT As Date = Now ' Use of Now.
TextBox17.Text = (CT.ToShortDateString)
Dim font1 As New Font("arial", 6, FontStyle.Bold)
Dim font2 As New Font("arial", 10, FontStyle.Bold)
Dim myBitmap As New System.Drawing.Bitmap(filename:="C:\Users\jtapellido\Desktop\sticker pictures\slogo1.png")
Dim index As Integer = 1
Do
e.Graphics.DrawImage(image:=myBitmap, point:=New Point(70, 20))
e.Graphics.DrawString("QUARANTINE", font2, Brushes.Black, 70, 40)
e.Graphics.DrawString("MATERIAL NAME:_________________________________", font1, Brushes.Black, 70, 60)
e.Graphics.DrawString("QC No:_____________", font1, Brushes.Black, 70, 80)
e.Graphics.DrawString("LOT No:____________", font1, Brushes.Black, 70, 100)
e.Graphics.DrawString("DATE RECEIVED:______________", font1, Brushes.Black, 170, 80)
e.Graphics.DrawString("EXPIRATION DATE:____________", font1, Brushes.Black, 170, 100)
e.Graphics.DrawString("QUANTITY:____________", font1, Brushes.Black, 70, 120)
e.Graphics.DrawString("VENDOR/MANUFATURER:", font1, Brushes.Black, 70, 140)
e.Graphics.DrawString("________________________________________________", font1, Brushes.Black, 70, 160)
e.Graphics.DrawString("CONTAINER No:________________OF________________", font1, Brushes.Black, 70, 180)
e.Graphics.DrawString("STORAGE:_______________________________________", font1, Brushes.Black, 70, 200)
e.Graphics.DrawString("REMARKS:_______________________________________", font1, Brushes.Black, 70, 220)
e.Graphics.DrawString("PREPARED BY/DATE:", font1, Brushes.Black, 70, 240)
e.Graphics.DrawString("CHECKED BY/DATE:", font1, Brushes.Black, 190, 240)
e.Graphics.DrawString("____________________", font1, Brushes.Black, 70, 255)
e.Graphics.DrawString("____________________", font1, Brushes.Black, 190, 255)
e.Graphics.DrawString(TextBox1.Text, font1, Brushes.Black, 145, 58)
e.Graphics.DrawString(TextBox2.Text, font1, Brushes.Black, 110, 78)
e.Graphics.DrawString(TextBox3.Text, font1, Brushes.Black, 110, 98)
e.Graphics.DrawString(TextBox4.Text, font1, Brushes.Black, 255, 78)
e.Graphics.DrawString(TextBox5.Text, font1, Brushes.Black, 255, 98)
e.Graphics.DrawString(TextBox6.Text, font1, Brushes.Black, 120, 118)
'e.Graphics.DrawString("VENDOR/MANUFATURER:", font1, Brushes.Black, 10, 120)
e.Graphics.DrawString(TextBox7.Text, font1, Brushes.Black, 70, 158)
e.Graphics.DrawString(index, font1, Brushes.Black, 170, 178)
e.Graphics.DrawString(TextBox9.Text, font1, Brushes.Black, 255, 178)
e.Graphics.DrawString(TextBox11.Text, font1, Brushes.Black, 120, 198)
e.Graphics.DrawString(TextBox12.Text, font1, Brushes.Black, 120, 218)
'e.Graphics.DrawString("PREPARED BY/DATE:", font1, Brushes.Black, 10, 220)
'e.Graphics.DrawString("CHECKED BY/DATE:", font1, Brushes.Black, 120, 220)
e.Graphics.DrawString(TextBox15.Text + " " + TextBox17.Text, font1, Brushes.Black, 70, 253)
e.Graphics.DrawString(ComboBox2.Text + " " + TextBox17.Text, font1, Brushes.Black, 190, 253)
'e.Graphics.DrawRectangle(Pens.Red, e.MarginBounds)
index += 1
Loop Until index > TextBox9.text
End Sub
The logic for printing multiple pages is pretty simple. The PrintPage event handler does just what the name suggests, i.e. it prints a page. At the end of the method, you set e.HasMorePages to True if there are more pages to print and that's it. The event will be raised again and another page printed.
It's up to you to provide logic at the start of the method to determine what page you're printing and at the end to determine whether there are more pages to print. Because you need to remember a state between calls to that method, you'll likely store the relevant data in one or more fields.
Here is an example of printing record from a list, ten to a page:
Private allData As New List(Of String)
Private dataToPrint As Queue(Of String)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Start printing.
PrintDocument1.Print()
End Sub
Private Sub PrintDocument1_BeginPrint(sender As Object, e As PrintEventArgs) Handles PrintDocument1.BeginPrint
'Create a new queue containing all the current data.
dataToPrint = New Queue(Of String)(allData)
End Sub
Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
'Print a maximum of 10 records.
For i = 1 To Math.Min(dataToPrint.Count, 10)
e.Graphics.DrawString(dataToPrint.Dequeue(),
Font,
Brushes.Black,
25,
i * 25)
Next
'Keep printing if and only if there are more records to print.
e.HasMorePages = (dataToPrint.Count > 0)
End Sub
Private Sub PrintDocument1_EndPrint(sender As Object, e As PrintEventArgs) Handles PrintDocument1.EndPrint
dataToPrint = Nothing
End Sub
If you wanted to print a specific number of pages based on the contents of a TextBox then you might convert that contents to an Integer and assign that to a field at the start. You could then decrement that variable at the end of the PrintPage event handler and print another page if and only if that counter is greater than zero.
Here is my code to Print:
Private Sub btnPrint_Click(sender As System.Object, e As System.EventArgs) Handles btnPrint.Click
PrintInvoiceDoc.DefaultPageSettings.Landscape = True
PrintPreviewDialog.ShowDialog()
End Sub
Private Sub PrintInvoiceDoc_PrintPage(sender As System.Object, e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintInvoiceDoc.PrintPage
Dim CenterAlign As New StringFormat
CenterAlign.Alignment = StringAlignment.Center
Dim RightAlign As New StringFormat
CenterAlign.Alignment = StringAlignment.Far
e.Graphics.DrawString("School Name", New Font("Arial", 16, FontStyle.Bold), Brushes.Black, New Point(25, 25))
e.Graphics.DrawString("Report", New Font("Arial", 16, FontStyle.Bold), Brushes.Black, New Point(1000, 25), RightAlign)
Dim mRow As Integer = 0
Dim newpage As Boolean = True
With DataGridView1
Dim fmt As StringFormat = New StringFormat(StringFormatFlags.LineLimit)
fmt.LineAlignment = StringAlignment.Center
fmt.Trimming = StringTrimming.EllipsisCharacter
Dim y As Single = e.MarginBounds.Top
Do While mRow < .RowCount
Dim row As DataGridViewRow = .Rows(mRow)
Dim x As Single = e.MarginBounds.Left
Dim h As Single = 0
For Each cell As DataGridViewCell In row.Cells
Dim rc As RectangleF = New RectangleF(x, y, cell.Size.Width, cell.Size.Height)
e.Graphics.DrawRectangle(Pens.Black, rc.Left, rc.Top, rc.Width, rc.Height)
If (newpage) Then
e.Graphics.DrawString(DataGridView1.Columns(cell.ColumnIndex).HeaderText, .Font, Brushes.Black, rc, fmt)
Else
e.Graphics.DrawString(DataGridView1.Rows(cell.RowIndex).Cells(cell.ColumnIndex).FormattedValue.ToString(), .Font, Brushes.Black, rc, fmt)
End If
x += rc.Width
h = Math.Max(h, rc.Height)
Next
newpage = False
y += h
mRow += 1
If y + h > e.MarginBounds.Bottom Then
e.HasMorePages = True
mRow -= 1
newpage = True
Exit Sub
End If
Loop
mRow = 0
End With
End Sub
So, when hit 'Print' I get the result shown in this image:
Problem #1: I am missing the first row [As you can see]
Problem #2: When the rows are enough that one page is not enough to store all of them, new page is created in infinite loop and same page is repeated so not all data is printed.
Note: I almost don't have any idea about using a DataGridView in PrintDocument, but any advice/tips are welcome!
ur first row behind header row... make new rectanglef for ur header row like this....
For Each cell As DataGridViewCell In row.Cells
Dim rc As RectangleF = New RectangleF(x, y, cell.Size.Width, cell.Size.Height)
Dim rc2 As RectangleF = New RectangleF(x, y - 22, cell.Size.Width, cell.Size.Height)
If (newpage) Then
e.Graphics.FillRectangle(New SolidBrush(Color.LightGray), New RectangleF(x, y - 22, cell.Size.Width, cell.Size.Height))
e.Graphics.DrawRectangle(Pens.Black, rc2.Left, rc2.Top, rc2.Width, rc2.Height)
e.Graphics.DrawString(dgpembelian.Columns(cell.ColumnIndex).HeaderText, cellheaderFont, Brushes.Black, rc2, drawFormat)
End If
e.Graphics.DrawRectangle(Pens.Black, rc.Left, rc.Top, rc.Width, rc.Height)
e.Graphics.DrawString(dgpembelian.Rows(cell.RowIndex).Cells(cell.ColumnIndex).FormattedValue.ToString, .Font, Brushes.Black, rc, drawFormat)
x += rc.Width
h = Math.Max(h, rc.Height)
Next