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

Free Support Forum - aspose.com

Saving pdf stream to file

I create a stream containing PDF using ASPOSE.WORD (.net) on server and sent the stream to a client.
On the client I can successfully save the stream to a file if I use Docment.Save but not if I directly save the stream to a file. In the example below file xxxx.pdf is a valid pdf. But file yyyy.pdf is not a valid pdf.
The client will not have the ASPOSE dll. I need to be able to save the stream containing the pdf to disk with using ASPOSE.

            var content = await response.Content.ReadAsStreamAsync();

            var pdfFileStream = File.Create(@"C:\temp\doc\yyyy.pdf"); 
            await content.CopyToAsync(pdfFileStream);

            var pdfDoc = new Document(pdfFileStream);
            PdfSaveOptions options = new PdfSaveOptions();
            options.TextCompression = PdfTextCompression.None;
            pdfDoc.Save(@"C:\temp\doc\xxxx.pdf", options);

On the server the stream is created as follows where buffer contains a docx (word) document.

            Stream stream = new MemoryStream();
            stream.Write(buffer, 0, buffer.Length);
            
            var pdfDoc = new Document(stream);
            pdfDoc.Save(stream, SaveFormat.Pdf);

I discovered the pdf file is partially ok when opening with Word. When opening with word there is an option to do a recover. When I recover the file contains are intact EXCEPT for the following (I am evaluating)
“Evaluation Only. Created with Aspose.Words. Copyright 2003-2017 Aspose Pty Ltd”

So it seems using Document.Save is doing some more then just saving. As I mentioned saving the stream directly to file and the file appears corrupt.

Work around

On the server use Document.Save to write a pdf file to disk
Read the file backing into a stream. On the client side I can save the stream directly to disk and successfully create a PDF file. If I create a Document object with the same stream the result is…

Aspose.Words.UnsupportedFileFormatException: Unknown file format: Unknown
at Aspose.Words.Document.:heart:(Stream :slight_smile:, LoadOptions :heart:)
at Aspose.Words.Document.:slight_smile:(Stream :slight_smile:, LoadOptions :heart:)
at Aspose.Words.Document…ctor(Stream stream, LoadOptions loadOptions)
at Aspose.Words.Document…ctor(Stream stream)
at FileServiceClient.Docx2PdfClient.d__4.MoveNext() in

So it seems streams created by Aspose do not just contain a pdf document.

@haler

Thanks for your inquiry. Please set stream position to 0 before saving to disk. Hopefully it will resolve the issue.

However, if the issue persists then Please create a standalone console application (source code without compilation errors) that helps us to reproduce your problem on our end and attach it here as ZIP file for testing. We will look into it and will guide you accordingly.