Click here to Skip to main content
15,881,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dim deviceInfo As String = "<DeviceInfo><OutputFormat>EMF</OutputFormat>" & _
                                    "<PageWidth>" & (pageSettings.PaperSize.Height) * 3000 & "in</PageWidth>" & _
                                    "<PageHeight>" & (pageSettings.PaperSize.Height) * 3000 & "in</PageHeight>" & _
                                    "<MarginTop>" & (pageSettings.Margins.Top) * 3000 & "in</MarginTop>" & _
                                    "<MarginLeft>" & (pageSettings.Margins.Left) * 3000 & "in</MarginLeft>" & _
                                    "<MarginRight>" & (pageSettings.Margins.Right) * 3000 & "in</MarginRight>" & _
                                    "<MarginBottom>" & (pageSettings.Margins.Bottom) * 3000 & "in</MarginBottom>" & _
                        "</DeviceInfo>"

        Dim warnings() As Warning
        Dim streams = New List(Of Stream)()
        Dim currentPageIndex = 0
        report.Render("Image", deviceInfo,
                      Function(name, fileNameExtension, encoding, mimeType, willSeek)
                          Dim stream = New MemoryStream()
                          streams.Add(stream)
                          Return stream
                      End Function, warnings)

        For Each stream As Stream In streams
            stream.Position = 0
        Next

        If streams Is Nothing OrElse streams.Count = 0 Then
            Throw New Exception("Error: no stream to print.")
        End If

        Dim printDocument = New PrintDocument()
        printDocument.DefaultPageSettings = pageSettings
        printDocument.PrinterSettings.PrinterName = gblPOSReceiptPrinter

        If Not printDocument.PrinterSettings.IsValid Then
            Throw New Exception("Error: cannot find the default printer.")
        Else
            AddHandler printDocument.PrintPage,
                Sub(sender, e)
                    Dim pageImage As Metafile = New Metafile(streams(currentPageIndex))
                    Dim adjustedRect As Rectangle = New Rectangle(
                        e.PageBounds.Left - CInt(e.PageSettings.HardMarginX),
                        e.PageBounds.Top - CInt(e.PageSettings.HardMarginY),
                        e.PageBounds.Width,
                        e.PageBounds.Height)
                    e.Graphics.FillRectangle(Brushes.White, adjustedRect)
                    e.Graphics.DrawImage(pageImage, adjustedRect)
                    currentPageIndex += 1
                    e.HasMorePages = (currentPageIndex < streams.Count)
                    e.Graphics.DrawRectangle(Pens.Red, adjustedRect)
                End Sub

            AddHandler printDocument.EndPrint,
                Sub(Sender, e)
                    If streams IsNot Nothing Then
                        For Each stream As Stream In streams
                            stream.Close()
                        Next
                        streams = Nothing
                    End If
                End Sub

            printDocument.Print()
        End If


What I have tried:

Dim deviceInfo As String = "<DeviceInfo><OutputFormat>EMF</OutputFormat>" & _
                                    "<PageWidth>" & (pageSettings.PaperSize.Height) * 3000 & "in</PageWidth>" & _
                                    "<PageHeight>" & (pageSettings.PaperSize.Height) * 3000 & "in</PageHeight>" & _
                                    "<MarginTop>" & (pageSettings.Margins.Top) * 3000 & "in</MarginTop>" & _
                                    "<MarginLeft>" & (pageSettings.Margins.Left) * 3000 & "in</MarginLeft>" & _
                                    "<MarginRight>" & (pageSettings.Margins.Right) * 3000 & "in</MarginRight>" & _
                                    "<MarginBottom>" & (pageSettings.Margins.Bottom) * 3000 & "in</MarginBottom>" & _
                        "</DeviceInfo>"
Posted
Comments
[no name] 3-Feb-23 13:06pm    
Insert Form Feed characters.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900