Export workbook to PDF, generated file corrupted

3 posts, 1 answers
  1. Dario Concilio
    Dario Concilio avatar
    129 posts
    Member since:
    Apr 2016

    Posted 15 Apr Link to this post

    Hi to all,

    I'm tring to creare an utility that receive a RadGridView and show me a preview of it.

    I created 2 static methods CreateOpenXMLDocument that creates a Workbook starting by RadGridView control.

    Second one is PreviewList that save the workbook in PDF File, and then I use PDFViewer.

    PDFViewer show me an error that I've attached here.

    If a try to open the PDF file, my PDF reader inform me that it's corrupted.

    Where I wrong?

    01.private static Workbook CreateOpenXMLDocument(RadGridView gridView)
    02.{
    03.    gridView.ElementExportingToDocument += P_GridView_ElementExportingToDocument;
    04.    var workbook = gridView.ExportToWorkbook();
    05.    var worksheet = workbook.Worksheets[0];
    06.    int columns = gridView.Columns.Count;
    07. 
    08.    ColumnSelection columnSelection = worksheet.Columns[0, columns - 1];
    09.    columnSelection.AutoFitWidth();
    10. 
    11.    return workbook;
    12.}
    13.         
    14.public static void PreviewList(RadGridView gridView)
    15.{
    16.    var workbook = CreateOpenXMLDocument(gridView);
    17.    PdfFormatProvider provider = new PdfFormatProvider();
    18. 
    19.    string tempPath = System.IO.Path.GetTempPath();
    20.    string tempGiud = Guid.NewGuid().ToString().Replace("{}-", "");
    21. 
    22.    string fileToPreview = string.Format(@"{0}{1}.pdf", tempPath, tempGiud);
    23. 
    24.    using (Stream output = System.IO.File.OpenWrite(fileToPreview))
    25.    {
    26.        var exportSettings = new Telerik.Windows.Documents.Spreadsheet.FormatProviders.Pdf.Export.PdfExportSettings(Telerik.Windows.Documents.Spreadsheet.FormatProviders.ExportWhat.ActiveSheet, true);
    27.        var pdfFileSettings = new Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.Export.PdfExportSettings();
    28. 
    29.        pdfFileSettings.ComplianceLevel = Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.Export.PdfComplianceLevel.None;
    30.        pdfFileSettings.ImageQuality = Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.Export.ImageQuality.High;
    31. 
    32.        exportSettings.PdfFileSettings = pdfFileSettings;
    33.        provider.ExportSettings = exportSettings;
    34. 
    35.        provider.Export(workbook);
    36.    }
    37. 
    38.    PDFPreviewDialog win = new PDFPreviewDialog();
    39.    win.Tag = fileToPreview;
    40.    win.Center2_3();
    41.    win.ShowDialog();
    42. 
    43.    System.IO.File.Delete(fileToPreview);
    44.}

  2. Answer
    Deyan
    Admin
    Deyan avatar
    136 posts

    Posted 20 Apr Link to this post

    Hello Potito,

    Looking at PreviewList method it seems that the Workbook instance is exported to PDF byte array and the output Stream variable is never used. So instead of using the Export method that takes one parameter, you should call the overload with two parameters as shown below:
    provider.Export(workbook, output);

    I hope this helps.

    Regards,
    Deyan
    the Telerik team
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
  3. Dario Concilio
    Dario Concilio avatar
    129 posts
    Member since:
    Apr 2016

    Posted 21 Apr in reply to Deyan Link to this post

    Oh yes thank you,

    Finally, I found same approach

    var buffer = provider.Export(workbook);
    output.Write(buffer, 0, buffer.Length);

Back to Top