Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Silverlight XPS Viewer in SL4, Support viewbox and viewport property

0.00/5 (No votes)
4 May 2010CPOL 12.7K  
Background...
Background

During my development last week I was working on a Silverlight based XPS viewer. During this viewer we came to a situation in which an XPS was made from power point slide. In that PPT two images were shown during rendering the images present in it.

The problem is that Silverlight doesn’t have any viewport or viewbox support. Along with these problems, the font files need to be downloaded externally so that we can render them in our XPS Viewer

Using the code

Now i have replace path into Image element, remove 1st image and change left and top to 2nd image plus increase hight.

// crate 2 rect, one of viewbox and 2nd is viewport
var viewbox = new Rect(Convert.ToDouble(tempviewbox[0]), Convert.ToDouble(tempviewbox[1]),
                                Convert.ToDouble(tempviewbox[2]), Convert.ToDouble(tempviewbox[3]));

var viewport = new Rect(Convert.ToDouble(tempviewPort[0]), Convert.ToDouble(tempviewPort[1]),
                                 Convert.ToDouble(tempviewPort[2]), Convert.ToDouble(tempviewPort[3]));

Image img = new Image(); 
img.Source = bitmapImage; 
img.Stretch = Stretch.Fill;

//Set Image Height (1st Image Height + 2nd Image Height) and Change Left and Top
img.Height = viewport.Height + Convert.ToDouble(vp[3]);  
img.SetValue(Canvas.LeftProperty, Convert.ToDouble(vp[0])); 
img.SetValue(Canvas.TopProperty, Convert.ToDouble(vp[1])); 
          
//Get Parent element of this image
var parent = path.Parent as Canvas;
//Get index of Image element
var index = parent.Children.IndexOf(path);
parent.Children.Insert(index, img);

//Microsoft has provided support in Silverlight 4, 
//Glyphs has a property of FontSource by which we can assign any font file stream to that. 
//By this provision I was able to provide font files to XPS Viewer, 
//this saved any extra DLL to be downloaded separately.
    
//Get Font file stream from xps
var stream = Application.GetResourceStream(_streamResourceInfo, ConvertPartName(glyphs.FontUri.ToString())).Stream;
glyphs.FontSource = new FontSource(stream); //assign stream 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)