PrintDocument.Print()仅打印空白页面! VB.Net

时间:2016-11-05 10:52:51

标签: vb.net pdf printing

早上好! 一位朋友让我创建一个打印.pdf文件的简单程序。但他需要使用两个纸张来源进行打印,一个用于奇数页面,另一个用于偶数页面。 问题是代码只打印空白页!

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

0 个答案:

没有答案