Completed
Last Updated: 22 Dec 2017 11:48 by ADMIN
ADMIN
Dess | Tech Support Engineer, Sr.
Created on: 24 Nov 2017 09:50
Category: GridView
Type: Bug Report
2
FIX. RadGridView - IOException when trying to open the exported file in the GridViewPdfExport.PdfExported event
To reproduce:

   Sub New()
         
        InitializeComponent()
        Dim dt As New DataTable
        dt.Columns.Add("Id", GetType(Integer))
        dt.Columns.Add("Name", GetType(String))
        For index = 1 To 1000
            dt.Rows.Add(index, "Item" & index)
        Next
        Me.RadGridView1.DataSource = dt


    End Sub
    Private Sub RadButton1_Click(sender As Object, e As EventArgs) Handles RadButton1.Click
        Dim pdfFile As String = "..\..\exportedFile.pdf"
        Dim pdfExporter As New GridViewPdfExport(Me.RadGridView1)
        pdfExporter.ShowHeaderAndFooter = True
        pdfExporter.FitToPageWidth = True
        AddHandler pdfExporter.PdfExported, AddressOf pdfExporter_PdfExported
        Dim renderer As New PdfExportRenderer()
        pdfExporter.RunExport(pdfFile, renderer)

        Process.Start(pdfFile)
    End Sub

    Private Sub pdfExporter_PdfExported(sender As Object, e As System.EventArgs)

        Dim pdfFile As String = "..\..\exportedFile.pdf"
        Dim document As RadFixedDocument
        Dim provider As Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.PdfFormatProvider = New Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.PdfFormatProvider()
        Using stream As Stream = File.OpenRead(pdfFile)
            document = provider.Import(stream)  '<==== Error Found
            For Each page As RadFixedPage In document.Pages
                Dim editor As FixedContentEditor = New FixedContentEditor(page)
                editor.Position.Translate(page.Size.Width / 2, page.Size.Height - 50)

                Dim pageNum As Integer = document.Pages.IndexOf(page) + 1
                editor.DrawText(pageNum + " of " + document.Pages.Count)
            Next
        End Using

        Using output As Stream = File.OpenWrite(pdfFile)
            provider.Export(document, output)    '<==== Error Found
        End Using

        Process.Start(pdfFile)
    End Sub

Workaround: use the PdfExportRenderer.PdfExporting event where you have access to the document and you can make any customizations to it:
        Dim pdfFile As String = "..\..\exportedFile.pdf"
        Dim pdfExporter As New GridViewPdfExport(Me.RadGridView1)
        pdfExporter.ShowHeaderAndFooter = True
        pdfExporter.FitToPageWidth = True
        Dim renderer As New PdfExportRenderer()
        AddHandler renderer.PdfExporting, AddressOf PdfExporting
        pdfExporter.RunExport(pdfFile, renderer)

    Private Sub PdfExporting(sender As Object, e As PdfExportingEventArgs)
        Dim document As RadFixedDocument = e.Document
        Dim provider As Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.PdfFormatProvider = New Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.PdfFormatProvider()

        For Each page As RadFixedPage In document.Pages
            Dim editor As FixedContentEditor = New FixedContentEditor(page)
            editor.Position.Translate(page.Size.Width / 2, page.Size.Height - 50)

            Dim pageNum As Integer = document.Pages.IndexOf(page) + 1
            editor.DrawText(pageNum & " of " & document.Pages.Count)
        Next 
    End Sub
0 comments