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()  
       _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=""""  

    <RotateTransform Angle="""+angle+ @""" />  
    <Grid Margin=""{Binding Source.PageSeparation}"" Cursor=""IBeam"">  
    <c1:ZoomToScaleTransformConverter x:Key=""zoomConverter""/>  
    <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}""/>  

    <Border BorderThickness=""1"" BorderBrush=""#FF858585""/>  

    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


GrapeCity Developer Tools
comments powered by Disqus