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

TestProject.zip
다운로드
result.xps
다운로드

▶ BannerDocumentPaginator.cs

using System;
using System.Globalization;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Media;

namespace TestProject
{
    /// <summary>
    /// 배너 문서 페이지 관리자
    /// </summary>
    public class BannerDocumentPaginator : DocumentPaginator
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 페이지 크기
        /// </summary>
        private Size pageSize;

        /// <summary>
        /// 테두리 펜
        /// </summary>
        private Pen borderPen = new Pen(Brushes.Transparent, 1);

        /// <summary>
        /// 타입 페이스
        /// </summary>
        private Typeface typeface = new Typeface(string.Empty);

        /// <summary>
        /// 텍스트
        /// </summary>
        private string text = string.Empty;

        /// <summary>
        /// 최대 크기
        /// </summary>
        private Size maximumSize = new Size(0, 0);

        #endregion

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

        #region 페이지 번호 무결성 여부 - IsPageCountValid

        /// <summary>
        /// 페이지 번호 무결성 여부
        /// </summary>
        public override bool IsPageCountValid
        {
            get
            {
                foreach(char character in this.text)
                {
                    FormattedText formattedText = GetFormattedText(character, this.typeface, 100);

                    this.maximumSize.Width  = Math.Max(this.maximumSize.Width , formattedText.Width );
                    this.maximumSize.Height = Math.Max(this.maximumSize.Height, formattedText.Height);
                }

                return true;
            }
        }

        #endregion
        #region 페이지 수 - PageCount

        /// <summary>
        /// 페이지 수
        /// </summary>
        public override int PageCount
        {
            get
            {
                return this.text == null ? 0 : this.text.Length;
            }
        }

        #endregion
        #region 페이지 크기 - PageSize

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

        #endregion
        #region 소스 - Source

        /// <summary>
        /// 소스
        /// </summary>
        public override IDocumentPaginatorSource Source
        {
            get
            {
                return null;
            }
        }

        #endregion
        #region 타입 페이스 - Typeface

        /// <summary>
        /// 타입 페이스
        /// </summary>
        public Typeface Typeface
        {
            set
            {
                this.typeface = value;
            }
            get
            {
                return this.typeface;
            }
        }

        #endregion
        #region 텍스트 - Text

        /// <summary>
        /// 텍스트
        /// </summary>
        public string Text
        {
            set
            {
                this.text = value;
            }
            get
            {
                return this.text;
            }
        }

        #endregion

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

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

        /// <summary>
        /// 페이지 구하기
        /// </summary>
        /// <param name="pageNumber">페이지 번호</param>
        /// <returns>DocumentPage</returns>
        public override DocumentPage GetPage(int pageNumber)
        {
            DrawingVisual drawingVisual = new DrawingVisual();

            DrawingContext drawingContext = drawingVisual.RenderOpen();

            #region 페이지 내 일정 영역만 출력하면 페이지가 정상적으로 출력되지 않기 때문에 아래 코드를 실행한다.

            drawingContext.DrawRectangle(Brushes.Transparent, this.borderPen, new Rect(PageSize));

            #endregion

            double factor = Math.Min((PageSize.Width - 96) / this.maximumSize.Width, (PageSize.Height - 96) / this.maximumSize.Height);

            FormattedText formattedText = GetFormattedText(this.text[pageNumber], this.typeface, factor * 100);

            Point textPoint = new Point((PageSize.Width - formattedText.Width) / 2, (PageSize.Height - formattedText.Height) / 2);

            drawingContext.DrawText(formattedText, textPoint);

            drawingContext.Close();

            return new DocumentPage(drawingVisual);
        }

        #endregion

        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region 포맷 텍스트 구하기 - GetFormattedText(character, typeface, fontSize)

        /// <summary>
        /// 포맷 텍스트 구하기
        /// </summary>
        /// <param name="character">문자</param>
        /// <param name="typeface">타입 페이스</param>
        /// <param name="fontSize">폰트 크기</param>
        /// <returns>FormattedText</returns>
        private FormattedText GetFormattedText(char character, Typeface typeface, double fontSize)
        {
            return new FormattedText
            (
                character.ToString(),
                CultureInfo.CurrentCulture,
                FlowDirection.LeftToRight,
                typeface,
                fontSize,
                Brushes.Black
            );
        }

        #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 클래스 : 배너 문자 출력하기"
    FontFamily="나눔고딕코딩"
    FontSize="16">
    <Grid>
        <StackPanel
            HorizontalAlignment="Center"
            VerticalAlignment="Center">
            <TextBox Name="textBox"
                Margin="10"
                Width="300"
                Height="25"
                VerticalContentAlignment="Center"
                Text="BANNER" />
            <Button Name="printButton"
                Margin="10"
                Width="100"
                Height="30"
                Content="인쇄하기" />
        </StackPanel>
    </Grid>
</Window>

 

300x250

 

▶ MainWindow.xaml.cs

using System.Printing;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

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

        #region 생성자 - MainWindow()

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

            this.printButton.Click += printButton_Click;
        }

        #endregion

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

        #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();

            if(printDialog.ShowDialog().GetValueOrDefault())
            {
                PrintTicket printTicket = printDialog.PrintTicket;

                printTicket.PageOrientation = PageOrientation.Portrait;

                printDialog.PrintTicket = printTicket;

                BannerDocumentPaginator paginator = new BannerDocumentPaginator();

                paginator.PageSize = new Size
                (
                    printDialog.PrintableAreaWidth,
                    printDialog.PrintableAreaHeight
                );
                paginator.Typeface = new Typeface
                (
                    new FontFamily("나눔고딕코딩"),
                    FontStyles.Normal,
                    FontWeights.Bold,
                    FontStretches.Normal
                );
                paginator.Text     = this.textBox.Text;

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

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

댓글을 달아 주세요