We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

File Corrupted issue when converting XLSX to PDF

Hi,
I am trying to convert a Excel(.xlsx) file to pdf using memory stream. But I am getting ‘The File is Corrupted’ error every time. I am first converting the xlsx file to input stream(Memory Stream) and then passing it to a method.

Below is my code,

Aspose.Cells.PdfSaveOptions soptions = new Aspose.Cells.PdfSaveOptions(SaveFormat.Pdf);

        soptions.Compliance = Aspose.Cells.Rendering.PdfCompliance.PdfA1b;
        soptions.AllColumnsInOnePagePerSheet = true;
        HTMLLoadOptions op = new HTMLLoadOptions(LoadFormat.Xlsx);
        LoadOptions loptions = new LoadOptions(LoadFormat.Xlsx);
        Inputstream.Seek(0, SeekOrigin.Begin);
        Aspose.Cells.Workbook book = new Aspose.Cells.Workbook(Inputstream);
        MemoryStream strm = new MemoryStream();

        book.Save(strm, soptions);

Please Let me know what is the issue with this(I couldn’t attach my Excel file here)

Thanks,
SubhaS

@subha.sundar,

Thanks for the sample code and details.

Please zip your XLSX file and attach it here, we will check your issue soon.
Moreover, could you try opening your Excel file and saving to PDF via Aspose.Cells APIs using filePath if it works fine or not.

Hi @Amjad_Sahi,

Here I have attached my sample code.

sample.zip (7.9 KB)

And I have tried using filepath and it works fine. But it is not working with Memory stream

Thanks,
Subha.S

@subha.sundar,

Thanks for the template file.

It should be an issue with your code, please fix it by yourself. I have tested using the following sample code with Aspose.Cells for .NET v17.8, it works fine (The PDF file is saved successfully to streams) and I do not find any issue with the output file (I convert the PDF stream to byte array and then write to the file via file streams):
e.g
Sample code:

Aspose.Cells.PdfSaveOptions soptions = new Aspose.Cells.PdfSaveOptions(SaveFormat.Pdf);

            soptions.Compliance = Aspose.Cells.Rendering.PdfCompliance.PdfA1b;
            soptions.AllColumnsInOnePagePerSheet = true;
            HTMLLoadOptions op = new HTMLLoadOptions(LoadFormat.Xlsx);
            LoadOptions loptions = new LoadOptions(LoadFormat.Xlsx);
            Aspose.Cells.Workbook book = new Aspose.Cells.Workbook("e:\\test2\\sample.xlsx");
            MemoryStream strm = new MemoryStream();
            book.Save(strm, soptions);
            
            string filename = "e:\\test2\\outsample1.pdf";

            FileStream fstream = new System.IO.FileStream(filename, FileMode.OpenOrCreate);
            strm.Position = 0;
            byte[] data = strm.ToArray();
            fstream.Write(data, 0, data.Length);

The output PDF file is also attached:
outsample1.pdf (70.6 KB)

Hi @Amjad_Sahi,
I am passing the file as Memory Stream and not File Stream. Here is the code I have converted and passed it as memory stream. Could you please try like this?

string stringFile = File.ReadAllText(“sample.xlsx”);
byte[] toBytes = Encoding.ASCII.GetBytes(stringFile);
MemoryStream Inputstream = new MemoryStream();
Inputstream.Write(toBytes, 0, toBytes.Length);

Aspose.Cells.PdfSaveOptions soptions = new Aspose.Cells.PdfSaveOptions(SaveFormat.Pdf);

        soptions.Compliance = Aspose.Cells.Rendering.PdfCompliance.PdfA1b;
        soptions.AllColumnsInOnePagePerSheet = true;
        HTMLLoadOptions op = new HTMLLoadOptions(LoadFormat.Xlsx);
        LoadOptions loptions = new LoadOptions(LoadFormat.Xlsx);
        Aspose.Cells.Workbook book = new Aspose.Cells.Workbook(Inputstream);
        MemoryStream strm = new MemoryStream();
        book.Save(strm, soptions);

I tried this because we don’t have physical file location for our excel file. We are trying to build it through string and loading it to Aspose as Memory Stream.

Please let me know whether it will work with memory streams and let me know if there is any mistake.

Thanks for you help,
Subha,S

@subha.sundar,

I have checked your code segment, your code is wrong. See the following updated sample code that works fine:
e.g
Sample code:

byte[] toBytes = File.ReadAllBytes("e:\\test2\\sample.xlsx");
MemoryStream Inputstream = new MemoryStream();
Inputstream.Write(toBytes, 0, toBytes.Length);
Inputstream.Position = 0;

Aspose.Cells.PdfSaveOptions soptions = new Aspose.Cells.PdfSaveOptions(SaveFormat.Pdf);

        soptions.Compliance = Aspose.Cells.Rendering.PdfCompliance.PdfA1b;
        soptions.AllColumnsInOnePagePerSheet = true;
        HTMLLoadOptions op = new HTMLLoadOptions(LoadFormat.Xlsx);
        LoadOptions loptions = new LoadOptions(LoadFormat.Xlsx);
        Aspose.Cells.Workbook book = new Aspose.Cells.Workbook(Inputstream);
        MemoryStream strm = new MemoryStream();
        book.Save(strm, soptions);

Hope, this helps a bit.