I am trying (and failing) right now to print a Windows Form as a PDF File in VB.Net
Now I know from doing some research that VB.Net does not have any built in function which could allow me to do this without using a third party application.
My way to get around this, was to attempt to print my Form using a PDF Printer, since logically that should work no? My issue is getting that to work properly.
This is currently my code for attempting to print:
Private Sub SelectPrinterThenPrint()
Dim PrintersDialog As New PrintDialog()
If PrintersDialog.SHowDialog(Me) = System.Forms.DialogResult.OK Then
Try
p_Document = New PrintDocument()
PrintersDialog.Document = p_Document
AddHandler p_Document.PrintPage, AddressOf HandleOnPrintPage
Catch ex As Exception
End Try
End If
End Sub
Private Sub HandleOnPrintPage(ByVal sender As Object, ByVal e As PrintPageEventArgs) Handles p_Document.PrintPage
Dim MorePagesPending = False
Dim bmp As New Bitmap(pnlContainer.Width, pnlContainer.Height)
pnlContainer.DrawToBitmap(bmp, pnlContainer.ClientRectangle)
e.Graphics.DrawImage(bmp, New Point(0,0))
If MorePagesPending Then
e.HasMorePages = True
Else
e.HasMorePages = False
End If
End Sub
The SelectPrinterThenPrint() method is called when a button on my Form is clicked. Right now I am having two issues.
1) I don't know how to setup where the file gets saved. The Print Dialog opens, and I get to select my PDF Printer, but it doesn't let me set a filename. So how do I set the filename I want to be printing to?
2) Because I can't tell if the file is actually being saved, I can't tell if I am actually setting up the print properly or not. I am trying to print the entire contents of a panel, which holds all the elements I want on my PDF file. One thing I am unsure about however, is that the form the panel is displayed in, is not the same size as the panel. The Form the panel is in has the same width, but not the same height, with the form being set up for auto scrolling. Will I get the whole Panel to print? Or just the part the is visible in the form at the time of printing?
Related
This question already has answers here:
Auto print without dialog
(3 answers)
Closed 3 years ago.
i'm working on window form application and i use print preview dialogue for the printing.it works that when i click on print button then print preview dialogue opens and then i have to click on print option in print preview dialogue and printer prints correctly.but my requirement is that when i click on print button then directly printer prints the document without going in print preview dialogue..i search many stack overflow question and from other sites but not get the required point,help available in java script or such other language which i not required. the code i tried is given below:
Private Sub print_Button_Click(sender As Object, e As EventArgs) Handles print_preview_Button.Click
PrintPreviewDialog1.Document = PrintDocument1
PrintPreviewDialog1.ShowDialog()
End Sub
and "PrintDocument1_PrintPage" event code is
Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
'printing code is here... working correctly
End Sub
where changing i should do that i get the required result....??
Instead of using the PrintDialog class, make use ofPrintDocument class. Make sure to pass your printer's name properly.
Imports System.Drawing.Printing
....
Dim pd as New PrintDocument()
pd.PrinterSettings.PrinterName = "my printer"
In case you want to get the name of all connected printers, try :
For each s as String in PrinterSettings.InstalledPrinters
Dim printerName as string = s
Next
Original answer reference
I'm still a bit new to programming, but I'm trying to make a notepad application has a password before you can get in to the notepad part. (The login in page works mostly). However, I have two questions:
Can I make it so I when I save the program, it will save it as a document only my program can read? Because I wanted it to be password protected. However, MS word can open the text documents made from my program. I only want my program to be able to open and view what is created from that program.
Here is the code to save the current text:
Private Sub SaveToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SaveToolStripMenuItem.Click
Try
Dim dlg As SaveFileDialog = New SaveFileDialog
dlg.Title = "Save"
dlg.Filter = "Rich Text Files (*.rtf)|*.rtf"
If dlg.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
RichTextBox1.SaveFile(dlg.FileName, RichTextBoxStreamType.RichText)
End If
Catch ex As Exception : End Try
End Sub
(I copied this part of code from somewhere else)
The second question is:
I have two forms (login and loginpassed). If I pass the login I have set the login form to hide (because (Close) closes the whole program even the login passed form. and if I click the red X to close the loginpassed form, the login form is still running, how can I close the first form without closing the second form?
In VB.Net, I'm trying to add a QuickTime ActiveX control to a Form when the user clicks a button.
My code is below. For testing I've got a design-time ActiveX control, "designed_control", which works fine, but I'm trying to place "dynamically_created_control" onto the form.
Public Class Form1
Private moviePath As String = "\\localhost\D$\Temp\Test.mov"
Friend WithEvents dynamically_created_control As AxQTOControlLib.AxQTControl = Nothing
Private Sub buttonLoadMovieIntoExisting_Click(sender As Object, e As EventArgs) Handles buttonLoadMovieIntoExisting.Click
' load movie into control created in designer, works fine:
MessageBox.Show(moviePath)
With designed_control
.URL = moviePath
MessageBox.Show("URL:" + .URL)
End With
End Sub
Private Sub buttonCreateNewControl_Click(sender As Object, e As EventArgs) Handles buttonCreateNewControl.Click
' create a new ActiveX control when button is clicked:
dynamically_created_control = New AxQTOControlLib.AxQTControl
CType(dynamically_created_control, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(Form1))
With dynamically_created_control
.CreateControl()
.Enabled = True
.Location = New System.Drawing.Point(160, 160)
.Name = "new_control"
.OcxState = CType(resources.GetObject("designed_control.OcxState"), System.Windows.Forms.AxHost.State)
.Size = New System.Drawing.Size(480, 270)
.TabIndex = 0
Me.Controls.Add(Me.dynamically_created_control)
.Visible = True
.URL = moviePath
End With
CType(dynamically_created_control, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(True)
With dynamically_created_control
MessageBox.Show("URL:" + vbCrLf + .URL)
.Movie.Play()
End With
End Sub
End Class
This doesn't work; when I click 'buttonCreateNewControl', it will pop up the 'URL:' messagebox with the correct URL, showing that the properties of dynamically_created_control are being set and the object is not nothing. However the rectangular shape of the control I'd expect doesn't appear on the form. As soon as I call the .Play() method of the control, it raises an exception because .Movie is nothing, when it shouldn't be.
Can anyone spot why the dynamically-generated ActiveX control simply doesn't appear (but without throwing errors), when the designer-based version is absolutely fine?
Incidentally, I'm aware of the security issues around QuickTime, which is why I'm now trying to code something which can optionally use QuickTime if the user decides to.
Thanks
I believe you may need to update buttonCreateNewControl_Click to use
.OcxState = CType(resources.GetObject("dynamically_created_control.OcxState"), System.Windows.Forms.AxHost.State)
Hope this helps.
I have spotted the error in my code.
The .createControl() method was attempting to create an external QuickTime window. That control works for something like VLC Player, but QuickTime doesn't support it, so nothing was happening.
As soon as I commented out createControl(), the behaviour became what I was expecting.
The .createControl() was a leftover from code I found online, I assumed it was essential for initiating the ActiveX control but it isn't.
I am creating a loan program that opens a Custom Form Dialog Box, you choose the picture, click open and then need to pass it to another form to use after hitting OK from the Dialog Box. This is my code when I click the Logo File button from my custom Dialog Form.
The Form is called Dialog Form and I need to send the picture to the NewLoanCaculatorForm to populate a picture area in the form.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogoFile.Click
Dim mystream As Stream = Nothing
'Open the File to pickup icon for Loan Calculator
Dim OpenFileDialog1 As New OpenFileDialog
'Set up and display the open File Dialog
With OpenFileDialog1
'Begin in the current Directory
.InitialDirectory = "C:\"
.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
End With
If OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
Try
mystream = OpenFileDialog1.OpenFile()
If (mystream IsNot Nothing) Then
' I believe the coded goes here but I'm stuck
End If
Catch Ex As Exception
MessageBox.Show("Cannot read file from disk. Original error: " & Ex.Message)
Finally
' Check this again, since we need to make sure we didn't throw an exception on open.
If (mystream IsNot Nothing) Then
mystream.Close()
End If
End Try
End If
End Sub
This is how I usually do things like this:
Create global variable in the DialogForm:
Public Property sPath as String
or
Public Property imgLogo as Image
To get the Image do this:
imgLogo = Image.FromFile(OpenFileDialog1.FileName)
Or simply do:
sPath = OpenFileDialog1.FileName
instead of
mystream = OpenFileDialog1.OpenFile()
Then when you have done this you close the form by clicking the OK button or whatever you call it.
Then in your main form NewLoanCaculatorForm where you call DialogForm you simply do:
img = DialogForm.imgLogo
or
path = DialogForm.sPath
img = Image.FromFile(path)
Depending on which way you stored the info in the DialogForm.
Also, if you are looking for images I would recommend you not to have .txt in your filter. That would seriously ruin the execution.
Does anyone have any code suggestions or samples for printing photos (BMP or TIFF or JPEG), using Visual Basic or .NET framework?
VB6 and .NET handle printing quite differently. These examples are the bare minumum, just to give you an idea of the process.
In VB6 you control the printer step by step:
Private Sub PrintMyPicture()
'Place to store my picture
Dim myPicture As IPictureDisp
'Load the picture into the variable
Set myPicture = LoadPicture("C:\temp\myPictureFile.bmp")
'Draw the picture on the printer, just off the edge of the page
Printer.PaintPicture myPicture, 10, 10
'Done printing!
Printer.EndDoc
End Sub
Lo and behold, your picture will come out of the default printer. the PaintPicture method accepts width, height and a few other parameters to help get the image to fit, and the Printer object gives you all sorts of info about the printer.
In .Net it's kind of the other way around. You start printing, and the printer will raise an event for each page until you tell it to stop. Every page event gives you as graphics object upon which you can draw using all of the standard System.Drawing classes and methods:
'Class variable
Private WithEvents printer As System.Drawing.Printing.PrintDocument
' Kick off the printing process. This will cause the printer_PrintPage event chain to start firing.
Public Sub PrintMyPicture()
'Set up the printer
printer.PrinterSettings.PrinterName = "MyPrinterNameInPrintersAndFaxes"
printer.Print()
End Sub
'This event will keep firing while e.HasMorePages = True.
Private Sub printer_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles printer.PrintPage
'Load the picture
Dim myPicture As System.Drawing.Image = System.Drawing.Image.FromFile("C:\temp\myPictureFile.bmp")
'Print the Image. 'e' is the Print events that the printer provides. In e is a graphics object on hwich you can draw.
'10, 10 is the position to print the picture.
e.Graphics.DrawImage(myPicture, 10, 10)
'Clean up
myPicture.Dispose()
myPicture = Nothing
'Tell the printer that there are no more pages to print. This will cause the document to be finalised and come out of the printer.
e.HasMorePages = False
End Sub
Again, there are a lot more parameters in DrawImage and the PrintDocument objects.