Visual Basic で簡単レポート作成（３）ページ指定印刷

はじめに
---
この記事は、
『[Visual Basic で簡単レポート作成](https://qiita.com/cyberCatvb/items/3db7aeea414424f6776b)』
『[Visual Basic で簡単レポート作成（２）複数ページのレポート](https://qiita.com/cyberCatvb/items/54b83e34a68f5d4ae666)』
の続きです。`MainWindow.xaml`など諸々は、これらが前提となっていますので、是非とも見てみてください。

印刷するページを指定したい
---
`FixedDocument`で作る`DocumentViewer`は以下のように最初から色々と機能するツールバー・ボタンが備わっていて便利だ。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/294209/878dac26-740e-77be-2c8f-e74d3e7491d1.png)
ツールバー左端の、印刷ボタン
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/294209/e3040167-931c-4be0-02af-1bf6ee68f04c.png)
をクリックすると以下の印刷ダイアログが現れる。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/294209/61747d5e-c9e4-4e50-abf3-582be5a59453.png)
しかし、ページ範囲のところは「すべて」以外はグレーアウトしていて選択できない。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/294209/0b6bb9df-4ef6-8daf-4a35-de7c6c6dae17.png)
そこで、今回は`DocumentViewer`をカスタマイズして「ページ指定」できるようにする方法を書く。ついでに古臭いボタンアイコンも入れ替えてみた:upside_down:




DocumentViewerのカスタマイズ
---
`DocumentViewer`のカスタマイズをするために、[**ここ**](https://docs.microsoft.com/ja-jp/dotnet/desktop/wpf/controls/documentviewer-styles-and-templates?view=netframeworkdesktop-4.8)にあるXAMLの例を使う。これを以下のように`ResourceDictionary`としてプロジェクトに追加する。

```xml:DocumentViewerDictionary.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style x:Key="{x:Type DocumentViewer}"
           TargetType="{x:Type DocumentViewer}">
      <Setter Property="Foreground"
              Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}" />
      <Setter Property="Background"
              Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" />
      <Setter Property="FocusVisualStyle"
              Value="{x:Null}" />
      <Setter Property="Template">
        <Setter.Value>
          <ControlTemplate TargetType="{x:Type DocumentViewer}">
            <Border BorderThickness="{TemplateBinding BorderThickness}"
                    BorderBrush="{TemplateBinding BorderBrush}"
                    Focusable="False">
              <Grid KeyboardNavigation.TabNavigation="Local">
                <Grid.Background>
                  <SolidColorBrush Color="{DynamicResource ControlLightColor}" />
                </Grid.Background>
                <Grid.RowDefinitions>
                  <RowDefinition Height="Auto" />
                  <RowDefinition Height="*" />
                  <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                <ToolBar ToolBarTray.IsLocked="True"
                         KeyboardNavigation.TabNavigation="Continue">
                  <Button Command="ApplicationCommands.Print"
                          CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}"
                          Content="Print" />
                  <Button Command="ApplicationCommands.Copy"
                          CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}"
                          Content="Copy" />
                  <Separator />
                  <Button Command="NavigationCommands.IncreaseZoom"
                          CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}"
                          Content="Zoom In" />
                  <Button Command="NavigationCommands.DecreaseZoom"
                          CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}"
                          Content="Zoom Out" />
                  <Separator />
                  <Button Command="NavigationCommands.Zoom"
                          CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}"
                          CommandParameter="100.0"
                          Content="Actual Size" />
                  <Button Command="DocumentViewer.FitToWidthCommand"
                          CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}"
                          Content="Fit to Width" />
                  <Button Command="DocumentViewer.FitToMaxPagesAcrossCommand"
                          CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}"
                          CommandParameter="1"
                          Content="Whole Page" />
                  <Button Command="DocumentViewer.FitToMaxPagesAcrossCommand"
                          CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}"
                          CommandParameter="2"
                          Content="Two Pages" />
                </ToolBar>
                <ScrollViewer Grid.Row="1"
                              CanContentScroll="true"
                              HorizontalScrollBarVisibility="Auto"
                              x:Name="PART_ContentHost"
                              IsTabStop="true">
                  <ScrollViewer.Background>
                    <LinearGradientBrush EndPoint="0.5,1"
                                         StartPoint="0.5,0">
                      <GradientStop Color="{DynamicResource ControlLightColor}"
                                    Offset="0" />
                      <GradientStop Color="{DynamicResource ControlMediumColor}"
                                    Offset="1" />
                    </LinearGradientBrush>
                  </ScrollViewer.Background>
                </ScrollViewer>
                <ContentControl Grid.Row="2"
                                x:Name="PART_FindToolBarHost"/>
              </Grid>
            </Border>
          </ControlTemplate>
        </Setter.Value>
      </Setter>
    </Style>
</ResourceDictionary>
```
これを追加してアプリを起動させると、以下のように表示される。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/294209/991dda2a-2cba-84cf-e0b3-f2f442f0f7bc.png)
ツールバー・ボタンがアイコンから文字列になってしまったが、それぞれのボタンをクリックすれば、今までどおり問題なく機能はする。
Buttonクリックのカスタマイズ
---
早速、ツールバー左端の印刷ボタンをカスタマイズする。上記XAMLでは`ToolBar`タグ内の最初の`Button`タグが印刷ボタンとなる。

