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

Free Support Forum - aspose.com

Pdf to stream

Hello,

Hello, I am bringing in a stream from a word doc and want to convert it to pdf and pass it back out as stream, when I do it the file is corrupt.

public void ConvertFileToPdf(Stream fileStream, string keyName)
{
Document doc = new Document(fileStream);
MemoryStream pdfFileStream = new MemoryStream();
doc.Save(pdfFileStream, SaveFormat.AsposePdf);
Aspose.Pdf.Pdf pdf = new Aspose.Pdf.Pdf();
pdf.BindXML(pdfFileStream, null);
pdf.Save(pdfFileStream);
_fileStorageService.SaveFile(keyName, pdfFileStream);
pdfFileStream.Close();
}

is it something silly I am doing? Thanks!

Here is where I send the file to write to disk:

public void SaveFile(string keyName, Stream stream)
{
byte[] memBytes = new byte[stream.Length];
stream.Write(memBytes, 0, (int)stream.Length);

FileStream fileStream = File.OpenWrite(GetFullPath(keyName));
fileStream.Write(memBytes, 0, memBytes.Length);
fileStream.Close();
}

Hello <?xml:namespace prefix = u1 /><?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" />Douglas,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

I have tested the scenario and have noticed the problem. The issue is occurring because you are using the same MemoryStream object to hold the intermediate XML contents and final Pdf document. I have modified the code snippet, you may be required to change the output path information. Please try using the following.

[C#]

public void ConvertFileToPdf(Stream fileStream, string keyName)
{
Document doc = new Document(fileStream);
MemoryStream pdfFileStream = new MemoryStream();
MemoryStream output = new MemoryStream(); // hold the pdf output

doc.Save(pdfFileStream, Aspose.Words.SaveFormat.AsposePdf);
pdfFileStream.Seek(0, SeekOrigin.Begin);
Aspose.Pdf.Pdf pdf = new Aspose.Pdf.Pdf();"
pdf.BindXML(pdfFileStream, null);
pdf.Save(output);

_fileStorageService.SaveFile(keyName, pdfFileStream);
pdfFileStream.Close();
output.Close();
}

public void SaveFile(string keyName, MemoryStream stream)
{
FileStream fs = new FileStream(keyName, FileMode.Create, FileAccess.ReadWrite);
stream.WriteTo(fs);
fs.Close();
}

Thanks,

When I look at the resultant file (after saving to disk) it is xml not pdf.

Disregard previous, I noticed we were passing pdfFileStream and not output.

Thanks so much, your support has been exceptional.

As mentioned before pdf.save is very slow with relatively small file, you said they are working on this?

Hello <?xml:namespace prefix = u1 /><?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" />Douglas,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Sorry for causing the confusion. During the time I was pasting the code, I mistakenly placed

_fileStorageService.SaveFile(keyName, pdfFileStream);

instead of

_fileStorageService.SaveFile(keyName, output);

in which the memory stream holding the XML file was passed as argument. Please correct it. In case of any further issue, please feel free to contact.

Regarding slow response of Pdf.Save() method, please share the resource word file, so that we can test the issue at our end.

Here it is, let me know what you find, thanks!

It takes seconds (5-10) for pdf.save(toStream) but not so with pdf.save(toFile)

Ok, I found this and it took care of the performance issue by doing the following before pdf.save():

pdf.IsTruetypeFontMapCached = true;
pdf.TruetypeFontMapPath = System.IO.Path.GetTempPath();

Thanks!