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

Export from RadFixedDocument to Stream

8 Answers 1742 Views
PdfProcessing
This is a migrated thread and some comments may be shown as answers.
Jamie
Top achievements
Rank 1
Jamie asked on 25 Sep 2014, 03:53 PM
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;
       }

8 Answers, 1 is accepted

Sort by
0
Petya
Telerik team
answered on 26 Sep 2014, 12:06 PM
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.
 
0
bill
Top achievements
Rank 1
answered on 19 Apr 2016, 05:55 PM
Have the limitations been removed?
0
Tanya
Telerik team
answered on 22 Apr 2016, 02:02 PM
Hi Bill,

The unification isn't completed yet and 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.
0
Paulo Goncalves
Top achievements
Rank 2
answered on 21 Oct 2016, 11:52 PM

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
0
Deyan
Telerik team
answered on 26 Oct 2016, 12:14 PM
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 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 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!
0
Paulo Goncalves
Top achievements
Rank 2
answered on 26 Oct 2016, 05:02 PM

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

0
Deyan
Telerik team
answered on 31 Oct 2016, 02:20 PM
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 of the exported documents as all 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!
0
Paulo Goncalves
Top achievements
Rank 2
answered on 31 Oct 2016, 04:31 PM

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.

 

Tags
PdfProcessing
Asked by
Jamie
Top achievements
Rank 1
Answers by
Petya
Telerik team
bill
Top achievements
Rank 1
Tanya
Telerik team
Paulo Goncalves
Top achievements
Rank 2
Deyan
Telerik team
Share this question
or