본문 바로가기
.NET WPF

[XPS] Fxied Documents

by 태디 2007. 8. 10.
728x90
XPS(XML Paper Specification)net Framework 3.0과 Windows Vista가 출시되면서 MS에서 밀고 있는 새로운 형태의 전자출판 문서형식입니다. 대표적인 전자출판 문서파일은 Adobe사의  PDF(portable document format)가 있습니다.

XPS는 크게 Fixed Documents와 Flow Documents 2가지 형태로 나누어 집니다. 이번강좌에서는 Fixed Documents에 대해 알아보겠습니다. Fixed Documents는 Read-Only의 읽기전용이며 용도는 XPS로 출판하여 보고서 형태의 기안을 작성한다거나 보안을 걸어서 특정사람에게만 열어보게 할 수도 있습니다.  

XPS 문서를 보려면 XPS DocumentViwer Control이 필요합니다.

XPS 문서형태는 Package형태로 여러가지 파일의 묶음입니다. Package안에는 XML, Image, Font등의 여러가지 파일이 Zip 파일 형태의 데이터 집합입니다.

XPS문서를 열어보면 [그림 2]에서 보시는 것처럼 각각의 폴더를 열어보시면 XML이 존재하고 여기에 들어가는 Image, Font 등의 Resource가 들어가고 Image, Font등의 Content Type을 정의하는 Text 파일이 존재합니다.

사용자 삽입 이미지

그림 1. 압축풀기

사용자 삽입 이미지

그림 2.XPS파일 Package

XPS Documents를 handling하기 위해서는 ReachFramework라는 어셈블리(Assembly)를 추가해야 합니다.

사용자 삽입 이미지


그림 3. ReachFramework Assembly추가

그리고 비하인드 코드에는 System.Windows.Xps.Packaging를 추가합니다.

using System.Windows.Xps.Packaging;

예제는  XPS문서를 볼 수 있는 XPS Documents Viewer 입니다. 파일메뉴에서 열기메뉴를 실행하면 OpenFileDialog 대화상자가 보여지고 XPS문서를 선택하면 DocumentViewer Control을 통해서 XPF문서를 볼 수 있는 형태의 간단한 프로그램입니다.

그럼 DocumentViewer Control와 메뉴를 추가하는 xaml코드와 OpenFileDialog 생성 및 XpsDocument를 추가하여 DocumentViewer Control에 XPS 문서를 읽기 모드로 출력하는 코드가 들어있는 cs코드를 보시겠습니다.

<Window x:Class="wpf26.Window1"
    xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"
    Title
="XPS Document Viewer" Height="450" Width="600">
   
   
<Window.CommandBindings>
     
<CommandBinding Command="ApplicationCommands.Open"  />
      <
CommandBinding Command="ApplicationCommands.Close" />
    </
Window.CommandBindings>
   
   
<DockPanel>
     
<Menu Name="MainMenu" DockPanel.Dock="Top">
       
<MenuItem Header="File">
         
<MenuItem Command="ApplicationCommands.Open" Header="Open..." />
           <
MenuItem Command="ApplicationCommands.Close" Header="Close" />
        </
MenuItem>
     
</Menu>
     
     
<Grid DockPanel.Dock="Top">
       
<DocumentViewer Name="docViewer">
       
</DocumentViewer>
     
</Grid>
   
</DockPanel>
</Window>

코드를 보시면 Menu, DocumentViewr Control 추가 및 Open과 Close 이벤트를 CommandBinding에 추가하는 부분이 들어가 있습니다.

CommandBinding에 명령어를 추가하는 부분은 http://msdn2.microsoft.com/en-us/library/ms752308.aspx 를 참고하시면 Commanding 에 대해 좀더 자세한 설명이 나와있습니다.

DocumentViewr Control 코드를 보시면 name에 대한 속성만 설정되어 있는 것을 보실 수 있습니다.

using System;
using
System.Collections;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Windows;
using
System.Windows.Controls;
using
System.Windows.Data;
using
System.Windows.Documents;
using
System.Windows.Input;
using
System.Windows.Shapes;
using
System.Windows.Media;
using
System.Windows.Media.Imaging;
using
System.Windows.Navigation;
using
System.Windows.Forms;

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

