Need to print to label printer with formatted text.

mechwarrior3

Well-known member
Joined
Dec 19, 2005
Messages
67
Programming Experience
Beginner
Hello everyone,

Here is the situation: I have a DYMO label printer. Currently, it is using 4" x 2.31" labels. I need to have my application print a label from this printer at the push of a button. This label needs to be formatted as such:

Arial, regular, size 36 (centered)
Arial, regular, size 10 ___________________________________________________________________________________________________________________________________________ Arial, regular, size 10
Then there will be four more lines below this one that will also be in two-column format like the above line (minus the slew of underscores).
I do not know how to do this. I have spent the last several hours trying to find out how to do this. If anyone knows how to do this or has any suggestions, I would greatly appreciate it. Thanks. :)
 
Crystal Reports

Have you used crystal reports?
You can define the printer and label size on the report, then fill in the data and format it to the way you need. Push the button on your form and you can send the report to the specific printer.
VB.NET:
ReportNameRpt.PrintOptions.PrinterName = PrintDialog1.PrinterSettings.PrinterName
ReportNameRpt.PrintToPrinter(PrintDialog1.PrinterSettings.Copies, True, 1, 99)
or something like this:

VB.NET:
Dim ShippingLabelRpt As New YourReportName
ShippingLabelRpt.PrintOptions.PrinterName = "\\SHIPPINGSYS\Label Printer"
ShippingLabelRpt.PrintOptions.PaperOrientation = CrystalDecisions.[Shared].PaperOrientation.Portrait
ShippingLabelRpt.PrintToPrinter(1, True, 1, 99)
Do a little research on Crystal Reports and you'll get what you want...
 
With regular .Net printing through the PrintDocument class you use regular drawing techniques with the Graphics instance provided by the PrintPage event (e.Graphics). There are examples in documention both for printing and drawing. To draw text you use the DrawString method and specify location and font/size. Example:
VB.NET:
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) _
Handles PrintDocument1.PrintPage
  Dim myFont As New Font("Arial", 10, FontStyle.Regular)
  e.Graphics.DrawString("the string", myFont, Brushes.Blue, 12, 34)
End Sub
 
I know, I know. It's only been an hour. But so far, I am curious, can I use Crystal Reports to generate a report based on data in the fields of my form, rather than the data in a database?

Also, when I try to use a wizard to generate a Crystal Report, the table's come out all funny and I can't figure out how to map field data to specific spots on the Report itself. Like, I need the title to be a single value from a field in the database. I also have a couple other parts that need to be single values from a field in another data table. And then, I only need a certain amount of values from a field in another data table. I'm guessing Crystal Reports can handle this and I probably just haven't found out how yet. I'll keep reading.

If anyone has any info in regards to my first question, all info would be greatly appreciated.
 
JohnH,

Wait...That's how you define a font?! The information I had found regarding that was so confusing and didn't seem to be applicable to my situation that I couldn't figure it out. Holy cow, John, that is exactly what I was looking for! You took what I had been pouring over yesterday and summed it up into something I could understand! Woot! Thank you so much, John. :D
 
Last edited by a moderator:
Crystal Reports Parameters

You can set a string variable to a field value on your form. Then pass that value to a crystal report parameter.

VB.NET:
Dim Report1 As New YourReportName
Dim StringVariable1 as String = Textbox1.text
Report1.SetParameterValue("ReportParameterName", StringVariable1)
Note:
ReportParameterName must be the name of the parameter in the crystal report and the type must match ie (string to string) or (date to date).

you could also set the value directly to the textbox value:
VB.NET:
Dim Report1 As New YourReportName
Report1.SetParameterValue("ReportParameterName", Textbox1.text)
 
now for the other questions:

Crystal reports will allow you to add datasets from your database and link them together.
Use the field explorer in the left side window to right click on the database fields. There will be an option to add/remove database... You can navigate to the tables you want and add and link them from there. Then it is just a matter of dragging and dropping the desired fields onto your report. When I create a report based on a specific record of a datatable, I pass the record identifier to a report parameter and use that parameter in the record selection formula. Where is the record selection formula, you ask? Right click on the report, select =>Report=>Edit Selection Formula=>Records.

Your formula will look like this:
{TableName.FieldName}={?ParameterName}

Hope this helps...
 
Still Having Trouble...

