15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

fixed Documents ❘ 1087<br />

The received ObservableCollection is assigned to the menus variable that is used to build<br />

up the fixed page:<br />

internal void NavigationService_LoadCompleted(object sender,<br />

NavigationEventArgs e)<br />

{<br />

menus = e.ExtraData as ObservableCollection;<br />

fixedDocument = new FixedDocument();<br />

var pageContent1 = new PageContent();<br />

fixedDocument.Pages.Add(pageContent1);<br />

var page1 = new FixedPage();<br />

pageContent1.Child = page1;<br />

page1.Children.Add(GetHeaderContent());<br />

page1.Children.Add(GetLogoContent());<br />

page1.Children.Add(GetDateContent());<br />

page1.Children.Add(GetMenuContent());<br />

viewer.Document = fixedDocument;<br />

}<br />

NavigationService.LoadCompleted -= NavigationService_LoadCompleted;<br />

code snippet CreateXps/DocumentPage.xaml.cs<br />

Fixed documents are created with the FixedDocument class. The FixedDocument element only contains<br />

PageContent elements that are accessible via the Pages property. The PageContent elements must be<br />

added to the document in the order they should appear on the page. PageContent defines the content of a<br />

single page.<br />

PageContent has a Child property such that a FixedPage can be associated with it. To the FixedPage you<br />

can add elements of type UIElement to the Children collection. This is where you can add all the elements<br />

you’ve learned about in the last two chapters, including a TextBlock element that itself can contain Inline<br />

<strong>and</strong> Block elements.<br />

In the sample code, the children to the FixedPage are created with helper methods GetHeaderContent(),<br />

GetLogoContent(), GetDateContent(), <strong>and</strong> GetMenuContent().<br />

The method GetHeaderContent() creates a TextBlock that is returned. The TextBlock has the Inline<br />

element Bold added, which in turn has the Run element added. The Run element then contains the header<br />

text for the document. With FixedPage.SetLeft() <strong>and</strong> FixedPage.SetTop() the position of the TextBox<br />

within the fixed page is defined:<br />

private UIElement GetHeaderContent()<br />

{<br />

var text1 = new TextBlock();<br />

text1.FontFamily = new FontFamily("Mangal");<br />

text1.FontSize = 34;<br />

text1.HorizontalAlignment = HorizontalAlignment.Center;<br />

text1.Inlines.Add(new Bold(new Run("cn|elements")));<br />

FixedPage.SetLeft(text1, 170);<br />

FixedPage.SetTop(text1, 40);<br />

return text1;<br />

}<br />

The method GetLogoContent() adds a logo in the form of an Ellipse with a RadialGradientBrush to<br />

the fixed document:<br />

private UIElement GetLogoContent()<br />

{<br />

var ellipse = new Ellipse<br />

{<br />

Width = 90,<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!