namespace
wpf26
{
   
public partial class Window1 : Window
    {
       
private XpsDocument _xpsDocument;
        private string
_fileName;

        public
Window1() : base()
        {
            InitializeComponent()
;

           
AddCommandBindings(ApplicationCommands.Open,
                OpenCommandHandler)
;

           
AddCommandBindings(ApplicationCommands.Close,
                CloseCommandHandler)
;
       
}

       
private void OpenCommandHandler(object sender,
            ExecutedRoutedEventArgs e)
        {
            OpenFileDialog dig
= new OpenFileDialog();            
           
dig.InitialDirectory = Directory.GetCurrentDirectory();
           
dig.Filter = "Xps Documents (*.xps)|*.xps";
           
dig.FilterIndex = 1;
            if
(dig.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
               
if (_xpsDocument != null)
                    _xpsDocument.Close()
;
                try
               
{
                    _xpsDocument
= new XpsDocument(dig.FileName,
                                       System.IO.FileAccess.Read)
;
               
}
               
catch (UnauthorizedAccessException)
                {
                    System.Windows.MessageBox.Show(
                       
string.Format("Unable to access {0}",
                        dig.FileName))
;
                    return;
               
}

                docViewer.Document
=
                   
_xpsDocument.GetFixedDocumentSequence();
               
_fileName = dig.FileName;
           
}
        }

       
private void CloseCommandHandler(object sender,
            ExecutedRoutedEventArgs e)
        {
           
this.Close();
       
}

       
private void AddCommandBindings(
                ICommand command, ExecutedRoutedEventHandler handler)
        {
            CommandBinding cmdBindings
= new CommandBinding(command);
           
cmdBindings.Executed += handler;
           
CommandBindings.Add(cmdBindings);
       
}
    }
}


OpenCommandHandler 메소드를 보시면 Using System.IO 네임스페이스를 선언하여 OpenFileDialog 클래스를 인스턴스 하여 대화상자를 만들고 _xpsDocument 변수에 XPS 문서를 읽기모드로 읽어와서 DocumentViewer에 보여줍니다.

AddCommandBindings 메소드는 CommandBinding에 Open, Close Command를 추가를 합니다. 그리고 Window1 생성자에서 AddCommandBindings 메소드를 추가하여 실행할 수 있도록 초기화를 합니다.

예제를 실행하면 다음과 같이 보실 수 있습니다.

사용자 삽입 이미지

그림 3. 프로그램 실행

사용자 삽입 이미지

그림 4. XPS 문서 출력

wpf26.zip



1. XPS Team Blog
    http://blogs.msdn.com/xps/
2. Msdn WPF Documents
   http://msdn2.microsoft.com/en-us/library/ms749165.aspx
3. XPS Specification
   http://www.microsoft.com/whdc/xps/xpsspec.mspx  
4. View and Generate XPS
   http://www.microsoft.com/whdc/xps/viewxps.mspx
5. Msdn Magazine
    http://msdn.microsoft.com/msdnmag/issues/06/01/XMLPaperSpecification/default.aspx
6.훈스닷넷 시샵 김수영님 동영상 강의(MS Visual C# MVP)
   http://www.hoons.kr/SeminarReview.aspx (How to develop XPS with WPF )


다음강좌에는 XPS문서를 편집 및 다양한 형태의 출판에 효과적인 Flow Documents에 대해 알아보겠습니다.


※ 강좌 포스팅후

아직까지 WPF의 막강한 화려함에 가려 그 빛을 못보고 있지만 언론사, 출판업체 같은 곳에서 많은 관심을 보이고 있으므로 향후 이런곳에서 프로젝트를 할 기회가 있다면 분명 충분한 도움이 될 수 있다고 생각합니다. WPF 화려한 것만 신경쓰시지 말고 이런 WPF의 다양한 이면에도 관심을 갖고 공부를 하시면 많은 도움이 되리라 생각됩니다.  


※ 테스트 환경
-----------------------------------------------------------------------------------------
운영체체 : Windows Vista Ultimate 32bit
개발툴 : Microsoft Visual C# Codename "Orcas"
-----------------------------------------------------------------------------------------
 

'.NET WPF' 카테고리의 다른 글

Ellipse(원) 그리기  (0) 2007.08.23
동적으로 오브젝트 생성  (0) 2007.08.16
실행주기(PageCycle)  (0) 2007.08.14
[XPS] View and Generate XPS  (0) 2007.08.10
[Controls] 25. BulletDecorator  (0) 2007.08.10
Path를 이용한 한글폰트 만들기  (0) 2007.08.09
Silverlight 1.1 설치환경  (0) 2007.07.19

댓글