Free Support Forum - groupdocs.com

Get resulted stream data


#1

Hello I’m trying to develop webservice for converting files.


I am sending file in serialized form and store it to memory stream.

http://pastebin.com/xZXKKrGX

How can i get resulted stream data from conversion in .net?
How can i use StreamConversionResult?
I tried to use GetConverted method(obsolute) and for test save the stream to file but it creates empty document.

In the web service I don’t want to save the file to disk, I just want to send it back in serialized form to client(written in different language).

#2

Hello,


Thank you for your request. Yes, this is possible to get conversion result as a stream you can use such code:
FileStream fileStream = File.OpenRead(Server.MapPath("~/App_Data/testfile.docx"));
var convertResult = conversion.Convert(fileStream, FileType.Docx, FileType.Pdf);
var streams = convertResult.StreamConversionResult();
And in the “streams” variable you will have converted file stream .

Best regards.

#3
Thanks, I need to also set position to 0

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Groupdocs.Web.UI.Conversion;
using Groupdocs.Common;
using System.Diagnostics;
namespace TestGroup
{
class Program
{
static void Main(string[] args)
{

using (MemoryStream memoryStream = new MemoryStream())
{
using (FileStream fileStream = new FileStream("ToConvert/doc1.docx", FileMode.Open, FileAccess.Read))
{
// copy file to memory
fileStream.CopyTo(memoryStream);

// init
GroupdocsConversion.SetRootStoragePath(Path.Combine(Environment.CurrentDirectory, ""));
GroupdocsConversion.Init();
var conversion = GroupdocsConversion.Instance();

// convert memory stream or filestream
memoryStream.Position = 0;
var converted = conversion.Convert(memoryStream, FileType.Docx, FileType.Pdf);
var streams = converted.StreamConversionResult();
FileType testresult = streams.StreamItems[0].FileType;
var result = (MemoryStream)streams.StreamItems[0].Stream;

// and save it to file
using (FileStream file2 = new FileStream("test.pdf", FileMode.Create, System.IO.FileAccess.Write))
{
result.Position = 0;
result.WriteTo(file2);
}

Console.WriteLine("Finished!");
Console.ReadKey();
}
}
}
}
}


#4

Hello,


Glad that my reply was useful. Yes, in some cases you should set stream position to 0.

Also after conversion I can advise you to close the source stream.

Best regards.