[Rant]Why a Filestream?[/Rant]

[Rant]

I am using the Pdf object and to my surprise it doesn’t support taking a Stream object but rather a FileStream object in the constructor.



I happen to have a SPFileStream object (which Microsoft for some odd reason decided not to let inherit from FileStream but directly from Stream, sigh!) so now I have to save the file to disk before loading it again into the Pdf object.



Why doesn’t the Pdf object take a Stream like the other Aspose main objects but rather the more restrictive FileStream object?



[/Rant]



Regards



Ryan Hougaard

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

Along with FileStream object Aspose.Pdf also supports MemoryStream object. Regarding support of SPFileStream I need to check with the development team.

Hi Ryan,

Thank you for considering Aspose.

There are two mode to create PDF using Aspose.Pdf: normal mode and streaming mode (direct-to-file mode). The direct-to-file mode (which has the FileStream parameter in Pdf constructor) is used to create large PDF so it supports FileStream only. If you want to use MemoryStream, you can simply use normal mode like the following:

Pdf p = new Pdf();

...

p.Save(aMemoryStream);

Ryan.Hougaard:
I am using the Pdf object and to my surprise it doesn't support taking a Stream object but rather a FileStream object in the constructor.

I've just experienced the exact same problem. I'm uploading a file via the ASP.NET FileUpload control and the posted file is accessible programatically via FileUpload.PostedFile.InputStream. However, that is a Steam object and the Aspose.PDF constructor only takes a FileStream object. It would sure be nice to be able to manipulate the file without having to write it to the disk and read it back solely because Aspose.PDF doesn't support loading from the Stream object.

I also save these files into a SQL database, so I'm probably going to have to do the same thing when I try to retrieve a file from the database. What a pain!!

Was anything ever done about this or will it be in the near future (like tomorrow)?

-David

Hi David,

We are working over this issue and will reply to you soon.

Hi David,

We have studied this issue in detail and here I would like to share my understanding over this issue. I am not sure what sort of files you are uploading, but if it's some kind of simple files i.e. Text files, than I think you can use the following code snippet to generate the Pdf file out of it.
Read the File as Stream Object, converted it into MemoryStream, and from MemoryStream we converted it to Byte Array. From Byte Array create a String Object, and finally it is added to paragraphs collection of Pdf object, once it is converted to Text object.

[C#]

MemoryStream m = new MemoryStream();
Stream receiveStream = myHttpWebResponse.GetResponseStream();
using (StreamReader sr = new StreamReader(receiveStream))
{
while (sr.Peek() >= 0)
{
memStream.WriteByte((byte)sr.Read());
}
}<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

myHttpWebResponse.Close();

Pdf pdf1 = new Pdf();
int count = (int)m.Length;
byte[] byteArray = new byte[m.Length];
// Read the remaining bytes, byte by byte.

while(count < m.Length)
{
byteArray[count++] =
Convert.ToByte(m.ReadByte());
}

System.Text.ASCIIEncoding enc = new
System.Text.ASCIIEncoding();
string str = enc.GetString(byteArray);
//Create a new section in the Pdf object
Aspose.Pdf.Section sec1 = pdf1.Sections.Add();
//Set the left margin of the section
sec1.PageInfo.Margin.Left = 110;
//Set the right margin of the section
sec1.PageInfo.Margin.Right = 120;
//Add this text paragraph to the section

Text t = new Text(str);
sec1.Paragraphs.Add(t);
// Save the Pdf file
pdf1.Save("d:/pdftest/MasterPageOnLandscape_version_test.pdf");

Or if you are trying to upload a Pdf file into Pdf object, it's not supported. It would be more convenient for us, if you could share some more details over this issue.