wpf简单的打印对话框页面大小问题

时间:2010-07-30 11:16:28

标签: wpf printing printdialog

我正在创建一个简单的WPF打印对话框来设置标签打印机。我希望它非常简单,所以我选择不使用标准的WPF printdialog。

一切都很顺利,纸张尺寸。

从一个组合框中选择了一台打印机后,第二个组合框将填充该设备提供的纸张尺寸。我目前正在使用selectedPrinter.GetPrintCapabilities.PageMediaSizeCapability并将其设置为组合框的itemssource。

但是,我的主要问题是:

似乎只获得了可用纸张尺寸的一部分(与普通打印对话框相比)

无法添加自定义大小,因为PageMediaSize不可继承,构造函数只允许您使用PageMediaSizeName Enum

我可以显示的唯一名称是Enum文本,它将diplaypath绑定到PageMediaSizeName,这对用户不是特别友好。

我还发现如果我将selectedPrinter.GetPrintCapabilitiesAsXml转储到一个文件并查看它,我得到了我需要的一切;所有可用的打印机的纸张尺寸都有尺寸,重要的是显示名称元素。

我的问题是,我是否遗漏了selectedPrinter.GetPrintCapabilities的内容,或者我是否需要为selectedPrinter.GetPrintCapabilitiesAsXml创建解析器并改为使用此信息?

1 个答案:

答案 0 :(得分:4)

我最终创建了自己的PaperSize类(如下所示)并使用此命令

PaperSize.ParsePaperSizeXML(New Xml.XmlTextReader(selectPrinter.GetPrintCapabilitiesAsXml))

将打印机的可用纸张尺寸检索为绑定列表(selectedPrinter是Printing.PrintQueue类的实例)