```xml
 <Button Command="ApplicationCommands.Print"
```
これを

```xml
 <Button Command="{Binding PrintCommand}"
```
と書き換え、`MainWindowViewModel.vb`に`PrintCommand`プロパティとその実装を追記する。`PrintCommand`プロパティを動作させるには、NuGetで`Microsoft.Toolkit.Mvvm`をインストールして`RelayCommand`を使えばいい。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/294209/496ed5ca-cef4-b74b-5a5b-338885fe3fc6.png)



```vb:MainWindowViewModel.vb
Imports System.Printing
Imports Microsoft.Toolkit.Mvvm.Input

' 『Visual Basic で簡単レポート作成（２）』を元に、今回必要な部分だけ書いています
Public Class MainWindowViewModel

    Public Property PrintCommand As RelayCommand

    Public Sub New()
        PrintCommand = New RelayCommand(
            Sub()
                Dim dlg As New PrintDialog With {
                    .PageRangeSelection = PageRangeSelection.AllPages,
                    .UserPageRangeEnabled = True
                }
                ' 以下で印刷ダイアログを表示、ユーザーの入力待ち状態となる
                ' [印刷]クリック(True)か、[キャンセル]クリック(False)でコードに復帰
                If Not dlg.ShowDialog() Then Return
                If dlg.PageRangeSelection = PageRangeSelection.UserPages Then
                    Dim pageCount = ReportViewer.Pages.Count
                    If dlg.PageRange.PageTo > pageCount Then
                        MsgBox("総ページ数（" & pageCount & "ページ）を超えています。",
                               vbExclamation + vbOKOnly, "印刷")
                        Return
                    End If
                    dlg.PrintDocument(New DocPaginator(ReportViewer.DocumentPaginator, dlg.PageRange), "印刷")
                Else
                    Dim queue = dlg.PrintQueue
                    Dim writer = PrintQueue.CreateXpsDocumentWriter(queue)
                    writer.Write(ReportViewer)
                End If
            End Sub
        )
    End Sub
```
> If dlg.PageRangeSelection = PageRangeSelection.UserPages Then

で`dlg.PageRangeSelection`プロパティの値が以下のどの状態かを判定する。

[**PageRangeSelection列挙型**](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.controls.pagerangeselection?view=windowsdesktop-6.0)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/294209/d5400e7e-d4ec-b04f-530e-083902b11da4.png)
今回は`AllPages`か`UserPages`かの二択である。（その他の状態については別途実装が必要）
DocumentPaginatorを継承するDocPaginator
--
> dlg.PrintDocument(New DocPaginator(ReportViewer.DocumentPaginator, dlg.PageRange), "印刷")

