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

Save PDF file

19 Answers 418 Views
PDFViewer
This is a migrated thread and some comments may be shown as answers.
figueiredorj
Top achievements
Rank 1
figueiredorj asked on 29 Mar 2012, 10:22 AM
Hi. Is there a implementation on how to save PDF file? I was expecting as in "print" and "open" also to find a "Save" command....

Thanks.

19 Answers, 1 is accepted

Sort by
0
figueiredorj
Top achievements
Rank 1
answered on 30 Mar 2012, 11:12 AM
I worked it out.

Although quite simple I do believe that as open it also should be in toolbar.

private SaveFileDialog saveFileDialog;
 
private byte[] _document;
public byte[] Document
{
    get { return _document; }
    set
    {
        if(_document != value)
        {
            _document = value;
            OnPropertyChanged("Document");
        }
    }
}
 
private void InitializeSaveDialog()
{
    saveFileDialog = new SaveFileDialog();
    saveFileDialog.DefaultExt = "pdf";
    saveFileDialog.Filter = "pdf Files (*.pdf)|*.pdf|All files (*.*)|*.*";
    saveFileDialog.FilterIndex = 1;
 
}
 
private void Print_Click(object sender, System.Windows.RoutedEventArgs e)
{
    InitializeSaveDialog();
    if(saveFileDialog.ShowDialog() == true)
    {
        using (Stream fs = (Stream)this.saveFileDialog.OpenFile())
        {
            fs.Write(Document, 0, Document.Length);
            fs.Close();
        
    }
}

In view only needed to add a new radbutton with Print_Click event on Click and it does the trick.

PS. someone mark it as answer. (if so simple why not a command as print and open?)
0
Kammen
Telerik team
answered on 30 Mar 2012, 02:39 PM
Hi Ricardo,

We have decided that it is not appropriate to introduce a save command until we implement export to PDF.

RadPdfViewer does not simply display PDF files, it parses the files to create RadFixedDocument. This means that theoretically you can create a document from scratch.

At this state, however, keeping the initial stream in memory is enough to complete the Save functionality. So there is one more thing to do to accomplish this task except the part you have already done. You should create a custom command that overrides OpenPdfDocumentCommand. There you should store the input stream to be used by the Save command later.

It will also be a better approach if you moved the Save logic in new command that inherits FixedDocumentViewerCommandBase. Please find attached a demo illustrating how you can create a custom command in RadPdfViewer.

Regards,
Kammen
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
Roshan
Top achievements
Rank 1
answered on 04 May 2012, 03:52 PM
Dear Team,

I also have the same requirement to save pdf file,I also downloaded your custom open command demo but still i didn't got it how to achieve save command functionality.Would you please provide me same example including save functionality also? Then it will be much clear to me. 

Please advice.

Thanks and best regards,
Roshan Salvi.
0
Iva Toteva
Telerik team
answered on 10 May 2012, 12:21 PM
Hi Roshan,

Please find attached a modified version of the demo, which includes two custom commands - an open document command, which saves the stream in a property, and a save command that writes this stream to a file. Hopefully, this approach will match the requirements of your application.

All the best,
Iva Toteva
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Mike
Top achievements
Rank 1
answered on 01 Aug 2012, 04:51 AM

I have similar requirement. I have server side pdf files available for viewing in the silverlight PDfViewer.

I have a button click event on a page that loads the file across the network.

        private void Button_Click_BlackMP(object sender, RoutedEventArgs e)
        {
    path = "/Report.pdf";
            this.RadPDFHolder.DocumentSource = new PdfDocumentSource(new System.Uri(path, System.UriKind.RelativeOrAbsolute)); 


Are you going to implement an Export (or save as) to PDF? 
Export is probably not the terminology since the users would expect that the viewer is showing a pdf, but from my understanding I believe it to be converted into a RadFixedDocument at this stage.

Cheers

Michael


0
Kammen
Telerik team
answered on 01 Aug 2012, 11:36 AM
Hi Mike,

We have export to PDF from RadPdfDocument in our to-do list, however we have not scheduled it yet.

Currently, the only way to save the file as PDF is to create your own save command. You can check the demo that Iva has provided. There you can see a simple implementation of a save command.

Greetings,

Kammen
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Mike
Top achievements
Rank 1
answered on 01 Aug 2012, 08:16 PM
Hi Kammen

I dont think the demo is applicable since in my case, the document is streamed from the server

ie.   this.RadPDFHolder.DocumentSource = new PdfDocumentSource(new System.Uri(path, System.UriKind.RelativeOrAbsolute)); 

I am not opening the file on the client side so there is no client side stream to access and subsequently save.. .

Mike
0
Kammen
Telerik team
answered on 02 Aug 2012, 03:54 PM

Hello Mike,

If you have full control over the documents that will be displayed in RadPdfViewer (the user cannot open PDF files on his own), then you should probably use the other constructor of PdfDocumentSource, which accepts a stream as a parameter.

First, you should open a stream from the Uri you have, store this stream, and later when the user tries to save the file, you will pass this stream to the user. You should note that RadPdfViewer runs on the client side, so saving must happen using the SaveFileDialog.

You can open a stream to a Uri using WebClient class. Your code should be something like this:

System.Net.WebClient webClient = new System.Net.WebClient();
webClient.OpenReadCompleted += (object sender, System.Net.OpenReadCompletedEventArgs e) =>
{
    if (e.Error == null)
    {
        //Save Stream here for future export using SaveFileDialog.
        this.viewer.DocumentSource = new PdfDocumentSource(e.Result);
    }
}
;
webClient.OpenReadAsync(MyUri, UriKind.RelativeOrAbsolute));

This is what RadPdfViewer does internally when you instantiate its DocumentSource using a Uri. In this way, you will set the document source of the viewer and will be able to use the stream when export is initiated.

Kind regards,

Kammen
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Henrik Skak Pedersen
Top achievements
Rank 1
answered on 15 May 2013, 08:15 PM
Hello,

I am also having this issue. I assume that there is still no save command?

If I load the document like this, instead of letting the viewer load the document, the loading progress bar is not being shown. Is there any way I can show the loading progress bar?

System.Net.WebClient webClient = new System.Net.WebClient();
webClient.OpenReadCompleted += (object sender, System.Net.OpenReadCompletedEventArgs e) =>
{
    if (e.Error == null)
    {
        //Save Stream here for future export using SaveFileDialog.
        this.viewer.DocumentSource = new PdfDocumentSource(e.Result);
    }
}
;
webClient.OpenReadAsync(MyUri, UriKind.RelativeOrAbsolute));