Okay, so I have tried printing using the DrawString command from the PrintDocument class and what not. Now, I can get paper to come out of the label printer. I can not, however, get anything to print. I even tried telling it to simply print "Hello World". That did not work. Here is the code I am using in the PrintPage event handler. Yes, it is quite messy, but I think it can work and I was hoping it would. As I post this, I am now looking at the Crystal Reports option. It's now at the point that whichever option I can get to work first, wins. In any case, all suggestions and help are greatly appreciated.

VB.NET:
[SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] myLargeFont [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Font("Arial", 36, FontStyle.Regular)
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] myNormalFont [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Font("Arial", 10, FontStyle.Regular)
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] linesPerPage [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Single[/COLOR][/SIZE][SIZE=2] = 0
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] yPos [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Single[/COLOR][/SIZE][SIZE=2] = 0
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] count [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = 0
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] leftMargin [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Single[/COLOR][/SIZE][SIZE=2] = ev.MarginBounds.Left
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] topMargin [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Single[/COLOR][/SIZE][SIZE=2] = ev.MarginBounds.Top
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] line [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#0000ff]Nothing
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] WSpct [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Double[/COLOR][/SIZE][SIZE=2] = VDWSCnt / (lngVDStartAmt)
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Voidpct [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Double[/COLOR][/SIZE][SIZE=2] = VDVoidCnt / (lngVDStartAmt - VDWSCnt - VDBSCnt - VDRSCnt)
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] TOpct [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Double[/COLOR][/SIZE][SIZE=2] = VDTearOutCnt / (lngVDStartAmt - VDWSCnt - VDBSCnt - VDRSCnt _ [/SIZE]
[SIZE=2]- VDVoidCnt)
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] CRKpct [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Double[/COLOR][/SIZE][SIZE=2] = VDCrackCnt / (lngVDStartAmt - VDWSCnt - VDBSCnt - VDRSCnt _ [/SIZE]
[SIZE=2]- VDVoidCnt - VDTearOutCnt)
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Chippct [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Double[/COLOR][/SIZE][SIZE=2] = (VDIDChipCnt + VDODChipCnt) / (lngVDStartAmt - VDWSCnt - VDBSCnt _ [/SIZE]
[SIZE=2]- VDRSCnt - VDVoidCnt - VDTearOutCnt - VDCrackCnt)
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Flashpct [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Double[/COLOR][/SIZE][SIZE=2] = VDFlashCnt / (lngVDStartAmt - VDWSCnt - VDBSCnt - VDRSCnt _[/SIZE]
[SIZE=2] - VDVoidCnt - VDTearOutCnt - VDCrackCnt - (VDIDChipCnt + VDODChipCnt) _[/SIZE]
[SIZE=2] - [/SIZE][SIZE=2]VDScratchCnt - VDCEGGCnt - VDOPCnt)
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] JNTpct [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Double[/COLOR][/SIZE][SIZE=2] = VDJointDefectCnt / (lngVDStartAmt - VDWSCnt - VDBSCnt - VDRSCnt _[/SIZE]
[SIZE=2] - VDVoidCnt - VDTearOutCnt - VDCrackCnt - (VDIDChipCnt + VDODChipCnt) _[/SIZE]
[SIZE=2] - VDScratchCnt - VDCEGGCnt - VDOPCnt - VDFlashCnt)
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Otherpct [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Double[/COLOR][/SIZE][SIZE=2] = VDOther1Cnt / (lngVDStartAmt - VDWSCnt - VDBSCnt - VDRSCnt _ [/SIZE]
[SIZE=2]- VDVoidCnt - VDTearOutCnt - VDCrackCnt - (VDIDChipCnt + VDODChipCnt) - VDScratchCnt _ [/SIZE]
[SIZE=2]- VDCEGGCnt - VDOPCnt - VDFlashCnt)
[/SIZE][SIZE=2][COLOR=#008000]' Calculate the number of lines per page.
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics)
[/COLOR][/SIZE][SIZE=2]linesPerPage = 7 [/SIZE][SIZE=2][COLOR=#008000]' This is what I have counted myself...So, it's probably wrong...
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]' Print each line of the file.
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]While[/COLOR][/SIZE][SIZE=2] count < linesPerPage
[/SIZE][SIZE=2][COLOR=#0000ff]Select[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Case[/COLOR][/SIZE][SIZE=2] count
[/SIZE][SIZE=2][COLOR=#0000ff]Case[/COLOR][/SIZE][SIZE=2] 1
line = " " & strMfgLotNum & " "
yPos = topMargin + count * myLargeFont.GetHeight(ev.Graphics)
ev.Graphics.DrawString(line, myLargeFont, Brushes.Black, leftMargin, yPos, [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] StringFormat)
[/SIZE][SIZE=2][COLOR=#0000ff]Case[/COLOR][/SIZE][SIZE=2] 2
line = "Gross " & lngVDStartAmt & " Mix # " & strMixLotNum
[/SIZE][SIZE=2][COLOR=#0000ff]Case[/COLOR][/SIZE][SIZE=2] 3
line = "Defect Cnt % Defect Cnt %"
[/SIZE][SIZE=2][COLOR=#0000ff]Case[/COLOR][/SIZE][SIZE=2] 4
line = "WS " & VDWSCnt & " " & WSpct & " CHP " & (VDIDChipCnt + VDODChipCnt) & " " & Chippct
[/SIZE][SIZE=2][COLOR=#0000ff]Case[/COLOR][/SIZE][SIZE=2] 5
line = "VD " & VDVoidCnt & " " & Voidpct & " FSH " & VDFlashCnt & " " & Flashpct
[/SIZE][SIZE=2][COLOR=#0000ff]Case[/COLOR][/SIZE][SIZE=2] 6
line = "TO " & VDTearOutCnt & " " & TOpct & " JNT " & VDJointDefectCnt & " " & JNTpct
[/SIZE][SIZE=2][COLOR=#0000ff]Case[/COLOR][/SIZE][SIZE=2] 7
line = "CRK " & VDCrackCnt & " " & CRKpct & " O1 " & VDOther1Cnt & " " & Otherpct
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Select
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] line [/SIZE][SIZE=2][COLOR=#0000ff]Is[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Nothing[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Exit[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]While
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] (count > 1) [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]yPos = topMargin + ((count - 1) * myNormalFont.GetHeight(ev.Graphics)) + myLargeFont.GetHeight(ev.Graphics)
ev.Graphics.DrawString(line, myNormalFont, Brushes.Black, leftMargin, yPos, [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] StringFormat)
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2]count += 1
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]While
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]' If more lines exist, print another page.
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Not[/COLOR][/SIZE][SIZE=2] (line [/SIZE][SIZE=2][COLOR=#0000ff]Is[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Nothing[/COLOR][/SIZE][SIZE=2]) [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]ev.HasMorePages = [/SIZE][SIZE=2][COLOR=#0000ff]True
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Else
[/COLOR][/SIZE][SIZE=2]ev.HasMorePages = [/SIZE][SIZE=2][COLOR=#0000ff]False
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff][COLOR=black]
[/COLOR]
[/COLOR][/SIZE]
 
How do you start the print job? You print the PrintDocument by calling PrintDocument1.Print.

(IF you use the Print Dialog or the PrintPreview Dialog you must assign them their Document property to your instance of PrintDocument - you can do this by selecting it in Designer view.)
 
Last edited:
And this is what I was testing with Crystal Reports. So far, It prints out the one parameter in landscape format on the label, except the last character is completely cut off and the parameter is about 3/4 down the page (in respect to landscape orientation). I have tried to set my own margins to no avail. I tried to specify that the paper size is the default. Why does this not work?

VB.NET:
[SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] rpt1 [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] CrystalReport1
rpt1.SetParameterValue("MfgLotNum", strMfgLotNum)
rpt1.PrintOptions.PrinterName = "DYMO LabelWriter 330 Turbo-USB"
[/SIZE][SIZE=2][COLOR=#008000]' rpt1.PrintOptions.PaperSize = CrystalDecisions.[Shared].PaperSize.PaperNote
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]' rpt1.PrintOptions.ApplyPageMargins(crMargins)
[/COLOR][/SIZE][SIZE=2]rpt1.PrintOptions.PaperOrientation = CrystalDecisions.[Shared].PaperOrientation.Landscape
rpt1.PrintToPrinter(1, [/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE][SIZE=2], 1, 99)[/SIZE]
[SIZE=2]
[/SIZE]
 
When you lay out the report are you doing it on the correct paper size and printer?
Right click on the report. Go to Designer=>PrinterSetup
Design the report on the printer and page size you will be printing on.
Also go to Designer=>PageSetup and specify your margins there.

If you want to hard code printer settings you can get the exact string to use from a print dialog box. Example:
VB.NET:
If PrintDialog1.ShowDialog() = DialogResult.OK Then
    MsgBox(PrintDialog1.PrinterSettings.DefaultPageSettings.PaperSize.ToString)
End If
Should get you the paper size string that the Dymo printer is expecting. Then just use that string to set the report paper size.

Hope it helps...
 
Back
Top