Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Fri Aug 24, 2018 3:13 am

I am trialing out the Word to PDF conversion and I am struggling to save a word DOCX file to a stream as it always corrupts the PDF.

Attached is my relevant code:
Code: Select all
using (MemoryStream pdfDocument = new MemoryStream())
{
    Spire.Doc.Document doc = new Spire.Doc.Document(wordStream, Spire.Doc.FileFormat.Auto);
    doc.SaveToFile(Path.Combine(BulkEmailTemplateSavePath, fileName), Spire.Doc.FileFormat.PDF); //-Works producing PDF that is readable
    doc.SaveToStream(pdfDocument, Spire.Doc.FileFormat.PDF); //-PDF saved out later is corrupt
    attachmentsToAdd.Add(new KeyValuePair<string, byte[]>(fileName, pdfDocument.GetBuffer()));
}


I've tried combinations of saving the PDF to disk and to stream and I can't get the saving to stream working.

Thanks

crichardson@mmcnz.co.nz
 
Posts: 9
Joined: Fri Aug 24, 2018 1:32 am

Fri Aug 24, 2018 7:57 am

Dear Craig,

Thanks for your inquiry.
Please replace the pdfDocument.GetBuffer() with the pdfDocument.ToArray(). If the issue still happens after replacing, please supply your input Word file as well as your full code for further investigation. You could send them to us via email (support@e-iceblue.com).

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1187
Joined: Tue Sep 27, 2016 1:06 am

Mon Aug 27, 2018 11:25 pm

Thanks for that. That fixed the corrupt stream. I'm not sure why GetBuffer() and ToArray() produce different results though.

Anyway it is fixed.

Thanks,

Craig

crichardson@mmcnz.co.nz
 
Posts: 9
Joined: Fri Aug 24, 2018 1:32 am

Tue Aug 28, 2018 3:28 am

Hello,

Thanks for your feedback.
The GetBuffer() method only gets the data of the buffer, but not all the data of the Pdf stream, that' why the generated Pdf has errors. If you have any confusion, just feel free to write back.

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1187
Joined: Tue Sep 27, 2016 1:06 am

Thu Jul 15, 2021 8:59 am

Hello, I have a similar problem when saving a doc document to a stream:

public async Task<FileModel> ConvertDocToPdfAsync(IFormFile file, string resultFileName)
{
Document document = new();
using (Stream stream = file.OpenReadStream())
using (MemoryStream targetStream = new())
{
document.LoadFromStream(stream, Spire.Doc.FileFormat.Auto);
document.SaveToStream(stream, Spire.Doc.FileFormat.PDF); -> error
return FileModel.Build(resultFileName, targetStream.ToArray());
}
}



"Message": "Specified method is not supported.",
"Data": null,
"InnerException": {
"$type": "System.NotSupportedException, System.Private.CoreLib",
"ClassName": "System.NotSupportedException",
"Message": "Specified method is not supported.",
"Data": null,
"InnerException": null,
"HelpURL": null,
"StackTraceString": " at Microsoft.AspNetCore.Http.ReferenceReadStream.Write(Byte[] buffer, Int32 offset, Int32 count)\r\n at System.IO.Stream.WriteByte(Byte value)\r\n at spr㚳.ᜂ(String A_0)\r\n at spr㚏.ᜅ(Stream A_0, spr㚞 A_1, Boolean A_2)\r\n

s.shevyakov
 
Posts: 18
Joined: Thu Jul 15, 2021 8:42 am

Thu Jul 15, 2021 9:48 am

Hello,

Thanks for your inquiry.
Please refer to the following modified code.
Code: Select all
                //document.SaveToStream(stream, Spire.Doc.FileFormat.PDF);
                document.SaveToStream(targetStream, Spire.Doc.FileFormat.PDF);

If there are any other issues related to our products, just feel free to contact us.

Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Return to Spire.Doc