この部分、`PrintDocument`メソッドの第一引数の`DocPaginator`は、複数のページ要素を作成できる抽象基本クラスである`DocumentPaginator`を継承した派生クラスで、[**ここ**](https://docs.microsoft.com/ja-jp/dotnet/desktop/wpf/documents/how-to-display-print-dialog?view=netdesktop-6.0)にあるものを使った。ここでページ指定した内容を印刷に反映させている。

```vb:DocPaginator.vb
Imports System.Reflection

Public Class DocPaginator : Inherits DocumentPaginator

    Private ReadOnly _documentPaginator As DocumentPaginator
    Private ReadOnly _startPageIndex As Integer
    Private ReadOnly _endPageIndex As Integer
    Private ReadOnly _pageCount As Integer

    Public Sub New(documentPaginator As DocumentPaginator, pageRange As PageRange)
        ' ドキュメントページネーターを設定
        _documentPaginator = documentPaginator
        ' ページインデックスを設定
        _startPageIndex = pageRange.PageFrom - 1
        _endPageIndex = pageRange.PageTo - 1
        ' ページ数を検証し、設定
        If _startPageIndex >= 0 AndAlso
            _endPageIndex >= 0 AndAlso
            _startPageIndex <= _documentPaginator.PageCount - 1 AndAlso
            _endPageIndex <= _documentPaginator.PageCount - 1 AndAlso
            _startPageIndex <= _endPageIndex _
        Then
            _pageCount = _endPageIndex - _startPageIndex + 1
        End If
    End Sub

    Public Overrides ReadOnly Property IsPageCountValid As Boolean
        Get
            Return True
        End Get
    End Property

    Public Overrides ReadOnly Property PageCount As Integer
        Get
            Return _pageCount
        End Get
    End Property

    Public Overrides ReadOnly Property Source As IDocumentPaginatorSource
        Get
            Return _documentPaginator.Source
        End Get
    End Property

    Public Overrides Property PageSize As Size
        Get
            Return _documentPaginator.PageSize
        End Get
        Set(value As Size)
            _documentPaginator.PageSize = value
        End Set
    End Property

    ' PrintDocument の内部で、指定したページ数の数だけ呼び出される
    ' 1ページずつ指定ページをディープコピーして新たな実体を作る
    ' 引数 pageNumber は 0 から始まる指定ページの通し番号
    Public Overrides Function GetPage(pageNumber As Integer) As DocumentPage
        Dim documentPage As DocumentPage = _documentPaginator.GetPage(_startPageIndex + pageNumber)
        '「FixedPage に別の FixedPage を含めることはできません。」の例外回避
        If documentPage.Visual.GetType() Is GetType(FixedPage) Then
            Dim fixedPage As FixedPage = documentPage.Visual
            Dim containerVisual = New ContainerVisual()
            For Each child As Object In fixedPage.Children
                ' child をディープコピー
                Dim childClone = CType(child.GetType().GetMethod("MemberwiseClone", BindingFlags.Instance Or BindingFlags.NonPublic).Invoke(child, Nothing), UIElement)
                ' child の _parent(FixedPage) を取得
                Dim parentField As FieldInfo = childClone.GetType().GetField("_parent", BindingFlags.Instance Or BindingFlags.NonPublic)
                If parentField IsNot Nothing Then
                    ' コピーした child の _parent(FixedPage) を Nothing にする
                    parentField.SetValue(childClone, Nothing)
                    containerVisual.Children.Add(childClone)
                End If
            Next
            Return New DocumentPage(containerVisual, documentPage.Size, documentPage.BleedBox, documentPage.ContentBox)
        End If
        Return documentPage
    End Function
End Class
```
ここまで書いて、アプリを起動して印刷ダイアログを表示させると、以下のように「ページ指定」ができるようになった:thumbsup:
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/294209/c77d50fc-9e7d-5e32-4b7b-6cced79fd9da.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/294209/d1229fb3-1742-14ab-9b71-6f4cd459e78a.png)

