`

Convert XAML Flow Document to XPS with Style (multiple page, page size, header,

    博客分类:
  • WPF
wpf 
阅读更多

The original post is available here: 

 

Convert XAML Flow Document to XPS with Style (multiple page, page size, header, margin)

 

 

 

 

XPS is a fixed document format in which pages are pre-formated to a fixed page sixe. On the opposite of the spectrum, WPF provides flow document which can be paginated dynamically in XAML viewer. To bridge the two, WPF provides features to convert a flow document to fixed document in XPS format. Actually, you can convert any WPF Visual content to XPS.

While the default conversion is very easy to use, if you want more features, you need to write some code. This acticle shows how to convert WPF flow document to multiple page XPS with page size, margin, and header.

When a flow document is loaded in WPF, it returns an IDocumentPaginatorSource object. From it, you can get its DocumentPaginator object. When you're using XpsSerializationManager to convert a loaded flow document to XPS, it accepts a DocumentPaginator object. But the little know fact is that WPF allows you to wrap a new DocumentPaginator around a DocumentPaginator to control the conversion process.

Here is our DocumentPaginator wrapper class:

 

 

 

 

public class DocumentPaginatorWrapper : DocumentPaginator
  {

    Size m_PageSize;
    Size m_Margin;
    DocumentPaginator m_Paginator;
    Typeface m_Typeface;


    public DocumentPaginatorWrapper(DocumentPaginator paginator, Size pageSize, Size margin)
    {
      m_PageSize = pageSize;
      m_Margin = margin;
      m_Paginator = paginator;
      

      m_Paginator.PageSize = new Size(m_PageSize.Width  - margin.Width  * 2,
                                        m_PageSize.Height - margin.Height * 2);

    }

    Rect Move(Rect rect)
    {
      if (rect.IsEmpty)
      {
        return rect;
      }
      else
      {
        return new Rect(rect.Left + m_Margin.Width, rect.Top + m_Margin.Height,
                        rect.Width, rect.Height);
      }
    }


    public override DocumentPage GetPage(int pageNumber)
    {
      DocumentPage page = m_Paginator.GetPage(pageNumber);

      // ContainerVisual - the container of visual
      ContainerVisual newpage = new ContainerVisual();
      DrawingVisual title = new DrawingVisual();

      using (DrawingContext ctx = title.RenderOpen())
      {
        if (m_Typeface == null)
        {
          m_Typeface = new Typeface("Times New Roman");
        }
        // drawing header
        FormattedText text = new FormattedText("Page " + (pageNumber + 1),
          System.Globalization.CultureInfo.CurrentCulture, FlowDirection.LeftToRight,
          m_Typeface,
          14, Brushes.Black);

        ctx.DrawText(text, new Point(0, -96 / 4)); // 1/4 inch above page content
      }

      DrawingVisual background = new DrawingVisual();
      using (DrawingContext ctx = background.RenderOpen())
      {
        ctx.DrawRectangle(new SolidColorBrush(Color.FromRgb(240, 240, 240)), null, page.ContentBox);
      }

      newpage.Children.Add(background); // scale down page and center
      // ContainerVisual - the container of visual
      ContainerVisual smallerpage = new ContainerVisual();
      smallerpage.Children.Add(page.Visual);
      smallerpage.Transform = new MatrixTransform(0.95, 0, 0, 0.95,
                0.025 * page.ContentBox.Width, 0.025 * page.ContentBox.Height);
      newpage.Children.Add(smallerpage);
      newpage.Children.Add(title);

      newpage.Transform = new TranslateTransform(m_Margin.Width, m_Margin.Height);

      return new DocumentPage(newpage, m_PageSize, Move(page.BleedBox), Move(page.ContentBox));
    }

    public override bool IsPageCountValid
    {
      get { return m_Paginator.IsPageCountValid; }
    }

    public override int PageCount
    {
      get { return m_Paginator.PageCount;  }
    }

    public override Size PageSize
    {
      get
      {
        return m_Paginator.PageSize;
      }
      set
      {
        m_Paginator.PageSize = value;
      }
    }

    public override IDocumentPaginatorSource Source
    {
      get { return m_Paginator.Source; }
    }
  }
 

 

 

and 

 

 

 

The DocumentPaginatorWrapper constructor modifies the page size of the original paginator based on required page size and margin. The new GetPage method calls the original GetPage method to get a page, then adds a header Visual, a background Visual (just to show the original page), and then move the combined Visual by top left margin. To demonstrate that you can easily transform a Visual, the original page Visual is scaled down and centered within its original box.

Here is the code calling the wrapper class to load a flow XAML document and convert to an XPS document:

 

 

 

 

 

 

 public static int SaveAsXps(string filename)
    {
      object doc;


      FileInfo fileinfo = new FileInfo(filename);
      using(FileStream file = fileinfo.OpenRead())
      {
        System.Windows.Markup.ParserContext context = new System.Windows.Markup.ParserContext();
        context.BaseUri = new Uri(fileinfo.FullName, UriKind.Absolute);
        doc = System.Windows.Markup.XamlReader.Load(file, context);

      }

      if (!(doc is IDocumentPaginatorSource))
      {
        Console.WriteLine("DocumentPaginatorSource expected");
        return -1;
      }

      using (Package container = Package.Open(filename + ".xps", FileMode.Create))
      {

        using (XpsDocument xpsDoc = new XpsDocument(container, CompressionOption.Maximum))
        {
          XpsSerializationManager rsm = new XpsSerializationManager(new XpsPackagingPolicy(xpsDoc), false);
          DocumentPaginator paginator = ((IDocumentPaginatorSource)doc).DocumentPaginator;

          paginator = new DocumentPaginatorWrapper(paginator, new Size(768, 676), new Size(48, 48));

          rsm.SaveAsXaml(paginator);
        }
      }
      Console.WriteLine("{0} generated.", filename + ".xps");

      return 0;
    
    }

  }
 

 

 

The code above loads a flow XAML document and paginates it to 8 inch by 6 inch, with half inch margin.

 

Below is the sample output 

 


as you can see, the xps document has the following elements.

 

 

1. the header "page 1"

2. the content is centered with margins.

3. background is gray (240, 240, 240)

 

while the true content of the XpsDocument is actually a flow document. Here is the content of the FlowDocument.

 

 

 

<FlowDocument 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>
  <Paragraph FontSize="18">Flow Format Example</Paragraph>

  <Paragraph>
    Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy 
      nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi 
      enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis 
      nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure.
  </Paragraph>
  <Paragraph>
    Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh 
      euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim 
      ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl 
      ut aliquip ex ea commodo consequat. Duis autem vel eum iriure.
  </Paragraph>

  <Paragraph FontSize="18">More flow elements</Paragraph>
  <Paragraph FontSize="15">Inline, font type and weight, and a List</Paragraph>

  <List>
    <ListItem>
      <Paragraph>ListItem 1</Paragraph>
    </ListItem>
    <ListItem>
      <Paragraph>ListItem 2</Paragraph>
    </ListItem>
    <ListItem>
      <Paragraph>ListItem 3</Paragraph>
    </ListItem>
    <ListItem>
      <Paragraph>ListItem 4</Paragraph>
    </ListItem>
    <ListItem>
      <Paragraph>ListItem 5</Paragraph>
    </ListItem>
  </List>

  <Paragraph>
    <Bold>Bolded</Bold>
  </Paragraph>
  <Paragraph>
    <Underline>Underlined</Underline>
  </Paragraph>
  <Paragraph>
    <Bold>
      <Underline>Bolded and Underlined</Underline>
    </Bold>
  </Paragraph>
  <Paragraph>
    <Italic>Italic</Italic>
  </Paragraph>

  <Paragraph>
    <Span>The Span element, no inherent rendering</Span>
  </Paragraph>
  <Paragraph>
    <Run>The Run element, no inherent rendering</Run>
  </Paragraph>

  <Paragraph FontSize="15">Subscript, Superscript</Paragraph>

  <Paragraph>
    <Run Typography.Variants="Superscript">This text is Superscripted.</Run> This text isn't.
  </Paragraph>
  <Paragraph>
    <Run Typography.Variants="Subscript">This text is Subscripted.</Run> This text isn't.
  </Paragraph>
  <Paragraph>
    If a font does not support a particular form (such as Superscript) a default font form will be displayed.
  </Paragraph>

  <Paragraph FontSize="15">Blocks, breaks, paragraph</Paragraph>

  <Section>
    <Paragraph>A block section of text</Paragraph>
  </Section>
  <Section>
    <Paragraph>Another block section of text</Paragraph>
  </Section>

  <Paragraph>
    <LineBreak/>
  </Paragraph>
  <Section>
    <Paragraph>... and another section, preceded by a LineBreak</Paragraph>
  </Section>

  <Section BreakPageBefore="True"/>
  <Section>
    <Paragraph>... and another section, preceded by a PageBreak</Paragraph>
  </Section>

  <Paragraph>Finally, a paragraph. Note the break between this paragraph ...</Paragraph>
  <Paragraph TextIndent="25">... and this paragraph, and also the left indention.</Paragraph>

  <Paragraph>
    <LineBreak/>
  </Paragraph>

</FlowDocument>
 

 

 

 

 

  • 大小: 34.7 KB
分享到:
评论

相关推荐

    documentViewer xps xpsdocumnet 完整打印示例

    在本文中,我们将深入探讨如何使用`DocumentViewer`控件,结合`XPS`文档和`XpsDocument`类在Windows Presentation Foundation (WPF)环境中实现完整的打印功能。`DocumentViewer`是WPF提供的一种用于展示XPS文档的UI...

    C# DoucmentViewer控件读取Xps格式的文档

    要使用`DocumentViewer`,你需要将其添加到你的XAML界面中: ```xml &lt;DocumentViewer Name="docViewer" /&gt; ``` 接下来,加载XPS文档到`DocumentViewer`。你可以使用`FixedDocumentSequence`类和`XpsDocument`类...

    Pro XAML with C#

    ### Pro XAML with C# — 关键知识点概览 #### 一、简介 《Pro XAML with C#》是一本全面介绍XAML及其在C#中的应用的专业书籍。该书虽然发布时间较早,但其内容至今仍具有很高的参考价值,特别是对于希望深入学习...

    XAML in a Nutshell

    - **Reduced Development Costs:** By eliminating the need for multiple file formats and plugins, XAML can lower development costs and reduce time-to-market. #### Understanding XAML in the Context of ...

    FW_TO_XAML转换工具

    "FW_TO_XAML转换工具"是一款专为Adobe Fireworks设计的插件,它允许设计师将图形、图片等设计元素高效地转换成XAML(eXtensible Application Markup Language)格式,这种格式主要用于Windows Presentation ...

    Programming.Windows.Writing.Windows.8.Apps.With.C#.and.XAML.6th.edition.2013

    You should emerge from Part I ready to create sophisticated page-oriented collection-based user interfaces using the powerful ListView and GridView controls. Part II, “Specialties,” explores ...

    Learn to Program with Python 无水印pdf

    Learn to Program with Python 英文无水印pdf pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者...

    XAML to HTML Conversion Demo

    在IT领域,XAML(eXtensible Application Markup Language)是一种标记语言,主要应用于Microsoft的Windows Presentation Foundation (WPF)框架中,用于构建用户界面。XAML提供了声明式的方式来描述UI元素及其属性,...

    一个宿主页面当中嵌入2个XAML页面并互相调用

    在我们的场景中,宿主页面(通常是默认的MainPage.xaml)将承载两个子XAML页面,我们假设它们名为Page1.xaml和Page2.xaml。这可以通过在宿主页面的XAML代码中添加`&lt;Frame&gt;`控件来实现,每个子页面将作为`&lt;Frame&gt;`的...

    Windows 8.1 Apps with XAML and C# Unleashed

    It is in full color, the content is laid out in an easy to read style, the author's writing style makes it easy to read, and the content is all valuable. There is no fluff like you find in a lot of ...

    Metro Style Using XAML Note

    ### Windows 8 Metro Style 使用 XAML 的关键技术点 在Windows 8时代,随着平板电脑和其他触摸设备的普及,Microsoft推出了全新的界面风格——Metro Style(现在称为Modern UI或UWP),并支持XAML作为构建这些应用...

    Windows 10 Development with XAML and C# epub

    Windows 10 Development with XAML and C# 7(2nd)Course.Technology.Java.Learning.to.Program.with.Robots.0619217243.rar 英文epub 第2版 本资源转载自网络,如有侵权,请联系上传者或csdn删除 查看此书详细...

    AI到XAML的导出插件

    标题中的“AI到XAML的导出插件”是指一个工具,它允许用户将Adobe Illustrator(AI)的设计图转换成Windows Presentation Foundation(WPF)或通用Windows平台(UWP)应用程序所使用的XAML(XAML是Extensible Application ...

    Html/Xaml 转换器

    通过结合 CSS(Cascading Style Sheets)和 JavaScript,可以创建交互式的动态网页。HTML5 是目前最常用的版本,添加了许多新特性,如离线存储、媒体元素和图形绘制等。 XAML (eXtensible Application Markup ...

    XAML Developer Reference

    - **XPS**:XPS(XML Paper Specification)是一种用于电子文档的标准,XAML是其核心组成部分之一。 - **其他XML基础格式**:除了上述应用场景外,XAML还被应用于各种基于XML的格式中,例如某些特定领域的数据交换...

    WPF XAML 文件格式批量修改为Utf8格式

    convert_to_utf8('path/to/your/wpf/project') ``` 这个脚本会遍历指定的目录,并将所有以`.xaml`结尾的文件从UTF-16转换为UTF-8。在实际操作中,你需要替换`'path/to/your/wpf/project'`为你的WPF项目的实际路径。...

    Pro Windows 8.1 Development with XAML and C#

    Windows 8.1 apps are revolutionizing development on the Windows ...This book is suitable for anyone wanting to get to grips with Windows 8.1 development using the cross-platform standards of XAML and C#.

    SVG转 XAML最好的免费工具!

    SVG(Scalable Vector Graphics)和XAML(Extensible Application Markup Language)是两种在图形设计和用户界面开发中广泛使用的格式。SVG是一种基于XML的矢量图像格式,它支持无损缩放,常用于Web和应用程序中的...

Global site tag (gtag.js) - Google Analytics