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

Need help on PDF split and merge

15 Answers 913 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
tchuat
Top achievements
Rank 1
tchuat asked on 23 Oct 2014, 03:59 AM
I am tryping to split pages from original pdf and save it as an individual pdf. No luck with the following code
Appreciate anyone can help. Thanks in advance.

 Dim provider As New Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.PdfFormatProvider

        Using input As System.IO.Stream = System.IO.File.OpenRead("C:\WebSite2\pdf001.pdf")
            Dim document As Telerik.Windows.Documents.Fixed.Model.RadFixedDocument = New Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.PdfFormatProvider(input, FormatProviderSettings.ReadOnDemand).Import()  
          
            For i As Integer = 0 To document.Pages.Count - 1
                Dim exportProvider As New Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.PdfFormatProvider
                Using out As System.IO.Stream = System.IO.File.OpenWrite("C:\WebSite2\Page" & i.ToString & ".pdf")

      
                    Dim outputDocument As New Telerik.Windows.Documents.Fixed.Model.RadFixedDocument
                    Dim page As RadFixedPage = New RadFixedPage()
                    page = document.Pages(i)

                    outputDocument.Pages.Add(page)
                    exportProvider.Export(outputDocument, out)
                End Using
            Next
        End Using


Exception:
System.ArgumentException was unhandled by user code
  HResult=-2147024809
  Message=The document element is associated with another parent.
Parameter name: item
  ParamName=item
  Source=Telerik.Windows.Documents.Fixed
 














15 Answers, 1 is accepted

Sort by
0
Pedro
Top achievements
Rank 1
answered on 23 Oct 2014, 03:42 PM
Can you add a screenshot?
_______________
mobile game hack
0
Deyan
Telerik team
answered on 27 Oct 2014, 12:54 PM
Hello,

Thank you for contacting us.

There are a few changes I would suggest to the code snippet you have sent:
  • When importing a PDF file in PdfProcessing you should be using the constructor shown in this documentation article. You may find more detailed information about the difference in PdfFormatProvider's constructors in this link to Telerik's forums.
  • Before adding the RadFixedPage to the new RadFixedDocument instance you should first remove the page from the old document Pages collection. This way you will not get the exception that "The document element is associated with another parent.".

I hope this is helpful. If you have any other questions or concerns please do not hesitate to contact us again.

Regards,
Deyan
the Telerik team
0
tchuat
Top achievements
Rank 1
answered on 28 Oct 2014, 02:55 AM
Thanks for your prompt reply.
I was first trying to use the sample code but the following code can't read the contain of the pdf, the caret position show object not reference to an object error.
Dim document As Telerik.Windows.Documents.Fixed.Model.RadFixedDocument = provider.Import(input)
So I change to the following code and it work
Dim document As Telerik.Windows.Documents.Fixed.Model.RadFixedDocument = New Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.PdfFormatProvider(input, FormatProviderSettings.ReadOnDemand).Import()

After i remove the page from the original pdf and attach to the new output document, the new pdf generated show an empty page with This document was generated by a trial version of Telerik RadPdfProcessing.

Below is the modified code
Dim provider As New Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.PdfFormatProvider
 
Using input As System.IO.Stream = System.IO.File.OpenRead("C:\WebSite2\082.pdf")
    'Dim document As Telerik.Windows.Documents.Fixed.Model.RadFixedDocument = provider.Import(input)
    Dim document As Telerik.Windows.Documents.Fixed.Model.RadFixedDocument = New Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.PdfFormatProvider(input, FormatProviderSettings.ReadOnDemand).Import() 'provider.Import(input)
     
    Dim j As Integer = 1
    For i As Integer = 0 To document.Pages.Count - 1
        If i = document.Pages.Count Then
            Exit For
        End If
        Dim exportProvider As New Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.PdfFormatProvider
        Using out As System.IO.Stream = System.IO.File.OpenWrite("C:\WebSite2\Page" & j.ToString & ".pdf")
            Dim outputDocument As New Telerik.Windows.Documents.Fixed.Model.RadFixedDocument
            Dim page As RadFixedPage = document.Pages(i)
            document.Pages.Remove(page)
            outputDocument.Pages.Add(page)
            exportProvider.Export(outputDocument, out)
            i -= 1
            j += 1
        End Using
    Next
 
End Using

0
Deyan
Telerik team
answered on 28 Oct 2014, 02:42 PM
Hello,

I am attaching a sample project that shows how to get the pages from two different PDF files and merge them into a single pdf. A few words about the project:
  • The project contains a singe WebForm with a button in it. 
  • It also contains two PDF files - Ellipses.pdf and Rectangles.pdf.
  • When pressing the button the two files are imported with a PdfFormatProvider. The pages from the imported documents are added to a new RadFixedDocument instance. The new document is being exported and downloaded as MergedDocument.pdf.
