Save PDF file

20 posts, 0 answers
  1. figueiredorj
    figueiredorj avatar
    68 posts
    Member since:
    Mar 2011

    Posted 29 Mar 2012 Link to this post

    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.
  2. figueiredorj
    figueiredorj avatar
    68 posts
    Member since:
    Mar 2011

    Posted 30 Mar 2012 Link to this post

    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?)
  3. DevCraft banner
  4. Kammen
    Admin
    Kammen avatar
    312 posts

    Posted 30 Mar 2012 Link to this post

    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 >>
  5. Roshan
    Roshan avatar
    1 posts
    Member since:
    Apr 2012

    Posted 04 May 2012 Link to this post

    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.
  6. Iva Toteva
    Admin
    Iva Toteva avatar
    1319 posts

    Posted 10 May 2012 Link to this post

    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 >>

  7. Mike
    Mike avatar
    10 posts
    Member since:
    Sep 2012

    Posted 31 Jul 2012 Link to this post


    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


  8. Kammen
    Admin
    Kammen avatar
    312 posts

    Posted 01 Aug 2012 Link to this post

    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.

  9. Mike
    Mike avatar
    10 posts
    Member since:
    Sep 2012

    Posted 01 Aug 2012 Link to this post

    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
  10. Kammen
    Admin
    Kammen avatar
    312 posts

    Posted 02 Aug 2012 Link to this post

    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.

  11. Henrik Skak Pedersen
    Henrik Skak Pedersen avatar
    16 posts
    Member since:
    Nov 2006

    Posted 15 May 2013 Link to this post

    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
  12. Kammen
    Admin
    Kammen avatar
    312 posts

    Posted 17 May 2013 Link to this post

    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.

  13. Henrik Skak Pedersen
    Henrik Skak Pedersen avatar
    16 posts
    Member since:
    Nov 2006

    Posted 17 May 2013 Link to this post

    Hi again,

    Ok thank you, I will do that.

    Thanks
    Henrik
  14. Kristine
    Kristine avatar
    1 posts
    Member since:
    Jan 2013

    Posted 10 Jun 2013 Link to this post

    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()
  15. Bindu
    Bindu avatar
    25 posts
    Member since:
    Aug 2014

    Posted 22 Sep 2014 Link to this post

    Am using RadPDFViewer with customcommands open and save,onclick save I want to pass PDF filename 

    How to pass parameters to customsavecommand

    Thanks 
    Bindu
  16. Petya
    Admin
    Petya avatar
    975 posts

    Posted 24 Sep 2014 Link to this post

    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.
     
  17. Bindu
    Bindu avatar
    25 posts
    Member since:
    Aug 2014

    Posted 25 Sep 2014 in reply to Petya Link to this post

    Hello Petya,

    It worked great.
    Thanks:)
  18. Bindu
    Bindu avatar
    25 posts
    Member since:
    Aug 2014

    Posted 25 Sep 2014 Link to this post

    When i open PDF file using RadPDFViewer, i need to get PDF filename in code behind

    Thanks in advance
    Bindu
  19. Petya
    Admin
    Petya avatar
    975 posts

    Posted 29 Sep 2014 Link to this post

    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.
     
  20. Bindu
    Bindu avatar
    25 posts
    Member since:
    Aug 2014

    Posted 30 Sep 2014 in reply to Petya Link to this post

    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
  21. Bindu
    Bindu avatar
    25 posts
    Member since:
    Aug 2014

    Posted 01 Oct 2014 in reply to Bindu Link to this post

    I got the solution,

    Thanks :)
Back to Top
DevCraft banner