Thanks
Henrik
0
Kammen
Telerik team
answered on 17 May 2013, 08:13 AM
Hello Henrik,

You can try to create the PdfDocumentSource using the Uri instead of opening the stream by yourself. Then, in the Save command, you can use the Uri to download the stream and save it. In fact, we recommend this approach, because using the same stream as RadPdfViewer can cause unexpected behavior in some cases.

If you want to show a busy indicator when you download the stream in the save command, you can use your own in your application as the save command is not an operation in RadPdfViewer.

Hope this answers your question.

Regards,
Kammen
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Henrik Skak Pedersen
Top achievements
Rank 1
answered on 17 May 2013, 12:15 PM
Hi again,

Ok thank you, I will do that.

Thanks
Henrik
0
Kristine
Top achievements
Rank 1
answered on 10 Jun 2013, 07:00 AM
Hi,

I tried doing this without the Open command since I'm opening a pdf via memory stream.

However, i wasn't able to save the pdf into file because the CustomSaveCommand can't the pdf viewer stream.

It is always returning False in this part:

Public Overrides Function CanExecuteOverride(parameter As Object) As Boolean
    Return documentStream IsNot Nothing AndAlso documentStream.Length > 0
End Function

Below is how I load the PDF. Please see if there is something I'm missing.

Thank You

Dim ms As New MemoryStream()
ms.Write(e.Result, 0, e.Result.Length)
 
Dim winPdf As New PdfLabelViewer            
winPdf.pdfViewer.DocumentSource = New PdfDocumentSource(ms)              
winPdf.Show()
0
Bindu
Top achievements
Rank 1
answered on 22 Sep 2014, 07:23 AM
Am using RadPDFViewer with customcommands open and save,onclick save I want to pass PDF filename 

How to pass parameters to customsavecommand

Thanks 
Bindu
0
Petya
Telerik team
answered on 24 Sep 2014, 05:31 PM
Hello Bindu,

I'm afraid I don't quite understand your question. The save command discussed in this thread is a custom command. That said, how you'd invoke the command and whether you would use a parameter depends on you. For example, the following snippet shows that I've passed the value of a TextBox as a parameter to the command:
<telerik:RadButton Padding="4" DataContext="{Binding ElementName=_this, Path=Commands}" Command="{Binding SaveCommand}" CommandParameter="{Binding ElementName=pathTextBox, Path=Text}">

Please note that the Silverlight framework does not allow direct access to the file system.

Regards,
Petya
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.
 
0
Bindu
Top achievements
Rank 1
answered on 25 Sep 2014, 07:03 AM
Hello Petya,

It worked great.
Thanks:)
0
Bindu
Top achievements
Rank 1
answered on 25 Sep 2014, 08:21 AM
When i open PDF file using RadPDFViewer, i need to get PDF filename in code behind

Thanks in advance
Bindu
0
Petya
Telerik team
answered on 29 Sep 2014, 05:07 PM
Hi Bindu,

RadPdfViewer is responsible for showing PDF documents in one of the approaches listed in this article. The Custom Save Command sample provides a base that can be extended, so that all documents shown in the control can also be saved to a desired location. However, the control does not keep information regarding the name or the path to the file that you are showing, so this is up to you to implement.

Like I mentioned in my previous reply, Silverlight's framework imposes restrictions preventing you to access the file system on the client which is why I'd advise you to try and parse the file name from the uri passed to the OpenCommand.

Regards,
Petya
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.
 
0
Bindu
Top achievements
Rank 1
answered on 30 Sep 2014, 07:03 AM
Hello Petya,

What i was trying to get is, i need the PDF filename  that is opened and bind the filename to a label or textbox

Thanks
Bindu
0
Bindu
Top achievements
Rank 1
answered on 01 Oct 2014, 11:13 AM
I got the solution,

Thanks :)
Tags
PDFViewer
Asked by
figueiredorj
Top achievements
Rank 1
Answers by
figueiredorj
Top achievements
Rank 1
Kammen
Telerik team
Roshan
Top achievements
Rank 1
Iva Toteva
Telerik team
Mike
Top achievements
Rank 1
Henrik Skak Pedersen
Top achievements
Rank 1
Kristine
Top achievements
Rank 1
Bindu
Top achievements
Rank 1
Petya
Telerik team
Share this question
or