This is a migrated thread and some comments may be shown as answers.

RadPDFViewer in a background application.

1 Answer 292 Views
PDFViewer
This is a migrated thread and some comments may be shown as answers.
Viju
Top achievements
Rank 1
Viju asked on 17 Jun 2019, 02:44 PM

Hi,

I am using telerik PDF viewer in my wpf application, i am following MVVM pattern to bind pdf to pdf viewer control. I have a PDF content as byte array which on load will be read into memory stream and then loaded to PDF document (code given below).

pdf is loading perfectly and i am able to get the selected page, selected content and  i am also able to save the start & end of the previous selection and apply the same back on the pdf.

currently i am trying to reload a new pdf content and apply the saved selection indexes (start and end) and extract the text between the indexes.

my application runs in background (no ui controls) and the pdf content (byte[]) is fetched from an API and this new content must be loaded to pdfdocument (Telerik.Windows.Documents.Fixed.PdfDocumentSource) and the saved indexes must be re applied and the content between the selected region must be extracted.

problem is when i load the byte[] then there is no error but the documentsource.Document is null . how do i get this working ? can i apply the selection indexes to Fixed.PdfDocumentSource and read selection content.

below is the code that i am using. 

Note: i am passing command descriptors to viewmodel constructor.

 <telerik:RadPdfViewer x:Name="pdfViewer" RenderOptions.BitmapScalingMode="HighQuality" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Row="1" DocumentSource="{Binding PDFDocument,Mode=TwoWay}" Mode="TextSelection">
  </telerik:RadPdfViewer>

 

    private Telerik.Windows.Documents.Fixed.PdfDocumentSource _PDFDocument;
    /// <summary>
    /// Pdf Document Source
    /// </summary>
    public Telerik.Windows.Documents.Fixed.PdfDocumentSource PDFDocument {
      get {
        return _PDFDocument;
      }
      set {
        _PDFDocument = value;
        OnPropertyChanged("PDFDocument");
      }
    }
     /// <summary>
    /// 
    /// </summary>
    private void LoadPDFDocument(byte[] pdfdata) {
      if (pdfdata != null) {
        MemoryStream DocumentStream = new MemoryStream(pdfdata);
        Telerik.Windows.Documents.Fixed.FormatProviders.FormatProviderSettings settings = new Telerik.Windows.Documents.Fixed.FormatProviders.FormatProviderSettings(Telerik.Windows.Documents.Fixed.FormatProviders.ReadingMode.OnDemand);
        PDFDocument = new Telerik.Windows.Documents.Fixed.PdfDocumentSource(DocumentStream, settings);
      }
    }

1 Answer, 1 is accepted

Sort by
0
Nikolay Demirev
Telerik team
answered on 20 Jun 2019, 10:07 AM
Hi Viju,

I didn't understand when your documentSource.Document is null, but I have checked the source code of the DocumentSource when the Document property is set. When you create a DocumentSource the Document property is null, and it is initialized after setting the document source to the RadPdfViewer. There is no public API that allows to load the document earlier.

In order to manipulate the selection you need to be able to get the current selection of the old document and then create new selection in the new document. Here you can read more about how to perform programmatic selection https://docs.telerik.com/devtools/wpf/controls/radpdfviewer/document-model/text/text-selection#programmatic-selection

In order to make the same selection in the new document you have to get the TextPositions from the old selection. The TextPosition is associated with only one RadFixedDocument and in order to apply the same selection on another document you have to create another TextPosition instance which refers to the same page and the same position in the page. Each TextPosition exposes Page property and Index property. You could use the Page property to find its index in the RadFixedDocument.Pages collection. After that you can create a new TextPosition object using the page on the same index of the newly imported document and the value of the TextPosition.Index property of the old document.

I hope I was able to help you.

Regards,
Nikolay Demirev
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
PDFViewer
Asked by
Viju
Top achievements
Rank 1
Answers by
Nikolay Demirev
Telerik team
Share this question
or