I creating a document using VBNET's "graphics" object and show them in the PictureBox. Here is Sub CreateDoc which draw the document and shortened part of program (mistakes possible because of out IDE editing).
My intention is to call CreateDoc again through buttons PrintPreview or Print (I read that this is possible). But calling from PrintPreview Sub (PrnDoc_PrintPage) dont make any "picture" on PrintPreview control. As you can see, for testing purposes I add one string to page before creating a document and this string comes on the result page.
I am new in VB.Net and until now i programmed mostly in DOS, qb45:).
Logic of those namespaces, instances, withevents, marshalers, overrides etc, etc are real nightmare for me.
So I have two questions here:
1. What to do (without significant changes of program logic) that Print Preview appears on the "paper"? What I was doing wrong?
2. Where and how to put "g.PageUnit = GraphicsUnit.Millimeter" so I can use those measurement instead of "AllWidth". If I put PageUnit at the top of Sub programm breaks. Yet better would be How can I get size of
"C:\SomePic.gif" in millimeters to use this for reference in later calculations? In this case SomePic.gif is exactly 210*297mm (A4).
Please help...
Expand|Select|Wrap|Line Numbers
- Imports System.Drawing.Printing
- etc...
- Public Class Dokument
- Dim g As Graphics
- Private PrnDoc As PrintDocument
- etc...
- Public Sub Dokument_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
- ' This one CreateDoc works fine on PictureBox
- ' I can see the document "as should be"
- CreateDoc(g)
- End Sub
- Public Sub CreateDoc(ByVal g As Graphics)
- Dim AllWidth = 800
- Dim b As New Bitmap(AllWidth, 1100)
- g = Graphics.FromImage(b)
- g.PageUnit = GraphicsUnit.Millimeter
- Dim img As Image = Image.FromFile("C:\SomePic.gif")
- g.DrawImage(img, 0, 0, A4_Width, A4_Height)
- cTabsIndex = 0
- Dim Rb As String = Format(z, "####")
- g.DrawString(Rb, f, Brushes.Black, LOdmak + cTabs(cTabsIndex) - g.MeasureString(Rb, f).Width, LineFromTop)
- cTabsIndex += 1
- g.DrawString(OUTCHAR$(Sot.MJedO, InCode$, DeCode$), f, Brushes.Black, LOdmak + cTabs(cTabsIndex), LineFromTop)
- cTabsIndex += 1
- Dim RKolic As String = Format(Sot.KolO, "#,##0.00")
- g.DrawString(RKolic, f, Brushes.Black, LOdmak + cTabs(cTabsIndex) - g.MeasureString(RKolic, f).Width, LineFromTop)
- ' ScreenPrint (boolean) is switch between outputs for PictureBox or PrintDocument object.
- If ScreenPrint Then
- PictureBox1.Image = b
- PictureBox1.Refresh()
- Else
- '?what else if something???
- End If
- g.Dispose()
- End Sub
- Private Sub PrnDoc_PrintPage(ByVal sender As Object, ByVal ev As PrintPageEventArgs)
- Dim FontName As String = "Microsoft Sans Serif"
- Dim f As Font = New Font(FontName, 8, FontStyle.Regular)
- ev.Graphics.DrawString("Only this visible", f, SomeBrush, 5, 5)
- ' String "Only this visible" is visible on PrintPreview. CreateDoc should ' ' add whole document in PPreview but don't add nothing! What is wrong?
- CreateDoc(ev.Graphics)
- End Sub
- Private Sub ButtonPPreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonPPreview.Click
- PrnDoc = New PrintDocument
- AddHandler PrnDoc.PrintPage, AddressOf PrnDoc_PrintPage
- PrnDoc.PrinterSettings.PrinterName = "SomePDFPrinter"
- PrnDoc.DocumentName = DocFileName
- Dim PPDialog As New System.Windows.Forms.PrintPreviewDialog
- With PPDialog
- .Document = PrnDoc
- .WindowState..., .ShowDialog(), .Dispose(), etc...
- End With
- End Sub