I'm trying to use the PDF viewer to look at a specific page number in a pdf document. I try to set the document source, and then set the CurrentPageNumber property. The viewer will still only load the pdf on the first page. Is there a different way to jump to a specific page in the document?
I used the following:
PdfDocumentSource newDoc =
new
PdfDocumentSource(
new
Uri(@
"C:\12345.pdf"
));
pdfViewer.DocumentSource = newDoc;
pdfViewer.CurrentPageNumber = 5;
6 Answers, 1 is accepted
The problem is that when you set the CurrentPageNumber, the document has not been loaded yet as the document is loaded asynchronously. Therefore, this code is ignored.
I can suggest you set this property on DocumentChanged event of the RadPdfViewer or the Loaded event of the PdfDocumentSource. Then this code should work as expected.
Greetings,
Kammen
the Telerik team
Time to cast your vote for Telerik! Tell DevPro Connections and Windows IT Pro why Telerik is your choice. Telerik is nominated in a total of 25 categories.
That worked great. Thank you for the quick response.
I am a little annoyed of this control even though it has a lot of good functionality. I have tried to use it in our grand application and had some issues going to a specific page after the document has loaded and I have created a sample project for you. A little issue there was that I couldn't see the control before I selected a telerik theme. Very frustrating!
I have tried a million different ways of loading the document and waiting for it to load, using timers, the events and other weird work arounds. Nothing worked until I tried the demo project with the code Kammen suggested, which I also tested in the other application, without luck.
It turns out, the problem is because I had the control inside another control, in my case a radtabcontrol. It works as long it's in the first tabitem, or in other words, the tab in focus, when it's loaded.
If I put it in another tab when it loads, then the page command is ignored event though the event is triggered. It doesn't
matter whether I use "GotoPage(10)" or "CurrentPageNumber = 10"
I would have attached the sample project if I could, but only pics are allowed. I will send it in a support ticket instead.
The PdfViwer does not load the desired page because the content of the tab items is not set to be preserved by default. First you need to set TabControl's IsContentPreserved to True. Then in order to make the PdfViwer change its page, attach to the SelectionChanged event of the TabControl. When the appropriate tab is selected, assure that the PdfViwer is finally loaded and make your changes there. For instance:
<
telerik:RadTabControl
IsContentPreserved
=
"True"
SelectionChanged
=
"RadTabControl_SelectionChanged"
>
private
void
RadTabControl_SelectionChanged(
object
sender, Telerik.Windows.Controls.RadSelectionChangedEventArgs e)
{
if
((e.AddedItems[0]
as
RadTabItem).Header.ToString() ==
"PDF"
)
{
pdfViewer.Loaded += PdfViewer_Loaded;
}
}
private
void
PdfViewer_Loaded(
object
sender, RoutedEventArgs e)
{
pdfViewer.CurrentPageNumber = 10;
//pdfViewer.GoToPage(10);
}
Regards,
Peshito
Progress Telerik
Hi Peshito, thx for the quick reply.
Is the only reason for "IsContentPreserved=true" just to make sure that the pdfviewer.Loaded event doesn't run more than once?
It seems that if I already have to wait to set the stuff until I am certain that the tab is in view, then I can just run the code there, it works even though don't set IsContentPreserved to true.
You are welcome!
The IsContentPreserved is used in cases when the user wants to keep the content state. If this is something you don't need for your application you could skip setting it. You can find more information about keeping the content state in our How to Keep the Content State article.
Regards,
Peshito
Progress Telerik