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

Obtain file data

8 Answers 223 Views
PdfViewer and PdfViewerNavigator
This is a migrated thread and some comments may be shown as answers.
Jeffrey
Top achievements
Rank 1
Jeffrey asked on 21 Mar 2014, 11:47 AM
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.

8 Answers, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 25 Mar 2014, 02:50 PM
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.

 
0
Jeffrey
Top achievements
Rank 1
answered on 25 Mar 2014, 03:54 PM
Hi,
Thanks for the response. Could you provide this example in basic?

J
0
Stefan
Telerik team
answered on 26 Mar 2014, 05:50 AM
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.

 
0
Jeffrey
Top achievements
Rank 1
answered on 27 Mar 2014, 02:12 PM
[quote]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.

 
[/quote]
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.
0
Stefan
Telerik team
answered on 28 Mar 2014, 07:38 AM
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.

 
0
Jeffrey
Top achievements
Rank 1
answered on 28 Mar 2014, 11:48 AM
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
0
Andrea
Top achievements
Rank 1
answered on 12 Jun 2014, 05:17 PM
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
0
Stefan
Telerik team
answered on 13 Jun 2014, 09:42 AM
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.
 
Tags
PdfViewer and PdfViewerNavigator
Asked by
Jeffrey
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Jeffrey
Top achievements
Rank 1
Andrea
Top achievements
Rank 1
Share this question
or