Obtain file data

9 posts, 0 answers
  1. Jeffrey
    Jeffrey avatar
    14 posts
    Member since:
    Mar 2014

    Posted 21 Mar 2014 Link to this post

    Hi,
    Not sure how to obtain file data/stream from a PDF the user loaded via the navigator. Would like either the path to the file or the PDF data as memory stream or similar.

  2. Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 25 Mar 2014 Link to this post

    Hi Jeffrey,

    Thank you for writing.

    The only way to get the document would be to use reflection to access the internal stream used by the control. Here is how to do that:
    private void radButton1_Click(object sender, EventArgs e)
    {
        PropertyInfo formatProvider = typeof(Telerik.Windows.Documents.Fixed.Model.RadFixedDocument).GetProperty("FormatProvider", BindingFlags.NonPublic | BindingFlags.Instance);
        Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.PdfFormatProvider pdfProvider = formatProvider.GetValue(radPdfViewer1.Document, null) as Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.PdfFormatProvider;
     
        FieldInfo field = typeof(Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.PdfFormatProvider).GetField("stream", BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.Instance);
        Stream stream = field.GetValue(pdfProvider) as Stream;
    }

    I hope this helps.

    Regards,
    Stefan
    Telerik
     

    Build cross-platform mobile apps using Visual Studio and .NET. Register for the online webinar on 03/27/2014, 11:00AM US ET.. Seats are limited.

     
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Jeffrey
    Jeffrey avatar
    14 posts
    Member since:
    Mar 2014

    Posted 25 Mar 2014 in reply to Stefan Link to this post

    Hi,
    Thanks for the response. Could you provide this example in basic?

    J
  5. Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 26 Mar 2014 Link to this post

    Hello,

    Here you are:
    Private Sub radButton1_Click(sender As Object, e As EventArgs)
        Dim formatProvider As PropertyInfo = GetType(Telerik.Windows.Documents.Fixed.Model.RadFixedDocument).GetProperty("FormatProvider", BindingFlags.NonPublic Or BindingFlags.Instance)
        Dim pdfProvider As Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.PdfFormatProvider = TryCast(formatProvider.GetValue(radPdfViewer1.Document, Nothing), Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.PdfFormatProvider)
     
        Dim field As FieldInfo = GetType(Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.PdfFormatProvider).GetField("stream", BindingFlags.NonPublic Or BindingFlags.GetField Or BindingFlags.Instance)
        Dim stream As Stream = TryCast(field.GetValue(pdfProvider), Stream)
    End Sub

    When you need conversion between C# and VB, you can use our free online converter: http://converter.telerik.com/.

    Regards,
    Stefan
    Telerik
     

    Build cross-platform mobile apps using Visual Studio and .NET. Register for the online webinar on 03/27/2014, 11:00AM US ET.. Seats are limited.

     
  6. Jeffrey
    Jeffrey avatar
    14 posts
    Member since:
    Mar 2014

    Posted 27 Mar 2014 in reply to Stefan Link to this post

    Stefan said:Hello,

    Here you are:
    Private Sub radButton1_Click(sender As Object, e As EventArgs)
        Dim formatProvider As PropertyInfo = GetType(Telerik.Windows.Documents.Fixed.Model.RadFixedDocument).GetProperty("FormatProvider", BindingFlags.NonPublic Or BindingFlags.Instance)
        Dim pdfProvider As Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.PdfFormatProvider = TryCast(formatProvider.GetValue(radPdfViewer1.Document, Nothing), Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.PdfFormatProvider)
     
        Dim field As FieldInfo = GetType(Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.PdfFormatProvider).GetField("stream", BindingFlags.NonPublic Or BindingFlags.GetField Or BindingFlags.Instance)
        Dim stream As Stream = TryCast(field.GetValue(pdfProvider), Stream)
    End Sub

    When you need conversion between C# and VB, you can use our free online converter: http://converter.telerik.com/.

    Regards,
    Stefan
    Telerik
     

    Build cross-platform mobile apps using Visual Studio and .NET. Register for the online webinar on 03/27/2014, 11:00AM US ET.. Seats are limited.

     

    Thanks for this. It does work and I can send the stream to other PDF navigators but I'm having a hard time saving the stream object as a file or to a mySQL database which is the ultimate goal.
  7. Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 28 Mar 2014 Link to this post

    Hi Jeffrey,

    To write the the stream to a file you should use FileStream. Here is a small example:
    Private Sub radButton1_Click(sender As Object, e As EventArgs)
        Dim formatProvider As PropertyInfo = GetType(Telerik.Windows.Documents.Fixed.Model.RadFixedDocument).GetProperty("FormatProvider", BindingFlags.NonPublic Or BindingFlags.Instance)
        Dim pdfProvider As Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.PdfFormatProvider = TryCast(formatProvider.GetValue(radPdfViewer1.Document, Nothing), Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.PdfFormatProvider)
     
        Dim field As FieldInfo = GetType(Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.PdfFormatProvider).GetField("stream", BindingFlags.NonPublic Or BindingFlags.GetField Or BindingFlags.Instance)
        Dim stream As FileStream = TryCast(field.GetValue(pdfProvider), FileStream)
     
        stream.Seek(0, SeekOrigin.Begin)
     
        Using fs As New FileStream("D:\test.pdf", FileMode.Create, FileAccess.Write)
            Dim b As Byte() = New Byte(1023) {}
     
            While stream.Read(b, 0, b.Length) > 0
                fs.Write(b, 0, b.Length)
            End While
        End Using
    End Sub

    Here you can find how to write a stream to mySQL: http://dev.mysql.com/doc/connector-net/en/connector-net-programming-blob-writing.html.

    I hope this helps.

    Regards,
    Stefan
    Telerik
     

    Build cross-platform mobile apps using Visual Studio and .NET. Register for the online webinar on 03/27/2014, 11:00AM US ET.. Seats are limited.

     
  8. Jeffrey
    Jeffrey avatar
    14 posts
    Member since:
    Mar 2014

    Posted 28 Mar 2014 in reply to Stefan Link to this post

    I actually found a way to do this. Posting it here in case others have need for it:

     
     Dim formatProvider1 As PropertyInfo = GetType(Telerik.Windows.Documents.Fixed.Model.RadFixedDocument).GetProperty("FormatProvider", BindingFlags.NonPublic Or BindingFlags.Instance)
    Dim pdfProvider1 As Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.PdfFormatProvider = TryCast(formatProvider1.GetValue(RadPdfViewer1.Document, Nothing), Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.PdfFormatProvider)
    Dim field1 As FieldInfo = GetType(Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.PdfFormatProvider).GetField("stream", BindingFlags.NonPublic Or BindingFlags.GetField Or BindingFlags.Instance)
    Dim stream1 As Stream = TryCast(field1.GetValue(pdfProvider1), Stream)
             
    stream1.Position = 0
    myCommand.CommandText = "INSERT INTO manalive.instest (filedata) VALUES (@filedata)"
    myCommand.Parameters.AddWithValue("@filedata", GetStreamAsByteArray(stream1))
    myCommand.ExecuteNonQuery()

     Private Function GetStreamAsByteArray(ByVal stream As System.IO.Stream) As Byte()


            Dim streamLength As Integer = Convert.ToInt32(stream.Length)
            Dim fileData As Byte() = New Byte(streamLength) {}
            ' Read the file into a byte array


            stream.Read(fileData, 0, streamLength)
            stream.Close()
            Return fileData


        End Function
  9. Andrea
    Andrea avatar
    52 posts
    Member since:
    Oct 2012

    Posted 12 Jun 2014 Link to this post

    I used a different approach, what is really missing is a property on Navigator for the last loaded file, so my idea was to replace the open button on the navigator with a custom open button, this custom button calls PdfViewer.LoadDocument and save the path of the last opened pdf file.

    void ReplaceOpenButton()
    {          
        CommandBarButton openButton = radPdfViewerNavigator1.DefaultStrip.Items[0] as CommandBarButton;
        CommandBarButton localOpenButton = new CommandBarButton();
        localOpenButton.Text = openButton.Text;
        localOpenButton.Image = openButton.Image;
        localOpenButton.MouseUp += new MouseEventHandler(OpenAPdf);
        openButton.Enabled = false;
        openButton.Dispose();
     
        radPdfViewerNavigator1.DefaultStrip.Items.Insert(0, localOpenButton);
    }
     
    string lastOpenedFileName = "";
    bool loading = true;
     
    private string LastOpenedFileName
    {
        get {
            return loading ? "" : lastOpenedFileName;
        }
    }
    void OpenAPdf(object sender, MouseEventArgs e)
    {                       
     
        OpenFileDialog openFileDialog = new OpenFileDialog();
        openFileDialog.Filter = "PDF Files|*.pdf";//should be localized
        openFileDialog.Title = "Select a PDF File";//should be localized
     
        if (openFileDialog.ShowDialog() == DialogResult.OK)
        {
            loading = true;
            lastOpenedFileName = openFileDialog.FileName;
            radPdfViewer1.LoadDocument(lastOpenedFileName);               
        }
    }
     
    private void radPdfViewer1_DocumentLoaded(object sender, EventArgs e)
    {
        // ..... cut
        loading = false;
    }

    Of course I would prefer to have a simple way to know which was the last document loaded using the navigator without the risk that next release of the navigator change the position of the save button.

    Best Regards
  10. Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 13 Jun 2014 Link to this post

    Hello Andrea,

    For the time being, we do not intend introducing such a property. We also, do not intend changing the element structure of the pdf navigator. However, if this ever happens, it will be noted in the Release notes of the product, so you will be aware of it.

    I hope that you find this information useful.

    Regards,
    Stefan
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
Back to Top
UI for WinForms is Visual Studio 2017 Ready