첨부 소스 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

TestProject.zip
다운로드

▶ PageRangeDocumentPaginator.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;

namespace TestProject
{
    /// <summary>
    /// 페이지 범위 문서 페이지 관리자
    /// </summary>
    public class PageRangeDocumentPaginator : DocumentPaginator
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 시작 인덱스
        /// </summary>
        private int startIndex;

        /// <summary>
        /// 종료 인덱스
        /// </summary>
        private int endIndex;

        /// <summary>
        /// 문서 페이지 관리자
        /// </summary>
        private DocumentPaginator paginator;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Property
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 페이지 카운트 적절성 여부 - IsPageCountValid

        /// <summary>
        /// 페이지 카운트 유효성 여부
        /// </summary>
        public override bool IsPageCountValid
        {
            get
            {
                return true;
            }
        }

        #endregion
        #region 페이지 카운트 - PageCount

        /// <summary>
        /// 페이지 카운트
        /// </summary>
        public override int PageCount
        {
            get
            {
                if(this.startIndex > this.paginator.PageCount - 1)
                {
                    return 0;
                }

                if(this.startIndex > this.endIndex)
                {
                    return 0;
                }

                return this.endIndex - this.startIndex + 1;
            }
        }

        #endregion
        #region 페이지 크기 - PageSize

        /// <summary>
        /// 페이지 크기
        /// </summary>
        public override Size PageSize
        {
            get
            {
                return this.paginator.PageSize;
            }
            set
            {
                this.paginator.PageSize = value;
            }
        }

        #endregion
        #region 문서 페이지 관리자 소스 - Source

        /// <summary>
        /// 문서 페이지 관리자 소스
        /// </summary>
        public override IDocumentPaginatorSource Source
        {
            get
            {
                return this.paginator.Source;
            }
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - PageRangeDocumentPaginator(paginator, pageRange)

        /// <summary>
        /// 생성자
        /// </summary>
        /// <param name="paginator">문서 페이지 관리자</param>
        /// <param name="pageRange">페이지 범위</param>
        public PageRangeDocumentPaginator(DocumentPaginator paginator, PageRange pageRange)
        {
            this.paginator  = paginator;
            this.startIndex = pageRange.PageFrom - 1;
            this.endIndex   = pageRange.PageTo - 1;

            this.endIndex = Math.Min(this.endIndex, this.paginator.PageCount - 1);
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 페이지 구하기 - GetPage(pageNumber)

        /// <summary>
        /// 페이지 구하기
        /// </summary>
        /// <param name="pageNumber">페이지 번호</param>
        /// <returns>문서 페이지</returns>
        public override DocumentPage GetPage(int pageNumber)
        {
            return this.paginator.GetPage(pageNumber + this.startIndex);
        }

        #endregion
    }
}

 

728x90

 

▶ MainWindow.xaml

<Window x:Class="TestProject.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="800"
    Height="600"
    Title="DocumentPaginator 클래스 : XPS 문서 파일 부분 인쇄하기"
    FontFamily="나눔고딕코딩"
    FontSize="16">
    <Window.Resources>
        <Style TargetType="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="DocumentViewer">
                        <Border
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}"
                            Focusable="False">
                            <Grid KeyboardNavigation.TabNavigation="Local">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto" />
                                    <RowDefinition Height="*"    />
                                    <RowDefinition Height="Auto" />
                                </Grid.RowDefinitions>
                                <ToolBar Grid.Row="0"
                                    ToolBarTray.IsLocked="True"
                                    KeyboardNavigation.TabNavigation="Continue">
                                    <Button
                                        Command="NavigationCommands.IncreaseZoom" 
                                        CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}"
                                        Content="확대" />
                                    <Separator />
                                    <Button
                                        Command="NavigationCommands.DecreaseZoom" 
                                        CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}"
                                        Content="축소" />
                                    <Separator />
                                    <Button
                                        Command="NavigationCommands.Zoom"
                                        CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}"
                                        CommandParameter="100.0"
                                        Content="실제 크기" />
                                    <Separator />
                                    <Button
                                        Command="DocumentViewer.FitToWidthCommand" 
                                        CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}"
                                        Content="너비 맞춤" />
                                    <Separator />
                                    <Button
                                        Command="DocumentViewer.FitToMaxPagesAcrossCommand" 
                                        CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}"
                                        CommandParameter="1"
                                        Content="전체 페이지" />
                                    <Separator />
                                    <Button
                                        Command="DocumentViewer.FitToMaxPagesAcrossCommand" 
                                        CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}"
                                        CommandParameter="2" 
                                        Content="2 페이지" />
                                </ToolBar>
                                <ScrollViewer x:Name="PART_ContentHost" Grid.Row="1"
                                    HorizontalScrollBarVisibility="Auto"
                                    CanContentScroll="true"
                                    IsTabStop="true" />
                                <ContentControl x:Name="PART_FindToolBarHost" Grid.Row="2" />
                            </Grid>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <Grid Margin="10">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*"    />
        </Grid.RowDefinitions>
        <Button Name="printButton"
            HorizontalAlignment="Left"
            Margin="0  0 0 10"
            Width="100"
            Height="30"
            Content="인쇄하기" />
        <DocumentViewer x:Name="documentViewer" Grid.Row="1"
            BorderBrush="Black"
            BorderThickness="1">
        </DocumentViewer>
    </Grid>
</Window>

 

300x250

 

▶ MainWindow.xaml.cs

using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Xps.Packaging;

namespace TestProject
{
    /// <summary>
    /// 메인 윈도우
    /// </summary>
    public partial class MainWindow : Window
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 고정 문서
        /// </summary>
        private FixedDocument fixedDocument;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - MainWindow()

        /// <summary>
        /// 생성자
        /// </summary>
        public MainWindow()
        {
            InitializeComponent();

            Loaded                 += Window_Loaded;
            this.printButton.Click += printButton_Click;
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region 윈도우 로드시 처리하기 - Window_Loaded(sender, e)

        /// <summary>
        /// 윈도우 로드시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            XpsDocument xpsDocument = new XpsDocument("DOCUMENT/sample.xps", FileAccess.Read);

            FixedDocumentSequence sequence = xpsDocument.GetFixedDocumentSequence();

            this.fixedDocument = sequence.References[0].GetDocument(false);

            this.documentViewer.Document = this.fixedDocument.DocumentPaginator.Source;
        }

        #endregion
        #region 인쇄하기 버튼 클릭시 처리하기 - printButton_Click(sender, e)

        /// <summary>
        /// 인쇄하기 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void printButton_Click(object sender, RoutedEventArgs e)
        {
            PrintDialog printDialog = new PrintDialog();

            printDialog.UserPageRangeEnabled = true;

            if(printDialog.ShowDialog().GetValueOrDefault())
            {
                DocumentPaginator paginator = this.fixedDocument.DocumentPaginator;

                if(printDialog.PageRangeSelection == PageRangeSelection.UserPages)
                {
                    paginator = new PageRangeDocumentPaginator(this.fixedDocument.DocumentPaginator, printDialog.PageRange);
                }

                printDialog.PrintDocument(paginator, "Sample");
            }
        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요