Click here to Skip to main content
15,897,226 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I'm facing a problem with DocumentViewer GoToPage() method.
I want to navigate XPS document to specific page number while showing the control, but it navigates to wrong page number during initializing the window.

Moreover, if I call GoToPage() later, e.g. in click button handler - DocumentViewer works fine and navigates to correct page.

Here is a piece of code:

C#
public partial class Window : Window
   {
       private string _XPSpath;

       public Window()
       {
           InitializeComponent();
           _XPSPath = @"C:\DocViewerTest\TestDocument.xps";
           DocViewer.Document = new XpsDocument(_XPSpath, FileAccess.Read).GetFixedDocumentSequence();

           DocViewer.GoToPage(67); // Navigates to 63 page - not OK.
       }

       private void GoToPageButton_Click(object sender, RoutedEventArgs e)
       {
           DocViewer.GoToPage(67); // Navigates to 67 page - OK.
       }

   }


Regards,
Anton Kurkov.
Posted

1 solution

I faced the same problem today and found what was the problem. You have to have your page size as Letter when you save your document to xps. Letter is 21,59cmx27,94cm and A4 is 21cmx29,97cm, so when you try to got to specific page before the document is in screen the xps is in letter format and you will "leave behind" in each page few cm. When the document is rendered visible it is back to the page size you saved the file and thus it will work normally.

I made also another solution to jump to the page without saving the document in Letter size.


C#
DispatcherTimer dispatcherTimer = new DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = TimeSpan.FromMilliseconds(50);
dispatcherTimer.Start();

int pagenum = 'your page';

void dispatcherTimer_Tick(object sender, EventArgs e)
{

           Document.GoToPage(pagenum );

           if(pagenum == Document.MasterPageNumber)
               dispatcherTimer.Stop();

}


Above code will jump to the page untill it is the correct page.

- Tuomas
 
Share this answer
 
v3

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900