ツールバー・ボタンのアイコン設定
---
ツールバー・ボタンがこのままではあまりにも見栄えが悪いので、アイコンを設定する。
アイコンは[**ここ**](https://www.microsoft.com/en-us/download/details.aspx?id=35825)からダウンロードすると良い。他でも使える様々なアイコンをまとめて取得できる。
ダウンロードしたアイコンはアイコンの種類ごとにフォルダが分かれていて`.png`、`.svg`、`.xaml`の3つのファイル形式が格納されている。WPFなのでここは`.xaml`を使おう。
今回必要なアイコンたちを選び出して、前出の`DocumentViewerDictionary.xaml`内に埋め込む。
各ボタンをプロパティ要素構文に書き換え、

```xml:プロパティ要素構文
<Button ...>
   <Button.Content>
       <!-- ここにアイコンXAMLをコピペ -->
   </Button.Content> 
</Button>
```
`Button.Content`タグ内に、選び出した各アイコンXAMLをそのままコピペする。

以下はアイコン設定した`DocumentViewerDictionary.xaml`。

<details><summary>**ここをクリック（長いので閉じときます:sweat_smile:）**</summary><div>

```xml:DocumentViewerDictionary.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style x:Key="{x:Type DocumentViewer}"
           TargetType="{x:Type DocumentViewer}">
        <Setter Property="Foreground"
                Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}" />
        <Setter Property="Background"
                Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" />
        <Setter Property="FocusVisualStyle"
                Value="{x:Null}" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type DocumentViewer}">
                    <Border BorderThickness="{TemplateBinding BorderThickness}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            Focusable="False">
                        <Grid KeyboardNavigation.TabNavigation="Local">
                            <Grid.Background>
                                <SolidColorBrush Color="{DynamicResource {x:Static SystemColors.ControlLightColorKey}}" />
                            </Grid.Background>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="*" />
                                <RowDefinition Height="Auto" />
                            </Grid.RowDefinitions>
                            <ToolBar ToolBarTray.IsLocked="True"
                                     KeyboardNavigation.TabNavigation="Continue">
                                <Button Command="{Binding PrintCommand}" ToolTip="印刷"
                                        CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}">
                                    <Button.Content>
                                        <!-- Print_16x -->
                                        <Viewbox Width="16" Height="16" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
                                            <Rectangle Width="16" Height="16">
                                                <Rectangle.Fill>
                                                    <DrawingBrush>
                                                        <DrawingBrush.Drawing>
                                                            <DrawingGroup>
                                                                <DrawingGroup.Children>
                                                                    <GeometryDrawing Brush="#00FFFFFF" Geometry="F1M16,16L0,16 0,0 16,0z" />
                                                                    <GeometryDrawing Brush="#FFF6F6F6" Geometry="F1M15.2686,6.73C14.7906,6.252,14.1786,6,13.4996,6L12.9996,6 12.9996,0 2.9996,0 2.9996,6 2.4996,6C1.8206,6 1.2076,6.252 0.730599999999999,6.73 0.252599999999999,7.208 -0.000400000000000844,7.82 -0.000400000000000844,8.5L-0.000400000000000844,14 2.9996,14 2.9996,16 12.9996,16 12.9996,14 15.9996,14 15.9996,8.5C15.9996,7.822,15.7476,7.21,15.2686,6.73" />
                                                                    <GeometryDrawing Brush="#FFEFEFF0" Geometry="F1M5,14L11,14 11,12 5,12z M11,2L5,2 5,7 11,7z M14,8.5L14,12 12,12 12,11 4,11 4,12 2,12 2,8.5C2,8.354 2.047,8.234 2.141,8.141 2.234,8.047 2.354,8 2.5,8L13.5,8C13.646,8 13.766,8.047 13.859,8.141 13.953,8.234 14,8.354 14,8.5" />
                                                                    <GeometryDrawing Brush="#FF424242" Geometry="F1M11,12L5,12 5,14 11,14z M5,7L11,7 11,2 5,2z M3,10L4,10 4,9 3,9z M14,8.5C14,8.354 13.953,8.234 13.859,8.141 13.766,8.047 13.646,8 13.5,8L2.5,8C2.354,8 2.234,8.047 2.141,8.141 2.047,8.234 2,8.354 2,8.5L2,12 4,12 4,11 12,11 12,12 14,12z M15,13L12,13 12,15 4,15 4,13 1,13 1,8.5C1,8.083 1.146,7.729 1.437,7.437 1.729,7.146 2.083,7 2.5,7L4,7 4,1 12,1 12,7 13.5,7C13.916,7 14.271,7.146 14.563,7.437 14.854,7.729 15,8.083 15,8.5z" />
                                                                </DrawingGroup.Children>
                                                            </DrawingGroup>
                                                        </DrawingBrush.Drawing>
                                                    </DrawingBrush>
                                                </Rectangle.Fill>
                                            </Rectangle>
                                        </Viewbox>
                                    </Button.Content>
                                </Button>
                                <Button Command="ApplicationCommands.Copy" 
                                        CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}"
                                        IsEnabled="False">
                                    <Button.Content>
                                        <!-- Copy_16x -->
                                        <Viewbox Width="16" Height="16" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
                                            <Rectangle Width="16" Height="16">
                                                <Rectangle.Fill>
                                                    <DrawingBrush>
                                                        <DrawingBrush.Drawing>
                                                            <DrawingGroup>
                                                                <DrawingGroup.Children>
                                                                    <GeometryDrawing Brush="#00FFFFFF" Geometry="F1M16,16L0,16 0,0 16,0z" />
                                                                    <GeometryDrawing Brush="#FFF6F6F6" Geometry="F1M0.9999,-0.000199999999999534L0.9999,13.0008 5.0009,13.0008 5.0009,15.9998 15.9999,15.9998 15.9999,7.3788 11.6209,3.0008 10.6049,3.0008 7.6179,-0.000199999999999534z" />
                                                                    <GeometryDrawing Brush="#FF424242" Geometry="F1M14,14L7,14 7,5 10,5 10,9 14,9z M6,11L3,11 3,2 6.798,2 8.81,4 6,4z M11,5.207L13.793,8 11,8z M11.207,4L10.19,4 7.202,1 2,1 2,12 6,12 6,15 15,15 15,7.793z" />
                                                                    <GeometryDrawing Brush="#FFF0EFF1" Geometry="F1M14,14L7,14 7,5 10,5 10,9 14,9z M6,11L3,11 3,2 6.798,2 8.81,4 6,4z M11,5.207L13.793,8 11,8z" />
                                                                </DrawingGroup.Children>
                                                            </DrawingGroup>
                                                        </DrawingBrush.Drawing>
                                                    </DrawingBrush>
                                                </Rectangle.Fill>
                                            </Rectangle>
                                        </Viewbox>
                                    </Button.Content>
                                </Button>
                                <Separator />
                                <Button Command="NavigationCommands.IncreaseZoom" ToolTip="ズームイン"
                                       CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}">
                                    <Button.Content>
                                        <!-- ZoomIn_16x -->
                                        <Viewbox Width="16" Height="16" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
                                            <Rectangle Width="16" Height="16">
                                                <Rectangle.Fill>
                                                    <DrawingBrush>
                                                        <DrawingBrush.Drawing>
                                                            <DrawingGroup>
                                                                <DrawingGroup.Children>
                                                                    <GeometryDrawing Brush="#00FFFFFF" Geometry="F1M16,16L0,16 0,0 16,0z" />
                                                                    <GeometryDrawing Brush="#FFF6F6F6" Geometry="F1M14.5,16C14.1,16,13.723,15.844,13.439,15.561L9.909,12.03C8.889,12.66 7.707,13 6.5,13 2.916,13 0,10.084 0,6.5 0,2.916 2.916,0 6.5,0 10.084,0 13,2.916 13,6.5 13,7.707 12.66,8.889 12.03,9.909L15.561,13.439C16.146,14.024 16.146,14.976 15.561,15.561 15.277,15.844 14.9,16 14.5,16" />
                                                                    <GeometryDrawing Brush="#FF424242" Geometry="F1M6.5,11C4.019,11 2,8.981 2,6.5 2,4.019 4.019,2 6.5,2 8.981,2 11,4.019 11,6.5 11,8.981 8.981,11 6.5,11 M14.854,14.146L10.724,10.017C11.52,9.063 12,7.836 12,6.5 12,3.467 9.532,1 6.5,1 3.467,1 1,3.467 1,6.5 1,9.532 3.467,12 6.5,12 7.836,12 9.063,11.52 10.017,10.724L14.146,14.854C14.244,14.951 14.372,15 14.5,15 14.628,15 14.756,14.951 14.854,14.854 15.049,14.658 15.049,14.342 14.854,14.146" />
                                                                    <GeometryDrawing Brush="#FF00529C" Geometry="F1M9,6L9,7 7,7 7,9 6,9 6,7 4,7 4,6 6,6 6,4 7,4 7,6z" />
                                                                </DrawingGroup.Children>
                                                            </DrawingGroup>
                                                        </DrawingBrush.Drawing>
                                                    </DrawingBrush>
                                                </Rectangle.Fill>
                                            </Rectangle>
                                        </Viewbox>
                                    </Button.Content>
                                </Button>
                                <Button Command="NavigationCommands.DecreaseZoom" ToolTip="ズームアウト"
                                       CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}">
                                    <Button.Content>
                                        <!-- ZoomOut_16x -->
                                        <Viewbox Width="16" Height="16" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
                                            <Rectangle Width="16" Height="16">
                                                <Rectangle.Fill>
                                                    <DrawingBrush>
                                                        <DrawingBrush.Drawing>
                                                            <DrawingGroup>
                                                                <DrawingGroup.Children>
                                                                    <GeometryDrawing Brush="#00FFFFFF" Geometry="F1M16,16L0,16 0,0 16,0z" />
                                                                    <GeometryDrawing Brush="#FFF6F6F6" Geometry="F1M14.5,16C14.1,16,13.723,15.844,13.439,15.561L9.909,12.03C8.889,12.66 7.707,13 6.5,13 2.916,13 0,10.084 0,6.5 0,2.916 2.916,0 6.5,0 10.084,0 13,2.916 13,6.5 13,7.707 12.66,8.889 12.03,9.909L15.561,13.439C16.146,14.024 16.146,14.976 15.561,15.561 15.277,15.844 14.9,16 14.5,16" />
                                                                    <GeometryDrawing Brush="#FF424242" Geometry="F1M6.5,11C4.019,11 2,8.981 2,6.5 2,4.019 4.019,2 6.5,2 8.981,2 11,4.019 11,6.5 11,8.981 8.981,11 6.5,11 M14.854,14.146L10.724,10.017C11.52,9.063 12,7.836 12,6.5 12,3.467 9.532,1 6.5,1 3.467,1 1,3.467 1,6.5 1,9.532 3.467,12 6.5,12 7.836,12 9.063,11.52 10.017,10.724L14.146,14.854C14.244,14.951 14.372,15 14.5,15 14.628,15 14.756,14.951 14.854,14.854 15.049,14.658 15.049,14.342 14.854,14.146" />
                                                                    <GeometryDrawing Brush="#FF00529C" Geometry="F1M4,7L9,7 9,6 4,6z" />
                                                                </DrawingGroup.Children>
                                                            </DrawingGroup>
                                                        </DrawingBrush.Drawing>
                                                    </DrawingBrush>
                                                </Rectangle.Fill>
                                            </Rectangle>
                                        </Viewbox>
                                    </Button.Content>
                                </Button>
                                <Separator />
                                <Button Command="NavigationCommands.Zoom" ToolTip="実際のサイズで表示"
                                       CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}"
                                       CommandParameter="100.0">
                                    <Button.Content>
                                        <!-- ZoomToOriginalSize_16x -->
                                        <Viewbox Width="16" Height="16" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
                                            <Rectangle Width="16" Height="16">
                                                <Rectangle.Fill>
                                                    <DrawingBrush>
                                                        <DrawingBrush.Drawing>
                                                            <DrawingGroup>
                                                                <DrawingGroup.Children>
                                                                    <GeometryDrawing Brush="#00FFFFFF" Geometry="F1M16,16L0,16 0,0 16,0z" />
                                                                    <GeometryDrawing Brush="#FFF6F6F6" Geometry="F1M16,10L8.949,10C8.974,10.164 9,10.328 9,10.5 9,12.43 7.43,14 5.5,14 5.179,14 4.86,13.954 4.551,13.863L2.414,16 1.586,16 0,14.414 0,13.586 2.137,11.449C2.046,11.139 2,10.822 2,10.5 2,10.328 2.026,10.164 2.051,10L0,10 0,0 16,0z" />
                                                                    <GeometryDrawing Brush="#FF424242" Geometry="F1M3.957,3.3672C3.866,3.4152,3.763,3.4442,3.666,3.4842L3.666,4.2562C3.706,4.2432 3.747,4.2322 3.785,4.2172 3.857,4.1852 3.924,4.1522 3.988,4.1152 4.052,4.0802 4.107,4.0402 4.153,3.9992L4.153,7.0002 5,7.0002 5,3.0352 4.486,3.0352C4.329,3.1562,4.152,3.2672,3.957,3.3672 M12.071,5.0062C12.071,4.1092 11.887,3.6622 11.518,3.6622 11.123,3.6622 10.926,4.1232 10.926,5.0452 10.926,5.9142 11.119,6.3492 11.506,6.3492 11.883,6.3492 12.071,5.9002 12.071,5.0062 M13,4.9782C13,5.6312 12.869,6.1312 12.605,6.4782 12.344,6.8262 11.967,7.0002 11.477,7.0002 10.492,7.0002 10,6.3532 10,5.0662 10,4.3982 10.133,3.8892 10.399,3.5392 10.666,3.1892 11.052,3.0132 11.558,3.0132 12.519,3.0132 13,3.6682 13,4.9782 M8.071,4.9922C8.071,4.0962 7.887,3.6482 7.518,3.6482 7.123,3.6482 6.926,4.1092 6.926,5.0312 6.926,5.9002 7.119,6.3362 7.506,6.3362 7.883,6.3362 8.071,5.8872 8.071,4.9922 M9,4.9652C9,5.6172 8.869,6.1172 8.605,6.4652 8.344,6.8122 7.967,6.9862 7.477,6.9862 6.492,6.9862 6,6.3402 6,5.0532 6,4.3852 6.133,3.8762 6.399,3.5252 6.666,3.1762 7.052,3.0002 7.558,3.0002 8.519,3.0002 9,3.6542 9,4.9652 M15,1.0002L15,9.0002 8.649,9.0002C8.47,8.6262,8.234,8.2862,7.941,8.0002L14,8.0002 14,2.0002 2,2.0002 2,8.0002 3.059,8.0002C2.766,8.2862,2.53,8.6262,2.351,9.0002L1,9.0002 1,1.0002z" />
                                                                    <GeometryDrawing Brush="#FF00539C" Geometry="F1M5.5,9C4.672,9 4,9.672 4,10.5 4,11.328 4.672,12 5.5,12 6.328,12 7,11.328 7,10.5 7,9.672 6.328,9 5.5,9 M8,10.5C8,11.881 6.881,13 5.5,13 5.067,13 4.667,12.881 4.313,12.688L2,15 1,14 3.312,11.688C3.12,11.332 3,10.932 3,10.5 3,9.119 4.119,8 5.5,8 6.881,8 8,9.119 8,10.5" />
                                                                    <GeometryDrawing Brush="#FFF0EFF1" Geometry="F1M13,4.9785C13,3.6675 12.52,3.0135 11.558,3.0135 11.052,3.0135 10.666,3.1895 10.399,3.5395 10.133,3.8895 10,4.3985 10,5.0665 10,6.3535 10.492,6.9995 11.477,6.9995 11.967,6.9995 12.344,6.8265 12.605,6.4785 12.869,6.1305 13,5.6305 13,4.9785 M9,4.9645C9,3.6545 8.52,2.9995 7.558,2.9995 7.052,2.9995 6.666,3.1755 6.399,3.5255 6.133,3.8755 6,4.3845 6,5.0525 6,6.3395 6.492,6.9865 7.477,6.9865 7.967,6.9865 8.344,6.8125 8.605,6.4645 8.869,6.1175 9,5.6175 9,4.9645 M5,3.0355L4.486,3.0355C4.329,3.1565 4.152,3.2675 3.957,3.3675 3.866,3.4155 3.763,3.4445 3.666,3.4845L3.666,4.2555C3.706,4.2435 3.747,4.2325 3.785,4.2165 3.857,4.1855 3.924,4.1525 3.988,4.1155 4.052,4.0805 4.107,4.0405 4.153,3.9995L4.153,6.9995 5,6.9995z M14,1.9995L14,7.9995 7.942,7.9995C7.311,7.3835 6.45,6.9995 5.5,6.9995 4.55,6.9995 3.689,7.3835 3.058,7.9995L2,7.9995 2,1.9995z M7.518,3.6485C7.123,3.6485 6.926,4.1095 6.926,5.0315 6.926,5.9005 7.119,6.3355 7.506,6.3355 7.883,6.3355 8.071,5.8865 8.071,4.9925 8.071,4.0955 7.887,3.6485 7.518,3.6485 M11.518,3.6625C11.123,3.6625 10.926,4.1235 10.926,5.0445 10.926,5.9145 11.119,6.3495 11.506,6.3495 11.883,6.3495 12.071,5.9005 12.071,5.0055 12.071,4.1095 11.887,3.6625 11.518,3.6625 M7,10.4995C7,11.3285 6.328,11.9995 5.5,11.9995 4.672,11.9995 4,11.3285 4,10.4995 4,9.6715 4.672,8.9995 5.5,8.9995 6.328,8.9995 7,9.6715 7,10.4995" />
                                                                </DrawingGroup.Children>
                                                            </DrawingGroup>
                                                        </DrawingBrush.Drawing>
                                                    </DrawingBrush>
                                                </Rectangle.Fill>
                                            </Rectangle>
                                        </Viewbox>
                                    </Button.Content>
                                </Button>
                                <Button Command="DocumentViewer.FitToWidthCommand" ToolTip="幅を合わせて表示"
                                       CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}">
                                    <Button.Content>
                                        <!-- ZoomToWidth_16x -->
                                        <Viewbox Width="16" Height="16" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
                                            <Rectangle Width="16" Height="16">
                                                <Rectangle.Fill>
                                                    <DrawingBrush>
                                                        <DrawingBrush.Drawing>
                                                            <DrawingGroup>
                                                                <DrawingGroup.Children>
                                                                    <GeometryDrawing Brush="#00FFFFFF" Geometry="F1M16,16L0,16 0,0 16,0z" />
                                                                    <GeometryDrawing Brush="#FFF6F6F6" Geometry="F1M12,9.8789L11.554,9.4329C11.622,9.2909,11.684,9.1469,11.737,8.9999L12,8.9999z M15,2.9999L12,2.9999 12,5.9999 11.743,5.9999C11.124,4.2539 9.456,2.9999 7.5,2.9999 5.544,2.9999 3.876,4.2539 3.257,5.9999L3,5.9999 3,2.9999 0,2.9999 0,11.9999 3,11.9999 3,8.9999 3.257,8.9999C3.876,10.7459 5.544,11.9999 7.5,11.9999 8.174,11.9999 8.829,11.8479 9.433,11.5539L12.439,14.5609C12.723,14.8439 13.1,14.9999 13.5,14.9999 13.9,14.9999 14.277,14.8439 14.561,14.5609 15.146,13.9759 15.146,13.0249 14.561,12.4399L14.121,11.9999 15,11.9999z" />
                                                                    <GeometryDrawing Brush="#FF424242" Geometry="F1M10,7.5C10,6.122 8.878,5 7.5,5 6.122,5 5,6.122 5,7.5 5,8.878 6.122,10 7.5,10 8.878,10 10,8.878 10,7.5 M13.854,13.146C14.049,13.342 14.049,13.658 13.854,13.854 13.756,13.951 13.628,14 13.5,14 13.372,14 13.244,13.951 13.146,13.854L9.646,10.354C9.626,10.333 9.616,10.306 9.6,10.282 9.013,10.727 8.291,11 7.5,11 5.57,11 4,9.43 4,7.5 4,5.57 5.57,4 7.5,4 9.43,4 11,5.57 11,7.5 11,8.291 10.727,9.013 10.282,9.6 10.306,9.616 10.333,9.626 10.354,9.646z M13,4L13,7 12,7 12,8 13,8 13,11 14,11 14,4z M2,7L3,7 3,8 2,8 2,11 1,11 1,4 2,4z" />
                                                                </DrawingGroup.Children>
                                                            </DrawingGroup>
                                                        </DrawingBrush.Drawing>
                                                    </DrawingBrush>
                                                </Rectangle.Fill>
                                            </Rectangle>
                                        </Viewbox>
                                    </Button.Content>
                                </Button>
                                <Button Command="DocumentViewer.FitToMaxPagesAcrossCommand" ToolTip="全体表示"
                                       CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}"
                                       CommandParameter="1">
                                    <Button.Content>
                                        <!-- ZoomToFit_16x -->
                                        <Viewbox Width="16" Height="16" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
                                            <Rectangle Width="16" Height="16">
                                                <Rectangle.Fill>
                                                    <DrawingBrush>
                                                        <DrawingBrush.Drawing>
                                                            <DrawingGroup>
                                                                <DrawingGroup.Children>
                                                                    <GeometryDrawing Brush="#00FFFFFF" Geometry="F1M16,16L0,16 0,0 16,0z" />
                                                                    <GeometryDrawing Brush="#FFF6F6F6" Geometry="F1M12.9766,15C12.9766,15,14.0066,15.98,14.0296,16L15.9736,16C15.9816,15.993,15.9926,15.991,15.9996,15.984L15.9996,13.755 14.9996,12.783 14.9996,3 12.9996,3 12.9996,0 1.9996,0 1.9996,3 -0.000400000000000844,3 -0.000400000000000844,13 1.9996,13 1.9996,15z" />
                                                                    <GeometryDrawing Brush="#FF424242" Geometry="F1M14,12L13,12 13,4 14,4z M3,13L12,13 12,14 3,14z M12,2L3,2 3,1 12,1z M1,4L2,4 2,12 1,12z M8,7L10,7 10,8 8,8 8,10 7,10 7,8 5,8 5,7 7,7 7,5 8,5z M3.875,7.5C3.875,5.501 5.501,3.875 7.5,3.875 9.499,3.875 11.125,5.501 11.125,7.5 11.125,9.5 9.499,11.126 7.5,11.126 5.501,11.126 3.875,9.5 3.875,7.5 M15.349,14.517L11.092,10.378C11.728,9.586 12.125,8.593 12.125,7.5 12.125,4.95 10.05,2.875 7.5,2.875 4.95,2.875 2.875,4.95 2.875,7.5 2.875,10.051 4.95,12.126 7.5,12.126 8.596,12.126 9.592,11.726 10.385,11.086L14.651,15.233C14.749,15.328 14.874,15.375 15,15.375 15.13,15.375 15.261,15.324 15.358,15.224 15.551,15.025 15.547,14.709 15.349,14.517" />
                                                                </DrawingGroup.Children>
                                                            </DrawingGroup>
                                                        </DrawingBrush.Drawing>
                                                    </DrawingBrush>
                                                </Rectangle.Fill>
                                            </Rectangle>
                                        </Viewbox>
                                    </Button.Content>
                                </Button>
                                <Button Command="DocumentViewer.FitToMaxPagesAcrossCommand" ToolTip="横2ページ表示"
                                       CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}"
                                       CommandParameter="2">
                                    <Button.Content>
                                        <!-- TwoColumns_16x -->
                                        <Viewbox Width="16" Height="16" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
                                            <Rectangle Width="16" Height="16">
                                                <Rectangle.Fill>
                                                    <DrawingBrush>
                                                        <DrawingBrush.Drawing>
                                                            <DrawingGroup>
                                                                <DrawingGroup.Children>
                                                                    <GeometryDrawing Brush="#00FFFFFF" Geometry="F1M16,16L0,16 0,0 16,0z" />
                                                                    <GeometryDrawing Brush="#FFF6F6F6" Geometry="F1M1,15L16,15 16,0 1,0z" />
                                                                    <GeometryDrawing Brush="#FF424242" Geometry="F1M14,13L9,13 9,2 14,2z M8,13L3,13 3,2 8,2z M2,14L15,14 15,1 2,1z" />
                                                                    <GeometryDrawing Brush="#FFF0EFF1" Geometry="F1M9,13L14,13 14,2 9,2z M3,2L8,2 8,13 3,13z" />
                                                                </DrawingGroup.Children>
                                                            </DrawingGroup>
                                                        </DrawingBrush.Drawing>
                                                    </DrawingBrush>
                                                </Rectangle.Fill>
                                            </Rectangle>
                                        </Viewbox>
                                    </Button.Content>
                                </Button>
                            </ToolBar>
                            <ScrollViewer Grid.Row="1"
                                          CanContentScroll="true"
                                          HorizontalScrollBarVisibility="Auto"
                                          x:Name="PART_ContentHost"
                                          IsTabStop="true">
                                <ScrollViewer.Background>
                                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                        <GradientStop Color="{DynamicResource {x:Static SystemColors.ControlLightColorKey}}"
                                                      Offset="0" />
                                        <GradientStop Color="{DynamicResource {x:Static SystemColors.ControlLightColorKey}}"
                                                      Offset="1" />
                                    </LinearGradientBrush>
                                </ScrollViewer.Background>
                            </ScrollViewer>
                            <ContentControl Grid.Row="2" x:Name="PART_FindToolBarHost" IsEnabled="False"/>
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

```
</div></details>


`ResourceDictionary`は`Application.xaml`に以下のように書けば`MainWindow`に反映される。

```xml:Application.xaml
<Application x:Class="Application"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/Dictionaries/DocumentViewerDictionary.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>
```
> \<ResourceDictionary Source="/Dictionaries/DocumentViewerDictionary.xaml"/>

上記は、`DocumentViewerDictionary.xaml`が`Dictionaries`フォルダにあるため、こういう書き方になる。各自のフォルダ構成に合わせて必要なら書き換える。
できあがり
---
アプリを立ち上げると以下のとおり。少し見栄えが良くなった:sunglasses:
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/294209/cf9184a1-b72d-39ba-c69b-4fc013b0649f.png)