RadPdfViewer (wpf ) throws exception trying open Pdf file produced by PdfStreamWriter

1 Answer 165 Views
PdfProcessing
Alexander
Top achievements
Rank 1
Iron
Alexander asked on 29 Sep 2022, 01:48 PM

I would like to copy some pages from original document tonew pdf Document.

my Source code is based on merge document example proveded Telerik documenttaion:

Public Function ExtractPagesToPDF(pdfDocument() As Byte, pages() As Integer) As Byte() Implements IPdfPageExtractor.ExtractPagesToPDF
        Dim ms = New MemoryStream()
        Using pdfWriter = New PdfStreamWriter(ms)
            pdfWriter.Settings.DocumentInfo.Author = "TMS"
            pdfWriter.Settings.WriteAnnotations = True
            Using pdfStream = New MemoryStream(pdfDocument)
                Dim document = New PdfFileSource(pdfStream)
                For Each index In pages
                    If index >= document.Pages.Count Then
                        Throw New ArgumentException($"Page index {index} > Document page count {document.Pages.Count}.")
                    End If
                    pdfWriter.WritePage(document.Pages(index))
                Next
                ms.Position = 0
                Return ms.ToArray()
            End Using
        End Using
    End Function

if I try open  new document with with Telerik WPF RadPdfViewer my programm throws exception:

NotSupportedException: StartXRef keyword cannot be found.

Do I  miss some settings?

 

1 Answer, 1 is accepted

Sort by
0
Martin
Telerik team
answered on 03 Oct 2022, 08:07 AM

Hi Alexander,

Most of the code snippet looks fine. The only thing you could change to make it work would be to leave the stream open after disposing of the PdfStreamWriter and to move the return of the memory stream outside of the using braces of the PdfStreamWriter

Check the following code snippet:

Private Function ExtractPagesToPDF(ByVal pdfDocument As Byte(), ByVal pages As Integer()) As Byte()
    Dim ms = New MemoryStream()

    Using pdfWriter = New PdfStreamWriter(ms, leaveStreamOpen:=True)
        pdfWriter.Settings.DocumentInfo.Author = "TMS"
        pdfWriter.Settings.WriteAnnotations = True

        Using pdfStream As MemoryStream = New MemoryStream(pdfDocument)
            Dim document As PdfFileSource = New PdfFileSource(pdfStream)

            For Each index As Integer In pages

                If index >= document.Pages.Count() Then
                    Throw New ArgumentException($"Page index {index} > Document page count {document.Pages.Count()}.")
                End If

                pdfWriter.WritePage(document.Pages(index))
            Next
        End Using
    End Using

    ms.Position = 0
    Return ms.ToArray()
End Function

If this doesn't solve the unexpected behavior, I would like to ask you to open a support ticket and send us the document causing it in order to deeper investigate the case. I must assure you we treat all client files in a strictly confidential manner and for testing purposes only.

Regards,
Martin
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
PdfProcessing
Asked by
Alexander
Top achievements
Rank 1
Iron
Answers by
Martin
Telerik team
Share this question
or