As for the import errors that you have mentioned - they may be caused by the specific PDF document you are importing. Could you please send us the document you are importing so that we can further investigate the issue causing the exception?

I am looking forward to contacting you again.

Regards,
Deyan
the Telerik team
0
tchuat
Top achievements
Rank 1
answered on 29 Oct 2014, 07:38 AM
Thanks for your prompt reply. The attach files does not allow me to attach pdf, so I had uploaded to https://www.sendspace.com/file/01cxvb
0
Deyan
Telerik team
answered on 29 Oct 2014, 04:46 PM
Hello,

Thank you for sending us the sample document.

The received file uses features that are still not supported by RadPdfProcessing. As mentioned in this forum post currently RadPdfViewer and RadPdfProcessing share the same public API, but internally their models are still differing. This is the cause of the different behaviour when using different PdfFormatProvider constructors. 

As PdfProcessing is under active development we are planning to unify both models soon, so that RadPdfProcessing supports all features supported by RadPdfViewer.

If you have any further questions do not hesitate to contact us again.

Regards,
Deyan
the Telerik team
0
tchuat
Top achievements
Rank 1
answered on 30 Oct 2014, 02:07 AM
Does this mean current version of the pdf library in telerik is not support to split (or open) my pdf?
0
Accepted
Deyan
Telerik team
answered on 30 Oct 2014, 04:17 PM
Hello,

That's correct, the current version of RadPdfProcessing cannot open the file you have sent because of still not supported features used in the PDF document. We are working on extending the features for our next couple of releases.

Regards,
Deyan
the Telerik team
0
tchuat
Top achievements
Rank 1
answered on 31 Oct 2014, 01:35 AM
Thanks. Looking forward for the new release.
0
Brian
Top achievements
Rank 1
answered on 05 May 2016, 03:40 PM

Hello, I was having similar issues and wondered if this will be fixed in the next release that is coming soon.

Thanks!

0
Deyan
Telerik team
answered on 10 May 2016, 08:44 AM
Hello Brian,

We are working on improving RadPdfProcessing import and we have already implemented a lot of new functionalities. However, it still has missing functionalities compared to RadPdfViewer. Could you send us the PDF document you are testing with, so that we can investigate the concrete issue you are facing?

Regards,
Deyan
the Telerik team
0
Alexander
Top achievements
Rank 2
answered on 06 Jun 2016, 10:05 AM

Hi

I have a VB.Net 4.5 solution and I'm having trouble using this code. As far as I can tell the import statement

Imports Telerik.Windows.Documents.Fixed.FormatProviders.Pdf
Imports Telerik.Windows.Documents.Fixed.Model

does not work because the 4.5 dll's does not contain the same code as the 4.0 ones that this example uses.

Reference to Telerik.Windows.Documents.Fixed, .Zip and .Core are ok.

Can you confirm this?

Is this a bug or by design? How should this be done in 4.5?

 

regards

Alexander Moan

0
Alexander
Top achievements
Rank 2
answered on 06 Jun 2016, 10:10 AM
Hi
I have a VB.Net 4.5 solution and I'm having trouble using this code. As far as I can tell the import statement
Imports Telerik.Windows.Documents.Fixed.FormatProviders.Pdf
Imports Telerik.Windows.Documents.Fixed.Model
does not work because the 4.5 dll's does not contain the same code as the 4.0 ones that this example uses.
Reference to Telerik.Windows.Documents.Fixed, .Zip and .Core are ok.
Can you confirm this?
Is this a bug or by design? How should this be done in 4.5?

regards
Alexander Moan
0
Deyan
Telerik team
answered on 07 Jun 2016, 08:21 AM
Hello Alexander,

I have tried to reproduce the issue by using Telerik binaries for .Net 4.5 and by changing the previosly sent demo target framework to .Net 4.5. By doing this the application is running correctly without any compile errors. Could you please check in your Visual Studio Project that when you right click and select "Properties" -> "Application" -> "Target framework" the value is ".NET Framework 4.5"?

If you still experience any issues do not hesitate to write again.

Regards,
Deyan
the Telerik team
0
Alexander
Top achievements
Rank 2
answered on 07 Jun 2016, 10:44 AM

Hi Deyan,

I had problems making the demo run yesterday, but today I got it working and were able to reproduce that it works for .Net 4.5.

After a bit of searching I found out that I had missed a reference, even if I said I did not. As usual it was my own fault....

Thanks for Your time and for kicking me in the right direction.

 

regards

Alexander

 

Tags
General Discussions
Asked by
tchuat
Top achievements
Rank 1
Answers by
Pedro
Top achievements
Rank 1
Deyan
Telerik team
tchuat
Top achievements
Rank 1
Brian
Top achievements
Rank 1
Alexander
Top achievements
Rank 2
Share this question
or