0

Good morning! A friend asked me to create a simple programme that prints a .pdf file. But he needs it to print using two paper sources, one for odd pages and another for even pages. The problem is that the code prints only blank pages!

Imports System.Drawing.Printing
Public Class Form1
    Dim pd As New PrintDocument

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        OpenFileDialog1.ShowDialog()
        ComboBox1.Items.Clear()
        ComboBox2.Items.Clear()
        ComboBox1.DisplayMember = "SourceName1"
        ComboBox2.DisplayMember = "SourceName2"
        Dim pkSource As PaperSource
        pd.DocumentName = TextBox1.Text
        For i = 0 To pd.PrinterSettings.PaperSources.Count - 1
            pkSource = pd.PrinterSettings.PaperSources.Item(i)
            ComboBox1.Items.Add(pkSource)
            ComboBox2.Items.Add(pkSource)
        Next
    End Sub

    Private Sub OpenFileDialog1_FileOk(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
        TextBox1.Text = OpenFileDialog1.FileName
    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        If TextBox1.Text <> "" And ComboBox1.SelectedIndex <> -1 And ComboBox2.SelectedIndex <> -1 Then
            With pd
                Dim n As Integer = 0
                Try
                    While (True)
                        n = n + 1
                        .DefaultPageSettings.PrinterSettings.FromPage = n
                        .DefaultPageSettings.PrinterSettings.ToPage = n
                        If (n Mod 2) = 0 Then
                            .DefaultPageSettings.PaperSource =
                            .PrinterSettings.PaperSources.Item(ComboBox1.SelectedIndex)
                        Else
                            .DefaultPageSettings.PaperSource =
                            .PrinterSettings.PaperSources.Item(ComboBox2.SelectedIndex)
                        End If
                        .Print()
                    End While
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            End With
        End If
    End Sub
End Class
  • 1
    Do you know how a `PrintDocument` actually works? It would appear not. When you call `Print`, it raises its `PrintPage` event. It's then up to you to handle that event and use GDI+ to draw a printed page. If there are multiple pages then you keep raising the event by setting `e.HasMorePages` to `True` until you've drawn all the pages. The thing is though, if you want to print a PDF then you're not going to be drawing the output yourself, so you can't use a `PrintDocument` at all. – jmcilhinney Nov 05 '16 at 13:07
  • http://stackoverflow.com/questions/6103705/how-can-i-send-a-file-document-to-the-printer-and-have-it-print – Alex B. Nov 05 '16 at 14:34

0 Answers0