Moin,
also die gute Nachricht: es funktioniert.
Folgender Code:
Imports System.Drawing.Printing
Public Class frmPrintDemoV2
Dim WithEvents button1 As New Button With {.Parent = Me, .Text = "Drucken"}
Dim zähler As Integer = 0
Dim arr As New List(Of String)
Dim schriftart As New Font("Arial", 12, FontStyle.Regular)
Private WithEvents doc As New PrintDocument()
'Angabe zu der Größe und Position des Druckbereichs der Etiketten
'in Millimeter
Dim edb_breite As Single = 30
Dim edb_höhe As Single = 23
Dim edb_abstandX As Single = 5
Dim edb_abstandY As Single = 2.4
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles MyBase.Load
For a As Integer = 0 To 73
arr.Add("Nummer " & CStr(a) & vbCrLf & _
"Kennung" & vbCrLf & Now.ToString)
Next a
End Sub
Private Sub Draw(ByVal g As Graphics, ByVal e As PrintPageEventArgs)
Dim br As New SolidBrush(Color.Black)
Dim f As Font = New Font("Calibri", 9)
Dim x, y As Single
x = CSng(e.PageSettings.Margins.Left * 25.4 / 100) '1/100 Zoll in
' Millimeter
y = CSng(e.PageSettings.Margins.Top * 25.4 / 100)
For i As Integer = zähler To arr.Count - 1
If (i + 1) Mod 6 = 0 Then
Dim h As Integer = CInt(Math.Ceiling(g.MeasureString(arr(i), _
f).Height))
g.DrawString(arr(i), f, br, New RectangleF(New PointF(x, y), _
New SizeF(edb_breite, edb_höhe)), StringFormat.GenericDefault)
x = CSng(e.PageSettings.Margins.Left * 25.4 / 100)
y += (edb_abstandY + edb_höhe)
Else
g.DrawString(arr(i), f, br, New RectangleF(New PointF(x, y), _
New SizeF(edb_breite, edb_höhe)), StringFormat.GenericDefault)
x += (edb_abstandX + edb_breite)
End If
zähler += 1
With e.PageSettings
Dim h As Single = .PrintableArea.Height - .Margins.Top - _
.Margins.Bottom
If (y - edb_abstandY) > CSng(h * 25.4 / 100) Then Exit For
End With
Next i
End Sub
Private Sub Drucker(ByVal sender As Object, ByVal e As PrintPageEventArgs) _
Handles doc.PrintPage
Dim g As Graphics = e.Graphics
g.PageUnit = GraphicsUnit.Millimeter
g.TextRenderingHint = _
Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit
Me.Draw(g, e)
e.HasMorePages = zähler < arr.Count - 1
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles button1.Click
Dim ps As New PageSetupDialog
Dim margins As New Margins(0, 0, 0, 0)
Dim papersize As New PaperSize("German Std Endlospapier", 850, 1200)
ps.Document = doc
ps.PageSettings.Margins = margins
ps.PageSettings.PaperSize = papersize
ps.ShowDialog()
zähler = 0
doc.Print()
End Sub
End Class So, das Einzige, was mich jetzt noch ein Bißchen stört ist, dass (auf eine Reihe Etiketten bezogen) der Abstand des Aufdrucks vom Rand des Etiketts von links nach rechts immer kleiner wird. Also beim ersten Etikett in einer Reihe beträgt der Abstand des Aufdrucks vom Etikettenrand 4mm, beim letzten Etikett in der Reihe aber nur noch 1,5mm.
Ich vermute, dass die Kommastellen bei den Margins abgeschnitten werden (abgerundet auf Ganzzahl).
Deshalb hatte ich ursprünglich mit Pixel gearbeitet. Welche Möglichkeit habe ich jetzt, dass der Abstand überall gleich ist? Wie gesagt, der Fehler ist marginal, wäre nur schöner, wenn's gleich wär.
Gruß |