locked
How to increase speed of pdf rendering via PDF API?

    Question


  • I use FlipView to show pdf pages. I found PDF rendering is so slow when render each page.I written in C# only.
    (I follow sample: http://code.msdn.microsoft.com/windowsapps/PDF-viewer-sample-85a4bb30)

    I found this sample: http://msdn.microsoft.com/en-us/library/windows/apps/dn263107.aspx . Pdf rendering is so fast.
    But library in this sample written in C++. I try to adpat with my program , but i can't. it's difficult c++ for me.

    Please suggest me to adpat Pdf C++ library to have below feature:
    - Redering selected page.
    - Pdf size change when page changed (Now , the sample can't do it)
    - FlipView ItemsTemplate can be changed (one or two pages show)

    Thanks.
    Thursday, April 03, 2014 6:41 AM

All replies

  • Did you modify the C# sample at all? If so, please post it here for us to review.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Thursday, April 03, 2014 12:20 PM
    Moderator
  • Here you are.

    MainPage.xaml

    <Page
        x:Class="PdfSample.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:PdfSample"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d">
    
        <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
            <FlipView x:Name="xFlipView">
                <FlipView.ItemTemplate>
                    <DataTemplate>
                        <Grid>
                            <Image Source="{Binding Image}" HorizontalAlignment="Center" Height="{Binding Height}" Width="{Binding Width}"></Image>                      
                        </Grid>
                    </DataTemplate>
                </FlipView.ItemTemplate>
                <FlipView.ItemsPanel>
                    <ItemsPanelTemplate>
                        <VirtualizingStackPanel AreScrollSnapPointsRegular="True" Orientation="Horizontal" CleanUpVirtualizedItemEvent="VirtualizingStackPanel_CleanUpVirtualizedItemEvent" />
                    </ItemsPanelTemplate>
                </FlipView.ItemsPanel>
            </FlipView>
        </Grid>
    </Page>
    

    MainPage.xaml.cs

     public sealed partial class MainPage : Page
        {
            ObservableCollection<FData> items = new ObservableCollection<FData>();
    
            public MainPage()
            {
                this.InitializeComponent();
            }
    
            protected override async void OnNavigatedTo(NavigationEventArgs e)
            {          
                StorageFolder installedLocation = Package.Current.InstalledLocation;
                var pdfFile = await installedLocation.GetFileAsync("Assets\\magazine.pdf");
                var pdfDocument = await PdfDocument.LoadFromFileAsync(pdfFile);
                for (int i = 0; i < pdfDocument.PageCount; i++)
                {
                    items.Add(new FData() { PageNumber = i ,PdfDoc = pdfDocument ,Width = Window.Current.Bounds.Width,Height = Window.Current.Bounds.Height});
                }
                xFlipView.ItemsSource = items;
            }
    
            private void VirtualizingStackPanel_CleanUpVirtualizedItemEvent(object sender, CleanUpVirtualizedItemEventArgs e)
            {
                var item = e.Value as FData;
                item.Dispose();
            }
        }
    
    
        public class FData : INotifyPropertyChanged
        {
            
            public int PageNumber { get; set; }
            public PdfDocument PdfDoc { get; set; }
            public double Height { get; set; }
            public double Width { get; set; }
            private ImageSource _image = null;
            public ImageSource Image 
            {
                get 
                {
                    if (_image == null)
                        Rendering();
                    return _image;
                }
                set 
                {
                    _image = value;
                }
            }
    
            private async void Rendering()
            {
                using (var page = PdfDoc.GetPage((uint)PageNumber)) 
                {
                    var renderOption = new PdfPageRenderOptions();
                    var img = new BitmapImage();
                    renderOption.DestinationHeight = (uint)(Height);              
                    InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream();               
                    await page.RenderToStreamAsync(stream, renderOption);               
                    img.SetSource(stream);
                    _image = img;
                    if (PropertyChanged != null)
                        PropertyChanged(this, new PropertyChangedEventArgs("Image"));
                    _image = null;
                }
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
    
            internal void Dispose()
            {
                _image = null;
            }
        }

    Friday, April 04, 2014 2:33 AM