Export from RadFixedDocument to Stream

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

    Posted 25 Sep 2014 Link to this post


    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";
               // 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))
                  // 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);    
               return pdfs;
  2. Petya
    Petya avatar
    973 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);
    provider.Export(document, output);

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

    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
    Tanya avatar
    377 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.

    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
    25 posts
    Member since:
    Aug 2004

    Posted 5 days and 11 hours ago Link to this post

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


    Dim Provider1 As New Pdf.PdfFormatProvider
    Dim Stream1 As Stream = File.OpenRead(ArqPDF)
    Dim Doc1 As RadFixedDocument = New Pdf.PdfFormatProvider(Stream1, FormatProviderSettings.ReadOnDemand).Import()
        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)
            Provider2.Export(Doc2, Stream2)
            P = P + 1
    Catch ex As Exception
    End Try
  6. Deyan
    Deyan avatar
    129 posts

    Posted 23 hours ago 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.

    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
    25 posts
    Member since:
    Aug 2004

    Posted 18 hours ago 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. :)



Back to Top