2

ラベル プリンターをセットアップするための単純な WPF 印刷ダイアログ ボックスを作成しています。非常にシンプルにしたいので、標準の WPF printdialog を使用しないことにしました。

用紙サイズについては、すべて順調に進んでいます。

1 つのコンボボックスからプリンターを選択すると、2 番目のコンボボックスにそのデバイスで使用できる用紙サイズが入力されます。私は現在 selectedPrinter.GetPrintCapabilities.PageMediaSizeCapability を使用しており、それをコンボボックスの項目ソースとして設定しています。

ただし、これに関する私の主な問題は次のとおりです。

利用可能な用紙サイズのサブセットのみを取得するようです (通常の印刷ダイアログと比較して)

PageMediaSize は継承可能ではなく、コンストラクターでは PageMediaSizeName Enum のみを使用できるため、カスタム サイズを追加する方法はありません。

私が表示できる唯一の名前は、diplaypath を PageMediaSizeName にバインドすることによる Enum テキストです。これは特にユーザーフレンドリーではありません。

私が見つけたのは、selectedPrinter.GetPrintCapabilitiesAsXmlをファイルにダンプしてそれを見ると、必要なものがすべて得られるということです。すべてのプリンタの用紙サイズには、サイズと重要な表示名要素が含まれています。

私の質問は、selectedPrinter.GetPrintCapabilities で何か不足していますか、それとも selectedPrinter.GetPrintCapabilitiesAsXml のパーサーを作成して代わりにこの情報を使用する必要がありますか?

4

1 に答える 1

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
于 2010-09-02T12:13:54.857 に答える