Export from RadFixedDocument to Stream

9 posts, 0 answers
  1. Jamie
    Jamie avatar
    1 posts
    Member since:
    Jul 2014

    Posted 25 Sep 2014 Link to this post

    Hello,

    I'm attempting to split a Pdf by removing pages.  After page removal, I export the RadFixedDocument back to a PDF through a stream.  I've tried various streams like MemoryStream and FileStream without luck.  All PDFs come about with blank pages.  Code is below.

    public static IEnumerable<string> SplitPdf(PdfFileDefinition pdfFileDef)
           {
               List<string> pdfs = new List<string>();
               Stream stream = null;
               PdfFormatProvider formatProvider = new PdfFormatProvider();
     
               // Save temp file to disk
                   var path = @"c:\temp";
                   if(!Directory.Exists(path))
                       Directory.CreateDirectory(path);
                
               // Pdf streams must support read & seek operations    
               var fileUri = new Uri(pdfFileDef.FilePath);
                
               // Is pdf a file or resource
               if (fileUri.Scheme == "file")
               {
                     var tempFilePath = Path.Combine(path, "temp.pdf");
                     File.Copy(Path.Combine(path,fileUri.LocalPath), tempFilePath,true);
                     stream = File.Open(tempFilePath, FileMode.Open,FileAccess.Read);              
               }
               else if (fileUri.Scheme == "pack" && fileUri.Host.Contains("siteoforigin"))
               {                              
                    stream = Application.GetRemoteStream(fileUri).Stream;   
               }
                            
               foreach(var pageBreak in pdfFileDef.PageBreaks)
               {                                                       
                   RadFixedDocument document = new PdfFormatProvider(stream, FormatProviderSettings.ReadOnDemand).Import();
                   RadFixedDocument splitDocument = new RadFixedDocument();  
                   splitDocument = document;
                               
                   for (int pageNumber = splitDocument.Pages.Count - 1; pageNumber >= 0; pageNumber--)
                   {
                       if(!pageNumber.IsBetween<int>(pageBreak.StartPage, pageBreak.EndPage))
                       {
                           splitDocument.Pages.RemoveAt(pageNumber);
                       }
     
                   }
                                  
                  // Generate temp pdf file for review
                   var fileName = Path.Combine(path, Path.GetRandomFileName());
                   fileName = Path.ChangeExtension(fileName, ".pdf");             
                                   
                   // Export filestream to Pdf
                   using(var fs = new FileStream(fileName,FileMode.Create))
                   {                               
                        formatProvider.Export(splitDocument, fs);    
                         
                   }                                           
     
                   pdfs.Add(fileName);
                                                                  
               }
     
               return pdfs;
           }
  2. Petya
    Admin
    Petya avatar
    975 posts

    Posted 26 Sep 2014 Link to this post

    Hi Jamie,

    Currently RadPdfViewer and RadPdfProcessing share the same public API, but differ internally. The reason for this is that soon we will merge both document models into one. It seems that the issue you are facing is related to using the improper methods of PdfFormatProvider. If you want to import PDF document, modify it and then export it back, you should use the following code:
    PdfFormatProvider provider = new PdfFormatProvider();
    RadFixedDocument document = provider.Import(input);
    Modify(document);
    provider.Export(document, output);

    Please note that RadPdfProcessing has some limitation when importing that will be removed when the two models are merged.

    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.
     
  3. bill
    bill avatar
    1 posts
    Member since:
    Sep 2015

    Posted 19 Apr in reply to Petya Link to this post

    Have the limitations been removed?
  4. Tanya
    Admin
    Tanya avatar
    404 posts

    Posted 22 Apr Link to this post

    Hi Bill,

    The unification isn't completed yet and currently I am unable to say when PdfViewer will be using the model of PdfProcessing.

    However, we introduced many improvements in the processing library with our recent releases and with the latest one - R2 2016, which will be available for download in a couple of weeks.

    In case you are facing issues importing a document with PdfProcessing or working with it, you could open a support ticket to share a sample document, so we can look further in the exact case.

    Hope this is helpful.

    Regards,
    Tanya
    Telerik
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
  5. Paulo Goncalves
    Paulo Goncalves avatar
    26 posts
    Member since:
    Aug 2004

    Posted 21 Oct Link to this post

    Hi, any news about split PDFs ? I install the 2016.3.1018 and add blank pages.

    Thanks

    Dim Provider1 As New Pdf.PdfFormatProvider
    Dim Stream1 As Stream = File.OpenRead(ArqPDF)
    Dim Doc1 As RadFixedDocument = New Pdf.PdfFormatProvider(Stream1, FormatProviderSettings.ReadOnDemand).Import()
     
    Try
        Dim P As Integer = 1
        Do While Doc1.Pages.Count > 0
            Dim Provider2 As New Pdf.PdfFormatProvider
            Dim ArqPDF2 As String = "c:\temp\Pag" & Format(P, "0####") & ".pdf"
            Dim Stream2 As Stream = File.OpenWrite(ArqPDF2)
            Dim Doc2 As New RadFixedDocument
            Dim PagPDF As RadFixedPage = Doc1.Pages(0)
            Doc1.Pages.Remove(PagPDF)
            Doc2.Pages.Add(PagPDF)
            Provider2.Export(Doc2, Stream2)
            Stream2.Close()
            P = P + 1
        Loop
    Catch ex As Exception
    End Try
  6. Deyan
    Admin
    Deyan avatar
    135 posts

    Posted 26 Oct Link to this post

    Hello Paolo,

    Looking at the snippet you have sent I see that when you import the RadFixedDocument instance you use the PdfFormatProvider constructor that has two parameters. Currently, this constructor should be called only when using RadPdfViewer control as shown in this documentation article. When creating document with RadPdfProcessing you should use the constructor mentioned in RadPdfProcessing Import documentation section. Although the two products share same public API their models are currently differing internally. In the future releases when we manage to make RadPdfProcessing Import functionality feature equal to RadPdfViewer's, we plan to use RadPdfProcessing internal model for both products and then there would be no difference which PdfFormatProvider's constructor is used. Until then you should only use the constructor without parameters mentioned in RadPdfProcessing documentation article.

    Additionally, I would like to mention that we are planning to implement API allowing fast merging, splitting and appending of PDF page content. This API will not depend on the PdfProcessing limited import functionality and would allow to process larger set of different PDF documents. We have logged a feature request in our feedback portal and you may vote for it and follow its implementation progress by following this feedback item.

    I hope this helps. If you have any other questions or concerns please contact us again.

    Regards,
    Deyan
    Telerik by Progress
    Do you need help with upgrading your WPF project? Try the Telerik API Analyzer and share your thoughts!
  7. Paulo Goncalves
    Paulo Goncalves avatar
    26 posts
    Member since:
    Aug 2004

    Posted 26 Oct in reply to Deyan Link to this post

    Hi Deyan, thanks for the answer.

    I change the API call and for sample.pdf the split work fine. But at my second test, use a PDF with 205 pages, only split the page 1 and 2, at 3th page return an error -2146232969, the message was displayed in portuguese, but translate to english is like some "The given key was not present in the dictionary.". I thing is the fact of RAdPdfProcessing was different internally and support less kinds of pdf files. I vote the feature request. I hope the implamentations ASAP. :)

     

    Regards

  8. Deyan
    Admin
    Deyan avatar
    135 posts

    Posted 31 Oct Link to this post

    Hello Paulo,

    Regarding the issue you are facing with the current RadPdfProcessing import capabilities - could you send us the document throwing the mentioned exception? We would like to investigate the cause of this exception so that we fix it in some of the following releases.

    As for the new API for fast merging, splitting and appending - last week we have started working on this item and its status is now "In Development". Currently, we have implemented a proof of concept and it shows quite impressive results - unmatched performance and memory footprint, as well as reliability of the exported documents as all PDF features related to the page content are supported out of the box and no page content is missing after the export. As the development process requires some more time for the implementation and proper testing, we plan to include this feature in our next official release (expected in January 2017). 

    I hope this sounds good to you.

    Regards,
    Deyan
    Telerik by Progress
    Do you need help with upgrading your WPF project? Try the Telerik API Analyzer and share your thoughts!
  9. Paulo Goncalves
    Paulo Goncalves avatar
    26 posts
    Member since:
    Aug 2004

    Posted 31 Oct in reply to Deyan Link to this post

    Hi Deyan, thats a good news !!! Even with the improvements which you mentioned.

    I will send the PDF at support ticket, it´s too big to forum.

     

Back to Top