Public Class PaperSize

    Const FEATURENODE As String = "psf:Feature"
    Const PAPERSIZEATTRIBUTE As String = "psk:PageMediaSize"
    Const PAPEROPTIONNODE As String = "psf:Option"
    Const SCOREDPROPERTYNODE As String = "psf:ScoredProperty"
    Const WIDTHATTRIBUTE As String = "psk:MediaSizeWidth"
    Const HEIGHTATTRIBUTE As String = "psk:MediaSizeHeight"
    Const VALUENODE As String = "psf:Value"
    Const PROPERTNODE As String = "psf:Property"
    Const DISPLAYNAMEATTRIBUTE As String = "psk:DisplayName"
    Const NAMEATTRIBUTE As String = "name"

    Public Sub New()

    End Sub

    Public Sub New(ByVal PaperKey As String, ByVal PaperDisplayName As String)
        DisplayName = PaperDisplayName
        Width = Nothing
        Height = Nothing
    End Sub

    Public Sub New(ByVal PaperKey As String, ByVal PaperDisplayName As String, ByVal PaperWidth As Double?, ByVal PaperHeight As Double?)
        Key = PaperKey
        DisplayName = PaperDisplayName
        Width = PaperWidth
        Height = PaperHeight
    End Sub

    Property Key As String
    Property DisplayName As String
    Property Width As Double?
    Property Height As Double?


    Public ReadOnly Property WidthInMM As Double?
        Get
            If Width.HasValue Then
                Return WidthInInches * 25.4
            Else
                Return Nothing
            End If

        End Get
    End Property

    Public ReadOnly Property HeightInMM As Double?
        Get
            If Height.HasValue Then
                Return HeightInInches * 25.4
            Else
                Return Nothing
            End If


        End Get
    End Property

    Public ReadOnly Property WidthInInches As Double?
        Get
            If Width.HasValue Then
                Return Width / 96
            Else
                Return Nothing
            End If
        End Get
    End Property

    Public ReadOnly Property HeightInInches As Double?
        Get
            If Height.HasValue Then
                Return Height / 96
            Else
                Return Nothing
            End If
        End Get
    End Property

    Public Shared Function ParsePaperSizeXML(ByVal XmlString As Xml.XmlReader) As ComponentModel.BindingList(Of PaperSize)

        Dim lstPaperSizes As New ComponentModel.BindingList(Of PaperSize)

        Try

            While XmlString.Read()

                If XmlString.NodeType = Xml.XmlNodeType.Element Then
                    Select Case XmlString.Name
                        Case FEATURENODE
                            If XmlString.AttributeCount = 1 Then
                                Select Case XmlString.GetAttribute(NAMEATTRIBUTE)
                                    Case PAPERSIZEATTRIBUTE
                                        lstPaperSizes = processAllPaperSizes(XmlString.ReadSubtree)
                                End Select
                            End If
                    End Select

                End If

            End While

        Catch ex As Exception
            Throw ex
        End Try

        Return lstPaperSizes

    End Function

    Private Shared Function processAllPaperSizes(ByVal PaperSizeXmlString As Xml.XmlReader) As ComponentModel.BindingList(Of PaperSize)
        Dim lstPaperSizes As New ComponentModel.BindingList(Of PaperSize)
        Dim currentKey As String

        Try

            While PaperSizeXmlString.Read()

                If PaperSizeXmlString.NodeType = Xml.XmlNodeType.Element Then
                    Select Case PaperSizeXmlString.Name
                        Case PAPEROPTIONNODE
                            currentKey = PaperSizeXmlString.GetAttribute(NAMEATTRIBUTE)

                            lstPaperSizes.Add(processPaperSize(currentKey, PaperSizeXmlString.ReadSubtree))

                    End Select

                End If

            End While

        Catch ex As Exception
            Throw ex
        End Try
        Return lstPaperSizes
    End Function

    Private Shared Function processPaperSize(ByVal currentPaperKey As String, ByVal PaperSizeXmlString As Xml.XmlReader) As PaperSize

        Dim currentWidth, currentHeight As Double?
        Dim currentName As String = String.Empty
        Dim stringwidth, stringheight As String

        Try

            While PaperSizeXmlString.Read()

                If PaperSizeXmlString.NodeType = Xml.XmlNodeType.Element Then
                    Select Case PaperSizeXmlString.Name
                        Case SCOREDPROPERTYNODE

                            If PaperSizeXmlString.AttributeCount = 1 Then
                                Select Case PaperSizeXmlString.GetAttribute(NAMEATTRIBUTE)
                                    Case WIDTHATTRIBUTE
                                        stringwidth = processPaperValue(PaperSizeXmlString.ReadSubtree)
                                        If String.IsNullOrEmpty(stringwidth) Then
                                            currentWidth = Nothing
                                        Else
                                            currentWidth = MasterWPFUtils.MMToDPI(CDbl(stringwidth)) / 1000
                                        End If

                                    Case HEIGHTATTRIBUTE
                                        stringheight = processPaperValue(PaperSizeXmlString.ReadSubtree)
                                        If String.IsNullOrEmpty(stringheight) Then
                                            currentHeight = Nothing
                                        Else
                                            currentHeight = MasterWPFUtils.MMToDPI(CDbl(stringheight)) / 1000
                                        End If
                                End Select
                            End If
                        Case PROPERTNODE
                            If PaperSizeXmlString.AttributeCount = 1 Then
                                Select Case PaperSizeXmlString.GetAttribute(NAMEATTRIBUTE)
                                    Case DISPLAYNAMEATTRIBUTE
                                        currentName = processPaperValue(PaperSizeXmlString.ReadSubtree)
                                End Select
                            End If
                    End Select

                End If

            End While
            Return New PaperSize(currentPaperKey, currentName, currentWidth, currentHeight)
        Catch ex As Exception
            Throw ex
        End Try

    End Function

    Private Shared Function processPaperValue(ByVal valueXmlString As Xml.XmlReader) As String
        Try

            While valueXmlString.Read()

                If valueXmlString.NodeType = Xml.XmlNodeType.Element Then
                    Select Case valueXmlString.Name
                        Case VALUENODE
                            Return valueXmlString.ReadElementContentAsString.Trim
                    End Select
                End If
            End While
        Catch ex As Exception
            Throw ex
        End Try

        Return String.Empty
    End Function

End Class