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

Export workbook to PDF, generated file corrupted

2 Answers 273 Views
SpreadProcessing
This is a migrated thread and some comments may be shown as answers.
Dario Concilio
Top achievements
Rank 2
Dario Concilio asked on 15 Apr 2016, 02:09 PM

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 Answers, 1 is accepted

Sort by
0
Accepted
Deyan
Telerik team
answered on 20 Apr 2016, 11:04 AM
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.
0
Dario Concilio
Top achievements
Rank 2
answered on 21 Apr 2016, 09:14 AM

Oh yes thank you,

Finally, I found same approach

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

Tags
SpreadProcessing
Asked by
Dario Concilio
Top achievements
Rank 2
Answers by
Deyan
Telerik team
Dario Concilio
Top achievements
Rank 2
Share this question
or