Skip to main content Skip to footer

Rotate Pages in C1PdfViewer

Some of the PDF viewers allow the rotation of pages and changes in orientation. The functionality is also easy to achieve with C1PdfViewer, because you simply change the 'PageTemplate' and apply transformations there. The only important trick here is enabling the horizontal scrollbar, which is not visible if the viewport width is less. To do this, we have to inherit the C1PdfViewer and expose the scrollbars there as property:

public class MyPdfViewer : C1.Silverlight.PdfViewer.C1PdfViewer  
 {  
    private ScrollBar _vsb;  
    private ScrollBar _hsb;  

    public ScrollBar VerticalScrollBar { get { return _vsb; } }  
    public ScrollBar HorizontalScrollBar { get { return _hsb; } }  

    public override void OnApplyTemplate()  
    {  
       base.OnApplyTemplate();  
       _hsb= GetTemplateChild("HorizontalScrollBar") as ScrollBar ;  
       _vsb = GetTemplateChild("VerticalScrollBar") as ScrollBar;  
    }  

 }

The below functions return the page template that needs to be applied depending on the required rotate angle:

public DataTemplate ApplyRotateStyle(double angle)  
 {  
    string str=@"<DataTemplate  xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""  
    xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""  

    xmlns:c1=""http://schemas.componentone.com/winfx/2006/xaml"">  
    <c1:C1LayoutTransformer>  
    <c1:C1LayoutTransformer.LayoutTransform>  
    <RotateTransform Angle="""+angle+ @""" />  
    </c1:C1LayoutTransformer.LayoutTransform>  
    <Grid Margin=""{Binding Source.PageSeparation}"" Cursor=""IBeam"">  
    <Grid.Resources>  
    <c1:ZoomToScaleTransformConverter x:Key=""zoomConverter""/>  
    </Grid.Resources>  
    <Border Background=""#19000000"" Margin=""4,4,-4,-4"" CornerRadius=""2"" />  
    <Border Background=""#19000000"" Margin=""3,3,-3,-3"" CornerRadius=""2""/>  
    <Border Background=""#19000000"" Margin=""2,2,-2,-2"" CornerRadius=""2"" />  
    <Border Background=""#19000000"" Margin=""1,1,-1,-1"" CornerRadius=""2"" />  
    <Border Background=""White""/>  

    <c1:C1LayoutTransformer LayoutTransform=""{Binding Source.Zoom, Converter={StaticResource zoomConverter}}""  
    CacheMode=""{Binding Source.CacheMode}"">  
       <ContentPresenter Content=""{Binding Content}""/>  
    </c1:C1LayoutTransformer>  

    <Border BorderThickness=""1"" BorderBrush=""#FF858585""/>  
    </Grid>  
    </c1:C1LayoutTransformer>  
    </DataTemplate>";  

    return  (DataTemplate)XamlReader.Load(str);  
 }

The rotation can be achieved using the following code:

void RotateLeft(object sender, RoutedEventArgs args)  
 {  
    pdfViewer.PageTemplate = ApplyRotateStyle(-90);  
    pdfViewer.HorizontalScrollBar.Visibility    = Visibility.Visible  ;  
    pdfViewer.HorizontalScrollBar.ViewportSize = pdfViewer.ViewportHeight;  
 }

This is it. Download Sample

MESCIUS inc.

comments powered by Disqus